blob: 066ee387bf250894b1067b023b2d1d760790ad02 [file] [log] [blame]
Jemma Denson5afc9a22015-04-14 09:04:50 -03001/*
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 Boettcher2e89a5e2015-04-28 13:18:05 -030039#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 Denson5afc9a22015-04-14 09:04:50 -030042
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -030043#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 Denson5afc9a22015-04-14 09:04:50 -030047
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -030048#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 Denson5afc9a22015-04-14 09:04:50 -030054
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -030055#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 Denson5afc9a22015-04-14 09:04:50 -030059
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -030060#define CX24120_REG_UCB_H 0x50 /* UCB high byte */
61#define CX24120_REG_UCB_L 0x51 /* UCB low byte */
Jemma Denson5afc9a22015-04-14 09:04:50 -030062
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -030063#define CX24120_REG_CLKDIV 0xe6
64#define CX24120_REG_RATEDIV 0xf0
Jemma Denson5afc9a22015-04-14 09:04:50 -030065
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -030066#define CX24120_REG_REVISION 0xff /* Chip revision (ro) */
Jemma Denson5afc9a22015-04-14 09:04:50 -030067
Jemma Denson5afc9a22015-04-14 09:04:50 -030068/* Command messages */
69enum 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
Jemma Densonddcb2522015-05-19 15:17:23 -030090 CMD_BER_CTRL = 0x3c, /* cmd.len = 0x03; */
Jemma Denson5afc9a22015-04-14 09:04:50 -030091};
92
93#define CX24120_MAX_CMD_LEN 30
94
95/* pilot mask */
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -030096#define CX24120_PILOT_OFF 0x00
97#define CX24120_PILOT_ON 0x40
98#define CX24120_PILOT_AUTO 0x80
Jemma Denson5afc9a22015-04-14 09:04:50 -030099
100/* signal status */
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -0300101#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 Denson5afc9a22015-04-14 09:04:50 -0300109
Jemma Densonddcb2522015-05-19 15:17:23 -0300110/* ber window */
111#define CX24120_BER_WINDOW 16
112#define CX24120_BER_WSIZE ((1 << CX24120_BER_WINDOW) * 208 * 8)
113
Patrick Boettcherc5fb0f52015-04-17 06:04:53 -0300114#define info(args...) pr_info("cx24120: " args)
115#define err(args...) pr_err("cx24120: ### ERROR: " args)
Jemma Denson5afc9a22015-04-14 09:04:50 -0300116
117/* The Demod/Tuner can't easily provide these, we cache them */
118struct cx24120_tuning {
119 u32 frequency;
120 u32 symbol_rate;
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300121 enum fe_spectral_inversion inversion;
122 enum fe_code_rate fec;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300123
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300124 enum fe_delivery_system delsys;
125 enum fe_modulation modulation;
126 enum fe_pilot pilot;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300127
128 /* Demod values */
129 u8 fec_val;
130 u8 fec_mask;
131 u8 clkdiv;
132 u8 ratediv;
133 u8 inversion_val;
134 u8 pilot_val;
135};
136
Jemma Denson5afc9a22015-04-14 09:04:50 -0300137/* Private state */
138struct cx24120_state {
139 struct i2c_adapter *i2c;
140 const struct cx24120_config *config;
141 struct dvb_frontend frontend;
142
143 u8 cold_init;
144 u8 mpeg_enabled;
Jemma Denson6138dc22015-04-30 16:37:42 -0300145 u8 need_clock_set;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300146
147 /* current and next tuning parameters */
148 struct cx24120_tuning dcur;
149 struct cx24120_tuning dnxt;
Jemma Denson14626122015-05-05 17:18:11 -0300150
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300151 enum fe_status fe_status;
Jemma Densonddcb2522015-05-19 15:17:23 -0300152
Jemma Densonc9adafa2015-05-19 17:32:55 -0300153 /* dvbv5 stats calculations */
Jemma Denson80e97102015-05-19 15:52:46 -0300154 u32 bitrate;
Jemma Densonddcb2522015-05-19 15:17:23 -0300155 u32 berw_usecs;
Jemma Densonfc443282015-05-19 15:29:44 -0300156 u32 ber_prev;
Jemma Densonbf8de2d2015-05-20 11:57:49 -0300157 u32 ucb_offset;
Jemma Densonddcb2522015-05-19 15:17:23 -0300158 unsigned long ber_jiffies_stats;
Jemma Denson80e97102015-05-19 15:52:46 -0300159 unsigned long per_jiffies_stats;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300160};
161
Jemma Denson5afc9a22015-04-14 09:04:50 -0300162/* Command message to firmware */
163struct cx24120_cmd {
164 u8 id;
165 u8 len;
166 u8 arg[CX24120_MAX_CMD_LEN];
167};
168
Jemma Denson5afc9a22015-04-14 09:04:50 -0300169/* Read single register */
170static int cx24120_readreg(struct cx24120_state *state, u8 reg)
171{
172 int ret;
173 u8 buf = 0;
174 struct i2c_msg msg[] = {
Jemma Densonfbdbab72015-05-05 18:31:10 -0300175 {
176 .addr = state->config->i2c_addr,
Jemma Denson5afc9a22015-04-14 09:04:50 -0300177 .flags = 0,
178 .len = 1,
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -0300179 .buf = &reg
180 }, {
181 .addr = state->config->i2c_addr,
Jemma Denson5afc9a22015-04-14 09:04:50 -0300182 .flags = I2C_M_RD,
183 .len = 1,
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -0300184 .buf = &buf
185 }
Jemma Denson5afc9a22015-04-14 09:04:50 -0300186 };
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -0300187
Jemma Denson5afc9a22015-04-14 09:04:50 -0300188 ret = i2c_transfer(state->i2c, msg, 2);
189 if (ret != 2) {
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -0300190 err("Read error: reg=0x%02x, ret=%i)\n", reg, ret);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300191 return ret;
192 }
193
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300194 dev_dbg(&state->i2c->dev, "reg=0x%02x; data=0x%02x\n", reg, buf);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300195
196 return buf;
197}
198
Jemma Denson5afc9a22015-04-14 09:04:50 -0300199/* Write single register */
200static int cx24120_writereg(struct cx24120_state *state, u8 reg, u8 data)
201{
202 u8 buf[] = { reg, data };
203 struct i2c_msg msg = {
204 .addr = state->config->i2c_addr,
205 .flags = 0,
206 .buf = buf,
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -0300207 .len = 2
208 };
Jemma Denson5afc9a22015-04-14 09:04:50 -0300209 int ret;
210
211 ret = i2c_transfer(state->i2c, &msg, 1);
212 if (ret != 1) {
213 err("Write error: i2c_write error(err == %i, 0x%02x: 0x%02x)\n",
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300214 ret, reg, data);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300215 return ret;
216 }
217
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300218 dev_dbg(&state->i2c->dev, "reg=0x%02x; data=0x%02x\n", reg, data);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300219
220 return 0;
221}
222
Patrick Boettcherf7a77eb2015-04-28 02:47:42 -0300223/* Write multiple registers in chunks of i2c_wr_max-sized buffers */
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300224static int cx24120_writeregs(struct cx24120_state *state,
225 u8 reg, const u8 *values, u16 len, u8 incr)
Jemma Denson5afc9a22015-04-14 09:04:50 -0300226{
227 int ret;
Patrick Boettcherf7a77eb2015-04-28 02:47:42 -0300228 u16 max = state->config->i2c_wr_max > 0 ?
229 state->config->i2c_wr_max :
230 len;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300231
232 struct i2c_msg msg = {
233 .addr = state->config->i2c_addr,
234 .flags = 0,
Patrick Boettcherf7a77eb2015-04-28 02:47:42 -0300235 };
236
237 msg.buf = kmalloc(max + 1, GFP_KERNEL);
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300238 if (!msg.buf)
Patrick Boettcherf7a77eb2015-04-28 02:47:42 -0300239 return -ENOMEM;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300240
241 while (len) {
Patrick Boettcherf7a77eb2015-04-28 02:47:42 -0300242 msg.buf[0] = reg;
243 msg.len = len > max ? max : len;
244 memcpy(&msg.buf[1], values, msg.len);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300245
Patrick Boettcherf7a77eb2015-04-28 02:47:42 -0300246 len -= msg.len; /* data length revers counter */
247 values += msg.len; /* incr data pointer */
Jemma Denson5afc9a22015-04-14 09:04:50 -0300248
249 if (incr)
250 reg += msg.len;
Patrick Boettcherf7a77eb2015-04-28 02:47:42 -0300251 msg.len++; /* don't forget the addr byte */
Jemma Denson5afc9a22015-04-14 09:04:50 -0300252
253 ret = i2c_transfer(state->i2c, &msg, 1);
254 if (ret != 1) {
255 err("i2c_write error(err == %i, 0x%02x)\n", ret, reg);
Patrick Boettcherf7a77eb2015-04-28 02:47:42 -0300256 goto out;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300257 }
258
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300259 dev_dbg(&state->i2c->dev, "reg=0x%02x; data=%*ph\n",
260 reg, msg.len - 1, msg.buf + 1);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300261 }
262
Patrick Boettcherf7a77eb2015-04-28 02:47:42 -0300263 ret = 0;
264
265out:
266 kfree(msg.buf);
267 return ret;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300268}
269
Jemma Denson5afc9a22015-04-14 09:04:50 -0300270static struct dvb_frontend_ops cx24120_ops;
271
272struct dvb_frontend *cx24120_attach(const struct cx24120_config *config,
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300273 struct i2c_adapter *i2c)
Jemma Denson5afc9a22015-04-14 09:04:50 -0300274{
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300275 struct cx24120_state *state;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300276 int demod_rev;
277
278 info("Conexant cx24120/cx24118 - DVBS/S2 Satellite demod/tuner\n");
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300279 state = kzalloc(sizeof(*state), GFP_KERNEL);
280 if (!state) {
Jemma Denson5afc9a22015-04-14 09:04:50 -0300281 err("Unable to allocate memory for cx24120_state\n");
282 goto error;
283 }
284
285 /* setup the state */
286 state->config = config;
287 state->i2c = i2c;
288
289 /* check if the demod is present and has proper type */
290 demod_rev = cx24120_readreg(state, CX24120_REG_REVISION);
291 switch (demod_rev) {
292 case 0x07:
293 info("Demod cx24120 rev. 0x07 detected.\n");
294 break;
295 case 0x05:
296 info("Demod cx24120 rev. 0x05 detected.\n");
297 break;
298 default:
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300299 err("Unsupported demod revision: 0x%x detected.\n", demod_rev);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300300 goto error;
301 }
302
303 /* create dvb_frontend */
304 state->cold_init = 0;
305 memcpy(&state->frontend.ops, &cx24120_ops,
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -0300306 sizeof(struct dvb_frontend_ops));
Jemma Denson5afc9a22015-04-14 09:04:50 -0300307 state->frontend.demodulator_priv = state;
308
309 info("Conexant cx24120/cx24118 attached.\n");
310 return &state->frontend;
311
312error:
313 kfree(state);
314 return NULL;
315}
316EXPORT_SYMBOL(cx24120_attach);
317
318static int cx24120_test_rom(struct cx24120_state *state)
319{
320 int err, ret;
321
322 err = cx24120_readreg(state, 0xfd);
323 if (err & 4) {
324 ret = cx24120_readreg(state, 0xdf) & 0xfe;
325 err = cx24120_writereg(state, 0xdf, ret);
326 }
327 return err;
328}
329
Jemma Denson5afc9a22015-04-14 09:04:50 -0300330static int cx24120_read_snr(struct dvb_frontend *fe, u16 *snr)
331{
Jemma Denson3b5eb502015-05-08 15:52:45 -0300332 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300333
Jemma Denson3b5eb502015-05-08 15:52:45 -0300334 if (c->cnr.stat[0].scale != FE_SCALE_DECIBEL)
335 *snr = 0;
336 else
337 *snr = div_s64(c->cnr.stat[0].svalue, 100);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300338
339 return 0;
340}
341
Jemma Denson5afc9a22015-04-14 09:04:50 -0300342static int cx24120_read_ber(struct dvb_frontend *fe, u32 *ber)
343{
344 struct cx24120_state *state = fe->demodulator_priv;
Jemma Densonfc443282015-05-19 15:29:44 -0300345 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300346
Jemma Densonfc443282015-05-19 15:29:44 -0300347 if (c->post_bit_error.stat[0].scale != FE_SCALE_COUNTER) {
348 *ber = 0;
349 return 0;
350 }
351
352 *ber = c->post_bit_error.stat[0].uvalue - state->ber_prev;
353 state->ber_prev = c->post_bit_error.stat[0].uvalue;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300354
355 return 0;
356}
357
358static int cx24120_msg_mpeg_output_global_config(struct cx24120_state *state,
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300359 u8 flag);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300360
361/* Check if we're running a command that needs to disable mpeg out */
362static void cx24120_check_cmd(struct cx24120_state *state, u8 id)
363{
364 switch (id) {
365 case CMD_TUNEREQUEST:
366 case CMD_CLOCK_READ:
367 case CMD_DISEQC_MSG1:
368 case CMD_DISEQC_MSG2:
369 case CMD_SETVOLTAGE:
370 case CMD_SETTONE:
Jemma Denson270e707132015-04-30 17:05:14 -0300371 case CMD_DISEQC_BURST:
Jemma Denson5afc9a22015-04-14 09:04:50 -0300372 cx24120_msg_mpeg_output_global_config(state, 0);
373 /* Old driver would do a msleep(100) here */
374 default:
375 return;
376 }
377}
378
Jemma Denson5afc9a22015-04-14 09:04:50 -0300379/* Send a message to the firmware */
380static int cx24120_message_send(struct cx24120_state *state,
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300381 struct cx24120_cmd *cmd)
Jemma Denson5afc9a22015-04-14 09:04:50 -0300382{
Mauro Carvalho Chehab65b01662015-05-19 08:04:35 -0300383 int ficus;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300384
385 if (state->mpeg_enabled) {
386 /* Disable mpeg out on certain commands */
387 cx24120_check_cmd(state, cmd->id);
388 }
389
Mauro Carvalho Chehab65b01662015-05-19 08:04:35 -0300390 cx24120_writereg(state, CX24120_REG_CMD_START, cmd->id);
391 cx24120_writeregs(state, CX24120_REG_CMD_ARGS, &cmd->arg[0],
392 cmd->len, 1);
393 cx24120_writereg(state, CX24120_REG_CMD_END, 0x01);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300394
395 ficus = 1000;
396 while (cx24120_readreg(state, CX24120_REG_CMD_END)) {
397 msleep(20);
398 ficus -= 20;
399 if (ficus == 0) {
400 err("Error sending message to firmware\n");
401 return -EREMOTEIO;
402 }
403 }
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300404 dev_dbg(&state->i2c->dev, "sent message 0x%02x\n", cmd->id);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300405
406 return 0;
407}
408
409/* Send a message and fill arg[] with the results */
410static int cx24120_message_sendrcv(struct cx24120_state *state,
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300411 struct cx24120_cmd *cmd, u8 numreg)
Jemma Denson5afc9a22015-04-14 09:04:50 -0300412{
413 int ret, i;
414
415 if (numreg > CX24120_MAX_CMD_LEN) {
416 err("Too many registers to read. cmd->reg = %d", numreg);
417 return -EREMOTEIO;
418 }
419
420 ret = cx24120_message_send(state, cmd);
421 if (ret != 0)
422 return ret;
423
424 if (!numreg)
425 return 0;
426
427 /* Read numreg registers starting from register cmd->len */
428 for (i = 0; i < numreg; i++)
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300429 cmd->arg[i] = cx24120_readreg(state, (cmd->len + i + 1));
Jemma Denson5afc9a22015-04-14 09:04:50 -0300430
431 return 0;
432}
433
Jemma Denson5afc9a22015-04-14 09:04:50 -0300434static int cx24120_read_signal_strength(struct dvb_frontend *fe,
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300435 u16 *signal_strength)
Jemma Denson5afc9a22015-04-14 09:04:50 -0300436{
Jemma Denson34ce4752015-05-05 17:47:55 -0300437 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300438
Jemma Denson34ce4752015-05-05 17:47:55 -0300439 if (c->strength.stat[0].scale != FE_SCALE_RELATIVE)
440 *signal_strength = 0;
441 else
442 *signal_strength = c->strength.stat[0].uvalue;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300443
Jemma Denson5afc9a22015-04-14 09:04:50 -0300444 return 0;
445}
446
Jemma Denson5afc9a22015-04-14 09:04:50 -0300447static int cx24120_msg_mpeg_output_global_config(struct cx24120_state *state,
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300448 u8 enable)
Jemma Denson5afc9a22015-04-14 09:04:50 -0300449{
450 struct cx24120_cmd cmd;
451 int ret;
452
453 cmd.id = CMD_MPEG_ONOFF;
454 cmd.len = 4;
455 cmd.arg[0] = 0x01;
456 cmd.arg[1] = 0x00;
457 cmd.arg[2] = enable ? 0 : (u8)(-1);
458 cmd.arg[3] = 0x01;
459
460 ret = cx24120_message_send(state, &cmd);
461 if (ret != 0) {
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300462 dev_dbg(&state->i2c->dev, "failed to %s MPEG output\n",
463 enable ? "enable" : "disable");
Jemma Denson5afc9a22015-04-14 09:04:50 -0300464 return ret;
465 }
466
467 state->mpeg_enabled = enable;
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300468 dev_dbg(&state->i2c->dev, "MPEG output %s\n",
469 enable ? "enabled" : "disabled");
Jemma Denson5afc9a22015-04-14 09:04:50 -0300470
471 return 0;
472}
473
Jemma Denson5afc9a22015-04-14 09:04:50 -0300474static int cx24120_msg_mpeg_output_config(struct cx24120_state *state, u8 seq)
475{
476 struct cx24120_cmd cmd;
477 struct cx24120_initial_mpeg_config i =
478 state->config->initial_mpeg_config;
479
480 cmd.id = CMD_MPEG_INIT;
481 cmd.len = 7;
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300482 cmd.arg[0] = seq; /* sequental number - can be 0,1,2 */
Jemma Denson5afc9a22015-04-14 09:04:50 -0300483 cmd.arg[1] = ((i.x1 & 0x01) << 1) | ((i.x1 >> 1) & 0x01);
484 cmd.arg[2] = 0x05;
485 cmd.arg[3] = 0x02;
486 cmd.arg[4] = ((i.x2 >> 1) & 0x01);
487 cmd.arg[5] = (i.x2 & 0xf0) | (i.x3 & 0x0f);
488 cmd.arg[6] = 0x10;
489
490 return cx24120_message_send(state, &cmd);
491}
492
Jemma Denson5afc9a22015-04-14 09:04:50 -0300493static int cx24120_diseqc_send_burst(struct dvb_frontend *fe,
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300494 enum fe_sec_mini_cmd burst)
Jemma Denson5afc9a22015-04-14 09:04:50 -0300495{
496 struct cx24120_state *state = fe->demodulator_priv;
497 struct cx24120_cmd cmd;
498
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300499 dev_dbg(&state->i2c->dev, "\n");
500
Jemma Densonfbdbab72015-05-05 18:31:10 -0300501 /*
502 * Yes, cmd.len is set to zero. The old driver
Jemma Denson5afc9a22015-04-14 09:04:50 -0300503 * didn't specify any len, but also had a
504 * memset 0 before every use of the cmd struct
505 * which would have set it to zero.
506 * This quite probably needs looking into.
507 */
508 cmd.id = CMD_DISEQC_BURST;
509 cmd.len = 0;
510 cmd.arg[0] = 0x00;
Jemma Denson7c95e252015-05-05 18:33:27 -0300511 cmd.arg[1] = (burst == SEC_MINI_B) ? 0x01 : 0x00;
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300512
Jemma Denson5afc9a22015-04-14 09:04:50 -0300513 return cx24120_message_send(state, &cmd);
514}
515
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300516static int cx24120_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
Jemma Denson5afc9a22015-04-14 09:04:50 -0300517{
518 struct cx24120_state *state = fe->demodulator_priv;
519 struct cx24120_cmd cmd;
520
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300521 dev_dbg(&state->i2c->dev, "(%d)\n", tone);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300522
523 if ((tone != SEC_TONE_ON) && (tone != SEC_TONE_OFF)) {
524 err("Invalid tone=%d\n", tone);
525 return -EINVAL;
526 }
527
528 cmd.id = CMD_SETTONE;
529 cmd.len = 4;
530 cmd.arg[0] = 0x00;
531 cmd.arg[1] = 0x00;
532 cmd.arg[2] = 0x00;
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300533 cmd.arg[3] = (tone == SEC_TONE_ON) ? 0x01 : 0x00;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300534
535 return cx24120_message_send(state, &cmd);
536}
537
Jemma Denson5afc9a22015-04-14 09:04:50 -0300538static int cx24120_set_voltage(struct dvb_frontend *fe,
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300539 enum fe_sec_voltage voltage)
Jemma Denson5afc9a22015-04-14 09:04:50 -0300540{
541 struct cx24120_state *state = fe->demodulator_priv;
542 struct cx24120_cmd cmd;
543
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300544 dev_dbg(&state->i2c->dev, "(%d)\n", voltage);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300545
546 cmd.id = CMD_SETVOLTAGE;
547 cmd.len = 2;
548 cmd.arg[0] = 0x00;
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300549 cmd.arg[1] = (voltage == SEC_VOLTAGE_18) ? 0x01 : 0x00;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300550
551 return cx24120_message_send(state, &cmd);
552}
553
Jemma Denson5afc9a22015-04-14 09:04:50 -0300554static int cx24120_send_diseqc_msg(struct dvb_frontend *fe,
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300555 struct dvb_diseqc_master_cmd *d)
Jemma Denson5afc9a22015-04-14 09:04:50 -0300556{
557 struct cx24120_state *state = fe->demodulator_priv;
558 struct cx24120_cmd cmd;
559 int back_count;
560
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300561 dev_dbg(&state->i2c->dev, "\n");
Jemma Denson5afc9a22015-04-14 09:04:50 -0300562
563 cmd.id = CMD_DISEQC_MSG1;
564 cmd.len = 11;
565 cmd.arg[0] = 0x00;
566 cmd.arg[1] = 0x00;
567 cmd.arg[2] = 0x03;
568 cmd.arg[3] = 0x16;
569 cmd.arg[4] = 0x28;
570 cmd.arg[5] = 0x01;
571 cmd.arg[6] = 0x01;
572 cmd.arg[7] = 0x14;
573 cmd.arg[8] = 0x19;
574 cmd.arg[9] = 0x14;
575 cmd.arg[10] = 0x1e;
576
577 if (cx24120_message_send(state, &cmd)) {
578 err("send 1st message(0x%x) failed\n", cmd.id);
579 return -EREMOTEIO;
580 }
581
582 cmd.id = CMD_DISEQC_MSG2;
583 cmd.len = d->msg_len + 6;
584 cmd.arg[0] = 0x00;
585 cmd.arg[1] = 0x01;
586 cmd.arg[2] = 0x02;
587 cmd.arg[3] = 0x00;
588 cmd.arg[4] = 0x00;
589 cmd.arg[5] = d->msg_len;
590
591 memcpy(&cmd.arg[6], &d->msg, d->msg_len);
592
593 if (cx24120_message_send(state, &cmd)) {
594 err("send 2nd message(0x%x) failed\n", cmd.id);
595 return -EREMOTEIO;
596 }
597
598 back_count = 500;
599 do {
600 if (!(cx24120_readreg(state, 0x93) & 0x01)) {
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300601 dev_dbg(&state->i2c->dev, "diseqc sequence sent\n");
Jemma Denson5afc9a22015-04-14 09:04:50 -0300602 return 0;
603 }
604 msleep(20);
605 back_count -= 20;
606 } while (back_count);
607
608 err("Too long waiting for diseqc.\n");
609 return -ETIMEDOUT;
610}
611
Jemma Denson14626122015-05-05 17:18:11 -0300612static void cx24120_get_stats(struct cx24120_state *state)
Jemma Denson9fc18f12015-05-05 16:59:27 -0300613{
614 struct dvb_frontend *fe = &state->frontend;
615 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Jemma Denson34ce4752015-05-05 17:47:55 -0300616 struct cx24120_cmd cmd;
Jemma Densonddcb2522015-05-19 15:17:23 -0300617 int ret, cnr, msecs;
Jemma Denson80e97102015-05-19 15:52:46 -0300618 u16 sig, ucb;
Jemma Densonddcb2522015-05-19 15:17:23 -0300619 u32 ber;
Jemma Denson9fc18f12015-05-05 16:59:27 -0300620
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300621 dev_dbg(&state->i2c->dev, "\n");
Jemma Denson9fc18f12015-05-05 16:59:27 -0300622
623 /* signal strength */
Jemma Denson14626122015-05-05 17:18:11 -0300624 if (state->fe_status & FE_HAS_SIGNAL) {
Jemma Denson34ce4752015-05-05 17:47:55 -0300625 cmd.id = CMD_READ_SNR;
626 cmd.len = 1;
627 cmd.arg[0] = 0x00;
628
629 ret = cx24120_message_send(state, &cmd);
630 if (ret != 0) {
631 err("error reading signal strength\n");
Jemma Denson9fc18f12015-05-05 16:59:27 -0300632 return;
Jemma Denson34ce4752015-05-05 17:47:55 -0300633 }
634
635 /* raw */
Jemma Densonb0cdf1a2015-05-05 18:09:45 -0300636 sig = cx24120_readreg(state, CX24120_REG_SIGSTR_H) >> 6;
637 sig = sig << 8;
638 sig |= cx24120_readreg(state, CX24120_REG_SIGSTR_L);
639 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300640 "signal strength from firmware = 0x%x\n", sig);
Jemma Denson34ce4752015-05-05 17:47:55 -0300641
642 /* cooked */
Jemma Densonb0cdf1a2015-05-05 18:09:45 -0300643 sig = -100 * sig + 94324;
Jemma Denson9fc18f12015-05-05 16:59:27 -0300644
645 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
Jemma Densonb0cdf1a2015-05-05 18:09:45 -0300646 c->strength.stat[0].uvalue = sig;
Jemma Denson9fc18f12015-05-05 16:59:27 -0300647 } else {
648 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
649 }
650
Jemma Denson3b5eb502015-05-08 15:52:45 -0300651 /* CNR */
652 if (state->fe_status & FE_HAS_VITERBI) {
653 cnr = cx24120_readreg(state, CX24120_REG_QUALITY_H) << 8;
654 cnr |= cx24120_readreg(state, CX24120_REG_QUALITY_L);
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300655 dev_dbg(&state->i2c->dev, "read SNR index = %d\n", cnr);
Jemma Denson3b5eb502015-05-08 15:52:45 -0300656
657 /* guessed - seems about right */
658 cnr = cnr * 100;
659
660 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
661 c->cnr.stat[0].svalue = cnr;
662 } else {
663 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
664 }
Jemma Denson9fc18f12015-05-05 16:59:27 -0300665
Jemma Densone3f2f632015-05-19 17:23:14 -0300666 /* BER & UCB require lock */
667 if (!(state->fe_status & FE_HAS_LOCK)) {
668 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
669 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
670 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
671 c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
672 return;
673 }
674
Jemma Densonddcb2522015-05-19 15:17:23 -0300675 /* BER */
676 if (time_after(jiffies, state->ber_jiffies_stats)) {
677 msecs = (state->berw_usecs + 500) / 1000;
678 state->ber_jiffies_stats = jiffies + msecs_to_jiffies(msecs);
679
Jemma Densonfc443282015-05-19 15:29:44 -0300680 ber = cx24120_readreg(state, CX24120_REG_BER_HH) << 24;
681 ber |= cx24120_readreg(state, CX24120_REG_BER_HL) << 16;
682 ber |= cx24120_readreg(state, CX24120_REG_BER_LH) << 8;
683 ber |= cx24120_readreg(state, CX24120_REG_BER_LL);
684 dev_dbg(&state->i2c->dev, "read BER index = %d\n", ber);
Jemma Densonddcb2522015-05-19 15:17:23 -0300685
686 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
687 c->post_bit_error.stat[0].uvalue += ber;
688
689 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
690 c->post_bit_count.stat[0].uvalue += CX24120_BER_WSIZE;
691 }
692
Jemma Denson80e97102015-05-19 15:52:46 -0300693 /* UCB */
694 if (time_after(jiffies, state->per_jiffies_stats)) {
695 state->per_jiffies_stats = jiffies + msecs_to_jiffies(1000);
696
697 ucb = cx24120_readreg(state, CX24120_REG_UCB_H) << 8;
698 ucb |= cx24120_readreg(state, CX24120_REG_UCB_L);
699 dev_dbg(&state->i2c->dev, "ucblocks = %d\n", ucb);
700
Jemma Densonbf8de2d2015-05-20 11:57:49 -0300701 /* handle reset */
702 if (ucb < state->ucb_offset)
703 state->ucb_offset = c->block_error.stat[0].uvalue;
704
Jemma Denson80e97102015-05-19 15:52:46 -0300705 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
Jemma Densonbf8de2d2015-05-20 11:57:49 -0300706 c->block_error.stat[0].uvalue = ucb + state->ucb_offset;
Jemma Denson80e97102015-05-19 15:52:46 -0300707
708 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
709 c->block_count.stat[0].uvalue += state->bitrate / 8 / 208;
710 }
Jemma Denson9fc18f12015-05-05 16:59:27 -0300711}
712
Jemma Denson6138dc22015-04-30 16:37:42 -0300713static void cx24120_set_clock_ratios(struct dvb_frontend *fe);
714
Jemma Denson5afc9a22015-04-14 09:04:50 -0300715/* Read current tuning status */
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300716static int cx24120_read_status(struct dvb_frontend *fe, enum fe_status *status)
Jemma Denson5afc9a22015-04-14 09:04:50 -0300717{
718 struct cx24120_state *state = fe->demodulator_priv;
719 int lock;
720
721 lock = cx24120_readreg(state, CX24120_REG_STATUS);
722
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300723 dev_dbg(&state->i2c->dev, "status = 0x%02x\n", lock);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300724
725 *status = 0;
726
727 if (lock & CX24120_HAS_SIGNAL)
728 *status = FE_HAS_SIGNAL;
729 if (lock & CX24120_HAS_CARRIER)
730 *status |= FE_HAS_CARRIER;
731 if (lock & CX24120_HAS_VITERBI)
732 *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
733 if (lock & CX24120_HAS_LOCK)
734 *status |= FE_HAS_LOCK;
735
Jemma Densonfbdbab72015-05-05 18:31:10 -0300736 /*
737 * TODO: is FE_HAS_SYNC in the right place?
Jemma Denson5afc9a22015-04-14 09:04:50 -0300738 * Other cx241xx drivers have this slightly
Jemma Densonfbdbab72015-05-05 18:31:10 -0300739 * different
740 */
Jemma Denson5afc9a22015-04-14 09:04:50 -0300741
Jemma Denson14626122015-05-05 17:18:11 -0300742 state->fe_status = *status;
743 cx24120_get_stats(state);
Jemma Denson9fc18f12015-05-05 16:59:27 -0300744
Jemma Denson6138dc22015-04-30 16:37:42 -0300745 /* Set the clock once tuned in */
746 if (state->need_clock_set && *status & FE_HAS_LOCK) {
747 /* Set clock ratios */
748 cx24120_set_clock_ratios(fe);
749
750 /* Old driver would do a msleep(200) here */
751
752 /* Renable mpeg output */
753 if (!state->mpeg_enabled)
754 cx24120_msg_mpeg_output_global_config(state, 1);
755
756 state->need_clock_set = 0;
757 }
758
Jemma Denson5afc9a22015-04-14 09:04:50 -0300759 return 0;
760}
761
Jemma Densonfbdbab72015-05-05 18:31:10 -0300762/*
763 * FEC & modulation lookup table
Jemma Denson5afc9a22015-04-14 09:04:50 -0300764 * Used for decoding the REG_FECMODE register
765 * once tuned in.
766 */
Mauro Carvalho Chehabec8fe6c2015-05-19 08:19:47 -0300767struct cx24120_modfec {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300768 enum fe_delivery_system delsys;
769 enum fe_modulation mod;
770 enum fe_code_rate fec;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300771 u8 val;
Mauro Carvalho Chehabec8fe6c2015-05-19 08:19:47 -0300772};
773
774static const struct cx24120_modfec modfec_lookup_table[] = {
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -0300775 /*delsys mod fec val */
776 { SYS_DVBS, QPSK, FEC_1_2, 0x01 },
777 { SYS_DVBS, QPSK, FEC_2_3, 0x02 },
778 { SYS_DVBS, QPSK, FEC_3_4, 0x03 },
779 { SYS_DVBS, QPSK, FEC_4_5, 0x04 },
780 { SYS_DVBS, QPSK, FEC_5_6, 0x05 },
781 { SYS_DVBS, QPSK, FEC_6_7, 0x06 },
782 { SYS_DVBS, QPSK, FEC_7_8, 0x07 },
Jemma Denson5afc9a22015-04-14 09:04:50 -0300783
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -0300784 { SYS_DVBS2, QPSK, FEC_1_2, 0x04 },
785 { SYS_DVBS2, QPSK, FEC_3_5, 0x05 },
786 { SYS_DVBS2, QPSK, FEC_2_3, 0x06 },
787 { SYS_DVBS2, QPSK, FEC_3_4, 0x07 },
788 { SYS_DVBS2, QPSK, FEC_4_5, 0x08 },
789 { SYS_DVBS2, QPSK, FEC_5_6, 0x09 },
790 { SYS_DVBS2, QPSK, FEC_8_9, 0x0a },
791 { SYS_DVBS2, QPSK, FEC_9_10, 0x0b },
Jemma Denson5afc9a22015-04-14 09:04:50 -0300792
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -0300793 { SYS_DVBS2, PSK_8, FEC_3_5, 0x0c },
794 { SYS_DVBS2, PSK_8, FEC_2_3, 0x0d },
795 { SYS_DVBS2, PSK_8, FEC_3_4, 0x0e },
796 { SYS_DVBS2, PSK_8, FEC_5_6, 0x0f },
797 { SYS_DVBS2, PSK_8, FEC_8_9, 0x10 },
798 { SYS_DVBS2, PSK_8, FEC_9_10, 0x11 },
Jemma Denson5afc9a22015-04-14 09:04:50 -0300799};
800
Jemma Denson5afc9a22015-04-14 09:04:50 -0300801/* Retrieve current fec, modulation & pilot values */
802static int cx24120_get_fec(struct dvb_frontend *fe)
803{
804 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
805 struct cx24120_state *state = fe->demodulator_priv;
806 int idx;
807 int ret;
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300808 int fec;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300809
Jemma Denson5afc9a22015-04-14 09:04:50 -0300810 ret = cx24120_readreg(state, CX24120_REG_FECMODE);
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300811 fec = ret & 0x3f; /* Lower 6 bits */
Jemma Denson5afc9a22015-04-14 09:04:50 -0300812
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300813 dev_dbg(&state->i2c->dev, "raw fec = %d\n", fec);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300814
815 for (idx = 0; idx < ARRAY_SIZE(modfec_lookup_table); idx++) {
816 if (modfec_lookup_table[idx].delsys != state->dcur.delsys)
817 continue;
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300818 if (modfec_lookup_table[idx].val != fec)
Jemma Denson5afc9a22015-04-14 09:04:50 -0300819 continue;
820
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -0300821 break; /* found */
Jemma Denson5afc9a22015-04-14 09:04:50 -0300822 }
823
824 if (idx >= ARRAY_SIZE(modfec_lookup_table)) {
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300825 dev_dbg(&state->i2c->dev, "couldn't find fec!\n");
Jemma Denson5afc9a22015-04-14 09:04:50 -0300826 return -EINVAL;
827 }
828
829 /* save values back to cache */
830 c->modulation = modfec_lookup_table[idx].mod;
831 c->fec_inner = modfec_lookup_table[idx].fec;
832 c->pilot = (ret & 0x80) ? PILOT_ON : PILOT_OFF;
833
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300834 dev_dbg(&state->i2c->dev, "mod(%d), fec(%d), pilot(%d)\n",
Jemma Denson5afc9a22015-04-14 09:04:50 -0300835 c->modulation, c->fec_inner, c->pilot);
836
837 return 0;
838}
839
Jemma Densonddcb2522015-05-19 15:17:23 -0300840/* Calculate ber window time */
Hans Verkuiledff2ba2015-05-22 10:44:49 -0300841static void cx24120_calculate_ber_window(struct cx24120_state *state, u32 rate)
Jemma Densonddcb2522015-05-19 15:17:23 -0300842{
843 struct dvb_frontend *fe = &state->frontend;
844 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Jemma Denson80e97102015-05-19 15:52:46 -0300845 u64 tmp;
Jemma Densonddcb2522015-05-19 15:17:23 -0300846
847 /*
848 * Calculate bitrate from rate in the clock ratios table.
849 * This isn't *exactly* right but close enough.
850 */
Jemma Denson80e97102015-05-19 15:52:46 -0300851 tmp = (u64)c->symbol_rate * rate;
852 do_div(tmp, 256);
853 state->bitrate = tmp;
Jemma Densonddcb2522015-05-19 15:17:23 -0300854
855 /* usecs per ber window */
856 tmp = 1000000ULL * CX24120_BER_WSIZE;
Jemma Denson80e97102015-05-19 15:52:46 -0300857 do_div(tmp, state->bitrate);
Jemma Densonddcb2522015-05-19 15:17:23 -0300858 state->berw_usecs = tmp;
859
Jemma Denson80e97102015-05-19 15:52:46 -0300860 dev_dbg(&state->i2c->dev, "bitrate: %u, berw_usecs: %u\n",
861 state->bitrate, state->berw_usecs);
Jemma Densonddcb2522015-05-19 15:17:23 -0300862}
863
Jemma Densonfbdbab72015-05-05 18:31:10 -0300864/*
865 * Clock ratios lookup table
Jemma Denson5afc9a22015-04-14 09:04:50 -0300866 *
867 * Values obtained from much larger table in old driver
868 * which had numerous entries which would never match.
869 *
870 * There's probably some way of calculating these but I
871 * can't determine the pattern
Jemma Densonfbdbab72015-05-05 18:31:10 -0300872 */
Mauro Carvalho Chehabec8fe6c2015-05-19 08:19:47 -0300873struct cx24120_clock_ratios_table {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300874 enum fe_delivery_system delsys;
875 enum fe_pilot pilot;
876 enum fe_modulation mod;
877 enum fe_code_rate fec;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300878 u32 m_rat;
879 u32 n_rat;
880 u32 rate;
Mauro Carvalho Chehabec8fe6c2015-05-19 08:19:47 -0300881};
882
883static const struct cx24120_clock_ratios_table clock_ratios_table[] = {
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -0300884 /*delsys pilot mod fec m_rat n_rat rate */
885 { SYS_DVBS2, PILOT_OFF, QPSK, FEC_1_2, 273088, 254505, 274 },
886 { SYS_DVBS2, PILOT_OFF, QPSK, FEC_3_5, 17272, 13395, 330 },
887 { SYS_DVBS2, PILOT_OFF, QPSK, FEC_2_3, 24344, 16967, 367 },
888 { SYS_DVBS2, PILOT_OFF, QPSK, FEC_3_4, 410788, 254505, 413 },
889 { SYS_DVBS2, PILOT_OFF, QPSK, FEC_4_5, 438328, 254505, 440 },
890 { SYS_DVBS2, PILOT_OFF, QPSK, FEC_5_6, 30464, 16967, 459 },
891 { SYS_DVBS2, PILOT_OFF, QPSK, FEC_8_9, 487832, 254505, 490 },
892 { SYS_DVBS2, PILOT_OFF, QPSK, FEC_9_10, 493952, 254505, 496 },
893 { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_3_5, 328168, 169905, 494 },
894 { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_2_3, 24344, 11327, 550 },
895 { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_3_4, 410788, 169905, 618 },
896 { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_5_6, 30464, 11327, 688 },
897 { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_8_9, 487832, 169905, 735 },
898 { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_9_10, 493952, 169905, 744 },
899 { SYS_DVBS2, PILOT_ON, QPSK, FEC_1_2, 273088, 260709, 268 },
900 { SYS_DVBS2, PILOT_ON, QPSK, FEC_3_5, 328168, 260709, 322 },
901 { SYS_DVBS2, PILOT_ON, QPSK, FEC_2_3, 121720, 86903, 358 },
902 { SYS_DVBS2, PILOT_ON, QPSK, FEC_3_4, 410788, 260709, 403 },
903 { SYS_DVBS2, PILOT_ON, QPSK, FEC_4_5, 438328, 260709, 430 },
904 { SYS_DVBS2, PILOT_ON, QPSK, FEC_5_6, 152320, 86903, 448 },
905 { SYS_DVBS2, PILOT_ON, QPSK, FEC_8_9, 487832, 260709, 479 },
906 { SYS_DVBS2, PILOT_ON, QPSK, FEC_9_10, 493952, 260709, 485 },
907 { SYS_DVBS2, PILOT_ON, PSK_8, FEC_3_5, 328168, 173853, 483 },
908 { SYS_DVBS2, PILOT_ON, PSK_8, FEC_2_3, 121720, 57951, 537 },
909 { SYS_DVBS2, PILOT_ON, PSK_8, FEC_3_4, 410788, 173853, 604 },
910 { SYS_DVBS2, PILOT_ON, PSK_8, FEC_5_6, 152320, 57951, 672 },
911 { SYS_DVBS2, PILOT_ON, PSK_8, FEC_8_9, 487832, 173853, 718 },
912 { SYS_DVBS2, PILOT_ON, PSK_8, FEC_9_10, 493952, 173853, 727 },
913 { SYS_DVBS, PILOT_OFF, QPSK, FEC_1_2, 152592, 152592, 256 },
914 { SYS_DVBS, PILOT_OFF, QPSK, FEC_2_3, 305184, 228888, 341 },
915 { SYS_DVBS, PILOT_OFF, QPSK, FEC_3_4, 457776, 305184, 384 },
916 { SYS_DVBS, PILOT_OFF, QPSK, FEC_5_6, 762960, 457776, 427 },
917 { SYS_DVBS, PILOT_OFF, QPSK, FEC_7_8, 1068144, 610368, 448 },
Jemma Denson5afc9a22015-04-14 09:04:50 -0300918};
919
Jemma Denson5afc9a22015-04-14 09:04:50 -0300920/* Set clock ratio from lookup table */
921static void cx24120_set_clock_ratios(struct dvb_frontend *fe)
922{
923 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
924 struct cx24120_state *state = fe->demodulator_priv;
925 struct cx24120_cmd cmd;
926 int ret, idx;
927
928 /* Find fec, modulation, pilot */
929 ret = cx24120_get_fec(fe);
930 if (ret != 0)
931 return;
932
933 /* Find the clock ratios in the lookup table */
934 for (idx = 0; idx < ARRAY_SIZE(clock_ratios_table); idx++) {
935 if (clock_ratios_table[idx].delsys != state->dcur.delsys)
936 continue;
937 if (clock_ratios_table[idx].mod != c->modulation)
938 continue;
939 if (clock_ratios_table[idx].fec != c->fec_inner)
940 continue;
941 if (clock_ratios_table[idx].pilot != c->pilot)
942 continue;
943
944 break; /* found */
945 }
946
947 if (idx >= ARRAY_SIZE(clock_ratios_table)) {
948 info("Clock ratio not found - data reception in danger\n");
949 return;
950 }
951
Jemma Denson5afc9a22015-04-14 09:04:50 -0300952 /* Read current values? */
953 cmd.id = CMD_CLOCK_READ;
954 cmd.len = 1;
955 cmd.arg[0] = 0x00;
956 ret = cx24120_message_sendrcv(state, &cmd, 6);
957 if (ret != 0)
958 return;
959 /* in cmd[0]-[5] - result */
960
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300961 dev_dbg(&state->i2c->dev, "m=%d, n=%d; idx: %d m=%d, n=%d, rate=%d\n",
Jemma Denson5afc9a22015-04-14 09:04:50 -0300962 cmd.arg[2] | (cmd.arg[1] << 8) | (cmd.arg[0] << 16),
963 cmd.arg[5] | (cmd.arg[4] << 8) | (cmd.arg[3] << 16),
964 idx,
965 clock_ratios_table[idx].m_rat,
966 clock_ratios_table[idx].n_rat,
967 clock_ratios_table[idx].rate);
968
Jemma Denson5afc9a22015-04-14 09:04:50 -0300969 /* Set the clock */
970 cmd.id = CMD_CLOCK_SET;
971 cmd.len = 10;
972 cmd.arg[0] = 0;
973 cmd.arg[1] = 0x10;
974 cmd.arg[2] = (clock_ratios_table[idx].m_rat >> 16) & 0xff;
975 cmd.arg[3] = (clock_ratios_table[idx].m_rat >> 8) & 0xff;
976 cmd.arg[4] = (clock_ratios_table[idx].m_rat >> 0) & 0xff;
977 cmd.arg[5] = (clock_ratios_table[idx].n_rat >> 16) & 0xff;
978 cmd.arg[6] = (clock_ratios_table[idx].n_rat >> 8) & 0xff;
979 cmd.arg[7] = (clock_ratios_table[idx].n_rat >> 0) & 0xff;
980 cmd.arg[8] = (clock_ratios_table[idx].rate >> 8) & 0xff;
981 cmd.arg[9] = (clock_ratios_table[idx].rate >> 0) & 0xff;
982
983 cx24120_message_send(state, &cmd);
Jemma Densonddcb2522015-05-19 15:17:23 -0300984
985 /* Calculate ber window rates for stat work */
986 cx24120_calculate_ber_window(state, clock_ratios_table[idx].rate);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300987}
988
Jemma Denson5afc9a22015-04-14 09:04:50 -0300989/* Set inversion value */
990static int cx24120_set_inversion(struct cx24120_state *state,
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300991 enum fe_spectral_inversion inversion)
Jemma Denson5afc9a22015-04-14 09:04:50 -0300992{
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300993 dev_dbg(&state->i2c->dev, "(%d)\n", inversion);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300994
995 switch (inversion) {
996 case INVERSION_OFF:
997 state->dnxt.inversion_val = 0x00;
998 break;
999 case INVERSION_ON:
1000 state->dnxt.inversion_val = 0x04;
1001 break;
1002 case INVERSION_AUTO:
1003 state->dnxt.inversion_val = 0x0c;
1004 break;
1005 default:
1006 return -EINVAL;
1007 }
1008
1009 state->dnxt.inversion = inversion;
1010
1011 return 0;
1012}
1013
Jemma Denson5c0a1c22015-05-15 16:08:26 -03001014/* FEC lookup table for tuning */
Mauro Carvalho Chehabec8fe6c2015-05-19 08:19:47 -03001015struct cx24120_modfec_table {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03001016 enum fe_delivery_system delsys;
1017 enum fe_modulation mod;
1018 enum fe_code_rate fec;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001019 u8 val;
Mauro Carvalho Chehabec8fe6c2015-05-19 08:19:47 -03001020};
1021
1022static const struct cx24120_modfec_table modfec_table[] = {
Jemma Denson5c0a1c22015-05-15 16:08:26 -03001023 /*delsys mod fec val */
1024 { SYS_DVBS, QPSK, FEC_1_2, 0x2e },
1025 { SYS_DVBS, QPSK, FEC_2_3, 0x2f },
1026 { SYS_DVBS, QPSK, FEC_3_4, 0x30 },
1027 { SYS_DVBS, QPSK, FEC_5_6, 0x31 },
1028 { SYS_DVBS, QPSK, FEC_6_7, 0x32 },
1029 { SYS_DVBS, QPSK, FEC_7_8, 0x33 },
Jemma Denson5afc9a22015-04-14 09:04:50 -03001030
Jemma Denson5c0a1c22015-05-15 16:08:26 -03001031 { SYS_DVBS2, QPSK, FEC_1_2, 0x04 },
1032 { SYS_DVBS2, QPSK, FEC_3_5, 0x05 },
1033 { SYS_DVBS2, QPSK, FEC_2_3, 0x06 },
1034 { SYS_DVBS2, QPSK, FEC_3_4, 0x07 },
1035 { SYS_DVBS2, QPSK, FEC_4_5, 0x08 },
1036 { SYS_DVBS2, QPSK, FEC_5_6, 0x09 },
1037 { SYS_DVBS2, QPSK, FEC_8_9, 0x0a },
1038 { SYS_DVBS2, QPSK, FEC_9_10, 0x0b },
Jemma Denson5afc9a22015-04-14 09:04:50 -03001039
Jemma Denson5c0a1c22015-05-15 16:08:26 -03001040 { SYS_DVBS2, PSK_8, FEC_3_5, 0x0c },
1041 { SYS_DVBS2, PSK_8, FEC_2_3, 0x0d },
1042 { SYS_DVBS2, PSK_8, FEC_3_4, 0x0e },
1043 { SYS_DVBS2, PSK_8, FEC_5_6, 0x0f },
1044 { SYS_DVBS2, PSK_8, FEC_8_9, 0x10 },
1045 { SYS_DVBS2, PSK_8, FEC_9_10, 0x11 },
Jemma Denson5afc9a22015-04-14 09:04:50 -03001046};
1047
1048/* Set fec_val & fec_mask values from delsys, modulation & fec */
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03001049static int cx24120_set_fec(struct cx24120_state *state, enum fe_modulation mod,
1050 enum fe_code_rate fec)
Jemma Denson5afc9a22015-04-14 09:04:50 -03001051{
1052 int idx;
1053
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001054 dev_dbg(&state->i2c->dev, "(0x%02x,0x%02x)\n", mod, fec);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001055
1056 state->dnxt.fec = fec;
1057
1058 /* Lookup fec_val from modfec table */
1059 for (idx = 0; idx < ARRAY_SIZE(modfec_table); idx++) {
1060 if (modfec_table[idx].delsys != state->dnxt.delsys)
1061 continue;
1062 if (modfec_table[idx].mod != mod)
1063 continue;
1064 if (modfec_table[idx].fec != fec)
1065 continue;
1066
1067 /* found */
1068 state->dnxt.fec_mask = 0x00;
1069 state->dnxt.fec_val = modfec_table[idx].val;
1070 return 0;
1071 }
1072
Jemma Denson5afc9a22015-04-14 09:04:50 -03001073 if (state->dnxt.delsys == SYS_DVBS2) {
1074 /* DVBS2 auto is 0x00/0x00 */
1075 state->dnxt.fec_mask = 0x00;
1076 state->dnxt.fec_val = 0x00;
1077 } else {
1078 /* Set DVB-S to auto */
1079 state->dnxt.fec_val = 0x2e;
1080 state->dnxt.fec_mask = 0xac;
1081 }
1082
1083 return 0;
1084}
1085
Jemma Denson5afc9a22015-04-14 09:04:50 -03001086/* Set pilot */
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03001087static int cx24120_set_pilot(struct cx24120_state *state, enum fe_pilot pilot)
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -03001088{
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001089 dev_dbg(&state->i2c->dev, "(%d)\n", pilot);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001090
1091 /* Pilot only valid in DVBS2 */
1092 if (state->dnxt.delsys != SYS_DVBS2) {
1093 state->dnxt.pilot_val = CX24120_PILOT_OFF;
1094 return 0;
1095 }
1096
Jemma Denson5afc9a22015-04-14 09:04:50 -03001097 switch (pilot) {
1098 case PILOT_OFF:
1099 state->dnxt.pilot_val = CX24120_PILOT_OFF;
1100 break;
1101 case PILOT_ON:
1102 state->dnxt.pilot_val = CX24120_PILOT_ON;
1103 break;
1104 case PILOT_AUTO:
1105 default:
1106 state->dnxt.pilot_val = CX24120_PILOT_AUTO;
1107 }
1108
1109 return 0;
1110}
1111
1112/* Set symbol rate */
1113static int cx24120_set_symbolrate(struct cx24120_state *state, u32 rate)
1114{
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001115 dev_dbg(&state->i2c->dev, "(%d)\n", rate);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001116
1117 state->dnxt.symbol_rate = rate;
1118
1119 /* Check symbol rate */
1120 if (rate > 31000000) {
1121 state->dnxt.clkdiv = (-(rate < 31000001) & 3) + 2;
1122 state->dnxt.ratediv = (-(rate < 31000001) & 6) + 4;
1123 } else {
1124 state->dnxt.clkdiv = 3;
1125 state->dnxt.ratediv = 6;
1126 }
1127
1128 return 0;
1129}
1130
Jemma Denson5afc9a22015-04-14 09:04:50 -03001131/* Overwrite the current tuning params, we are about to tune */
1132static void cx24120_clone_params(struct dvb_frontend *fe)
1133{
1134 struct cx24120_state *state = fe->demodulator_priv;
1135
1136 state->dcur = state->dnxt;
1137}
1138
Jemma Denson5afc9a22015-04-14 09:04:50 -03001139static int cx24120_set_frontend(struct dvb_frontend *fe)
1140{
1141 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1142 struct cx24120_state *state = fe->demodulator_priv;
1143 struct cx24120_cmd cmd;
1144 int ret;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001145
1146 switch (c->delivery_system) {
1147 case SYS_DVBS2:
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001148 dev_dbg(&state->i2c->dev, "DVB-S2\n");
Jemma Denson5afc9a22015-04-14 09:04:50 -03001149 break;
1150 case SYS_DVBS:
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001151 dev_dbg(&state->i2c->dev, "DVB-S\n");
Jemma Denson5afc9a22015-04-14 09:04:50 -03001152 break;
1153 default:
1154 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001155 "delivery system(%d) not supported\n",
1156 c->delivery_system);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001157 ret = -EINVAL;
1158 break;
1159 }
1160
Jemma Denson5afc9a22015-04-14 09:04:50 -03001161 state->dnxt.delsys = c->delivery_system;
1162 state->dnxt.modulation = c->modulation;
1163 state->dnxt.frequency = c->frequency;
1164 state->dnxt.pilot = c->pilot;
1165
1166 ret = cx24120_set_inversion(state, c->inversion);
1167 if (ret != 0)
1168 return ret;
1169
1170 ret = cx24120_set_fec(state, c->modulation, c->fec_inner);
1171 if (ret != 0)
1172 return ret;
1173
1174 ret = cx24120_set_pilot(state, c->pilot);
1175 if (ret != 0)
1176 return ret;
1177
1178 ret = cx24120_set_symbolrate(state, c->symbol_rate);
1179 if (ret != 0)
1180 return ret;
1181
Jemma Denson5afc9a22015-04-14 09:04:50 -03001182 /* discard the 'current' tuning parameters and prepare to tune */
1183 cx24120_clone_params(fe);
1184
1185 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001186 "delsys = %d\n", state->dcur.delsys);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001187 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001188 "modulation = %d\n", state->dcur.modulation);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001189 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001190 "frequency = %d\n", state->dcur.frequency);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001191 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001192 "pilot = %d (val = 0x%02x)\n",
Jemma Denson5afc9a22015-04-14 09:04:50 -03001193 state->dcur.pilot, state->dcur.pilot_val);
1194 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001195 "symbol_rate = %d (clkdiv/ratediv = 0x%02x/0x%02x)\n",
1196 state->dcur.symbol_rate,
Jemma Denson5afc9a22015-04-14 09:04:50 -03001197 state->dcur.clkdiv, state->dcur.ratediv);
1198 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001199 "FEC = %d (mask/val = 0x%02x/0x%02x)\n",
Jemma Denson5afc9a22015-04-14 09:04:50 -03001200 state->dcur.fec, state->dcur.fec_mask, state->dcur.fec_val);
1201 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001202 "Inversion = %d (val = 0x%02x)\n",
Jemma Denson5afc9a22015-04-14 09:04:50 -03001203 state->dcur.inversion, state->dcur.inversion_val);
1204
Jemma Denson6138dc22015-04-30 16:37:42 -03001205 /* Flag that clock needs to be set after tune */
1206 state->need_clock_set = 1;
1207
Jemma Denson5afc9a22015-04-14 09:04:50 -03001208 /* Tune in */
1209 cmd.id = CMD_TUNEREQUEST;
1210 cmd.len = 15;
1211 cmd.arg[0] = 0;
1212 cmd.arg[1] = (state->dcur.frequency & 0xff0000) >> 16;
1213 cmd.arg[2] = (state->dcur.frequency & 0x00ff00) >> 8;
1214 cmd.arg[3] = (state->dcur.frequency & 0x0000ff);
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -03001215 cmd.arg[4] = ((state->dcur.symbol_rate / 1000) & 0xff00) >> 8;
1216 cmd.arg[5] = ((state->dcur.symbol_rate / 1000) & 0x00ff);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001217 cmd.arg[6] = state->dcur.inversion;
1218 cmd.arg[7] = state->dcur.fec_val | state->dcur.pilot_val;
1219 cmd.arg[8] = CX24120_SEARCH_RANGE_KHZ >> 8;
1220 cmd.arg[9] = CX24120_SEARCH_RANGE_KHZ & 0xff;
1221 cmd.arg[10] = 0; /* maybe rolloff? */
1222 cmd.arg[11] = state->dcur.fec_mask;
1223 cmd.arg[12] = state->dcur.ratediv;
1224 cmd.arg[13] = state->dcur.clkdiv;
1225 cmd.arg[14] = 0;
1226
Jemma Denson5afc9a22015-04-14 09:04:50 -03001227 /* Send tune command */
1228 ret = cx24120_message_send(state, &cmd);
1229 if (ret != 0)
1230 return ret;
1231
1232 /* Write symbol rate values */
1233 ret = cx24120_writereg(state, CX24120_REG_CLKDIV, state->dcur.clkdiv);
1234 ret = cx24120_readreg(state, CX24120_REG_RATEDIV);
1235 ret &= 0xfffffff0;
1236 ret |= state->dcur.ratediv;
1237 ret = cx24120_writereg(state, CX24120_REG_RATEDIV, ret);
1238
Jemma Denson5afc9a22015-04-14 09:04:50 -03001239 return 0;
1240}
1241
Jemma Densonc84251b2015-05-03 08:55:15 -03001242/* Set vco from config */
1243static int cx24120_set_vco(struct cx24120_state *state)
Jemma Denson5afc9a22015-04-14 09:04:50 -03001244{
Jemma Densonc84251b2015-05-03 08:55:15 -03001245 struct cx24120_cmd cmd;
1246 u32 nxtal_khz, vco;
1247 u64 inv_vco;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001248 u32 xtal_khz = state->config->xtal_khz;
1249
Jemma Densonc84251b2015-05-03 08:55:15 -03001250 nxtal_khz = xtal_khz * 4;
1251 vco = nxtal_khz * 10;
1252 inv_vco = DIV_ROUND_CLOSEST_ULL(0x400000000ULL, vco);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001253
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001254 dev_dbg(&state->i2c->dev, "xtal=%d, vco=%d, inv_vco=%lld\n",
1255 xtal_khz, vco, inv_vco);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001256
Jemma Densonc84251b2015-05-03 08:55:15 -03001257 cmd.id = CMD_VCO_SET;
1258 cmd.len = 12;
1259 cmd.arg[0] = (vco >> 16) & 0xff;
1260 cmd.arg[1] = (vco >> 8) & 0xff;
1261 cmd.arg[2] = vco & 0xff;
1262 cmd.arg[3] = (inv_vco >> 8) & 0xff;
1263 cmd.arg[4] = (inv_vco) & 0xff;
1264 cmd.arg[5] = 0x03;
1265 cmd.arg[6] = (nxtal_khz >> 8) & 0xff;
1266 cmd.arg[7] = nxtal_khz & 0xff;
1267 cmd.arg[8] = 0x06;
1268 cmd.arg[9] = 0x03;
1269 cmd.arg[10] = (xtal_khz >> 16) & 0xff;
1270 cmd.arg[11] = xtal_khz & 0xff;
1271
1272 return cx24120_message_send(state, &cmd);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001273}
1274
Mauro Carvalho Chehab5b8bc802015-05-19 08:06:52 -03001275static int cx24120_init(struct dvb_frontend *fe)
Jemma Denson5afc9a22015-04-14 09:04:50 -03001276{
1277 const struct firmware *fw;
Jemma Densond3cf06b2015-05-05 17:10:13 -03001278 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001279 struct cx24120_state *state = fe->demodulator_priv;
1280 struct cx24120_cmd cmd;
Jemma Denson41336012015-05-08 17:36:19 -03001281 u8 reg;
1282 int ret, i;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001283 unsigned char vers[4];
1284
1285 if (state->cold_init)
1286 return 0;
1287
1288 /* ???? */
Jemma Denson92443cd2015-05-08 16:57:56 -03001289 cx24120_writereg(state, 0xea, 0x00);
1290 cx24120_test_rom(state);
Jemma Denson16687972015-05-08 17:26:34 -03001291 reg = cx24120_readreg(state, 0xfb) & 0xfe;
1292 cx24120_writereg(state, 0xfb, reg);
1293 reg = cx24120_readreg(state, 0xfc) & 0xfe;
1294 cx24120_writereg(state, 0xfc, reg);
Jemma Denson92443cd2015-05-08 16:57:56 -03001295 cx24120_writereg(state, 0xc3, 0x04);
1296 cx24120_writereg(state, 0xc4, 0x04);
1297 cx24120_writereg(state, 0xce, 0x00);
1298 cx24120_writereg(state, 0xcf, 0x00);
Jemma Denson16687972015-05-08 17:26:34 -03001299 reg = cx24120_readreg(state, 0xea) & 0xfe;
1300 cx24120_writereg(state, 0xea, reg);
Jemma Denson92443cd2015-05-08 16:57:56 -03001301 cx24120_writereg(state, 0xeb, 0x0c);
1302 cx24120_writereg(state, 0xec, 0x06);
1303 cx24120_writereg(state, 0xed, 0x05);
1304 cx24120_writereg(state, 0xee, 0x03);
1305 cx24120_writereg(state, 0xef, 0x05);
1306 cx24120_writereg(state, 0xf3, 0x03);
1307 cx24120_writereg(state, 0xf4, 0x44);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001308
Jemma Denson41336012015-05-08 17:36:19 -03001309 for (i = 0; i < 3; i++) {
1310 cx24120_writereg(state, 0xf0 + i, 0x04);
1311 cx24120_writereg(state, 0xe6 + i, 0x02);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001312 }
1313
Jemma Denson16687972015-05-08 17:26:34 -03001314 cx24120_writereg(state, 0xea, (reg | 0x01));
Jemma Denson41336012015-05-08 17:36:19 -03001315 for (i = 0; i < 6; i += 2) {
1316 cx24120_writereg(state, 0xc5 + i, 0x00);
1317 cx24120_writereg(state, 0xc6 + i, 0x00);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001318 }
1319
Jemma Denson92443cd2015-05-08 16:57:56 -03001320 cx24120_writereg(state, 0xe4, 0x03);
1321 cx24120_writereg(state, 0xeb, 0x0a);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001322
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001323 dev_dbg(&state->i2c->dev, "requesting firmware (%s) to download...\n",
1324 CX24120_FIRMWARE);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001325
1326 ret = state->config->request_firmware(fe, &fw, CX24120_FIRMWARE);
1327 if (ret) {
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -03001328 err("Could not load firmware (%s): %d\n", CX24120_FIRMWARE,
1329 ret);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001330 return ret;
1331 }
1332
1333 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001334 "Firmware found, size %d bytes (%02x %02x .. %02x %02x)\n",
Jemma Denson5afc9a22015-04-14 09:04:50 -03001335 (int)fw->size, /* firmware_size in bytes */
1336 fw->data[0], /* fw 1st byte */
1337 fw->data[1], /* fw 2d byte */
1338 fw->data[fw->size - 2], /* fw before last byte */
1339 fw->data[fw->size - 1]); /* fw last byte */
1340
Jemma Denson92443cd2015-05-08 16:57:56 -03001341 cx24120_test_rom(state);
Jemma Denson16687972015-05-08 17:26:34 -03001342 reg = cx24120_readreg(state, 0xfb) & 0xfe;
1343 cx24120_writereg(state, 0xfb, reg);
Jemma Denson92443cd2015-05-08 16:57:56 -03001344 cx24120_writereg(state, 0xe0, 0x76);
1345 cx24120_writereg(state, 0xf7, 0x81);
1346 cx24120_writereg(state, 0xf8, 0x00);
1347 cx24120_writereg(state, 0xf9, 0x00);
1348 cx24120_writeregs(state, 0xfa, fw->data, (fw->size - 1), 0x00);
1349 cx24120_writereg(state, 0xf7, 0xc0);
1350 cx24120_writereg(state, 0xe0, 0x00);
Jemma Denson16687972015-05-08 17:26:34 -03001351 reg = (fw->size - 2) & 0x00ff;
1352 cx24120_writereg(state, 0xf8, reg);
1353 reg = ((fw->size - 2) >> 8) & 0x00ff;
1354 cx24120_writereg(state, 0xf9, reg);
Jemma Denson92443cd2015-05-08 16:57:56 -03001355 cx24120_writereg(state, 0xf7, 0x00);
1356 cx24120_writereg(state, 0xdc, 0x00);
1357 cx24120_writereg(state, 0xdc, 0x07);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001358 msleep(500);
1359
1360 /* Check final byte matches final byte of firmware */
Jemma Denson16687972015-05-08 17:26:34 -03001361 reg = cx24120_readreg(state, 0xe1);
1362 if (reg == fw->data[fw->size - 1]) {
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001363 dev_dbg(&state->i2c->dev, "Firmware uploaded successfully\n");
Jemma Denson41336012015-05-08 17:36:19 -03001364 ret = 0;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001365 } else {
1366 err("Firmware upload failed. Last byte returned=0x%x\n", ret);
Jemma Denson41336012015-05-08 17:36:19 -03001367 ret = -EREMOTEIO;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001368 }
Jemma Denson92443cd2015-05-08 16:57:56 -03001369 cx24120_writereg(state, 0xdc, 0x00);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001370 release_firmware(fw);
Jemma Denson41336012015-05-08 17:36:19 -03001371 if (ret != 0)
1372 return ret;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001373
Jemma Denson5afc9a22015-04-14 09:04:50 -03001374 /* Start tuner */
1375 cmd.id = CMD_START_TUNER;
1376 cmd.len = 3;
1377 cmd.arg[0] = 0x00;
1378 cmd.arg[1] = 0x00;
1379 cmd.arg[2] = 0x00;
1380
1381 if (cx24120_message_send(state, &cmd) != 0) {
1382 err("Error tuner start! :(\n");
1383 return -EREMOTEIO;
1384 }
1385
1386 /* Set VCO */
Jemma Densonc84251b2015-05-03 08:55:15 -03001387 ret = cx24120_set_vco(state);
1388 if (ret != 0) {
Jemma Denson5afc9a22015-04-14 09:04:50 -03001389 err("Error set VCO! :(\n");
Jemma Densonc84251b2015-05-03 08:55:15 -03001390 return ret;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001391 }
1392
Jemma Denson5afc9a22015-04-14 09:04:50 -03001393 /* set bandwidth */
1394 cmd.id = CMD_BANDWIDTH;
1395 cmd.len = 12;
1396 cmd.arg[0] = 0x00;
1397 cmd.arg[1] = 0x00;
1398 cmd.arg[2] = 0x00;
1399 cmd.arg[3] = 0x00;
1400 cmd.arg[4] = 0x05;
1401 cmd.arg[5] = 0x02;
1402 cmd.arg[6] = 0x02;
1403 cmd.arg[7] = 0x00;
1404 cmd.arg[8] = 0x05;
1405 cmd.arg[9] = 0x02;
1406 cmd.arg[10] = 0x02;
1407 cmd.arg[11] = 0x00;
1408
1409 if (cx24120_message_send(state, &cmd)) {
1410 err("Error set bandwidth!\n");
1411 return -EREMOTEIO;
1412 }
1413
Jemma Denson16687972015-05-08 17:26:34 -03001414 reg = cx24120_readreg(state, 0xba);
1415 if (reg > 3) {
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001416 dev_dbg(&state->i2c->dev, "Reset-readreg 0xba: %x\n", ret);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001417 err("Error initialising tuner!\n");
1418 return -EREMOTEIO;
1419 }
1420
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001421 dev_dbg(&state->i2c->dev, "Tuner initialised correctly.\n");
Jemma Denson5afc9a22015-04-14 09:04:50 -03001422
1423 /* Initialise mpeg outputs */
Jemma Denson92443cd2015-05-08 16:57:56 -03001424 cx24120_writereg(state, 0xeb, 0x0a);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001425 if (cx24120_msg_mpeg_output_global_config(state, 0) ||
1426 cx24120_msg_mpeg_output_config(state, 0) ||
1427 cx24120_msg_mpeg_output_config(state, 1) ||
1428 cx24120_msg_mpeg_output_config(state, 2)) {
1429 err("Error initialising mpeg output. :(\n");
1430 return -EREMOTEIO;
1431 }
1432
Jemma Densonddcb2522015-05-19 15:17:23 -03001433 /* Set size of BER window */
1434 cmd.id = CMD_BER_CTRL;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001435 cmd.len = 3;
1436 cmd.arg[0] = 0x00;
Jemma Densonddcb2522015-05-19 15:17:23 -03001437 cmd.arg[1] = CX24120_BER_WINDOW;
1438 cmd.arg[2] = CX24120_BER_WINDOW;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001439 if (cx24120_message_send(state, &cmd)) {
Jemma Densonddcb2522015-05-19 15:17:23 -03001440 err("Error setting ber window\n");
Jemma Denson5afc9a22015-04-14 09:04:50 -03001441 return -EREMOTEIO;
1442 }
1443
Jemma Denson5afc9a22015-04-14 09:04:50 -03001444 /* Firmware CMD 35: Get firmware version */
1445 cmd.id = CMD_FWVERSION;
1446 cmd.len = 1;
1447 for (i = 0; i < 4; i++) {
1448 cmd.arg[0] = i;
1449 ret = cx24120_message_send(state, &cmd);
1450 if (ret != 0)
1451 return ret;
1452 vers[i] = cx24120_readreg(state, CX24120_REG_MAILBOX);
1453 }
1454 info("FW version %i.%i.%i.%i\n", vers[0], vers[1], vers[2], vers[3]);
1455
Jemma Densond3cf06b2015-05-05 17:10:13 -03001456 /* init stats here in order signal app which stats are supported */
1457 c->strength.len = 1;
1458 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Jemma Denson3b5eb502015-05-08 15:52:45 -03001459 c->cnr.len = 1;
1460 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Jemma Densonddcb2522015-05-19 15:17:23 -03001461 c->post_bit_error.len = 1;
1462 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1463 c->post_bit_count.len = 1;
1464 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Jemma Denson80e97102015-05-19 15:52:46 -03001465 c->block_error.len = 1;
1466 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1467 c->block_count.len = 1;
1468 c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1469
Jemma Denson5afc9a22015-04-14 09:04:50 -03001470 state->cold_init = 1;
Patrick Boettcher71df6732015-05-20 04:58:49 -03001471
Jemma Denson5afc9a22015-04-14 09:04:50 -03001472 return 0;
1473}
1474
Jemma Denson5afc9a22015-04-14 09:04:50 -03001475static int cx24120_tune(struct dvb_frontend *fe, bool re_tune,
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -03001476 unsigned int mode_flags, unsigned int *delay,
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03001477 enum fe_status *status)
Jemma Denson5afc9a22015-04-14 09:04:50 -03001478{
1479 struct cx24120_state *state = fe->demodulator_priv;
1480 int ret;
1481
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001482 dev_dbg(&state->i2c->dev, "(%d)\n", re_tune);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001483
1484 /* TODO: Do we need to set delay? */
1485
1486 if (re_tune) {
1487 ret = cx24120_set_frontend(fe);
1488 if (ret)
1489 return ret;
1490 }
1491
1492 return cx24120_read_status(fe, status);
1493}
1494
Jemma Denson5afc9a22015-04-14 09:04:50 -03001495static int cx24120_get_algo(struct dvb_frontend *fe)
1496{
1497 return DVBFE_ALGO_HW;
1498}
1499
Jemma Denson5afc9a22015-04-14 09:04:50 -03001500static int cx24120_sleep(struct dvb_frontend *fe)
1501{
1502 return 0;
1503}
1504
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -02001505static int cx24120_get_frontend(struct dvb_frontend *fe,
1506 struct dtv_frontend_properties *c)
Jemma Denson5afc9a22015-04-14 09:04:50 -03001507{
Jemma Denson5afc9a22015-04-14 09:04:50 -03001508 struct cx24120_state *state = fe->demodulator_priv;
1509 u8 freq1, freq2, freq3;
Jemma Denson035cad52016-02-28 15:29:50 -03001510 int status;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001511
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001512 dev_dbg(&state->i2c->dev, "\n");
Jemma Denson5afc9a22015-04-14 09:04:50 -03001513
1514 /* don't return empty data if we're not tuned in */
Jemma Denson035cad52016-02-28 15:29:50 -03001515 status = cx24120_readreg(state, CX24120_REG_STATUS);
1516 if (!(status & CX24120_HAS_LOCK))
Jemma Denson5afc9a22015-04-14 09:04:50 -03001517 return 0;
1518
1519 /* Get frequency */
1520 freq1 = cx24120_readreg(state, CX24120_REG_FREQ1);
1521 freq2 = cx24120_readreg(state, CX24120_REG_FREQ2);
1522 freq3 = cx24120_readreg(state, CX24120_REG_FREQ3);
1523 c->frequency = (freq3 << 16) | (freq2 << 8) | freq1;
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001524 dev_dbg(&state->i2c->dev, "frequency = %d\n", c->frequency);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001525
1526 /* Get modulation, fec, pilot */
1527 cx24120_get_fec(fe);
1528
1529 return 0;
1530}
1531
Jemma Denson5afc9a22015-04-14 09:04:50 -03001532static void cx24120_release(struct dvb_frontend *fe)
1533{
1534 struct cx24120_state *state = fe->demodulator_priv;
1535
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001536 dev_dbg(&state->i2c->dev, "Clear state structure\n");
Jemma Denson5afc9a22015-04-14 09:04:50 -03001537 kfree(state);
1538}
1539
Jemma Denson5afc9a22015-04-14 09:04:50 -03001540static int cx24120_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1541{
1542 struct cx24120_state *state = fe->demodulator_priv;
Jemma Denson80e97102015-05-19 15:52:46 -03001543 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001544
Jemma Denson80e97102015-05-19 15:52:46 -03001545 if (c->block_error.stat[0].scale != FE_SCALE_COUNTER) {
1546 *ucblocks = 0;
1547 return 0;
1548 }
Jemma Denson5afc9a22015-04-14 09:04:50 -03001549
Jemma Densonbf8de2d2015-05-20 11:57:49 -03001550 *ucblocks = c->block_error.stat[0].uvalue - state->ucb_offset;
Jemma Denson80e97102015-05-19 15:52:46 -03001551
Jemma Denson5afc9a22015-04-14 09:04:50 -03001552 return 0;
1553}
1554
Jemma Denson5afc9a22015-04-14 09:04:50 -03001555static struct dvb_frontend_ops cx24120_ops = {
1556 .delsys = { SYS_DVBS, SYS_DVBS2 },
1557 .info = {
1558 .name = "Conexant CX24120/CX24118",
1559 .frequency_min = 950000,
1560 .frequency_max = 2150000,
1561 .frequency_stepsize = 1011, /* kHz for QPSK frontends */
1562 .frequency_tolerance = 5000,
1563 .symbol_rate_min = 1000000,
1564 .symbol_rate_max = 45000000,
1565 .caps = FE_CAN_INVERSION_AUTO |
1566 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1567 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
1568 FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1569 FE_CAN_2G_MODULATION |
1570 FE_CAN_QPSK | FE_CAN_RECOVER
1571 },
1572 .release = cx24120_release,
1573
1574 .init = cx24120_init,
1575 .sleep = cx24120_sleep,
1576
1577 .tune = cx24120_tune,
1578 .get_frontend_algo = cx24120_get_algo,
1579 .set_frontend = cx24120_set_frontend,
1580
1581 .get_frontend = cx24120_get_frontend,
1582 .read_status = cx24120_read_status,
1583 .read_ber = cx24120_read_ber,
1584 .read_signal_strength = cx24120_read_signal_strength,
1585 .read_snr = cx24120_read_snr,
1586 .read_ucblocks = cx24120_read_ucblocks,
1587
1588 .diseqc_send_master_cmd = cx24120_send_diseqc_msg,
1589
1590 .diseqc_send_burst = cx24120_diseqc_send_burst,
1591 .set_tone = cx24120_set_tone,
1592 .set_voltage = cx24120_set_voltage,
1593};
1594
1595MODULE_DESCRIPTION("DVB Frontend module for Conexant CX24120/CX24118 hardware");
1596MODULE_AUTHOR("Jemma Denson");
1597MODULE_LICENSE("GPL");