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