Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Support for OR51211 (pcHDTV HD-2000) - VSB |
| 3 | * |
| 4 | * Copyright (C) 2005 Kirk Lapray <kirk_lapray@bigfoot.com> |
| 5 | * |
| 6 | * Based on code from Jack Kelliher (kelliher@xmission.com) |
| 7 | * Copyright (C) 2002 & pcHDTV, inc. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | * |
| 23 | */ |
| 24 | |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 25 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | /* |
| 28 | * This driver needs external firmware. Please use the command |
| 29 | * "<kerneldir>/Documentation/dvb/get_dvb_firmware or51211" to |
Ville Skytt\รค | 12e66f6 | 2006-01-09 15:25:38 -0200 | [diff] [blame] | 30 | * download/extract it, and then copy it to /usr/lib/hotplug/firmware |
| 31 | * or /lib/firmware (depending on configuration of firmware hotplug). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | */ |
| 33 | #define OR51211_DEFAULT_FIRMWARE "dvb-fe-or51211.fw" |
| 34 | |
| 35 | #include <linux/kernel.h> |
| 36 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/device.h> |
| 38 | #include <linux/firmware.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 39 | #include <linux/string.h> |
| 40 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <asm/byteorder.h> |
| 42 | |
Rusty Scott | 1444e5f | 2006-12-04 18:04:16 -0300 | [diff] [blame] | 43 | #include "dvb_math.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include "dvb_frontend.h" |
| 45 | #include "or51211.h" |
| 46 | |
| 47 | static int debug; |
| 48 | #define dprintk(args...) \ |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 49 | do { if (debug) pr_debug(args); } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | static u8 run_buf[] = {0x7f,0x01}; |
| 52 | static u8 cmd_buf[] = {0x04,0x01,0x50,0x80,0x06}; // ATSC |
| 53 | |
| 54 | struct or51211_state { |
| 55 | |
| 56 | struct i2c_adapter* i2c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | /* Configuration settings */ |
| 59 | const struct or51211_config* config; |
| 60 | |
| 61 | struct dvb_frontend frontend; |
| 62 | struct bt878* bt; |
| 63 | |
| 64 | /* Demodulator private data */ |
| 65 | u8 initialized:1; |
Rusty Scott | 1444e5f | 2006-12-04 18:04:16 -0300 | [diff] [blame] | 66 | u32 snr; /* Result of last SNR claculation */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | /* Tuner private data */ |
| 69 | u32 current_frequency; |
| 70 | }; |
| 71 | |
David Woodhouse | bc17915 | 2008-05-24 00:12:23 +0100 | [diff] [blame] | 72 | static int i2c_writebytes (struct or51211_state* state, u8 reg, const u8 *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | int len) |
| 74 | { |
| 75 | int err; |
| 76 | struct i2c_msg msg; |
| 77 | msg.addr = reg; |
| 78 | msg.flags = 0; |
| 79 | msg.len = len; |
David Woodhouse | bc17915 | 2008-05-24 00:12:23 +0100 | [diff] [blame] | 80 | msg.buf = (u8 *)buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 83 | pr_warn("error (addr %02x, err == %i)\n", reg, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return -EREMOTEIO; |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
Hans Verkuil | eda9e4e | 2008-08-22 17:12:08 -0300 | [diff] [blame] | 90 | static int i2c_readbytes(struct or51211_state *state, u8 reg, u8 *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
| 92 | int err; |
| 93 | struct i2c_msg msg; |
| 94 | msg.addr = reg; |
| 95 | msg.flags = I2C_M_RD; |
| 96 | msg.len = len; |
| 97 | msg.buf = buf; |
| 98 | |
| 99 | if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 100 | pr_warn("error (addr %02x, err == %i)\n", reg, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | return -EREMOTEIO; |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int or51211_load_firmware (struct dvb_frontend* fe, |
| 108 | const struct firmware *fw) |
| 109 | { |
| 110 | struct or51211_state* state = fe->demodulator_priv; |
| 111 | u8 tudata[585]; |
| 112 | int i; |
| 113 | |
Mauro Carvalho Chehab | 35f30f3 | 2014-09-24 20:35:12 -0300 | [diff] [blame] | 114 | dprintk("Firmware is %zu bytes\n", fw->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | /* Get eprom data */ |
| 117 | tudata[0] = 17; |
| 118 | if (i2c_writebytes(state,0x50,tudata,1)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 119 | pr_warn("error eprom addr\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return -1; |
| 121 | } |
| 122 | if (i2c_readbytes(state,0x50,&tudata[145],192)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 123 | pr_warn("error eprom\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | /* Create firmware buffer */ |
| 128 | for (i = 0; i < 145; i++) |
| 129 | tudata[i] = fw->data[i]; |
| 130 | |
| 131 | for (i = 0; i < 248; i++) |
| 132 | tudata[i+337] = fw->data[145+i]; |
| 133 | |
| 134 | state->config->reset(fe); |
| 135 | |
| 136 | if (i2c_writebytes(state,state->config->demod_address,tudata,585)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 137 | pr_warn("error 1\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | return -1; |
| 139 | } |
| 140 | msleep(1); |
| 141 | |
| 142 | if (i2c_writebytes(state,state->config->demod_address, |
| 143 | &fw->data[393],8125)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 144 | pr_warn("error 2\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | return -1; |
| 146 | } |
| 147 | msleep(1); |
| 148 | |
| 149 | if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 150 | pr_warn("error 3\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | /* Wait at least 5 msec */ |
| 155 | msleep(10); |
| 156 | if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 157 | pr_warn("error 4\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | return -1; |
| 159 | } |
| 160 | msleep(10); |
| 161 | |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 162 | pr_info("Done.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | return 0; |
| 164 | }; |
| 165 | |
| 166 | static int or51211_setmode(struct dvb_frontend* fe, int mode) |
| 167 | { |
| 168 | struct or51211_state* state = fe->demodulator_priv; |
| 169 | u8 rec_buf[14]; |
| 170 | |
| 171 | state->config->setmode(fe, mode); |
| 172 | |
| 173 | if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 174 | pr_warn("error 1\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | return -1; |
| 176 | } |
| 177 | |
| 178 | /* Wait at least 5 msec */ |
| 179 | msleep(10); |
| 180 | if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 181 | pr_warn("error 2\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | return -1; |
| 183 | } |
| 184 | |
| 185 | msleep(10); |
| 186 | |
| 187 | /* Set operation mode in Receiver 1 register; |
| 188 | * type 1: |
| 189 | * data 0x50h Automatic sets receiver channel conditions |
| 190 | * Automatic NTSC rejection filter |
| 191 | * Enable MPEG serial data output |
| 192 | * MPEG2tr |
| 193 | * High tuner phase noise |
| 194 | * normal +/-150kHz Carrier acquisition range |
| 195 | */ |
| 196 | if (i2c_writebytes(state,state->config->demod_address,cmd_buf,3)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 197 | pr_warn("error 3\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | rec_buf[0] = 0x04; |
| 202 | rec_buf[1] = 0x00; |
| 203 | rec_buf[2] = 0x03; |
| 204 | rec_buf[3] = 0x00; |
| 205 | msleep(20); |
| 206 | if (i2c_writebytes(state,state->config->demod_address,rec_buf,3)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 207 | pr_warn("error 5\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | msleep(3); |
| 210 | if (i2c_readbytes(state,state->config->demod_address,&rec_buf[10],2)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 211 | pr_warn("error 6\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | return -1; |
| 213 | } |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 214 | dprintk("rec status %02x %02x\n", rec_buf[10], rec_buf[11]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
Mauro Carvalho Chehab | d42c086 | 2011-12-26 15:02:20 -0300 | [diff] [blame] | 219 | static int or51211_set_parameters(struct dvb_frontend *fe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
Mauro Carvalho Chehab | d42c086 | 2011-12-26 15:02:20 -0300 | [diff] [blame] | 221 | struct dtv_frontend_properties *p = &fe->dtv_property_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | struct or51211_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | /* Change only if we are actually changing the channel */ |
Mauro Carvalho Chehab | d42c086 | 2011-12-26 15:02:20 -0300 | [diff] [blame] | 225 | if (state->current_frequency != p->frequency) { |
Michael Krufky | cd2cd0a | 2007-06-24 18:14:52 -0300 | [diff] [blame] | 226 | if (fe->ops.tuner_ops.set_params) { |
Mauro Carvalho Chehab | 14d24d1 | 2011-12-24 12:24:33 -0300 | [diff] [blame] | 227 | fe->ops.tuner_ops.set_params(fe); |
Michael Krufky | cd2cd0a | 2007-06-24 18:14:52 -0300 | [diff] [blame] | 228 | if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 231 | /* Set to ATSC mode */ |
| 232 | or51211_setmode(fe,0); |
| 233 | |
| 234 | /* Update current frequency */ |
Mauro Carvalho Chehab | d42c086 | 2011-12-26 15:02:20 -0300 | [diff] [blame] | 235 | state->current_frequency = p->frequency; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static int or51211_read_status(struct dvb_frontend* fe, fe_status_t* status) |
| 241 | { |
| 242 | struct or51211_state* state = fe->demodulator_priv; |
| 243 | unsigned char rec_buf[2]; |
| 244 | unsigned char snd_buf[] = {0x04,0x00,0x03,0x00}; |
| 245 | *status = 0; |
| 246 | |
| 247 | /* Receiver Status */ |
| 248 | if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 249 | pr_warn("write error\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | return -1; |
| 251 | } |
| 252 | msleep(3); |
| 253 | if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 254 | pr_warn("read error\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | return -1; |
| 256 | } |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 257 | dprintk("%x %x\n", rec_buf[0], rec_buf[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
| 259 | if (rec_buf[0] & 0x01) { /* Receiver Lock */ |
| 260 | *status |= FE_HAS_SIGNAL; |
| 261 | *status |= FE_HAS_CARRIER; |
| 262 | *status |= FE_HAS_VITERBI; |
| 263 | *status |= FE_HAS_SYNC; |
| 264 | *status |= FE_HAS_LOCK; |
| 265 | } |
| 266 | return 0; |
| 267 | } |
| 268 | |
Rusty Scott | 1444e5f | 2006-12-04 18:04:16 -0300 | [diff] [blame] | 269 | /* Calculate SNR estimation (scaled by 2^24) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Rusty Scott | 1444e5f | 2006-12-04 18:04:16 -0300 | [diff] [blame] | 271 | 8-VSB SNR equation from Oren datasheets |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
Rusty Scott | 1444e5f | 2006-12-04 18:04:16 -0300 | [diff] [blame] | 273 | For 8-VSB: |
| 274 | SNR[dB] = 10 * log10(219037.9454 / MSE^2 ) |
| 275 | |
| 276 | We re-write the snr equation as: |
| 277 | SNR * 2^24 = 10*(c - 2*intlog10(MSE)) |
| 278 | Where for 8-VSB, c = log10(219037.9454) * 2^24 */ |
| 279 | |
| 280 | static u32 calculate_snr(u32 mse, u32 c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
Rusty Scott | 1444e5f | 2006-12-04 18:04:16 -0300 | [diff] [blame] | 282 | if (mse == 0) /* No signal */ |
| 283 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
Rusty Scott | 1444e5f | 2006-12-04 18:04:16 -0300 | [diff] [blame] | 285 | mse = 2*intlog10(mse); |
| 286 | if (mse > c) { |
| 287 | /* Negative SNR, which is possible, but realisticly the |
| 288 | demod will lose lock before the signal gets this bad. The |
| 289 | API only allows for unsigned values, so just return 0 */ |
| 290 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
Rusty Scott | 1444e5f | 2006-12-04 18:04:16 -0300 | [diff] [blame] | 292 | return 10*(c - mse); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static int or51211_read_snr(struct dvb_frontend* fe, u16* snr) |
| 296 | { |
| 297 | struct or51211_state* state = fe->demodulator_priv; |
| 298 | u8 rec_buf[2]; |
Rusty Scott | 1444e5f | 2006-12-04 18:04:16 -0300 | [diff] [blame] | 299 | u8 snd_buf[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
| 301 | /* SNR after Equalizer */ |
| 302 | snd_buf[0] = 0x04; |
| 303 | snd_buf[1] = 0x00; |
| 304 | snd_buf[2] = 0x04; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
| 306 | if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 307 | pr_warn("error writing snr reg\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | return -1; |
| 309 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 311 | pr_warn("read_status read error\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | return -1; |
| 313 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
Rusty Scott | 1444e5f | 2006-12-04 18:04:16 -0300 | [diff] [blame] | 315 | state->snr = calculate_snr(rec_buf[0], 89599047); |
| 316 | *snr = (state->snr) >> 16; |
| 317 | |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 318 | dprintk("noise = 0x%02x, snr = %d.%02d dB\n", rec_buf[0], |
Rusty Scott | 1444e5f | 2006-12-04 18:04:16 -0300 | [diff] [blame] | 319 | state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16); |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | static int or51211_read_signal_strength(struct dvb_frontend* fe, u16* strength) |
| 325 | { |
| 326 | /* Calculate Strength from SNR up to 35dB */ |
| 327 | /* Even though the SNR can go higher than 35dB, there is some comfort */ |
| 328 | /* factor in having a range of strong signals that can show at 100% */ |
| 329 | struct or51211_state* state = (struct or51211_state*)fe->demodulator_priv; |
| 330 | u16 snr; |
| 331 | int ret; |
| 332 | |
| 333 | ret = fe->ops.read_snr(fe, &snr); |
| 334 | if (ret != 0) |
| 335 | return ret; |
| 336 | /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */ |
| 337 | /* scale the range 0 - 35*2^24 into 0 - 65535 */ |
| 338 | if (state->snr >= 8960 * 0x10000) |
| 339 | *strength = 0xffff; |
| 340 | else |
| 341 | *strength = state->snr / 8960; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | static int or51211_read_ber(struct dvb_frontend* fe, u32* ber) |
| 347 | { |
| 348 | *ber = -ENOSYS; |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | static int or51211_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks) |
| 353 | { |
| 354 | *ucblocks = -ENOSYS; |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static int or51211_sleep(struct dvb_frontend* fe) |
| 359 | { |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static int or51211_init(struct dvb_frontend* fe) |
| 364 | { |
| 365 | struct or51211_state* state = fe->demodulator_priv; |
| 366 | const struct or51211_config* config = state->config; |
| 367 | const struct firmware* fw; |
| 368 | unsigned char get_ver_buf[] = {0x04,0x00,0x30,0x00,0x00}; |
| 369 | unsigned char rec_buf[14]; |
| 370 | int ret,i; |
| 371 | |
| 372 | if (!state->initialized) { |
| 373 | /* Request the firmware, this will block until it uploads */ |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 374 | pr_info("Waiting for firmware upload (%s)...\n", |
| 375 | OR51211_DEFAULT_FIRMWARE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | ret = config->request_firmware(fe, &fw, |
| 377 | OR51211_DEFAULT_FIRMWARE); |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 378 | pr_info("Got Hotplug firmware\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | if (ret) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 380 | pr_warn("No firmware uploaded " |
| 381 | "(timeout or file not found?)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | ret = or51211_load_firmware(fe, fw); |
Magnus Damm | 73ca66b | 2006-07-10 04:44:09 -0700 | [diff] [blame] | 386 | release_firmware(fw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | if (ret) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 388 | pr_warn("Writing firmware to device failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | return ret; |
| 390 | } |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 391 | pr_info("Firmware upload complete.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
| 393 | /* Set operation mode in Receiver 1 register; |
| 394 | * type 1: |
| 395 | * data 0x50h Automatic sets receiver channel conditions |
| 396 | * Automatic NTSC rejection filter |
| 397 | * Enable MPEG serial data output |
| 398 | * MPEG2tr |
| 399 | * High tuner phase noise |
| 400 | * normal +/-150kHz Carrier acquisition range |
| 401 | */ |
| 402 | if (i2c_writebytes(state,state->config->demod_address, |
| 403 | cmd_buf,3)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 404 | pr_warn("Load DVR Error 5\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | /* Read back ucode version to besure we loaded correctly */ |
| 409 | /* and are really up and running */ |
| 410 | rec_buf[0] = 0x04; |
| 411 | rec_buf[1] = 0x00; |
| 412 | rec_buf[2] = 0x03; |
| 413 | rec_buf[3] = 0x00; |
| 414 | msleep(30); |
| 415 | if (i2c_writebytes(state,state->config->demod_address, |
| 416 | rec_buf,3)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 417 | pr_warn("Load DVR Error A\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | return -1; |
| 419 | } |
| 420 | msleep(3); |
| 421 | if (i2c_readbytes(state,state->config->demod_address, |
| 422 | &rec_buf[10],2)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 423 | pr_warn("Load DVR Error B\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | return -1; |
| 425 | } |
| 426 | |
| 427 | rec_buf[0] = 0x04; |
| 428 | rec_buf[1] = 0x00; |
| 429 | rec_buf[2] = 0x01; |
| 430 | rec_buf[3] = 0x00; |
| 431 | msleep(20); |
| 432 | if (i2c_writebytes(state,state->config->demod_address, |
| 433 | rec_buf,3)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 434 | pr_warn("Load DVR Error C\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | return -1; |
| 436 | } |
| 437 | msleep(3); |
| 438 | if (i2c_readbytes(state,state->config->demod_address, |
| 439 | &rec_buf[12],2)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 440 | pr_warn("Load DVR Error D\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | return -1; |
| 442 | } |
| 443 | |
| 444 | for (i = 0; i < 8; i++) |
| 445 | rec_buf[i]=0xed; |
| 446 | |
| 447 | for (i = 0; i < 5; i++) { |
| 448 | msleep(30); |
| 449 | get_ver_buf[4] = i+1; |
| 450 | if (i2c_writebytes(state,state->config->demod_address, |
| 451 | get_ver_buf,5)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 452 | pr_warn("Load DVR Error 6 - %d\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | return -1; |
| 454 | } |
| 455 | msleep(3); |
| 456 | |
| 457 | if (i2c_readbytes(state,state->config->demod_address, |
| 458 | &rec_buf[i*2],2)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 459 | pr_warn("Load DVR Error 7 - %d\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | return -1; |
| 461 | } |
| 462 | /* If we didn't receive the right index, try again */ |
| 463 | if ((int)rec_buf[i*2+1]!=i+1){ |
| 464 | i--; |
| 465 | } |
| 466 | } |
Andy Shevchenko | 870f31c | 2012-12-17 22:10:00 -0300 | [diff] [blame] | 467 | dprintk("read_fwbits %10ph\n", rec_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 469 | pr_info("ver TU%02x%02x%02x VSB mode %02x Status %02x\n", |
| 470 | rec_buf[2], rec_buf[4], rec_buf[6], rec_buf[12], |
| 471 | rec_buf[10]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
| 473 | rec_buf[0] = 0x04; |
| 474 | rec_buf[1] = 0x00; |
| 475 | rec_buf[2] = 0x03; |
| 476 | rec_buf[3] = 0x00; |
| 477 | msleep(20); |
| 478 | if (i2c_writebytes(state,state->config->demod_address, |
| 479 | rec_buf,3)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 480 | pr_warn("Load DVR Error 8\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | return -1; |
| 482 | } |
| 483 | msleep(20); |
| 484 | if (i2c_readbytes(state,state->config->demod_address, |
| 485 | &rec_buf[8],2)) { |
Andy Shevchenko | bb9e31f | 2012-12-18 10:20:28 -0300 | [diff] [blame] | 486 | pr_warn("Load DVR Error 9\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | return -1; |
| 488 | } |
| 489 | state->initialized = 1; |
| 490 | } |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static int or51211_get_tune_settings(struct dvb_frontend* fe, |
| 496 | struct dvb_frontend_tune_settings* fesettings) |
| 497 | { |
| 498 | fesettings->min_delay_ms = 500; |
| 499 | fesettings->step_size = 0; |
| 500 | fesettings->max_drift = 0; |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | static void or51211_release(struct dvb_frontend* fe) |
| 505 | { |
| 506 | struct or51211_state* state = fe->demodulator_priv; |
| 507 | state->config->sleep(fe); |
| 508 | kfree(state); |
| 509 | } |
| 510 | |
| 511 | static struct dvb_frontend_ops or51211_ops; |
| 512 | |
| 513 | struct dvb_frontend* or51211_attach(const struct or51211_config* config, |
| 514 | struct i2c_adapter* i2c) |
| 515 | { |
| 516 | struct or51211_state* state = NULL; |
| 517 | |
| 518 | /* Allocate memory for the internal state */ |
Matthias Schwarzott | 084e24a | 2009-08-10 22:51:01 -0300 | [diff] [blame] | 519 | state = kzalloc(sizeof(struct or51211_state), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | if (state == NULL) |
Mauro Carvalho Chehab | 3473e34 | 2008-01-07 10:45:47 -0300 | [diff] [blame] | 521 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
| 523 | /* Setup the state */ |
| 524 | state->config = config; |
| 525 | state->i2c = i2c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | state->initialized = 0; |
| 527 | state->current_frequency = 0; |
| 528 | |
| 529 | /* Create dvb_frontend */ |
Patrick Boettcher | dea7486 | 2006-05-14 05:01:31 -0300 | [diff] [blame] | 530 | memcpy(&state->frontend.ops, &or51211_ops, sizeof(struct dvb_frontend_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | state->frontend.demodulator_priv = state; |
| 532 | return &state->frontend; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | static struct dvb_frontend_ops or51211_ops = { |
Mauro Carvalho Chehab | d42c086 | 2011-12-26 15:02:20 -0300 | [diff] [blame] | 536 | .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | .info = { |
| 538 | .name = "Oren OR51211 VSB Frontend", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | .frequency_min = 44000000, |
| 540 | .frequency_max = 958000000, |
| 541 | .frequency_stepsize = 166666, |
| 542 | .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 | |
| 543 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO | |
| 544 | FE_CAN_8VSB |
| 545 | }, |
| 546 | |
| 547 | .release = or51211_release, |
| 548 | |
| 549 | .init = or51211_init, |
| 550 | .sleep = or51211_sleep, |
| 551 | |
Mauro Carvalho Chehab | d42c086 | 2011-12-26 15:02:20 -0300 | [diff] [blame] | 552 | .set_frontend = or51211_set_parameters, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | .get_tune_settings = or51211_get_tune_settings, |
| 554 | |
| 555 | .read_status = or51211_read_status, |
| 556 | .read_ber = or51211_read_ber, |
| 557 | .read_signal_strength = or51211_read_signal_strength, |
| 558 | .read_snr = or51211_read_snr, |
| 559 | .read_ucblocks = or51211_read_ucblocks, |
| 560 | }; |
| 561 | |
| 562 | module_param(debug, int, 0644); |
| 563 | MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); |
| 564 | |
| 565 | MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver"); |
| 566 | MODULE_AUTHOR("Kirk Lapray"); |
| 567 | MODULE_LICENSE("GPL"); |
| 568 | |
| 569 | EXPORT_SYMBOL(or51211_attach); |
| 570 | |