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