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 | |
Jean Delvare | 2476410 | 2010-09-10 14:03:07 -0300 | [diff] [blame] | 95 | if (unlikely(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); |
Jean Delvare | 2476410 | 2010-09-10 14:03:07 -0300 | [diff] [blame] | 99 | return -1; |
| 100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Jean Delvare | 2476410 | 2010-09-10 14:03:07 -0300 | [diff] [blame] | 102 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 105 | static u8 cx22702_readreg(struct cx22702_state *state, u8 reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
| 107 | int ret; |
Jean Delvare | 2476410 | 2010-09-10 14:03:07 -0300 | [diff] [blame] | 108 | u8 data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 110 | struct i2c_msg msg[] = { |
| 111 | { .addr = state->config->demod_address, .flags = 0, |
Jean Delvare | 2476410 | 2010-09-10 14:03:07 -0300 | [diff] [blame] | 112 | .buf = ®, .len = 1 }, |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 113 | { .addr = state->config->demod_address, .flags = I2C_M_RD, |
Jean Delvare | 2476410 | 2010-09-10 14:03:07 -0300 | [diff] [blame] | 114 | .buf = &data, .len = 1 } }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | ret = i2c_transfer(state->i2c, msg, 2); |
| 117 | |
Jean Delvare | 2476410 | 2010-09-10 14:03:07 -0300 | [diff] [blame] | 118 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
Jean Delvare | 2476410 | 2010-09-10 14:03:07 -0300 | [diff] [blame] | 124 | return data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 127 | static int cx22702_set_inversion(struct cx22702_state *state, int inversion) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
| 129 | u8 val; |
| 130 | |
| 131 | switch (inversion) { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 132 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | } |
| 145 | |
| 146 | /* Retrieve the demod settings */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 147 | static int cx22702_get_tps(struct cx22702_state *state, |
| 148 | struct dvb_ofdm_parameters *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
| 150 | u8 val; |
| 151 | |
| 152 | /* Make sure the TPS regs are valid */ |
| 153 | if (!(cx22702_readreg(state, 0x0A) & 0x20)) |
| 154 | return -EAGAIN; |
| 155 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 156 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 168 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 184 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 202 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 220 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 235 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 247 | static int cx22702_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) |
Steven Toth | 611900c | 2006-01-09 15:25:12 -0200 | [diff] [blame] | 248 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 249 | struct cx22702_state *state = fe->demodulator_priv; |
| 250 | dprintk("%s(%d)\n", __func__, enable); |
Steven Toth | 611900c | 2006-01-09 15:25:12 -0200 | [diff] [blame] | 251 | if (enable) |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 252 | return cx22702_writereg(state, 0x0D, |
| 253 | cx22702_readreg(state, 0x0D) & 0xfe); |
Steven Toth | 611900c | 2006-01-09 15:25:12 -0200 | [diff] [blame] | 254 | else |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 255 | return cx22702_writereg(state, 0x0D, |
| 256 | cx22702_readreg(state, 0x0D) | 1); |
Steven Toth | 611900c | 2006-01-09 15:25:12 -0200 | [diff] [blame] | 257 | } |
| 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | /* Talk to the demod, set the FEC, GUARD, QAM settings etc */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 260 | static int cx22702_set_tps(struct dvb_frontend *fe, |
| 261 | struct dvb_frontend_parameters *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
| 263 | u8 val; |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 264 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Patrick Boettcher | dea7486 | 2006-05-14 05:01:31 -0300 | [diff] [blame] | 266 | if (fe->ops.tuner_ops.set_params) { |
| 267 | fe->ops.tuner_ops.set_params(fe, p); |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 268 | if (fe->ops.i2c_gate_ctrl) |
| 269 | fe->ops.i2c_gate_ctrl(fe, 0); |
Gerd Knorr | 9990d74 | 2005-05-01 08:59:20 -0700 | [diff] [blame] | 270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | /* set inversion */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 273 | cx22702_set_inversion(state, p->inversion); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | /* set bandwidth */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 276 | switch (p->u.ofdm.bandwidth) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | case BANDWIDTH_6_MHZ: |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 278 | cx22702_writereg(state, 0x0C, |
| 279 | (cx22702_readreg(state, 0x0C) & 0xcf) | 0x20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | break; |
| 281 | case BANDWIDTH_7_MHZ: |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 282 | cx22702_writereg(state, 0x0C, |
| 283 | (cx22702_readreg(state, 0x0C) & 0xcf) | 0x10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | break; |
| 285 | case BANDWIDTH_8_MHZ: |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 286 | cx22702_writereg(state, 0x0C, |
| 287 | cx22702_readreg(state, 0x0C) & 0xcf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | break; |
| 289 | default: |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 290 | dprintk("%s: invalid bandwidth\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | return -EINVAL; |
| 292 | } |
| 293 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 294 | 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] | 295 | |
| 296 | /* use auto configuration? */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 297 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
| 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 Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 308 | cx22702_writereg(state, 0x0B, cx22702_readreg(state, 0x0B) |
| 309 | & 0xfc); |
| 310 | cx22702_writereg(state, 0x0C, |
| 311 | (cx22702_readreg(state, 0x0C) & 0xBF) | 0x40); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | cx22702_writereg(state, 0x00, 0x01); /* Begin aquisition */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 313 | dprintk("%s: Autodetecting\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | /* manually programmed values */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 318 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 333 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 350 | cx22702_writereg(state, 0x06, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 352 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 374 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 395 | cx22702_writereg(state, 0x07, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 397 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 415 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | cx22702_writereg(state, 0x08, val); |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 427 | cx22702_writereg(state, 0x0B, |
| 428 | (cx22702_readreg(state, 0x0B) & 0xfc) | 0x02); |
| 429 | cx22702_writereg(state, 0x0C, |
| 430 | (cx22702_readreg(state, 0x0C) & 0xBF) | 0x40); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 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 Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 440 | static int cx22702_init(struct dvb_frontend *fe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | { |
| 442 | int i; |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 443 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 445 | cx22702_writereg(state, 0x00, 0x02); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
| 447 | msleep(10); |
| 448 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 449 | for (i = 0; i < ARRAY_SIZE(init_tab); i += 2) |
| 450 | cx22702_writereg(state, init_tab[i], init_tab[i + 1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 452 | cx22702_writereg(state, 0xf8, (state->config->output_mode << 1) |
| 453 | & 0x02); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Steven Toth | 611900c | 2006-01-09 15:25:12 -0200 | [diff] [blame] | 455 | cx22702_i2c_gate_ctrl(fe, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 460 | 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] | 461 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 462 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | u8 reg0A; |
| 464 | u8 reg23; |
| 465 | |
| 466 | *status = 0; |
| 467 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 468 | reg0A = cx22702_readreg(state, 0x0A); |
| 469 | reg23 = cx22702_readreg(state, 0x23); |
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 | dprintk("%s: status demod=0x%02x agc=0x%02x\n" |
| 472 | , __func__, reg0A, reg23); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 474 | if (reg0A & 0x10) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | *status |= FE_HAS_LOCK; |
| 476 | *status |= FE_HAS_VITERBI; |
| 477 | *status |= FE_HAS_SYNC; |
| 478 | } |
| 479 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 480 | if (reg0A & 0x20) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | *status |= FE_HAS_CARRIER; |
| 482 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 483 | if (reg23 < 0xf0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | *status |= FE_HAS_SIGNAL; |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 489 | static int cx22702_read_ber(struct dvb_frontend *fe, u32 *ber) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 491 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 493 | if (cx22702_readreg(state, 0xE4) & 0x02) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | /* Realtime statistics */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 495 | *ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7 |
| 496 | | (cx22702_readreg(state, 0xDF) & 0x7F); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } else { |
| 498 | /* Averagtine statistics */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 499 | *ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7 |
| 500 | | cx22702_readreg(state, 0xDF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 506 | static int cx22702_read_signal_strength(struct dvb_frontend *fe, |
| 507 | u16 *signal_strength) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 509 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Jean Delvare | 5b9a6f3 | 2010-09-10 10:32:21 -0300 | [diff] [blame^] | 511 | u16 rs_ber; |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 512 | rs_ber = cx22702_readreg(state, 0x23); |
Bradley Kite | 1e9dadb | 2006-09-02 21:14:27 -0300 | [diff] [blame] | 513 | *signal_strength = (rs_ber << 8) | rs_ber; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 518 | static int cx22702_read_snr(struct dvb_frontend *fe, u16 *snr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 520 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
Jean Delvare | 5b9a6f3 | 2010-09-10 10:32:21 -0300 | [diff] [blame^] | 522 | u16 rs_ber; |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 523 | if (cx22702_readreg(state, 0xE4) & 0x02) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | /* Realtime statistics */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 525 | rs_ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7 |
| 526 | | (cx22702_readreg(state, 0xDF) & 0x7F); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | } else { |
| 528 | /* Averagine statistics */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 529 | rs_ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 8 |
| 530 | | cx22702_readreg(state, 0xDF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } |
| 532 | *snr = ~rs_ber; |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 537 | static int cx22702_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 539 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
| 541 | u8 _ucblocks; |
| 542 | |
| 543 | /* RS Uncorrectable Packet Count then reset */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 544 | _ucblocks = cx22702_readreg(state, 0xE3); |
Patrick Boettcher | f46dbb0 | 2005-07-07 17:57:44 -0700 | [diff] [blame] | 545 | if (state->prevUCBlocks < _ucblocks) |
| 546 | *ucblocks = (_ucblocks - state->prevUCBlocks); |
| 547 | else |
| 548 | *ucblocks = state->prevUCBlocks - _ucblocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | state->prevUCBlocks = _ucblocks; |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 554 | static int cx22702_get_frontend(struct dvb_frontend *fe, |
| 555 | struct dvb_frontend_parameters *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 557 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 559 | u8 reg0C = cx22702_readreg(state, 0x0C); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
| 561 | p->inversion = reg0C & 0x1 ? INVERSION_ON : INVERSION_OFF; |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 562 | return cx22702_get_tps(state, &p->u.ofdm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 565 | static int cx22702_get_tune_settings(struct dvb_frontend *fe, |
| 566 | struct dvb_frontend_tune_settings *tune) |
Patrick Boettcher | f46dbb0 | 2005-07-07 17:57:44 -0700 | [diff] [blame] | 567 | { |
| 568 | tune->min_delay_ms = 1000; |
| 569 | return 0; |
| 570 | } |
| 571 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 572 | static void cx22702_release(struct dvb_frontend *fe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 574 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | kfree(state); |
| 576 | } |
| 577 | |
| 578 | static struct dvb_frontend_ops cx22702_ops; |
| 579 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 580 | struct dvb_frontend *cx22702_attach(const struct cx22702_config *config, |
| 581 | struct i2c_adapter *i2c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 583 | struct cx22702_state *state = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | |
| 585 | /* allocate memory for the internal state */ |
Matthias Schwarzott | 084e24a | 2009-08-10 22:51:01 -0300 | [diff] [blame] | 586 | state = kzalloc(sizeof(struct cx22702_state), GFP_KERNEL); |
Patrick Boettcher | f46dbb0 | 2005-07-07 17:57:44 -0700 | [diff] [blame] | 587 | if (state == NULL) |
| 588 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
| 590 | /* setup the state */ |
| 591 | state->config = config; |
| 592 | state->i2c = i2c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
| 594 | /* check if the demod is there */ |
Patrick Boettcher | f46dbb0 | 2005-07-07 17:57:44 -0700 | [diff] [blame] | 595 | if (cx22702_readreg(state, 0x1f) != 0x3) |
| 596 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
| 598 | /* create dvb_frontend */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 599 | memcpy(&state->frontend.ops, &cx22702_ops, |
| 600 | sizeof(struct dvb_frontend_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | state->frontend.demodulator_priv = state; |
| 602 | return &state->frontend; |
| 603 | |
| 604 | error: |
| 605 | kfree(state); |
| 606 | return NULL; |
| 607 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 608 | EXPORT_SYMBOL(cx22702_attach); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | |
| 610 | static 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 Quincey | 0244422 | 2006-04-18 17:47:09 -0300 | [diff] [blame] | 628 | .i2c_gate_ctrl = cx22702_i2c_gate_ctrl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
| 630 | .set_frontend = cx22702_set_tps, |
| 631 | .get_frontend = cx22702_get_frontend, |
Patrick Boettcher | f46dbb0 | 2005-07-07 17:57:44 -0700 | [diff] [blame] | 632 | .get_tune_settings = cx22702_get_tune_settings, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | }; |
| 640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | MODULE_DESCRIPTION("Conexant CX22702 DVB-T Demodulator driver"); |
| 642 | MODULE_AUTHOR("Steven Toth"); |
| 643 | MODULE_LICENSE("GPL"); |