Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1 | /* |
| 2 | Conexant cx24120/cx24118 - DVBS/S2 Satellite demod/tuner driver |
| 3 | |
| 4 | Copyright (C) 2008 Patrick Boettcher <pb@linuxtv.org> |
| 5 | Copyright (C) 2009 Sergey Tyurin <forum.free-x.de> |
| 6 | Updated 2012 by Jannis Achstetter <jannis_achstetter@web.de> |
| 7 | Copyright (C) 2015 Jemma Denson <jdenson@gmail.com> |
| 8 | April 2015 |
| 9 | Refactored & simplified driver |
| 10 | Updated to work with delivery system supplied by DVBv5 |
| 11 | Add frequency, fec & pilot to get_frontend |
| 12 | |
| 13 | Cards supported: Technisat Skystar S2 |
| 14 | |
| 15 | This program is free software; you can redistribute it and/or modify |
| 16 | it under the terms of the GNU General Public License as published by |
| 17 | the Free Software Foundation; either version 2 of the License, or |
| 18 | (at your option) any later version. |
| 19 | |
| 20 | This program is distributed in the hope that it will be useful, |
| 21 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | GNU General Public License for more details. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/moduleparam.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/firmware.h> |
| 32 | #include "dvb_frontend.h" |
| 33 | #include "cx24120.h" |
| 34 | |
| 35 | #define CX24120_SEARCH_RANGE_KHZ 5000 |
| 36 | #define CX24120_FIRMWARE "dvb-fe-cx24120-1.20.58.2.fw" |
| 37 | |
| 38 | /* cx24120 i2c registers */ |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 39 | #define CX24120_REG_CMD_START 0x00 /* write cmd_id */ |
| 40 | #define CX24120_REG_CMD_ARGS 0x01 /* write command arguments */ |
| 41 | #define CX24120_REG_CMD_END 0x1f /* write 0x01 for end */ |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 42 | |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 43 | #define CX24120_REG_MAILBOX 0x33 |
| 44 | #define CX24120_REG_FREQ3 0x34 /* frequency */ |
| 45 | #define CX24120_REG_FREQ2 0x35 |
| 46 | #define CX24120_REG_FREQ1 0x36 |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 47 | |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 48 | #define CX24120_REG_FECMODE 0x39 /* FEC status */ |
| 49 | #define CX24120_REG_STATUS 0x3a /* Tuner status */ |
| 50 | #define CX24120_REG_SIGSTR_H 0x3a /* Signal strength high */ |
| 51 | #define CX24120_REG_SIGSTR_L 0x3b /* Signal strength low byte */ |
| 52 | #define CX24120_REG_QUALITY_H 0x40 /* SNR high byte */ |
| 53 | #define CX24120_REG_QUALITY_L 0x41 /* SNR low byte */ |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 54 | |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 55 | #define CX24120_REG_BER_HH 0x47 /* BER high byte of high word */ |
| 56 | #define CX24120_REG_BER_HL 0x48 /* BER low byte of high word */ |
| 57 | #define CX24120_REG_BER_LH 0x49 /* BER high byte of low word */ |
| 58 | #define CX24120_REG_BER_LL 0x4a /* BER low byte of low word */ |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 59 | |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 60 | #define CX24120_REG_UCB_H 0x50 /* UCB high byte */ |
| 61 | #define CX24120_REG_UCB_L 0x51 /* UCB low byte */ |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 62 | |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 63 | #define CX24120_REG_CLKDIV 0xe6 |
| 64 | #define CX24120_REG_RATEDIV 0xf0 |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 65 | |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 66 | #define CX24120_REG_REVISION 0xff /* Chip revision (ro) */ |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 67 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 68 | /* Command messages */ |
| 69 | enum command_message_id { |
| 70 | CMD_VCO_SET = 0x10, /* cmd.len = 12; */ |
| 71 | CMD_TUNEREQUEST = 0x11, /* cmd.len = 15; */ |
| 72 | |
| 73 | CMD_MPEG_ONOFF = 0x13, /* cmd.len = 4; */ |
| 74 | CMD_MPEG_INIT = 0x14, /* cmd.len = 7; */ |
| 75 | CMD_BANDWIDTH = 0x15, /* cmd.len = 12; */ |
| 76 | CMD_CLOCK_READ = 0x16, /* read clock */ |
| 77 | CMD_CLOCK_SET = 0x17, /* cmd.len = 10; */ |
| 78 | |
| 79 | CMD_DISEQC_MSG1 = 0x20, /* cmd.len = 11; */ |
| 80 | CMD_DISEQC_MSG2 = 0x21, /* cmd.len = d->msg_len + 6; */ |
| 81 | CMD_SETVOLTAGE = 0x22, /* cmd.len = 2; */ |
| 82 | CMD_SETTONE = 0x23, /* cmd.len = 4; */ |
| 83 | CMD_DISEQC_BURST = 0x24, /* cmd.len not used !!! */ |
| 84 | |
| 85 | CMD_READ_SNR = 0x1a, /* Read signal strength */ |
| 86 | CMD_START_TUNER = 0x1b, /* ??? */ |
| 87 | |
| 88 | CMD_FWVERSION = 0x35, |
| 89 | |
| 90 | CMD_TUNER_INIT = 0x3c, /* cmd.len = 0x03; */ |
| 91 | }; |
| 92 | |
| 93 | #define CX24120_MAX_CMD_LEN 30 |
| 94 | |
| 95 | /* pilot mask */ |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 96 | #define CX24120_PILOT_OFF 0x00 |
| 97 | #define CX24120_PILOT_ON 0x40 |
| 98 | #define CX24120_PILOT_AUTO 0x80 |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 99 | |
| 100 | /* signal status */ |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 101 | #define CX24120_HAS_SIGNAL 0x01 |
| 102 | #define CX24120_HAS_CARRIER 0x02 |
| 103 | #define CX24120_HAS_VITERBI 0x04 |
| 104 | #define CX24120_HAS_LOCK 0x08 |
| 105 | #define CX24120_HAS_UNK1 0x10 |
| 106 | #define CX24120_HAS_UNK2 0x20 |
| 107 | #define CX24120_STATUS_MASK 0x0f |
| 108 | #define CX24120_SIGNAL_MASK 0xc0 |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 109 | |
Patrick Boettcher | c5fb0f5 | 2015-04-17 06:04:53 -0300 | [diff] [blame] | 110 | #define info(args...) pr_info("cx24120: " args) |
| 111 | #define err(args...) pr_err("cx24120: ### ERROR: " args) |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 112 | |
| 113 | /* The Demod/Tuner can't easily provide these, we cache them */ |
| 114 | struct cx24120_tuning { |
| 115 | u32 frequency; |
| 116 | u32 symbol_rate; |
| 117 | fe_spectral_inversion_t inversion; |
| 118 | fe_code_rate_t fec; |
| 119 | |
| 120 | fe_delivery_system_t delsys; |
| 121 | fe_modulation_t modulation; |
| 122 | fe_pilot_t pilot; |
| 123 | |
| 124 | /* Demod values */ |
| 125 | u8 fec_val; |
| 126 | u8 fec_mask; |
| 127 | u8 clkdiv; |
| 128 | u8 ratediv; |
| 129 | u8 inversion_val; |
| 130 | u8 pilot_val; |
| 131 | }; |
| 132 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 133 | /* Private state */ |
| 134 | struct cx24120_state { |
| 135 | struct i2c_adapter *i2c; |
| 136 | const struct cx24120_config *config; |
| 137 | struct dvb_frontend frontend; |
| 138 | |
| 139 | u8 cold_init; |
| 140 | u8 mpeg_enabled; |
Jemma Denson | 6138dc2 | 2015-04-30 16:37:42 -0300 | [diff] [blame] | 141 | u8 need_clock_set; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 142 | |
| 143 | /* current and next tuning parameters */ |
| 144 | struct cx24120_tuning dcur; |
| 145 | struct cx24120_tuning dnxt; |
Jemma Denson | 1462612 | 2015-05-05 17:18:11 -0300 | [diff] [blame] | 146 | |
| 147 | fe_status_t fe_status; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 148 | }; |
| 149 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 150 | /* Command message to firmware */ |
| 151 | struct cx24120_cmd { |
| 152 | u8 id; |
| 153 | u8 len; |
| 154 | u8 arg[CX24120_MAX_CMD_LEN]; |
| 155 | }; |
| 156 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 157 | /* Read single register */ |
| 158 | static int cx24120_readreg(struct cx24120_state *state, u8 reg) |
| 159 | { |
| 160 | int ret; |
| 161 | u8 buf = 0; |
| 162 | struct i2c_msg msg[] = { |
Jemma Denson | fbdbab7 | 2015-05-05 18:31:10 -0300 | [diff] [blame] | 163 | { |
| 164 | .addr = state->config->i2c_addr, |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 165 | .flags = 0, |
| 166 | .len = 1, |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 167 | .buf = ® |
| 168 | }, { |
| 169 | .addr = state->config->i2c_addr, |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 170 | .flags = I2C_M_RD, |
| 171 | .len = 1, |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 172 | .buf = &buf |
| 173 | } |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 174 | }; |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 175 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 176 | ret = i2c_transfer(state->i2c, msg, 2); |
| 177 | if (ret != 2) { |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 178 | err("Read error: reg=0x%02x, ret=%i)\n", reg, ret); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 179 | return ret; |
| 180 | } |
| 181 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 182 | dev_dbg(&state->i2c->dev, "reg=0x%02x; data=0x%02x\n", reg, buf); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 183 | |
| 184 | return buf; |
| 185 | } |
| 186 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 187 | /* Write single register */ |
| 188 | static int cx24120_writereg(struct cx24120_state *state, u8 reg, u8 data) |
| 189 | { |
| 190 | u8 buf[] = { reg, data }; |
| 191 | struct i2c_msg msg = { |
| 192 | .addr = state->config->i2c_addr, |
| 193 | .flags = 0, |
| 194 | .buf = buf, |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 195 | .len = 2 |
| 196 | }; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 197 | int ret; |
| 198 | |
| 199 | ret = i2c_transfer(state->i2c, &msg, 1); |
| 200 | if (ret != 1) { |
| 201 | err("Write error: i2c_write error(err == %i, 0x%02x: 0x%02x)\n", |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 202 | ret, reg, data); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 203 | return ret; |
| 204 | } |
| 205 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 206 | dev_dbg(&state->i2c->dev, "reg=0x%02x; data=0x%02x\n", reg, data); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Patrick Boettcher | f7a77eb | 2015-04-28 02:47:42 -0300 | [diff] [blame] | 211 | /* Write multiple registers in chunks of i2c_wr_max-sized buffers */ |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 212 | static int cx24120_writeregs(struct cx24120_state *state, |
| 213 | u8 reg, const u8 *values, u16 len, u8 incr) |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 214 | { |
| 215 | int ret; |
Patrick Boettcher | f7a77eb | 2015-04-28 02:47:42 -0300 | [diff] [blame] | 216 | u16 max = state->config->i2c_wr_max > 0 ? |
| 217 | state->config->i2c_wr_max : |
| 218 | len; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 219 | |
| 220 | struct i2c_msg msg = { |
| 221 | .addr = state->config->i2c_addr, |
| 222 | .flags = 0, |
Patrick Boettcher | f7a77eb | 2015-04-28 02:47:42 -0300 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | msg.buf = kmalloc(max + 1, GFP_KERNEL); |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 226 | if (!msg.buf) |
Patrick Boettcher | f7a77eb | 2015-04-28 02:47:42 -0300 | [diff] [blame] | 227 | return -ENOMEM; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 228 | |
| 229 | while (len) { |
Patrick Boettcher | f7a77eb | 2015-04-28 02:47:42 -0300 | [diff] [blame] | 230 | msg.buf[0] = reg; |
| 231 | msg.len = len > max ? max : len; |
| 232 | memcpy(&msg.buf[1], values, msg.len); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 233 | |
Patrick Boettcher | f7a77eb | 2015-04-28 02:47:42 -0300 | [diff] [blame] | 234 | len -= msg.len; /* data length revers counter */ |
| 235 | values += msg.len; /* incr data pointer */ |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 236 | |
| 237 | if (incr) |
| 238 | reg += msg.len; |
Patrick Boettcher | f7a77eb | 2015-04-28 02:47:42 -0300 | [diff] [blame] | 239 | msg.len++; /* don't forget the addr byte */ |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 240 | |
| 241 | ret = i2c_transfer(state->i2c, &msg, 1); |
| 242 | if (ret != 1) { |
| 243 | err("i2c_write error(err == %i, 0x%02x)\n", ret, reg); |
Patrick Boettcher | f7a77eb | 2015-04-28 02:47:42 -0300 | [diff] [blame] | 244 | goto out; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 245 | } |
| 246 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 247 | dev_dbg(&state->i2c->dev, "reg=0x%02x; data=%*ph\n", |
| 248 | reg, msg.len - 1, msg.buf + 1); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 249 | } |
| 250 | |
Patrick Boettcher | f7a77eb | 2015-04-28 02:47:42 -0300 | [diff] [blame] | 251 | ret = 0; |
| 252 | |
| 253 | out: |
| 254 | kfree(msg.buf); |
| 255 | return ret; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 256 | } |
| 257 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 258 | static struct dvb_frontend_ops cx24120_ops; |
| 259 | |
| 260 | struct dvb_frontend *cx24120_attach(const struct cx24120_config *config, |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 261 | struct i2c_adapter *i2c) |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 262 | { |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 263 | struct cx24120_state *state; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 264 | int demod_rev; |
| 265 | |
| 266 | info("Conexant cx24120/cx24118 - DVBS/S2 Satellite demod/tuner\n"); |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 267 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
| 268 | if (!state) { |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 269 | err("Unable to allocate memory for cx24120_state\n"); |
| 270 | goto error; |
| 271 | } |
| 272 | |
| 273 | /* setup the state */ |
| 274 | state->config = config; |
| 275 | state->i2c = i2c; |
| 276 | |
| 277 | /* check if the demod is present and has proper type */ |
| 278 | demod_rev = cx24120_readreg(state, CX24120_REG_REVISION); |
| 279 | switch (demod_rev) { |
| 280 | case 0x07: |
| 281 | info("Demod cx24120 rev. 0x07 detected.\n"); |
| 282 | break; |
| 283 | case 0x05: |
| 284 | info("Demod cx24120 rev. 0x05 detected.\n"); |
| 285 | break; |
| 286 | default: |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 287 | err("Unsupported demod revision: 0x%x detected.\n", demod_rev); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 288 | goto error; |
| 289 | } |
| 290 | |
| 291 | /* create dvb_frontend */ |
| 292 | state->cold_init = 0; |
| 293 | memcpy(&state->frontend.ops, &cx24120_ops, |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 294 | sizeof(struct dvb_frontend_ops)); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 295 | state->frontend.demodulator_priv = state; |
| 296 | |
| 297 | info("Conexant cx24120/cx24118 attached.\n"); |
| 298 | return &state->frontend; |
| 299 | |
| 300 | error: |
| 301 | kfree(state); |
| 302 | return NULL; |
| 303 | } |
| 304 | EXPORT_SYMBOL(cx24120_attach); |
| 305 | |
| 306 | static int cx24120_test_rom(struct cx24120_state *state) |
| 307 | { |
| 308 | int err, ret; |
| 309 | |
| 310 | err = cx24120_readreg(state, 0xfd); |
| 311 | if (err & 4) { |
| 312 | ret = cx24120_readreg(state, 0xdf) & 0xfe; |
| 313 | err = cx24120_writereg(state, 0xdf, ret); |
| 314 | } |
| 315 | return err; |
| 316 | } |
| 317 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 318 | static int cx24120_read_snr(struct dvb_frontend *fe, u16 *snr) |
| 319 | { |
Jemma Denson | 3b5eb50 | 2015-05-08 15:52:45 -0300 | [diff] [blame] | 320 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 321 | |
Jemma Denson | 3b5eb50 | 2015-05-08 15:52:45 -0300 | [diff] [blame] | 322 | if (c->cnr.stat[0].scale != FE_SCALE_DECIBEL) |
| 323 | *snr = 0; |
| 324 | else |
| 325 | *snr = div_s64(c->cnr.stat[0].svalue, 100); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 330 | static int cx24120_read_ber(struct dvb_frontend *fe, u32 *ber) |
| 331 | { |
| 332 | struct cx24120_state *state = fe->demodulator_priv; |
| 333 | |
| 334 | *ber = (cx24120_readreg(state, CX24120_REG_BER_HH) << 24) | |
| 335 | (cx24120_readreg(state, CX24120_REG_BER_HL) << 16) | |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 336 | (cx24120_readreg(state, CX24120_REG_BER_LH) << 8) | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 337 | cx24120_readreg(state, CX24120_REG_BER_LL); |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 338 | dev_dbg(&state->i2c->dev, "read BER index = %d\n", *ber); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static int cx24120_msg_mpeg_output_global_config(struct cx24120_state *state, |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 344 | u8 flag); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 345 | |
| 346 | /* Check if we're running a command that needs to disable mpeg out */ |
| 347 | static void cx24120_check_cmd(struct cx24120_state *state, u8 id) |
| 348 | { |
| 349 | switch (id) { |
| 350 | case CMD_TUNEREQUEST: |
| 351 | case CMD_CLOCK_READ: |
| 352 | case CMD_DISEQC_MSG1: |
| 353 | case CMD_DISEQC_MSG2: |
| 354 | case CMD_SETVOLTAGE: |
| 355 | case CMD_SETTONE: |
Jemma Denson | 270e70713 | 2015-04-30 17:05:14 -0300 | [diff] [blame] | 356 | case CMD_DISEQC_BURST: |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 357 | cx24120_msg_mpeg_output_global_config(state, 0); |
| 358 | /* Old driver would do a msleep(100) here */ |
| 359 | default: |
| 360 | return; |
| 361 | } |
| 362 | } |
| 363 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 364 | /* Send a message to the firmware */ |
| 365 | static int cx24120_message_send(struct cx24120_state *state, |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 366 | struct cx24120_cmd *cmd) |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 367 | { |
Mauro Carvalho Chehab | 65b0166 | 2015-05-19 08:04:35 -0300 | [diff] [blame] | 368 | int ficus; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 369 | |
| 370 | if (state->mpeg_enabled) { |
| 371 | /* Disable mpeg out on certain commands */ |
| 372 | cx24120_check_cmd(state, cmd->id); |
| 373 | } |
| 374 | |
Mauro Carvalho Chehab | 65b0166 | 2015-05-19 08:04:35 -0300 | [diff] [blame] | 375 | cx24120_writereg(state, CX24120_REG_CMD_START, cmd->id); |
| 376 | cx24120_writeregs(state, CX24120_REG_CMD_ARGS, &cmd->arg[0], |
| 377 | cmd->len, 1); |
| 378 | cx24120_writereg(state, CX24120_REG_CMD_END, 0x01); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 379 | |
| 380 | ficus = 1000; |
| 381 | while (cx24120_readreg(state, CX24120_REG_CMD_END)) { |
| 382 | msleep(20); |
| 383 | ficus -= 20; |
| 384 | if (ficus == 0) { |
| 385 | err("Error sending message to firmware\n"); |
| 386 | return -EREMOTEIO; |
| 387 | } |
| 388 | } |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 389 | dev_dbg(&state->i2c->dev, "sent message 0x%02x\n", cmd->id); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | /* Send a message and fill arg[] with the results */ |
| 395 | static int cx24120_message_sendrcv(struct cx24120_state *state, |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 396 | struct cx24120_cmd *cmd, u8 numreg) |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 397 | { |
| 398 | int ret, i; |
| 399 | |
| 400 | if (numreg > CX24120_MAX_CMD_LEN) { |
| 401 | err("Too many registers to read. cmd->reg = %d", numreg); |
| 402 | return -EREMOTEIO; |
| 403 | } |
| 404 | |
| 405 | ret = cx24120_message_send(state, cmd); |
| 406 | if (ret != 0) |
| 407 | return ret; |
| 408 | |
| 409 | if (!numreg) |
| 410 | return 0; |
| 411 | |
| 412 | /* Read numreg registers starting from register cmd->len */ |
| 413 | for (i = 0; i < numreg; i++) |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 414 | cmd->arg[i] = cx24120_readreg(state, (cmd->len + i + 1)); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 419 | static int cx24120_read_signal_strength(struct dvb_frontend *fe, |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 420 | u16 *signal_strength) |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 421 | { |
Jemma Denson | 34ce475 | 2015-05-05 17:47:55 -0300 | [diff] [blame] | 422 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 423 | |
Jemma Denson | 34ce475 | 2015-05-05 17:47:55 -0300 | [diff] [blame] | 424 | if (c->strength.stat[0].scale != FE_SCALE_RELATIVE) |
| 425 | *signal_strength = 0; |
| 426 | else |
| 427 | *signal_strength = c->strength.stat[0].uvalue; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 428 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 429 | return 0; |
| 430 | } |
| 431 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 432 | static int cx24120_msg_mpeg_output_global_config(struct cx24120_state *state, |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 433 | u8 enable) |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 434 | { |
| 435 | struct cx24120_cmd cmd; |
| 436 | int ret; |
| 437 | |
| 438 | cmd.id = CMD_MPEG_ONOFF; |
| 439 | cmd.len = 4; |
| 440 | cmd.arg[0] = 0x01; |
| 441 | cmd.arg[1] = 0x00; |
| 442 | cmd.arg[2] = enable ? 0 : (u8)(-1); |
| 443 | cmd.arg[3] = 0x01; |
| 444 | |
| 445 | ret = cx24120_message_send(state, &cmd); |
| 446 | if (ret != 0) { |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 447 | dev_dbg(&state->i2c->dev, "failed to %s MPEG output\n", |
| 448 | enable ? "enable" : "disable"); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 449 | return ret; |
| 450 | } |
| 451 | |
| 452 | state->mpeg_enabled = enable; |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 453 | dev_dbg(&state->i2c->dev, "MPEG output %s\n", |
| 454 | enable ? "enabled" : "disabled"); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 459 | static int cx24120_msg_mpeg_output_config(struct cx24120_state *state, u8 seq) |
| 460 | { |
| 461 | struct cx24120_cmd cmd; |
| 462 | struct cx24120_initial_mpeg_config i = |
| 463 | state->config->initial_mpeg_config; |
| 464 | |
| 465 | cmd.id = CMD_MPEG_INIT; |
| 466 | cmd.len = 7; |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 467 | cmd.arg[0] = seq; /* sequental number - can be 0,1,2 */ |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 468 | cmd.arg[1] = ((i.x1 & 0x01) << 1) | ((i.x1 >> 1) & 0x01); |
| 469 | cmd.arg[2] = 0x05; |
| 470 | cmd.arg[3] = 0x02; |
| 471 | cmd.arg[4] = ((i.x2 >> 1) & 0x01); |
| 472 | cmd.arg[5] = (i.x2 & 0xf0) | (i.x3 & 0x0f); |
| 473 | cmd.arg[6] = 0x10; |
| 474 | |
| 475 | return cx24120_message_send(state, &cmd); |
| 476 | } |
| 477 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 478 | static int cx24120_diseqc_send_burst(struct dvb_frontend *fe, |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 479 | fe_sec_mini_cmd_t burst) |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 480 | { |
| 481 | struct cx24120_state *state = fe->demodulator_priv; |
| 482 | struct cx24120_cmd cmd; |
| 483 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 484 | dev_dbg(&state->i2c->dev, "\n"); |
| 485 | |
Jemma Denson | fbdbab7 | 2015-05-05 18:31:10 -0300 | [diff] [blame] | 486 | /* |
| 487 | * Yes, cmd.len is set to zero. The old driver |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 488 | * didn't specify any len, but also had a |
| 489 | * memset 0 before every use of the cmd struct |
| 490 | * which would have set it to zero. |
| 491 | * This quite probably needs looking into. |
| 492 | */ |
| 493 | cmd.id = CMD_DISEQC_BURST; |
| 494 | cmd.len = 0; |
| 495 | cmd.arg[0] = 0x00; |
Jemma Denson | 7c95e25 | 2015-05-05 18:33:27 -0300 | [diff] [blame] | 496 | cmd.arg[1] = (burst == SEC_MINI_B) ? 0x01 : 0x00; |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 497 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 498 | return cx24120_message_send(state, &cmd); |
| 499 | } |
| 500 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 501 | static int cx24120_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone) |
| 502 | { |
| 503 | struct cx24120_state *state = fe->demodulator_priv; |
| 504 | struct cx24120_cmd cmd; |
| 505 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 506 | dev_dbg(&state->i2c->dev, "(%d)\n", tone); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 507 | |
| 508 | if ((tone != SEC_TONE_ON) && (tone != SEC_TONE_OFF)) { |
| 509 | err("Invalid tone=%d\n", tone); |
| 510 | return -EINVAL; |
| 511 | } |
| 512 | |
| 513 | cmd.id = CMD_SETTONE; |
| 514 | cmd.len = 4; |
| 515 | cmd.arg[0] = 0x00; |
| 516 | cmd.arg[1] = 0x00; |
| 517 | cmd.arg[2] = 0x00; |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 518 | cmd.arg[3] = (tone == SEC_TONE_ON) ? 0x01 : 0x00; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 519 | |
| 520 | return cx24120_message_send(state, &cmd); |
| 521 | } |
| 522 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 523 | static int cx24120_set_voltage(struct dvb_frontend *fe, |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 524 | fe_sec_voltage_t voltage) |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 525 | { |
| 526 | struct cx24120_state *state = fe->demodulator_priv; |
| 527 | struct cx24120_cmd cmd; |
| 528 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 529 | dev_dbg(&state->i2c->dev, "(%d)\n", voltage); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 530 | |
| 531 | cmd.id = CMD_SETVOLTAGE; |
| 532 | cmd.len = 2; |
| 533 | cmd.arg[0] = 0x00; |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 534 | cmd.arg[1] = (voltage == SEC_VOLTAGE_18) ? 0x01 : 0x00; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 535 | |
| 536 | return cx24120_message_send(state, &cmd); |
| 537 | } |
| 538 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 539 | static int cx24120_send_diseqc_msg(struct dvb_frontend *fe, |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 540 | struct dvb_diseqc_master_cmd *d) |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 541 | { |
| 542 | struct cx24120_state *state = fe->demodulator_priv; |
| 543 | struct cx24120_cmd cmd; |
| 544 | int back_count; |
| 545 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 546 | dev_dbg(&state->i2c->dev, "\n"); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 547 | |
| 548 | cmd.id = CMD_DISEQC_MSG1; |
| 549 | cmd.len = 11; |
| 550 | cmd.arg[0] = 0x00; |
| 551 | cmd.arg[1] = 0x00; |
| 552 | cmd.arg[2] = 0x03; |
| 553 | cmd.arg[3] = 0x16; |
| 554 | cmd.arg[4] = 0x28; |
| 555 | cmd.arg[5] = 0x01; |
| 556 | cmd.arg[6] = 0x01; |
| 557 | cmd.arg[7] = 0x14; |
| 558 | cmd.arg[8] = 0x19; |
| 559 | cmd.arg[9] = 0x14; |
| 560 | cmd.arg[10] = 0x1e; |
| 561 | |
| 562 | if (cx24120_message_send(state, &cmd)) { |
| 563 | err("send 1st message(0x%x) failed\n", cmd.id); |
| 564 | return -EREMOTEIO; |
| 565 | } |
| 566 | |
| 567 | cmd.id = CMD_DISEQC_MSG2; |
| 568 | cmd.len = d->msg_len + 6; |
| 569 | cmd.arg[0] = 0x00; |
| 570 | cmd.arg[1] = 0x01; |
| 571 | cmd.arg[2] = 0x02; |
| 572 | cmd.arg[3] = 0x00; |
| 573 | cmd.arg[4] = 0x00; |
| 574 | cmd.arg[5] = d->msg_len; |
| 575 | |
| 576 | memcpy(&cmd.arg[6], &d->msg, d->msg_len); |
| 577 | |
| 578 | if (cx24120_message_send(state, &cmd)) { |
| 579 | err("send 2nd message(0x%x) failed\n", cmd.id); |
| 580 | return -EREMOTEIO; |
| 581 | } |
| 582 | |
| 583 | back_count = 500; |
| 584 | do { |
| 585 | if (!(cx24120_readreg(state, 0x93) & 0x01)) { |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 586 | dev_dbg(&state->i2c->dev, "diseqc sequence sent\n"); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 587 | return 0; |
| 588 | } |
| 589 | msleep(20); |
| 590 | back_count -= 20; |
| 591 | } while (back_count); |
| 592 | |
| 593 | err("Too long waiting for diseqc.\n"); |
| 594 | return -ETIMEDOUT; |
| 595 | } |
| 596 | |
Jemma Denson | 1462612 | 2015-05-05 17:18:11 -0300 | [diff] [blame] | 597 | static void cx24120_get_stats(struct cx24120_state *state) |
Jemma Denson | 9fc18f1 | 2015-05-05 16:59:27 -0300 | [diff] [blame] | 598 | { |
| 599 | struct dvb_frontend *fe = &state->frontend; |
| 600 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Jemma Denson | 34ce475 | 2015-05-05 17:47:55 -0300 | [diff] [blame] | 601 | struct cx24120_cmd cmd; |
Jemma Denson | 3b5eb50 | 2015-05-08 15:52:45 -0300 | [diff] [blame] | 602 | int ret, cnr; |
Jemma Denson | b0cdf1a | 2015-05-05 18:09:45 -0300 | [diff] [blame] | 603 | u16 sig; |
Jemma Denson | 9fc18f1 | 2015-05-05 16:59:27 -0300 | [diff] [blame] | 604 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 605 | dev_dbg(&state->i2c->dev, "\n"); |
Jemma Denson | 9fc18f1 | 2015-05-05 16:59:27 -0300 | [diff] [blame] | 606 | |
| 607 | /* signal strength */ |
Jemma Denson | 1462612 | 2015-05-05 17:18:11 -0300 | [diff] [blame] | 608 | if (state->fe_status & FE_HAS_SIGNAL) { |
Jemma Denson | 34ce475 | 2015-05-05 17:47:55 -0300 | [diff] [blame] | 609 | cmd.id = CMD_READ_SNR; |
| 610 | cmd.len = 1; |
| 611 | cmd.arg[0] = 0x00; |
| 612 | |
| 613 | ret = cx24120_message_send(state, &cmd); |
| 614 | if (ret != 0) { |
| 615 | err("error reading signal strength\n"); |
Jemma Denson | 9fc18f1 | 2015-05-05 16:59:27 -0300 | [diff] [blame] | 616 | return; |
Jemma Denson | 34ce475 | 2015-05-05 17:47:55 -0300 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /* raw */ |
Jemma Denson | b0cdf1a | 2015-05-05 18:09:45 -0300 | [diff] [blame] | 620 | sig = cx24120_readreg(state, CX24120_REG_SIGSTR_H) >> 6; |
| 621 | sig = sig << 8; |
| 622 | sig |= cx24120_readreg(state, CX24120_REG_SIGSTR_L); |
| 623 | dev_dbg(&state->i2c->dev, |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 624 | "signal strength from firmware = 0x%x\n", sig); |
Jemma Denson | 34ce475 | 2015-05-05 17:47:55 -0300 | [diff] [blame] | 625 | |
| 626 | /* cooked */ |
Jemma Denson | b0cdf1a | 2015-05-05 18:09:45 -0300 | [diff] [blame] | 627 | sig = -100 * sig + 94324; |
Jemma Denson | 9fc18f1 | 2015-05-05 16:59:27 -0300 | [diff] [blame] | 628 | |
| 629 | c->strength.stat[0].scale = FE_SCALE_RELATIVE; |
Jemma Denson | b0cdf1a | 2015-05-05 18:09:45 -0300 | [diff] [blame] | 630 | c->strength.stat[0].uvalue = sig; |
Jemma Denson | 9fc18f1 | 2015-05-05 16:59:27 -0300 | [diff] [blame] | 631 | } else { |
| 632 | c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 633 | } |
| 634 | |
Jemma Denson | 3b5eb50 | 2015-05-08 15:52:45 -0300 | [diff] [blame] | 635 | /* CNR */ |
| 636 | if (state->fe_status & FE_HAS_VITERBI) { |
| 637 | cnr = cx24120_readreg(state, CX24120_REG_QUALITY_H) << 8; |
| 638 | cnr |= cx24120_readreg(state, CX24120_REG_QUALITY_L); |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 639 | dev_dbg(&state->i2c->dev, "read SNR index = %d\n", cnr); |
Jemma Denson | 3b5eb50 | 2015-05-08 15:52:45 -0300 | [diff] [blame] | 640 | |
| 641 | /* guessed - seems about right */ |
| 642 | cnr = cnr * 100; |
| 643 | |
| 644 | c->cnr.stat[0].scale = FE_SCALE_DECIBEL; |
| 645 | c->cnr.stat[0].svalue = cnr; |
| 646 | } else { |
| 647 | c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 648 | } |
Jemma Denson | 9fc18f1 | 2015-05-05 16:59:27 -0300 | [diff] [blame] | 649 | |
| 650 | /* FIXME: add UCB/BER */ |
| 651 | } |
| 652 | |
Jemma Denson | 6138dc2 | 2015-04-30 16:37:42 -0300 | [diff] [blame] | 653 | static void cx24120_set_clock_ratios(struct dvb_frontend *fe); |
| 654 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 655 | /* Read current tuning status */ |
| 656 | static int cx24120_read_status(struct dvb_frontend *fe, fe_status_t *status) |
| 657 | { |
| 658 | struct cx24120_state *state = fe->demodulator_priv; |
| 659 | int lock; |
| 660 | |
| 661 | lock = cx24120_readreg(state, CX24120_REG_STATUS); |
| 662 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 663 | dev_dbg(&state->i2c->dev, "status = 0x%02x\n", lock); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 664 | |
| 665 | *status = 0; |
| 666 | |
| 667 | if (lock & CX24120_HAS_SIGNAL) |
| 668 | *status = FE_HAS_SIGNAL; |
| 669 | if (lock & CX24120_HAS_CARRIER) |
| 670 | *status |= FE_HAS_CARRIER; |
| 671 | if (lock & CX24120_HAS_VITERBI) |
| 672 | *status |= FE_HAS_VITERBI | FE_HAS_SYNC; |
| 673 | if (lock & CX24120_HAS_LOCK) |
| 674 | *status |= FE_HAS_LOCK; |
| 675 | |
Jemma Denson | fbdbab7 | 2015-05-05 18:31:10 -0300 | [diff] [blame] | 676 | /* |
| 677 | * TODO: is FE_HAS_SYNC in the right place? |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 678 | * Other cx241xx drivers have this slightly |
Jemma Denson | fbdbab7 | 2015-05-05 18:31:10 -0300 | [diff] [blame] | 679 | * different |
| 680 | */ |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 681 | |
Jemma Denson | 1462612 | 2015-05-05 17:18:11 -0300 | [diff] [blame] | 682 | state->fe_status = *status; |
| 683 | cx24120_get_stats(state); |
Jemma Denson | 9fc18f1 | 2015-05-05 16:59:27 -0300 | [diff] [blame] | 684 | |
Jemma Denson | 6138dc2 | 2015-04-30 16:37:42 -0300 | [diff] [blame] | 685 | /* Set the clock once tuned in */ |
| 686 | if (state->need_clock_set && *status & FE_HAS_LOCK) { |
| 687 | /* Set clock ratios */ |
| 688 | cx24120_set_clock_ratios(fe); |
| 689 | |
| 690 | /* Old driver would do a msleep(200) here */ |
| 691 | |
| 692 | /* Renable mpeg output */ |
| 693 | if (!state->mpeg_enabled) |
| 694 | cx24120_msg_mpeg_output_global_config(state, 1); |
| 695 | |
| 696 | state->need_clock_set = 0; |
| 697 | } |
| 698 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 699 | return 0; |
| 700 | } |
| 701 | |
Jemma Denson | fbdbab7 | 2015-05-05 18:31:10 -0300 | [diff] [blame] | 702 | /* |
| 703 | * FEC & modulation lookup table |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 704 | * Used for decoding the REG_FECMODE register |
| 705 | * once tuned in. |
| 706 | */ |
Mauro Carvalho Chehab | ec8fe6c | 2015-05-19 08:19:47 -0300 | [diff] [blame^] | 707 | struct cx24120_modfec { |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 708 | fe_delivery_system_t delsys; |
| 709 | fe_modulation_t mod; |
| 710 | fe_code_rate_t fec; |
| 711 | u8 val; |
Mauro Carvalho Chehab | ec8fe6c | 2015-05-19 08:19:47 -0300 | [diff] [blame^] | 712 | }; |
| 713 | |
| 714 | static const struct cx24120_modfec modfec_lookup_table[] = { |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 715 | /*delsys mod fec val */ |
| 716 | { SYS_DVBS, QPSK, FEC_1_2, 0x01 }, |
| 717 | { SYS_DVBS, QPSK, FEC_2_3, 0x02 }, |
| 718 | { SYS_DVBS, QPSK, FEC_3_4, 0x03 }, |
| 719 | { SYS_DVBS, QPSK, FEC_4_5, 0x04 }, |
| 720 | { SYS_DVBS, QPSK, FEC_5_6, 0x05 }, |
| 721 | { SYS_DVBS, QPSK, FEC_6_7, 0x06 }, |
| 722 | { SYS_DVBS, QPSK, FEC_7_8, 0x07 }, |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 723 | |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 724 | { SYS_DVBS2, QPSK, FEC_1_2, 0x04 }, |
| 725 | { SYS_DVBS2, QPSK, FEC_3_5, 0x05 }, |
| 726 | { SYS_DVBS2, QPSK, FEC_2_3, 0x06 }, |
| 727 | { SYS_DVBS2, QPSK, FEC_3_4, 0x07 }, |
| 728 | { SYS_DVBS2, QPSK, FEC_4_5, 0x08 }, |
| 729 | { SYS_DVBS2, QPSK, FEC_5_6, 0x09 }, |
| 730 | { SYS_DVBS2, QPSK, FEC_8_9, 0x0a }, |
| 731 | { SYS_DVBS2, QPSK, FEC_9_10, 0x0b }, |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 732 | |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 733 | { SYS_DVBS2, PSK_8, FEC_3_5, 0x0c }, |
| 734 | { SYS_DVBS2, PSK_8, FEC_2_3, 0x0d }, |
| 735 | { SYS_DVBS2, PSK_8, FEC_3_4, 0x0e }, |
| 736 | { SYS_DVBS2, PSK_8, FEC_5_6, 0x0f }, |
| 737 | { SYS_DVBS2, PSK_8, FEC_8_9, 0x10 }, |
| 738 | { SYS_DVBS2, PSK_8, FEC_9_10, 0x11 }, |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 739 | }; |
| 740 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 741 | /* Retrieve current fec, modulation & pilot values */ |
| 742 | static int cx24120_get_fec(struct dvb_frontend *fe) |
| 743 | { |
| 744 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 745 | struct cx24120_state *state = fe->demodulator_priv; |
| 746 | int idx; |
| 747 | int ret; |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 748 | int fec; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 749 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 750 | ret = cx24120_readreg(state, CX24120_REG_FECMODE); |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 751 | fec = ret & 0x3f; /* Lower 6 bits */ |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 752 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 753 | dev_dbg(&state->i2c->dev, "raw fec = %d\n", fec); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 754 | |
| 755 | for (idx = 0; idx < ARRAY_SIZE(modfec_lookup_table); idx++) { |
| 756 | if (modfec_lookup_table[idx].delsys != state->dcur.delsys) |
| 757 | continue; |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 758 | if (modfec_lookup_table[idx].val != fec) |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 759 | continue; |
| 760 | |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 761 | break; /* found */ |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | if (idx >= ARRAY_SIZE(modfec_lookup_table)) { |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 765 | dev_dbg(&state->i2c->dev, "couldn't find fec!\n"); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 766 | return -EINVAL; |
| 767 | } |
| 768 | |
| 769 | /* save values back to cache */ |
| 770 | c->modulation = modfec_lookup_table[idx].mod; |
| 771 | c->fec_inner = modfec_lookup_table[idx].fec; |
| 772 | c->pilot = (ret & 0x80) ? PILOT_ON : PILOT_OFF; |
| 773 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 774 | dev_dbg(&state->i2c->dev, "mod(%d), fec(%d), pilot(%d)\n", |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 775 | c->modulation, c->fec_inner, c->pilot); |
| 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | |
Jemma Denson | fbdbab7 | 2015-05-05 18:31:10 -0300 | [diff] [blame] | 780 | /* |
| 781 | * Clock ratios lookup table |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 782 | * |
| 783 | * Values obtained from much larger table in old driver |
| 784 | * which had numerous entries which would never match. |
| 785 | * |
| 786 | * There's probably some way of calculating these but I |
| 787 | * can't determine the pattern |
Jemma Denson | fbdbab7 | 2015-05-05 18:31:10 -0300 | [diff] [blame] | 788 | */ |
Mauro Carvalho Chehab | ec8fe6c | 2015-05-19 08:19:47 -0300 | [diff] [blame^] | 789 | struct cx24120_clock_ratios_table { |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 790 | fe_delivery_system_t delsys; |
| 791 | fe_pilot_t pilot; |
| 792 | fe_modulation_t mod; |
| 793 | fe_code_rate_t fec; |
| 794 | u32 m_rat; |
| 795 | u32 n_rat; |
| 796 | u32 rate; |
Mauro Carvalho Chehab | ec8fe6c | 2015-05-19 08:19:47 -0300 | [diff] [blame^] | 797 | }; |
| 798 | |
| 799 | static const struct cx24120_clock_ratios_table clock_ratios_table[] = { |
Patrick Boettcher | 2e89a5e | 2015-04-28 13:18:05 -0300 | [diff] [blame] | 800 | /*delsys pilot mod fec m_rat n_rat rate */ |
| 801 | { SYS_DVBS2, PILOT_OFF, QPSK, FEC_1_2, 273088, 254505, 274 }, |
| 802 | { SYS_DVBS2, PILOT_OFF, QPSK, FEC_3_5, 17272, 13395, 330 }, |
| 803 | { SYS_DVBS2, PILOT_OFF, QPSK, FEC_2_3, 24344, 16967, 367 }, |
| 804 | { SYS_DVBS2, PILOT_OFF, QPSK, FEC_3_4, 410788, 254505, 413 }, |
| 805 | { SYS_DVBS2, PILOT_OFF, QPSK, FEC_4_5, 438328, 254505, 440 }, |
| 806 | { SYS_DVBS2, PILOT_OFF, QPSK, FEC_5_6, 30464, 16967, 459 }, |
| 807 | { SYS_DVBS2, PILOT_OFF, QPSK, FEC_8_9, 487832, 254505, 490 }, |
| 808 | { SYS_DVBS2, PILOT_OFF, QPSK, FEC_9_10, 493952, 254505, 496 }, |
| 809 | { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_3_5, 328168, 169905, 494 }, |
| 810 | { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_2_3, 24344, 11327, 550 }, |
| 811 | { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_3_4, 410788, 169905, 618 }, |
| 812 | { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_5_6, 30464, 11327, 688 }, |
| 813 | { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_8_9, 487832, 169905, 735 }, |
| 814 | { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_9_10, 493952, 169905, 744 }, |
| 815 | { SYS_DVBS2, PILOT_ON, QPSK, FEC_1_2, 273088, 260709, 268 }, |
| 816 | { SYS_DVBS2, PILOT_ON, QPSK, FEC_3_5, 328168, 260709, 322 }, |
| 817 | { SYS_DVBS2, PILOT_ON, QPSK, FEC_2_3, 121720, 86903, 358 }, |
| 818 | { SYS_DVBS2, PILOT_ON, QPSK, FEC_3_4, 410788, 260709, 403 }, |
| 819 | { SYS_DVBS2, PILOT_ON, QPSK, FEC_4_5, 438328, 260709, 430 }, |
| 820 | { SYS_DVBS2, PILOT_ON, QPSK, FEC_5_6, 152320, 86903, 448 }, |
| 821 | { SYS_DVBS2, PILOT_ON, QPSK, FEC_8_9, 487832, 260709, 479 }, |
| 822 | { SYS_DVBS2, PILOT_ON, QPSK, FEC_9_10, 493952, 260709, 485 }, |
| 823 | { SYS_DVBS2, PILOT_ON, PSK_8, FEC_3_5, 328168, 173853, 483 }, |
| 824 | { SYS_DVBS2, PILOT_ON, PSK_8, FEC_2_3, 121720, 57951, 537 }, |
| 825 | { SYS_DVBS2, PILOT_ON, PSK_8, FEC_3_4, 410788, 173853, 604 }, |
| 826 | { SYS_DVBS2, PILOT_ON, PSK_8, FEC_5_6, 152320, 57951, 672 }, |
| 827 | { SYS_DVBS2, PILOT_ON, PSK_8, FEC_8_9, 487832, 173853, 718 }, |
| 828 | { SYS_DVBS2, PILOT_ON, PSK_8, FEC_9_10, 493952, 173853, 727 }, |
| 829 | { SYS_DVBS, PILOT_OFF, QPSK, FEC_1_2, 152592, 152592, 256 }, |
| 830 | { SYS_DVBS, PILOT_OFF, QPSK, FEC_2_3, 305184, 228888, 341 }, |
| 831 | { SYS_DVBS, PILOT_OFF, QPSK, FEC_3_4, 457776, 305184, 384 }, |
| 832 | { SYS_DVBS, PILOT_OFF, QPSK, FEC_5_6, 762960, 457776, 427 }, |
| 833 | { SYS_DVBS, PILOT_OFF, QPSK, FEC_7_8, 1068144, 610368, 448 }, |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 834 | }; |
| 835 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 836 | /* Set clock ratio from lookup table */ |
| 837 | static void cx24120_set_clock_ratios(struct dvb_frontend *fe) |
| 838 | { |
| 839 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 840 | struct cx24120_state *state = fe->demodulator_priv; |
| 841 | struct cx24120_cmd cmd; |
| 842 | int ret, idx; |
| 843 | |
| 844 | /* Find fec, modulation, pilot */ |
| 845 | ret = cx24120_get_fec(fe); |
| 846 | if (ret != 0) |
| 847 | return; |
| 848 | |
| 849 | /* Find the clock ratios in the lookup table */ |
| 850 | for (idx = 0; idx < ARRAY_SIZE(clock_ratios_table); idx++) { |
| 851 | if (clock_ratios_table[idx].delsys != state->dcur.delsys) |
| 852 | continue; |
| 853 | if (clock_ratios_table[idx].mod != c->modulation) |
| 854 | continue; |
| 855 | if (clock_ratios_table[idx].fec != c->fec_inner) |
| 856 | continue; |
| 857 | if (clock_ratios_table[idx].pilot != c->pilot) |
| 858 | continue; |
| 859 | |
| 860 | break; /* found */ |
| 861 | } |
| 862 | |
| 863 | if (idx >= ARRAY_SIZE(clock_ratios_table)) { |
| 864 | info("Clock ratio not found - data reception in danger\n"); |
| 865 | return; |
| 866 | } |
| 867 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 868 | /* Read current values? */ |
| 869 | cmd.id = CMD_CLOCK_READ; |
| 870 | cmd.len = 1; |
| 871 | cmd.arg[0] = 0x00; |
| 872 | ret = cx24120_message_sendrcv(state, &cmd, 6); |
| 873 | if (ret != 0) |
| 874 | return; |
| 875 | /* in cmd[0]-[5] - result */ |
| 876 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 877 | dev_dbg(&state->i2c->dev, "m=%d, n=%d; idx: %d m=%d, n=%d, rate=%d\n", |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 878 | cmd.arg[2] | (cmd.arg[1] << 8) | (cmd.arg[0] << 16), |
| 879 | cmd.arg[5] | (cmd.arg[4] << 8) | (cmd.arg[3] << 16), |
| 880 | idx, |
| 881 | clock_ratios_table[idx].m_rat, |
| 882 | clock_ratios_table[idx].n_rat, |
| 883 | clock_ratios_table[idx].rate); |
| 884 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 885 | /* Set the clock */ |
| 886 | cmd.id = CMD_CLOCK_SET; |
| 887 | cmd.len = 10; |
| 888 | cmd.arg[0] = 0; |
| 889 | cmd.arg[1] = 0x10; |
| 890 | cmd.arg[2] = (clock_ratios_table[idx].m_rat >> 16) & 0xff; |
| 891 | cmd.arg[3] = (clock_ratios_table[idx].m_rat >> 8) & 0xff; |
| 892 | cmd.arg[4] = (clock_ratios_table[idx].m_rat >> 0) & 0xff; |
| 893 | cmd.arg[5] = (clock_ratios_table[idx].n_rat >> 16) & 0xff; |
| 894 | cmd.arg[6] = (clock_ratios_table[idx].n_rat >> 8) & 0xff; |
| 895 | cmd.arg[7] = (clock_ratios_table[idx].n_rat >> 0) & 0xff; |
| 896 | cmd.arg[8] = (clock_ratios_table[idx].rate >> 8) & 0xff; |
| 897 | cmd.arg[9] = (clock_ratios_table[idx].rate >> 0) & 0xff; |
| 898 | |
| 899 | cx24120_message_send(state, &cmd); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 900 | } |
| 901 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 902 | /* Set inversion value */ |
| 903 | static int cx24120_set_inversion(struct cx24120_state *state, |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 904 | fe_spectral_inversion_t inversion) |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 905 | { |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 906 | dev_dbg(&state->i2c->dev, "(%d)\n", inversion); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 907 | |
| 908 | switch (inversion) { |
| 909 | case INVERSION_OFF: |
| 910 | state->dnxt.inversion_val = 0x00; |
| 911 | break; |
| 912 | case INVERSION_ON: |
| 913 | state->dnxt.inversion_val = 0x04; |
| 914 | break; |
| 915 | case INVERSION_AUTO: |
| 916 | state->dnxt.inversion_val = 0x0c; |
| 917 | break; |
| 918 | default: |
| 919 | return -EINVAL; |
| 920 | } |
| 921 | |
| 922 | state->dnxt.inversion = inversion; |
| 923 | |
| 924 | return 0; |
| 925 | } |
| 926 | |
Jemma Denson | 5c0a1c2 | 2015-05-15 16:08:26 -0300 | [diff] [blame] | 927 | /* FEC lookup table for tuning */ |
Mauro Carvalho Chehab | ec8fe6c | 2015-05-19 08:19:47 -0300 | [diff] [blame^] | 928 | struct cx24120_modfec_table { |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 929 | fe_delivery_system_t delsys; |
| 930 | fe_modulation_t mod; |
| 931 | fe_code_rate_t fec; |
| 932 | u8 val; |
Mauro Carvalho Chehab | ec8fe6c | 2015-05-19 08:19:47 -0300 | [diff] [blame^] | 933 | }; |
| 934 | |
| 935 | static const struct cx24120_modfec_table modfec_table[] = { |
Jemma Denson | 5c0a1c2 | 2015-05-15 16:08:26 -0300 | [diff] [blame] | 936 | /*delsys mod fec val */ |
| 937 | { SYS_DVBS, QPSK, FEC_1_2, 0x2e }, |
| 938 | { SYS_DVBS, QPSK, FEC_2_3, 0x2f }, |
| 939 | { SYS_DVBS, QPSK, FEC_3_4, 0x30 }, |
| 940 | { SYS_DVBS, QPSK, FEC_5_6, 0x31 }, |
| 941 | { SYS_DVBS, QPSK, FEC_6_7, 0x32 }, |
| 942 | { SYS_DVBS, QPSK, FEC_7_8, 0x33 }, |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 943 | |
Jemma Denson | 5c0a1c2 | 2015-05-15 16:08:26 -0300 | [diff] [blame] | 944 | { SYS_DVBS2, QPSK, FEC_1_2, 0x04 }, |
| 945 | { SYS_DVBS2, QPSK, FEC_3_5, 0x05 }, |
| 946 | { SYS_DVBS2, QPSK, FEC_2_3, 0x06 }, |
| 947 | { SYS_DVBS2, QPSK, FEC_3_4, 0x07 }, |
| 948 | { SYS_DVBS2, QPSK, FEC_4_5, 0x08 }, |
| 949 | { SYS_DVBS2, QPSK, FEC_5_6, 0x09 }, |
| 950 | { SYS_DVBS2, QPSK, FEC_8_9, 0x0a }, |
| 951 | { SYS_DVBS2, QPSK, FEC_9_10, 0x0b }, |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 952 | |
Jemma Denson | 5c0a1c2 | 2015-05-15 16:08:26 -0300 | [diff] [blame] | 953 | { SYS_DVBS2, PSK_8, FEC_3_5, 0x0c }, |
| 954 | { SYS_DVBS2, PSK_8, FEC_2_3, 0x0d }, |
| 955 | { SYS_DVBS2, PSK_8, FEC_3_4, 0x0e }, |
| 956 | { SYS_DVBS2, PSK_8, FEC_5_6, 0x0f }, |
| 957 | { SYS_DVBS2, PSK_8, FEC_8_9, 0x10 }, |
| 958 | { SYS_DVBS2, PSK_8, FEC_9_10, 0x11 }, |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 959 | }; |
| 960 | |
| 961 | /* Set fec_val & fec_mask values from delsys, modulation & fec */ |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 962 | static int cx24120_set_fec(struct cx24120_state *state, fe_modulation_t mod, |
| 963 | fe_code_rate_t fec) |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 964 | { |
| 965 | int idx; |
| 966 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 967 | dev_dbg(&state->i2c->dev, "(0x%02x,0x%02x)\n", mod, fec); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 968 | |
| 969 | state->dnxt.fec = fec; |
| 970 | |
| 971 | /* Lookup fec_val from modfec table */ |
| 972 | for (idx = 0; idx < ARRAY_SIZE(modfec_table); idx++) { |
| 973 | if (modfec_table[idx].delsys != state->dnxt.delsys) |
| 974 | continue; |
| 975 | if (modfec_table[idx].mod != mod) |
| 976 | continue; |
| 977 | if (modfec_table[idx].fec != fec) |
| 978 | continue; |
| 979 | |
| 980 | /* found */ |
| 981 | state->dnxt.fec_mask = 0x00; |
| 982 | state->dnxt.fec_val = modfec_table[idx].val; |
| 983 | return 0; |
| 984 | } |
| 985 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 986 | if (state->dnxt.delsys == SYS_DVBS2) { |
| 987 | /* DVBS2 auto is 0x00/0x00 */ |
| 988 | state->dnxt.fec_mask = 0x00; |
| 989 | state->dnxt.fec_val = 0x00; |
| 990 | } else { |
| 991 | /* Set DVB-S to auto */ |
| 992 | state->dnxt.fec_val = 0x2e; |
| 993 | state->dnxt.fec_mask = 0xac; |
| 994 | } |
| 995 | |
| 996 | return 0; |
| 997 | } |
| 998 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 999 | /* Set pilot */ |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 1000 | static int cx24120_set_pilot(struct cx24120_state *state, fe_pilot_t pilot) |
| 1001 | { |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1002 | dev_dbg(&state->i2c->dev, "(%d)\n", pilot); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1003 | |
| 1004 | /* Pilot only valid in DVBS2 */ |
| 1005 | if (state->dnxt.delsys != SYS_DVBS2) { |
| 1006 | state->dnxt.pilot_val = CX24120_PILOT_OFF; |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1010 | switch (pilot) { |
| 1011 | case PILOT_OFF: |
| 1012 | state->dnxt.pilot_val = CX24120_PILOT_OFF; |
| 1013 | break; |
| 1014 | case PILOT_ON: |
| 1015 | state->dnxt.pilot_val = CX24120_PILOT_ON; |
| 1016 | break; |
| 1017 | case PILOT_AUTO: |
| 1018 | default: |
| 1019 | state->dnxt.pilot_val = CX24120_PILOT_AUTO; |
| 1020 | } |
| 1021 | |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
| 1025 | /* Set symbol rate */ |
| 1026 | static int cx24120_set_symbolrate(struct cx24120_state *state, u32 rate) |
| 1027 | { |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1028 | dev_dbg(&state->i2c->dev, "(%d)\n", rate); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1029 | |
| 1030 | state->dnxt.symbol_rate = rate; |
| 1031 | |
| 1032 | /* Check symbol rate */ |
| 1033 | if (rate > 31000000) { |
| 1034 | state->dnxt.clkdiv = (-(rate < 31000001) & 3) + 2; |
| 1035 | state->dnxt.ratediv = (-(rate < 31000001) & 6) + 4; |
| 1036 | } else { |
| 1037 | state->dnxt.clkdiv = 3; |
| 1038 | state->dnxt.ratediv = 6; |
| 1039 | } |
| 1040 | |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1044 | /* Overwrite the current tuning params, we are about to tune */ |
| 1045 | static void cx24120_clone_params(struct dvb_frontend *fe) |
| 1046 | { |
| 1047 | struct cx24120_state *state = fe->demodulator_priv; |
| 1048 | |
| 1049 | state->dcur = state->dnxt; |
| 1050 | } |
| 1051 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1052 | static int cx24120_set_frontend(struct dvb_frontend *fe) |
| 1053 | { |
| 1054 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 1055 | struct cx24120_state *state = fe->demodulator_priv; |
| 1056 | struct cx24120_cmd cmd; |
| 1057 | int ret; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1058 | |
| 1059 | switch (c->delivery_system) { |
| 1060 | case SYS_DVBS2: |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1061 | dev_dbg(&state->i2c->dev, "DVB-S2\n"); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1062 | break; |
| 1063 | case SYS_DVBS: |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1064 | dev_dbg(&state->i2c->dev, "DVB-S\n"); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1065 | break; |
| 1066 | default: |
| 1067 | dev_dbg(&state->i2c->dev, |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1068 | "delivery system(%d) not supported\n", |
| 1069 | c->delivery_system); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1070 | ret = -EINVAL; |
| 1071 | break; |
| 1072 | } |
| 1073 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1074 | state->dnxt.delsys = c->delivery_system; |
| 1075 | state->dnxt.modulation = c->modulation; |
| 1076 | state->dnxt.frequency = c->frequency; |
| 1077 | state->dnxt.pilot = c->pilot; |
| 1078 | |
| 1079 | ret = cx24120_set_inversion(state, c->inversion); |
| 1080 | if (ret != 0) |
| 1081 | return ret; |
| 1082 | |
| 1083 | ret = cx24120_set_fec(state, c->modulation, c->fec_inner); |
| 1084 | if (ret != 0) |
| 1085 | return ret; |
| 1086 | |
| 1087 | ret = cx24120_set_pilot(state, c->pilot); |
| 1088 | if (ret != 0) |
| 1089 | return ret; |
| 1090 | |
| 1091 | ret = cx24120_set_symbolrate(state, c->symbol_rate); |
| 1092 | if (ret != 0) |
| 1093 | return ret; |
| 1094 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1095 | /* discard the 'current' tuning parameters and prepare to tune */ |
| 1096 | cx24120_clone_params(fe); |
| 1097 | |
| 1098 | dev_dbg(&state->i2c->dev, |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1099 | "delsys = %d\n", state->dcur.delsys); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1100 | dev_dbg(&state->i2c->dev, |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1101 | "modulation = %d\n", state->dcur.modulation); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1102 | dev_dbg(&state->i2c->dev, |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1103 | "frequency = %d\n", state->dcur.frequency); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1104 | dev_dbg(&state->i2c->dev, |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1105 | "pilot = %d (val = 0x%02x)\n", |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1106 | state->dcur.pilot, state->dcur.pilot_val); |
| 1107 | dev_dbg(&state->i2c->dev, |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1108 | "symbol_rate = %d (clkdiv/ratediv = 0x%02x/0x%02x)\n", |
| 1109 | state->dcur.symbol_rate, |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1110 | state->dcur.clkdiv, state->dcur.ratediv); |
| 1111 | dev_dbg(&state->i2c->dev, |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1112 | "FEC = %d (mask/val = 0x%02x/0x%02x)\n", |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1113 | state->dcur.fec, state->dcur.fec_mask, state->dcur.fec_val); |
| 1114 | dev_dbg(&state->i2c->dev, |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1115 | "Inversion = %d (val = 0x%02x)\n", |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1116 | state->dcur.inversion, state->dcur.inversion_val); |
| 1117 | |
Jemma Denson | 6138dc2 | 2015-04-30 16:37:42 -0300 | [diff] [blame] | 1118 | /* Flag that clock needs to be set after tune */ |
| 1119 | state->need_clock_set = 1; |
| 1120 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1121 | /* Tune in */ |
| 1122 | cmd.id = CMD_TUNEREQUEST; |
| 1123 | cmd.len = 15; |
| 1124 | cmd.arg[0] = 0; |
| 1125 | cmd.arg[1] = (state->dcur.frequency & 0xff0000) >> 16; |
| 1126 | cmd.arg[2] = (state->dcur.frequency & 0x00ff00) >> 8; |
| 1127 | cmd.arg[3] = (state->dcur.frequency & 0x0000ff); |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 1128 | cmd.arg[4] = ((state->dcur.symbol_rate / 1000) & 0xff00) >> 8; |
| 1129 | cmd.arg[5] = ((state->dcur.symbol_rate / 1000) & 0x00ff); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1130 | cmd.arg[6] = state->dcur.inversion; |
| 1131 | cmd.arg[7] = state->dcur.fec_val | state->dcur.pilot_val; |
| 1132 | cmd.arg[8] = CX24120_SEARCH_RANGE_KHZ >> 8; |
| 1133 | cmd.arg[9] = CX24120_SEARCH_RANGE_KHZ & 0xff; |
| 1134 | cmd.arg[10] = 0; /* maybe rolloff? */ |
| 1135 | cmd.arg[11] = state->dcur.fec_mask; |
| 1136 | cmd.arg[12] = state->dcur.ratediv; |
| 1137 | cmd.arg[13] = state->dcur.clkdiv; |
| 1138 | cmd.arg[14] = 0; |
| 1139 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1140 | /* Send tune command */ |
| 1141 | ret = cx24120_message_send(state, &cmd); |
| 1142 | if (ret != 0) |
| 1143 | return ret; |
| 1144 | |
| 1145 | /* Write symbol rate values */ |
| 1146 | ret = cx24120_writereg(state, CX24120_REG_CLKDIV, state->dcur.clkdiv); |
| 1147 | ret = cx24120_readreg(state, CX24120_REG_RATEDIV); |
| 1148 | ret &= 0xfffffff0; |
| 1149 | ret |= state->dcur.ratediv; |
| 1150 | ret = cx24120_writereg(state, CX24120_REG_RATEDIV, ret); |
| 1151 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1152 | return 0; |
| 1153 | } |
| 1154 | |
Jemma Denson | c84251b | 2015-05-03 08:55:15 -0300 | [diff] [blame] | 1155 | /* Set vco from config */ |
| 1156 | static int cx24120_set_vco(struct cx24120_state *state) |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1157 | { |
Jemma Denson | c84251b | 2015-05-03 08:55:15 -0300 | [diff] [blame] | 1158 | struct cx24120_cmd cmd; |
| 1159 | u32 nxtal_khz, vco; |
| 1160 | u64 inv_vco; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1161 | u32 xtal_khz = state->config->xtal_khz; |
| 1162 | |
Jemma Denson | c84251b | 2015-05-03 08:55:15 -0300 | [diff] [blame] | 1163 | nxtal_khz = xtal_khz * 4; |
| 1164 | vco = nxtal_khz * 10; |
| 1165 | inv_vco = DIV_ROUND_CLOSEST_ULL(0x400000000ULL, vco); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1166 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1167 | dev_dbg(&state->i2c->dev, "xtal=%d, vco=%d, inv_vco=%lld\n", |
| 1168 | xtal_khz, vco, inv_vco); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1169 | |
Jemma Denson | c84251b | 2015-05-03 08:55:15 -0300 | [diff] [blame] | 1170 | cmd.id = CMD_VCO_SET; |
| 1171 | cmd.len = 12; |
| 1172 | cmd.arg[0] = (vco >> 16) & 0xff; |
| 1173 | cmd.arg[1] = (vco >> 8) & 0xff; |
| 1174 | cmd.arg[2] = vco & 0xff; |
| 1175 | cmd.arg[3] = (inv_vco >> 8) & 0xff; |
| 1176 | cmd.arg[4] = (inv_vco) & 0xff; |
| 1177 | cmd.arg[5] = 0x03; |
| 1178 | cmd.arg[6] = (nxtal_khz >> 8) & 0xff; |
| 1179 | cmd.arg[7] = nxtal_khz & 0xff; |
| 1180 | cmd.arg[8] = 0x06; |
| 1181 | cmd.arg[9] = 0x03; |
| 1182 | cmd.arg[10] = (xtal_khz >> 16) & 0xff; |
| 1183 | cmd.arg[11] = xtal_khz & 0xff; |
| 1184 | |
| 1185 | return cx24120_message_send(state, &cmd); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1186 | } |
| 1187 | |
Mauro Carvalho Chehab | 5b8bc80 | 2015-05-19 08:06:52 -0300 | [diff] [blame] | 1188 | static int cx24120_init(struct dvb_frontend *fe) |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1189 | { |
| 1190 | const struct firmware *fw; |
Jemma Denson | d3cf06b | 2015-05-05 17:10:13 -0300 | [diff] [blame] | 1191 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1192 | struct cx24120_state *state = fe->demodulator_priv; |
| 1193 | struct cx24120_cmd cmd; |
Jemma Denson | 4133601 | 2015-05-08 17:36:19 -0300 | [diff] [blame] | 1194 | u8 reg; |
| 1195 | int ret, i; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1196 | unsigned char vers[4]; |
| 1197 | |
| 1198 | if (state->cold_init) |
| 1199 | return 0; |
| 1200 | |
| 1201 | /* ???? */ |
Jemma Denson | 92443cd | 2015-05-08 16:57:56 -0300 | [diff] [blame] | 1202 | cx24120_writereg(state, 0xea, 0x00); |
| 1203 | cx24120_test_rom(state); |
Jemma Denson | 1668797 | 2015-05-08 17:26:34 -0300 | [diff] [blame] | 1204 | reg = cx24120_readreg(state, 0xfb) & 0xfe; |
| 1205 | cx24120_writereg(state, 0xfb, reg); |
| 1206 | reg = cx24120_readreg(state, 0xfc) & 0xfe; |
| 1207 | cx24120_writereg(state, 0xfc, reg); |
Jemma Denson | 92443cd | 2015-05-08 16:57:56 -0300 | [diff] [blame] | 1208 | cx24120_writereg(state, 0xc3, 0x04); |
| 1209 | cx24120_writereg(state, 0xc4, 0x04); |
| 1210 | cx24120_writereg(state, 0xce, 0x00); |
| 1211 | cx24120_writereg(state, 0xcf, 0x00); |
Jemma Denson | 1668797 | 2015-05-08 17:26:34 -0300 | [diff] [blame] | 1212 | reg = cx24120_readreg(state, 0xea) & 0xfe; |
| 1213 | cx24120_writereg(state, 0xea, reg); |
Jemma Denson | 92443cd | 2015-05-08 16:57:56 -0300 | [diff] [blame] | 1214 | cx24120_writereg(state, 0xeb, 0x0c); |
| 1215 | cx24120_writereg(state, 0xec, 0x06); |
| 1216 | cx24120_writereg(state, 0xed, 0x05); |
| 1217 | cx24120_writereg(state, 0xee, 0x03); |
| 1218 | cx24120_writereg(state, 0xef, 0x05); |
| 1219 | cx24120_writereg(state, 0xf3, 0x03); |
| 1220 | cx24120_writereg(state, 0xf4, 0x44); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1221 | |
Jemma Denson | 4133601 | 2015-05-08 17:36:19 -0300 | [diff] [blame] | 1222 | for (i = 0; i < 3; i++) { |
| 1223 | cx24120_writereg(state, 0xf0 + i, 0x04); |
| 1224 | cx24120_writereg(state, 0xe6 + i, 0x02); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1225 | } |
| 1226 | |
Jemma Denson | 1668797 | 2015-05-08 17:26:34 -0300 | [diff] [blame] | 1227 | cx24120_writereg(state, 0xea, (reg | 0x01)); |
Jemma Denson | 4133601 | 2015-05-08 17:36:19 -0300 | [diff] [blame] | 1228 | for (i = 0; i < 6; i += 2) { |
| 1229 | cx24120_writereg(state, 0xc5 + i, 0x00); |
| 1230 | cx24120_writereg(state, 0xc6 + i, 0x00); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1231 | } |
| 1232 | |
Jemma Denson | 92443cd | 2015-05-08 16:57:56 -0300 | [diff] [blame] | 1233 | cx24120_writereg(state, 0xe4, 0x03); |
| 1234 | cx24120_writereg(state, 0xeb, 0x0a); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1235 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1236 | dev_dbg(&state->i2c->dev, "requesting firmware (%s) to download...\n", |
| 1237 | CX24120_FIRMWARE); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1238 | |
| 1239 | ret = state->config->request_firmware(fe, &fw, CX24120_FIRMWARE); |
| 1240 | if (ret) { |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 1241 | err("Could not load firmware (%s): %d\n", CX24120_FIRMWARE, |
| 1242 | ret); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1243 | return ret; |
| 1244 | } |
| 1245 | |
| 1246 | dev_dbg(&state->i2c->dev, |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1247 | "Firmware found, size %d bytes (%02x %02x .. %02x %02x)\n", |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1248 | (int)fw->size, /* firmware_size in bytes */ |
| 1249 | fw->data[0], /* fw 1st byte */ |
| 1250 | fw->data[1], /* fw 2d byte */ |
| 1251 | fw->data[fw->size - 2], /* fw before last byte */ |
| 1252 | fw->data[fw->size - 1]); /* fw last byte */ |
| 1253 | |
Jemma Denson | 92443cd | 2015-05-08 16:57:56 -0300 | [diff] [blame] | 1254 | cx24120_test_rom(state); |
Jemma Denson | 1668797 | 2015-05-08 17:26:34 -0300 | [diff] [blame] | 1255 | reg = cx24120_readreg(state, 0xfb) & 0xfe; |
| 1256 | cx24120_writereg(state, 0xfb, reg); |
Jemma Denson | 92443cd | 2015-05-08 16:57:56 -0300 | [diff] [blame] | 1257 | cx24120_writereg(state, 0xe0, 0x76); |
| 1258 | cx24120_writereg(state, 0xf7, 0x81); |
| 1259 | cx24120_writereg(state, 0xf8, 0x00); |
| 1260 | cx24120_writereg(state, 0xf9, 0x00); |
| 1261 | cx24120_writeregs(state, 0xfa, fw->data, (fw->size - 1), 0x00); |
| 1262 | cx24120_writereg(state, 0xf7, 0xc0); |
| 1263 | cx24120_writereg(state, 0xe0, 0x00); |
Jemma Denson | 1668797 | 2015-05-08 17:26:34 -0300 | [diff] [blame] | 1264 | reg = (fw->size - 2) & 0x00ff; |
| 1265 | cx24120_writereg(state, 0xf8, reg); |
| 1266 | reg = ((fw->size - 2) >> 8) & 0x00ff; |
| 1267 | cx24120_writereg(state, 0xf9, reg); |
Jemma Denson | 92443cd | 2015-05-08 16:57:56 -0300 | [diff] [blame] | 1268 | cx24120_writereg(state, 0xf7, 0x00); |
| 1269 | cx24120_writereg(state, 0xdc, 0x00); |
| 1270 | cx24120_writereg(state, 0xdc, 0x07); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1271 | msleep(500); |
| 1272 | |
| 1273 | /* Check final byte matches final byte of firmware */ |
Jemma Denson | 1668797 | 2015-05-08 17:26:34 -0300 | [diff] [blame] | 1274 | reg = cx24120_readreg(state, 0xe1); |
| 1275 | if (reg == fw->data[fw->size - 1]) { |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1276 | dev_dbg(&state->i2c->dev, "Firmware uploaded successfully\n"); |
Jemma Denson | 4133601 | 2015-05-08 17:36:19 -0300 | [diff] [blame] | 1277 | ret = 0; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1278 | } else { |
| 1279 | err("Firmware upload failed. Last byte returned=0x%x\n", ret); |
Jemma Denson | 4133601 | 2015-05-08 17:36:19 -0300 | [diff] [blame] | 1280 | ret = -EREMOTEIO; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1281 | } |
Jemma Denson | 92443cd | 2015-05-08 16:57:56 -0300 | [diff] [blame] | 1282 | cx24120_writereg(state, 0xdc, 0x00); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1283 | release_firmware(fw); |
Jemma Denson | 4133601 | 2015-05-08 17:36:19 -0300 | [diff] [blame] | 1284 | if (ret != 0) |
| 1285 | return ret; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1286 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1287 | /* Start tuner */ |
| 1288 | cmd.id = CMD_START_TUNER; |
| 1289 | cmd.len = 3; |
| 1290 | cmd.arg[0] = 0x00; |
| 1291 | cmd.arg[1] = 0x00; |
| 1292 | cmd.arg[2] = 0x00; |
| 1293 | |
| 1294 | if (cx24120_message_send(state, &cmd) != 0) { |
| 1295 | err("Error tuner start! :(\n"); |
| 1296 | return -EREMOTEIO; |
| 1297 | } |
| 1298 | |
| 1299 | /* Set VCO */ |
Jemma Denson | c84251b | 2015-05-03 08:55:15 -0300 | [diff] [blame] | 1300 | ret = cx24120_set_vco(state); |
| 1301 | if (ret != 0) { |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1302 | err("Error set VCO! :(\n"); |
Jemma Denson | c84251b | 2015-05-03 08:55:15 -0300 | [diff] [blame] | 1303 | return ret; |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1304 | } |
| 1305 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1306 | /* set bandwidth */ |
| 1307 | cmd.id = CMD_BANDWIDTH; |
| 1308 | cmd.len = 12; |
| 1309 | cmd.arg[0] = 0x00; |
| 1310 | cmd.arg[1] = 0x00; |
| 1311 | cmd.arg[2] = 0x00; |
| 1312 | cmd.arg[3] = 0x00; |
| 1313 | cmd.arg[4] = 0x05; |
| 1314 | cmd.arg[5] = 0x02; |
| 1315 | cmd.arg[6] = 0x02; |
| 1316 | cmd.arg[7] = 0x00; |
| 1317 | cmd.arg[8] = 0x05; |
| 1318 | cmd.arg[9] = 0x02; |
| 1319 | cmd.arg[10] = 0x02; |
| 1320 | cmd.arg[11] = 0x00; |
| 1321 | |
| 1322 | if (cx24120_message_send(state, &cmd)) { |
| 1323 | err("Error set bandwidth!\n"); |
| 1324 | return -EREMOTEIO; |
| 1325 | } |
| 1326 | |
Jemma Denson | 1668797 | 2015-05-08 17:26:34 -0300 | [diff] [blame] | 1327 | reg = cx24120_readreg(state, 0xba); |
| 1328 | if (reg > 3) { |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1329 | dev_dbg(&state->i2c->dev, "Reset-readreg 0xba: %x\n", ret); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1330 | err("Error initialising tuner!\n"); |
| 1331 | return -EREMOTEIO; |
| 1332 | } |
| 1333 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1334 | dev_dbg(&state->i2c->dev, "Tuner initialised correctly.\n"); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1335 | |
| 1336 | /* Initialise mpeg outputs */ |
Jemma Denson | 92443cd | 2015-05-08 16:57:56 -0300 | [diff] [blame] | 1337 | cx24120_writereg(state, 0xeb, 0x0a); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1338 | if (cx24120_msg_mpeg_output_global_config(state, 0) || |
| 1339 | cx24120_msg_mpeg_output_config(state, 0) || |
| 1340 | cx24120_msg_mpeg_output_config(state, 1) || |
| 1341 | cx24120_msg_mpeg_output_config(state, 2)) { |
| 1342 | err("Error initialising mpeg output. :(\n"); |
| 1343 | return -EREMOTEIO; |
| 1344 | } |
| 1345 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1346 | /* ???? */ |
| 1347 | cmd.id = CMD_TUNER_INIT; |
| 1348 | cmd.len = 3; |
| 1349 | cmd.arg[0] = 0x00; |
| 1350 | cmd.arg[1] = 0x10; |
| 1351 | cmd.arg[2] = 0x10; |
| 1352 | if (cx24120_message_send(state, &cmd)) { |
| 1353 | err("Error sending final init message. :(\n"); |
| 1354 | return -EREMOTEIO; |
| 1355 | } |
| 1356 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1357 | /* Firmware CMD 35: Get firmware version */ |
| 1358 | cmd.id = CMD_FWVERSION; |
| 1359 | cmd.len = 1; |
| 1360 | for (i = 0; i < 4; i++) { |
| 1361 | cmd.arg[0] = i; |
| 1362 | ret = cx24120_message_send(state, &cmd); |
| 1363 | if (ret != 0) |
| 1364 | return ret; |
| 1365 | vers[i] = cx24120_readreg(state, CX24120_REG_MAILBOX); |
| 1366 | } |
| 1367 | info("FW version %i.%i.%i.%i\n", vers[0], vers[1], vers[2], vers[3]); |
| 1368 | |
Jemma Denson | d3cf06b | 2015-05-05 17:10:13 -0300 | [diff] [blame] | 1369 | /* init stats here in order signal app which stats are supported */ |
| 1370 | c->strength.len = 1; |
| 1371 | c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
Jemma Denson | 3b5eb50 | 2015-05-08 15:52:45 -0300 | [diff] [blame] | 1372 | c->cnr.len = 1; |
| 1373 | c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
Jemma Denson | d3cf06b | 2015-05-05 17:10:13 -0300 | [diff] [blame] | 1374 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1375 | state->cold_init = 1; |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1379 | static int cx24120_tune(struct dvb_frontend *fe, bool re_tune, |
Patrick Boettcher | 1ff2e8e | 2015-04-28 13:39:20 -0300 | [diff] [blame] | 1380 | unsigned int mode_flags, unsigned int *delay, |
| 1381 | fe_status_t *status) |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1382 | { |
| 1383 | struct cx24120_state *state = fe->demodulator_priv; |
| 1384 | int ret; |
| 1385 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1386 | dev_dbg(&state->i2c->dev, "(%d)\n", re_tune); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1387 | |
| 1388 | /* TODO: Do we need to set delay? */ |
| 1389 | |
| 1390 | if (re_tune) { |
| 1391 | ret = cx24120_set_frontend(fe); |
| 1392 | if (ret) |
| 1393 | return ret; |
| 1394 | } |
| 1395 | |
| 1396 | return cx24120_read_status(fe, status); |
| 1397 | } |
| 1398 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1399 | static int cx24120_get_algo(struct dvb_frontend *fe) |
| 1400 | { |
| 1401 | return DVBFE_ALGO_HW; |
| 1402 | } |
| 1403 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1404 | static int cx24120_sleep(struct dvb_frontend *fe) |
| 1405 | { |
| 1406 | return 0; |
| 1407 | } |
| 1408 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1409 | static int cx24120_get_frontend(struct dvb_frontend *fe) |
| 1410 | { |
| 1411 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 1412 | struct cx24120_state *state = fe->demodulator_priv; |
| 1413 | u8 freq1, freq2, freq3; |
| 1414 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1415 | dev_dbg(&state->i2c->dev, "\n"); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1416 | |
| 1417 | /* don't return empty data if we're not tuned in */ |
Jemma Denson | 1462612 | 2015-05-05 17:18:11 -0300 | [diff] [blame] | 1418 | if ((state->fe_status & FE_HAS_LOCK) == 0) |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1419 | return 0; |
| 1420 | |
| 1421 | /* Get frequency */ |
| 1422 | freq1 = cx24120_readreg(state, CX24120_REG_FREQ1); |
| 1423 | freq2 = cx24120_readreg(state, CX24120_REG_FREQ2); |
| 1424 | freq3 = cx24120_readreg(state, CX24120_REG_FREQ3); |
| 1425 | c->frequency = (freq3 << 16) | (freq2 << 8) | freq1; |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1426 | dev_dbg(&state->i2c->dev, "frequency = %d\n", c->frequency); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1427 | |
| 1428 | /* Get modulation, fec, pilot */ |
| 1429 | cx24120_get_fec(fe); |
| 1430 | |
| 1431 | return 0; |
| 1432 | } |
| 1433 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1434 | static void cx24120_release(struct dvb_frontend *fe) |
| 1435 | { |
| 1436 | struct cx24120_state *state = fe->demodulator_priv; |
| 1437 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1438 | dev_dbg(&state->i2c->dev, "Clear state structure\n"); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1439 | kfree(state); |
| 1440 | } |
| 1441 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1442 | static int cx24120_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) |
| 1443 | { |
| 1444 | struct cx24120_state *state = fe->demodulator_priv; |
| 1445 | |
| 1446 | *ucblocks = (cx24120_readreg(state, CX24120_REG_UCB_H) << 8) | |
| 1447 | cx24120_readreg(state, CX24120_REG_UCB_L); |
| 1448 | |
Jemma Denson | 2f3f07f | 2015-05-08 16:34:31 -0300 | [diff] [blame] | 1449 | dev_dbg(&state->i2c->dev, "ucblocks = %d\n", *ucblocks); |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1450 | return 0; |
| 1451 | } |
| 1452 | |
Jemma Denson | 5afc9a2 | 2015-04-14 09:04:50 -0300 | [diff] [blame] | 1453 | static struct dvb_frontend_ops cx24120_ops = { |
| 1454 | .delsys = { SYS_DVBS, SYS_DVBS2 }, |
| 1455 | .info = { |
| 1456 | .name = "Conexant CX24120/CX24118", |
| 1457 | .frequency_min = 950000, |
| 1458 | .frequency_max = 2150000, |
| 1459 | .frequency_stepsize = 1011, /* kHz for QPSK frontends */ |
| 1460 | .frequency_tolerance = 5000, |
| 1461 | .symbol_rate_min = 1000000, |
| 1462 | .symbol_rate_max = 45000000, |
| 1463 | .caps = FE_CAN_INVERSION_AUTO | |
| 1464 | FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 | |
| 1465 | FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 | |
| 1466 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO | |
| 1467 | FE_CAN_2G_MODULATION | |
| 1468 | FE_CAN_QPSK | FE_CAN_RECOVER |
| 1469 | }, |
| 1470 | .release = cx24120_release, |
| 1471 | |
| 1472 | .init = cx24120_init, |
| 1473 | .sleep = cx24120_sleep, |
| 1474 | |
| 1475 | .tune = cx24120_tune, |
| 1476 | .get_frontend_algo = cx24120_get_algo, |
| 1477 | .set_frontend = cx24120_set_frontend, |
| 1478 | |
| 1479 | .get_frontend = cx24120_get_frontend, |
| 1480 | .read_status = cx24120_read_status, |
| 1481 | .read_ber = cx24120_read_ber, |
| 1482 | .read_signal_strength = cx24120_read_signal_strength, |
| 1483 | .read_snr = cx24120_read_snr, |
| 1484 | .read_ucblocks = cx24120_read_ucblocks, |
| 1485 | |
| 1486 | .diseqc_send_master_cmd = cx24120_send_diseqc_msg, |
| 1487 | |
| 1488 | .diseqc_send_burst = cx24120_diseqc_send_burst, |
| 1489 | .set_tone = cx24120_set_tone, |
| 1490 | .set_voltage = cx24120_set_voltage, |
| 1491 | }; |
| 1492 | |
| 1493 | MODULE_DESCRIPTION("DVB Frontend module for Conexant CX24120/CX24118 hardware"); |
| 1494 | MODULE_AUTHOR("Jemma Denson"); |
| 1495 | MODULE_LICENSE("GPL"); |