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