blob: dc80e6affeae67ff4c43b3ff98d0cdd00d8875f9 [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;
121 fe_spectral_inversion_t inversion;
122 fe_code_rate_t fec;
123
124 fe_delivery_system_t delsys;
125 fe_modulation_t modulation;
126 fe_pilot_t pilot;
127
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
151 fe_status_t fe_status;
Jemma Densonddcb2522015-05-19 15:17:23 -0300152
153 /* ber stats calulations */
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 Denson80e97102015-05-19 15:52:46 -0300157 u32 per_prev;
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,
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300494 fe_sec_mini_cmd_t 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
Jemma Denson5afc9a22015-04-14 09:04:50 -0300516static int cx24120_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
517{
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,
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300539 fe_sec_voltage_t 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
701 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
702 c->block_error.stat[0].uvalue += ucb;
703
704 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
705 c->block_count.stat[0].uvalue += state->bitrate / 8 / 208;
706 }
Jemma Denson9fc18f12015-05-05 16:59:27 -0300707}
708
Jemma Denson6138dc22015-04-30 16:37:42 -0300709static void cx24120_set_clock_ratios(struct dvb_frontend *fe);
710
Jemma Denson5afc9a22015-04-14 09:04:50 -0300711/* Read current tuning status */
712static int cx24120_read_status(struct dvb_frontend *fe, fe_status_t *status)
713{
714 struct cx24120_state *state = fe->demodulator_priv;
715 int lock;
716
717 lock = cx24120_readreg(state, CX24120_REG_STATUS);
718
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300719 dev_dbg(&state->i2c->dev, "status = 0x%02x\n", lock);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300720
721 *status = 0;
722
723 if (lock & CX24120_HAS_SIGNAL)
724 *status = FE_HAS_SIGNAL;
725 if (lock & CX24120_HAS_CARRIER)
726 *status |= FE_HAS_CARRIER;
727 if (lock & CX24120_HAS_VITERBI)
728 *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
729 if (lock & CX24120_HAS_LOCK)
730 *status |= FE_HAS_LOCK;
731
Jemma Densonfbdbab72015-05-05 18:31:10 -0300732 /*
733 * TODO: is FE_HAS_SYNC in the right place?
Jemma Denson5afc9a22015-04-14 09:04:50 -0300734 * Other cx241xx drivers have this slightly
Jemma Densonfbdbab72015-05-05 18:31:10 -0300735 * different
736 */
Jemma Denson5afc9a22015-04-14 09:04:50 -0300737
Jemma Denson14626122015-05-05 17:18:11 -0300738 state->fe_status = *status;
739 cx24120_get_stats(state);
Jemma Denson9fc18f12015-05-05 16:59:27 -0300740
Jemma Denson6138dc22015-04-30 16:37:42 -0300741 /* Set the clock once tuned in */
742 if (state->need_clock_set && *status & FE_HAS_LOCK) {
743 /* Set clock ratios */
744 cx24120_set_clock_ratios(fe);
745
746 /* Old driver would do a msleep(200) here */
747
748 /* Renable mpeg output */
749 if (!state->mpeg_enabled)
750 cx24120_msg_mpeg_output_global_config(state, 1);
751
752 state->need_clock_set = 0;
753 }
754
Jemma Denson5afc9a22015-04-14 09:04:50 -0300755 return 0;
756}
757
Jemma Densonfbdbab72015-05-05 18:31:10 -0300758/*
759 * FEC & modulation lookup table
Jemma Denson5afc9a22015-04-14 09:04:50 -0300760 * Used for decoding the REG_FECMODE register
761 * once tuned in.
762 */
Mauro Carvalho Chehabec8fe6c2015-05-19 08:19:47 -0300763struct cx24120_modfec {
Jemma Denson5afc9a22015-04-14 09:04:50 -0300764 fe_delivery_system_t delsys;
765 fe_modulation_t mod;
766 fe_code_rate_t fec;
767 u8 val;
Mauro Carvalho Chehabec8fe6c2015-05-19 08:19:47 -0300768};
769
770static const struct cx24120_modfec modfec_lookup_table[] = {
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -0300771 /*delsys mod fec val */
772 { SYS_DVBS, QPSK, FEC_1_2, 0x01 },
773 { SYS_DVBS, QPSK, FEC_2_3, 0x02 },
774 { SYS_DVBS, QPSK, FEC_3_4, 0x03 },
775 { SYS_DVBS, QPSK, FEC_4_5, 0x04 },
776 { SYS_DVBS, QPSK, FEC_5_6, 0x05 },
777 { SYS_DVBS, QPSK, FEC_6_7, 0x06 },
778 { SYS_DVBS, QPSK, FEC_7_8, 0x07 },
Jemma Denson5afc9a22015-04-14 09:04:50 -0300779
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -0300780 { SYS_DVBS2, QPSK, FEC_1_2, 0x04 },
781 { SYS_DVBS2, QPSK, FEC_3_5, 0x05 },
782 { SYS_DVBS2, QPSK, FEC_2_3, 0x06 },
783 { SYS_DVBS2, QPSK, FEC_3_4, 0x07 },
784 { SYS_DVBS2, QPSK, FEC_4_5, 0x08 },
785 { SYS_DVBS2, QPSK, FEC_5_6, 0x09 },
786 { SYS_DVBS2, QPSK, FEC_8_9, 0x0a },
787 { SYS_DVBS2, QPSK, FEC_9_10, 0x0b },
Jemma Denson5afc9a22015-04-14 09:04:50 -0300788
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -0300789 { SYS_DVBS2, PSK_8, FEC_3_5, 0x0c },
790 { SYS_DVBS2, PSK_8, FEC_2_3, 0x0d },
791 { SYS_DVBS2, PSK_8, FEC_3_4, 0x0e },
792 { SYS_DVBS2, PSK_8, FEC_5_6, 0x0f },
793 { SYS_DVBS2, PSK_8, FEC_8_9, 0x10 },
794 { SYS_DVBS2, PSK_8, FEC_9_10, 0x11 },
Jemma Denson5afc9a22015-04-14 09:04:50 -0300795};
796
Jemma Denson5afc9a22015-04-14 09:04:50 -0300797/* Retrieve current fec, modulation & pilot values */
798static int cx24120_get_fec(struct dvb_frontend *fe)
799{
800 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
801 struct cx24120_state *state = fe->demodulator_priv;
802 int idx;
803 int ret;
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300804 int fec;
Jemma Denson5afc9a22015-04-14 09:04:50 -0300805
Jemma Denson5afc9a22015-04-14 09:04:50 -0300806 ret = cx24120_readreg(state, CX24120_REG_FECMODE);
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300807 fec = ret & 0x3f; /* Lower 6 bits */
Jemma Denson5afc9a22015-04-14 09:04:50 -0300808
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300809 dev_dbg(&state->i2c->dev, "raw fec = %d\n", fec);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300810
811 for (idx = 0; idx < ARRAY_SIZE(modfec_lookup_table); idx++) {
812 if (modfec_lookup_table[idx].delsys != state->dcur.delsys)
813 continue;
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300814 if (modfec_lookup_table[idx].val != fec)
Jemma Denson5afc9a22015-04-14 09:04:50 -0300815 continue;
816
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -0300817 break; /* found */
Jemma Denson5afc9a22015-04-14 09:04:50 -0300818 }
819
820 if (idx >= ARRAY_SIZE(modfec_lookup_table)) {
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300821 dev_dbg(&state->i2c->dev, "couldn't find fec!\n");
Jemma Denson5afc9a22015-04-14 09:04:50 -0300822 return -EINVAL;
823 }
824
825 /* save values back to cache */
826 c->modulation = modfec_lookup_table[idx].mod;
827 c->fec_inner = modfec_lookup_table[idx].fec;
828 c->pilot = (ret & 0x80) ? PILOT_ON : PILOT_OFF;
829
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300830 dev_dbg(&state->i2c->dev, "mod(%d), fec(%d), pilot(%d)\n",
Jemma Denson5afc9a22015-04-14 09:04:50 -0300831 c->modulation, c->fec_inner, c->pilot);
832
833 return 0;
834}
835
Jemma Densonddcb2522015-05-19 15:17:23 -0300836/* Calculate ber window time */
837void cx24120_calculate_ber_window(struct cx24120_state *state, u32 rate)
838{
839 struct dvb_frontend *fe = &state->frontend;
840 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Jemma Denson80e97102015-05-19 15:52:46 -0300841 u64 tmp;
Jemma Densonddcb2522015-05-19 15:17:23 -0300842
843 /*
844 * Calculate bitrate from rate in the clock ratios table.
845 * This isn't *exactly* right but close enough.
846 */
Jemma Denson80e97102015-05-19 15:52:46 -0300847 tmp = (u64)c->symbol_rate * rate;
848 do_div(tmp, 256);
849 state->bitrate = tmp;
Jemma Densonddcb2522015-05-19 15:17:23 -0300850
851 /* usecs per ber window */
852 tmp = 1000000ULL * CX24120_BER_WSIZE;
Jemma Denson80e97102015-05-19 15:52:46 -0300853 do_div(tmp, state->bitrate);
Jemma Densonddcb2522015-05-19 15:17:23 -0300854 state->berw_usecs = tmp;
855
Jemma Denson80e97102015-05-19 15:52:46 -0300856 dev_dbg(&state->i2c->dev, "bitrate: %u, berw_usecs: %u\n",
857 state->bitrate, state->berw_usecs);
Jemma Densonddcb2522015-05-19 15:17:23 -0300858}
859
Jemma Densonfbdbab72015-05-05 18:31:10 -0300860/*
861 * Clock ratios lookup table
Jemma Denson5afc9a22015-04-14 09:04:50 -0300862 *
863 * Values obtained from much larger table in old driver
864 * which had numerous entries which would never match.
865 *
866 * There's probably some way of calculating these but I
867 * can't determine the pattern
Jemma Densonfbdbab72015-05-05 18:31:10 -0300868 */
Mauro Carvalho Chehabec8fe6c2015-05-19 08:19:47 -0300869struct cx24120_clock_ratios_table {
Jemma Denson5afc9a22015-04-14 09:04:50 -0300870 fe_delivery_system_t delsys;
871 fe_pilot_t pilot;
872 fe_modulation_t mod;
873 fe_code_rate_t fec;
874 u32 m_rat;
875 u32 n_rat;
876 u32 rate;
Mauro Carvalho Chehabec8fe6c2015-05-19 08:19:47 -0300877};
878
879static const struct cx24120_clock_ratios_table clock_ratios_table[] = {
Patrick Boettcher2e89a5e2015-04-28 13:18:05 -0300880 /*delsys pilot mod fec m_rat n_rat rate */
881 { SYS_DVBS2, PILOT_OFF, QPSK, FEC_1_2, 273088, 254505, 274 },
882 { SYS_DVBS2, PILOT_OFF, QPSK, FEC_3_5, 17272, 13395, 330 },
883 { SYS_DVBS2, PILOT_OFF, QPSK, FEC_2_3, 24344, 16967, 367 },
884 { SYS_DVBS2, PILOT_OFF, QPSK, FEC_3_4, 410788, 254505, 413 },
885 { SYS_DVBS2, PILOT_OFF, QPSK, FEC_4_5, 438328, 254505, 440 },
886 { SYS_DVBS2, PILOT_OFF, QPSK, FEC_5_6, 30464, 16967, 459 },
887 { SYS_DVBS2, PILOT_OFF, QPSK, FEC_8_9, 487832, 254505, 490 },
888 { SYS_DVBS2, PILOT_OFF, QPSK, FEC_9_10, 493952, 254505, 496 },
889 { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_3_5, 328168, 169905, 494 },
890 { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_2_3, 24344, 11327, 550 },
891 { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_3_4, 410788, 169905, 618 },
892 { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_5_6, 30464, 11327, 688 },
893 { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_8_9, 487832, 169905, 735 },
894 { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_9_10, 493952, 169905, 744 },
895 { SYS_DVBS2, PILOT_ON, QPSK, FEC_1_2, 273088, 260709, 268 },
896 { SYS_DVBS2, PILOT_ON, QPSK, FEC_3_5, 328168, 260709, 322 },
897 { SYS_DVBS2, PILOT_ON, QPSK, FEC_2_3, 121720, 86903, 358 },
898 { SYS_DVBS2, PILOT_ON, QPSK, FEC_3_4, 410788, 260709, 403 },
899 { SYS_DVBS2, PILOT_ON, QPSK, FEC_4_5, 438328, 260709, 430 },
900 { SYS_DVBS2, PILOT_ON, QPSK, FEC_5_6, 152320, 86903, 448 },
901 { SYS_DVBS2, PILOT_ON, QPSK, FEC_8_9, 487832, 260709, 479 },
902 { SYS_DVBS2, PILOT_ON, QPSK, FEC_9_10, 493952, 260709, 485 },
903 { SYS_DVBS2, PILOT_ON, PSK_8, FEC_3_5, 328168, 173853, 483 },
904 { SYS_DVBS2, PILOT_ON, PSK_8, FEC_2_3, 121720, 57951, 537 },
905 { SYS_DVBS2, PILOT_ON, PSK_8, FEC_3_4, 410788, 173853, 604 },
906 { SYS_DVBS2, PILOT_ON, PSK_8, FEC_5_6, 152320, 57951, 672 },
907 { SYS_DVBS2, PILOT_ON, PSK_8, FEC_8_9, 487832, 173853, 718 },
908 { SYS_DVBS2, PILOT_ON, PSK_8, FEC_9_10, 493952, 173853, 727 },
909 { SYS_DVBS, PILOT_OFF, QPSK, FEC_1_2, 152592, 152592, 256 },
910 { SYS_DVBS, PILOT_OFF, QPSK, FEC_2_3, 305184, 228888, 341 },
911 { SYS_DVBS, PILOT_OFF, QPSK, FEC_3_4, 457776, 305184, 384 },
912 { SYS_DVBS, PILOT_OFF, QPSK, FEC_5_6, 762960, 457776, 427 },
913 { SYS_DVBS, PILOT_OFF, QPSK, FEC_7_8, 1068144, 610368, 448 },
Jemma Denson5afc9a22015-04-14 09:04:50 -0300914};
915
Jemma Denson5afc9a22015-04-14 09:04:50 -0300916/* Set clock ratio from lookup table */
917static void cx24120_set_clock_ratios(struct dvb_frontend *fe)
918{
919 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
920 struct cx24120_state *state = fe->demodulator_priv;
921 struct cx24120_cmd cmd;
922 int ret, idx;
923
924 /* Find fec, modulation, pilot */
925 ret = cx24120_get_fec(fe);
926 if (ret != 0)
927 return;
928
929 /* Find the clock ratios in the lookup table */
930 for (idx = 0; idx < ARRAY_SIZE(clock_ratios_table); idx++) {
931 if (clock_ratios_table[idx].delsys != state->dcur.delsys)
932 continue;
933 if (clock_ratios_table[idx].mod != c->modulation)
934 continue;
935 if (clock_ratios_table[idx].fec != c->fec_inner)
936 continue;
937 if (clock_ratios_table[idx].pilot != c->pilot)
938 continue;
939
940 break; /* found */
941 }
942
943 if (idx >= ARRAY_SIZE(clock_ratios_table)) {
944 info("Clock ratio not found - data reception in danger\n");
945 return;
946 }
947
Jemma Denson5afc9a22015-04-14 09:04:50 -0300948 /* Read current values? */
949 cmd.id = CMD_CLOCK_READ;
950 cmd.len = 1;
951 cmd.arg[0] = 0x00;
952 ret = cx24120_message_sendrcv(state, &cmd, 6);
953 if (ret != 0)
954 return;
955 /* in cmd[0]-[5] - result */
956
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300957 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 -0300958 cmd.arg[2] | (cmd.arg[1] << 8) | (cmd.arg[0] << 16),
959 cmd.arg[5] | (cmd.arg[4] << 8) | (cmd.arg[3] << 16),
960 idx,
961 clock_ratios_table[idx].m_rat,
962 clock_ratios_table[idx].n_rat,
963 clock_ratios_table[idx].rate);
964
Jemma Denson5afc9a22015-04-14 09:04:50 -0300965 /* Set the clock */
966 cmd.id = CMD_CLOCK_SET;
967 cmd.len = 10;
968 cmd.arg[0] = 0;
969 cmd.arg[1] = 0x10;
970 cmd.arg[2] = (clock_ratios_table[idx].m_rat >> 16) & 0xff;
971 cmd.arg[3] = (clock_ratios_table[idx].m_rat >> 8) & 0xff;
972 cmd.arg[4] = (clock_ratios_table[idx].m_rat >> 0) & 0xff;
973 cmd.arg[5] = (clock_ratios_table[idx].n_rat >> 16) & 0xff;
974 cmd.arg[6] = (clock_ratios_table[idx].n_rat >> 8) & 0xff;
975 cmd.arg[7] = (clock_ratios_table[idx].n_rat >> 0) & 0xff;
976 cmd.arg[8] = (clock_ratios_table[idx].rate >> 8) & 0xff;
977 cmd.arg[9] = (clock_ratios_table[idx].rate >> 0) & 0xff;
978
979 cx24120_message_send(state, &cmd);
Jemma Densonddcb2522015-05-19 15:17:23 -0300980
981 /* Calculate ber window rates for stat work */
982 cx24120_calculate_ber_window(state, clock_ratios_table[idx].rate);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300983}
984
Jemma Denson5afc9a22015-04-14 09:04:50 -0300985/* Set inversion value */
986static int cx24120_set_inversion(struct cx24120_state *state,
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -0300987 fe_spectral_inversion_t inversion)
Jemma Denson5afc9a22015-04-14 09:04:50 -0300988{
Jemma Denson2f3f07f2015-05-08 16:34:31 -0300989 dev_dbg(&state->i2c->dev, "(%d)\n", inversion);
Jemma Denson5afc9a22015-04-14 09:04:50 -0300990
991 switch (inversion) {
992 case INVERSION_OFF:
993 state->dnxt.inversion_val = 0x00;
994 break;
995 case INVERSION_ON:
996 state->dnxt.inversion_val = 0x04;
997 break;
998 case INVERSION_AUTO:
999 state->dnxt.inversion_val = 0x0c;
1000 break;
1001 default:
1002 return -EINVAL;
1003 }
1004
1005 state->dnxt.inversion = inversion;
1006
1007 return 0;
1008}
1009
Jemma Denson5c0a1c282015-05-15 16:08:26 -03001010/* FEC lookup table for tuning */
Mauro Carvalho Chehabec8fe6c2015-05-19 08:19:47 -03001011struct cx24120_modfec_table {
Jemma Denson5afc9a22015-04-14 09:04:50 -03001012 fe_delivery_system_t delsys;
1013 fe_modulation_t mod;
1014 fe_code_rate_t fec;
1015 u8 val;
Mauro Carvalho Chehabec8fe6c2015-05-19 08:19:47 -03001016};
1017
1018static const struct cx24120_modfec_table modfec_table[] = {
Jemma Denson5c0a1c282015-05-15 16:08:26 -03001019 /*delsys mod fec val */
1020 { SYS_DVBS, QPSK, FEC_1_2, 0x2e },
1021 { SYS_DVBS, QPSK, FEC_2_3, 0x2f },
1022 { SYS_DVBS, QPSK, FEC_3_4, 0x30 },
1023 { SYS_DVBS, QPSK, FEC_5_6, 0x31 },
1024 { SYS_DVBS, QPSK, FEC_6_7, 0x32 },
1025 { SYS_DVBS, QPSK, FEC_7_8, 0x33 },
Jemma Denson5afc9a22015-04-14 09:04:50 -03001026
Jemma Denson5c0a1c282015-05-15 16:08:26 -03001027 { SYS_DVBS2, QPSK, FEC_1_2, 0x04 },
1028 { SYS_DVBS2, QPSK, FEC_3_5, 0x05 },
1029 { SYS_DVBS2, QPSK, FEC_2_3, 0x06 },
1030 { SYS_DVBS2, QPSK, FEC_3_4, 0x07 },
1031 { SYS_DVBS2, QPSK, FEC_4_5, 0x08 },
1032 { SYS_DVBS2, QPSK, FEC_5_6, 0x09 },
1033 { SYS_DVBS2, QPSK, FEC_8_9, 0x0a },
1034 { SYS_DVBS2, QPSK, FEC_9_10, 0x0b },
Jemma Denson5afc9a22015-04-14 09:04:50 -03001035
Jemma Denson5c0a1c282015-05-15 16:08:26 -03001036 { SYS_DVBS2, PSK_8, FEC_3_5, 0x0c },
1037 { SYS_DVBS2, PSK_8, FEC_2_3, 0x0d },
1038 { SYS_DVBS2, PSK_8, FEC_3_4, 0x0e },
1039 { SYS_DVBS2, PSK_8, FEC_5_6, 0x0f },
1040 { SYS_DVBS2, PSK_8, FEC_8_9, 0x10 },
1041 { SYS_DVBS2, PSK_8, FEC_9_10, 0x11 },
Jemma Denson5afc9a22015-04-14 09:04:50 -03001042};
1043
1044/* Set fec_val & fec_mask values from delsys, modulation & fec */
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -03001045static int cx24120_set_fec(struct cx24120_state *state, fe_modulation_t mod,
1046 fe_code_rate_t fec)
Jemma Denson5afc9a22015-04-14 09:04:50 -03001047{
1048 int idx;
1049
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001050 dev_dbg(&state->i2c->dev, "(0x%02x,0x%02x)\n", mod, fec);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001051
1052 state->dnxt.fec = fec;
1053
1054 /* Lookup fec_val from modfec table */
1055 for (idx = 0; idx < ARRAY_SIZE(modfec_table); idx++) {
1056 if (modfec_table[idx].delsys != state->dnxt.delsys)
1057 continue;
1058 if (modfec_table[idx].mod != mod)
1059 continue;
1060 if (modfec_table[idx].fec != fec)
1061 continue;
1062
1063 /* found */
1064 state->dnxt.fec_mask = 0x00;
1065 state->dnxt.fec_val = modfec_table[idx].val;
1066 return 0;
1067 }
1068
Jemma Denson5afc9a22015-04-14 09:04:50 -03001069 if (state->dnxt.delsys == SYS_DVBS2) {
1070 /* DVBS2 auto is 0x00/0x00 */
1071 state->dnxt.fec_mask = 0x00;
1072 state->dnxt.fec_val = 0x00;
1073 } else {
1074 /* Set DVB-S to auto */
1075 state->dnxt.fec_val = 0x2e;
1076 state->dnxt.fec_mask = 0xac;
1077 }
1078
1079 return 0;
1080}
1081
Jemma Denson5afc9a22015-04-14 09:04:50 -03001082/* Set pilot */
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -03001083static int cx24120_set_pilot(struct cx24120_state *state, fe_pilot_t pilot)
1084{
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001085 dev_dbg(&state->i2c->dev, "(%d)\n", pilot);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001086
1087 /* Pilot only valid in DVBS2 */
1088 if (state->dnxt.delsys != SYS_DVBS2) {
1089 state->dnxt.pilot_val = CX24120_PILOT_OFF;
1090 return 0;
1091 }
1092
Jemma Denson5afc9a22015-04-14 09:04:50 -03001093 switch (pilot) {
1094 case PILOT_OFF:
1095 state->dnxt.pilot_val = CX24120_PILOT_OFF;
1096 break;
1097 case PILOT_ON:
1098 state->dnxt.pilot_val = CX24120_PILOT_ON;
1099 break;
1100 case PILOT_AUTO:
1101 default:
1102 state->dnxt.pilot_val = CX24120_PILOT_AUTO;
1103 }
1104
1105 return 0;
1106}
1107
1108/* Set symbol rate */
1109static int cx24120_set_symbolrate(struct cx24120_state *state, u32 rate)
1110{
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001111 dev_dbg(&state->i2c->dev, "(%d)\n", rate);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001112
1113 state->dnxt.symbol_rate = rate;
1114
1115 /* Check symbol rate */
1116 if (rate > 31000000) {
1117 state->dnxt.clkdiv = (-(rate < 31000001) & 3) + 2;
1118 state->dnxt.ratediv = (-(rate < 31000001) & 6) + 4;
1119 } else {
1120 state->dnxt.clkdiv = 3;
1121 state->dnxt.ratediv = 6;
1122 }
1123
1124 return 0;
1125}
1126
Jemma Denson5afc9a22015-04-14 09:04:50 -03001127/* Overwrite the current tuning params, we are about to tune */
1128static void cx24120_clone_params(struct dvb_frontend *fe)
1129{
1130 struct cx24120_state *state = fe->demodulator_priv;
1131
1132 state->dcur = state->dnxt;
1133}
1134
Jemma Denson5afc9a22015-04-14 09:04:50 -03001135static int cx24120_set_frontend(struct dvb_frontend *fe)
1136{
1137 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1138 struct cx24120_state *state = fe->demodulator_priv;
1139 struct cx24120_cmd cmd;
1140 int ret;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001141
1142 switch (c->delivery_system) {
1143 case SYS_DVBS2:
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001144 dev_dbg(&state->i2c->dev, "DVB-S2\n");
Jemma Denson5afc9a22015-04-14 09:04:50 -03001145 break;
1146 case SYS_DVBS:
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001147 dev_dbg(&state->i2c->dev, "DVB-S\n");
Jemma Denson5afc9a22015-04-14 09:04:50 -03001148 break;
1149 default:
1150 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001151 "delivery system(%d) not supported\n",
1152 c->delivery_system);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001153 ret = -EINVAL;
1154 break;
1155 }
1156
Jemma Denson5afc9a22015-04-14 09:04:50 -03001157 state->dnxt.delsys = c->delivery_system;
1158 state->dnxt.modulation = c->modulation;
1159 state->dnxt.frequency = c->frequency;
1160 state->dnxt.pilot = c->pilot;
1161
1162 ret = cx24120_set_inversion(state, c->inversion);
1163 if (ret != 0)
1164 return ret;
1165
1166 ret = cx24120_set_fec(state, c->modulation, c->fec_inner);
1167 if (ret != 0)
1168 return ret;
1169
1170 ret = cx24120_set_pilot(state, c->pilot);
1171 if (ret != 0)
1172 return ret;
1173
1174 ret = cx24120_set_symbolrate(state, c->symbol_rate);
1175 if (ret != 0)
1176 return ret;
1177
Jemma Denson5afc9a22015-04-14 09:04:50 -03001178 /* discard the 'current' tuning parameters and prepare to tune */
1179 cx24120_clone_params(fe);
1180
1181 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001182 "delsys = %d\n", state->dcur.delsys);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001183 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001184 "modulation = %d\n", state->dcur.modulation);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001185 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001186 "frequency = %d\n", state->dcur.frequency);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001187 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001188 "pilot = %d (val = 0x%02x)\n",
Jemma Denson5afc9a22015-04-14 09:04:50 -03001189 state->dcur.pilot, state->dcur.pilot_val);
1190 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001191 "symbol_rate = %d (clkdiv/ratediv = 0x%02x/0x%02x)\n",
1192 state->dcur.symbol_rate,
Jemma Denson5afc9a22015-04-14 09:04:50 -03001193 state->dcur.clkdiv, state->dcur.ratediv);
1194 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001195 "FEC = %d (mask/val = 0x%02x/0x%02x)\n",
Jemma Denson5afc9a22015-04-14 09:04:50 -03001196 state->dcur.fec, state->dcur.fec_mask, state->dcur.fec_val);
1197 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001198 "Inversion = %d (val = 0x%02x)\n",
Jemma Denson5afc9a22015-04-14 09:04:50 -03001199 state->dcur.inversion, state->dcur.inversion_val);
1200
Jemma Denson6138dc22015-04-30 16:37:42 -03001201 /* Flag that clock needs to be set after tune */
1202 state->need_clock_set = 1;
1203
Jemma Denson5afc9a22015-04-14 09:04:50 -03001204 /* Tune in */
1205 cmd.id = CMD_TUNEREQUEST;
1206 cmd.len = 15;
1207 cmd.arg[0] = 0;
1208 cmd.arg[1] = (state->dcur.frequency & 0xff0000) >> 16;
1209 cmd.arg[2] = (state->dcur.frequency & 0x00ff00) >> 8;
1210 cmd.arg[3] = (state->dcur.frequency & 0x0000ff);
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -03001211 cmd.arg[4] = ((state->dcur.symbol_rate / 1000) & 0xff00) >> 8;
1212 cmd.arg[5] = ((state->dcur.symbol_rate / 1000) & 0x00ff);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001213 cmd.arg[6] = state->dcur.inversion;
1214 cmd.arg[7] = state->dcur.fec_val | state->dcur.pilot_val;
1215 cmd.arg[8] = CX24120_SEARCH_RANGE_KHZ >> 8;
1216 cmd.arg[9] = CX24120_SEARCH_RANGE_KHZ & 0xff;
1217 cmd.arg[10] = 0; /* maybe rolloff? */
1218 cmd.arg[11] = state->dcur.fec_mask;
1219 cmd.arg[12] = state->dcur.ratediv;
1220 cmd.arg[13] = state->dcur.clkdiv;
1221 cmd.arg[14] = 0;
1222
Jemma Denson5afc9a22015-04-14 09:04:50 -03001223 /* Send tune command */
1224 ret = cx24120_message_send(state, &cmd);
1225 if (ret != 0)
1226 return ret;
1227
1228 /* Write symbol rate values */
1229 ret = cx24120_writereg(state, CX24120_REG_CLKDIV, state->dcur.clkdiv);
1230 ret = cx24120_readreg(state, CX24120_REG_RATEDIV);
1231 ret &= 0xfffffff0;
1232 ret |= state->dcur.ratediv;
1233 ret = cx24120_writereg(state, CX24120_REG_RATEDIV, ret);
1234
Jemma Denson5afc9a22015-04-14 09:04:50 -03001235 return 0;
1236}
1237
Jemma Densonc84251b2015-05-03 08:55:15 -03001238/* Set vco from config */
1239static int cx24120_set_vco(struct cx24120_state *state)
Jemma Denson5afc9a22015-04-14 09:04:50 -03001240{
Jemma Densonc84251b2015-05-03 08:55:15 -03001241 struct cx24120_cmd cmd;
1242 u32 nxtal_khz, vco;
1243 u64 inv_vco;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001244 u32 xtal_khz = state->config->xtal_khz;
1245
Jemma Densonc84251b2015-05-03 08:55:15 -03001246 nxtal_khz = xtal_khz * 4;
1247 vco = nxtal_khz * 10;
1248 inv_vco = DIV_ROUND_CLOSEST_ULL(0x400000000ULL, vco);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001249
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001250 dev_dbg(&state->i2c->dev, "xtal=%d, vco=%d, inv_vco=%lld\n",
1251 xtal_khz, vco, inv_vco);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001252
Jemma Densonc84251b2015-05-03 08:55:15 -03001253 cmd.id = CMD_VCO_SET;
1254 cmd.len = 12;
1255 cmd.arg[0] = (vco >> 16) & 0xff;
1256 cmd.arg[1] = (vco >> 8) & 0xff;
1257 cmd.arg[2] = vco & 0xff;
1258 cmd.arg[3] = (inv_vco >> 8) & 0xff;
1259 cmd.arg[4] = (inv_vco) & 0xff;
1260 cmd.arg[5] = 0x03;
1261 cmd.arg[6] = (nxtal_khz >> 8) & 0xff;
1262 cmd.arg[7] = nxtal_khz & 0xff;
1263 cmd.arg[8] = 0x06;
1264 cmd.arg[9] = 0x03;
1265 cmd.arg[10] = (xtal_khz >> 16) & 0xff;
1266 cmd.arg[11] = xtal_khz & 0xff;
1267
1268 return cx24120_message_send(state, &cmd);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001269}
1270
Mauro Carvalho Chehab5b8bc802015-05-19 08:06:52 -03001271static int cx24120_init(struct dvb_frontend *fe)
Jemma Denson5afc9a22015-04-14 09:04:50 -03001272{
1273 const struct firmware *fw;
Jemma Densond3cf06b2015-05-05 17:10:13 -03001274 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001275 struct cx24120_state *state = fe->demodulator_priv;
1276 struct cx24120_cmd cmd;
Jemma Denson41336012015-05-08 17:36:19 -03001277 u8 reg;
1278 int ret, i;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001279 unsigned char vers[4];
1280
1281 if (state->cold_init)
1282 return 0;
1283
1284 /* ???? */
Jemma Denson92443cd2015-05-08 16:57:56 -03001285 cx24120_writereg(state, 0xea, 0x00);
1286 cx24120_test_rom(state);
Jemma Denson16687972015-05-08 17:26:34 -03001287 reg = cx24120_readreg(state, 0xfb) & 0xfe;
1288 cx24120_writereg(state, 0xfb, reg);
1289 reg = cx24120_readreg(state, 0xfc) & 0xfe;
1290 cx24120_writereg(state, 0xfc, reg);
Jemma Denson92443cd2015-05-08 16:57:56 -03001291 cx24120_writereg(state, 0xc3, 0x04);
1292 cx24120_writereg(state, 0xc4, 0x04);
1293 cx24120_writereg(state, 0xce, 0x00);
1294 cx24120_writereg(state, 0xcf, 0x00);
Jemma Denson16687972015-05-08 17:26:34 -03001295 reg = cx24120_readreg(state, 0xea) & 0xfe;
1296 cx24120_writereg(state, 0xea, reg);
Jemma Denson92443cd2015-05-08 16:57:56 -03001297 cx24120_writereg(state, 0xeb, 0x0c);
1298 cx24120_writereg(state, 0xec, 0x06);
1299 cx24120_writereg(state, 0xed, 0x05);
1300 cx24120_writereg(state, 0xee, 0x03);
1301 cx24120_writereg(state, 0xef, 0x05);
1302 cx24120_writereg(state, 0xf3, 0x03);
1303 cx24120_writereg(state, 0xf4, 0x44);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001304
Jemma Denson41336012015-05-08 17:36:19 -03001305 for (i = 0; i < 3; i++) {
1306 cx24120_writereg(state, 0xf0 + i, 0x04);
1307 cx24120_writereg(state, 0xe6 + i, 0x02);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001308 }
1309
Jemma Denson16687972015-05-08 17:26:34 -03001310 cx24120_writereg(state, 0xea, (reg | 0x01));
Jemma Denson41336012015-05-08 17:36:19 -03001311 for (i = 0; i < 6; i += 2) {
1312 cx24120_writereg(state, 0xc5 + i, 0x00);
1313 cx24120_writereg(state, 0xc6 + i, 0x00);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001314 }
1315
Jemma Denson92443cd2015-05-08 16:57:56 -03001316 cx24120_writereg(state, 0xe4, 0x03);
1317 cx24120_writereg(state, 0xeb, 0x0a);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001318
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001319 dev_dbg(&state->i2c->dev, "requesting firmware (%s) to download...\n",
1320 CX24120_FIRMWARE);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001321
1322 ret = state->config->request_firmware(fe, &fw, CX24120_FIRMWARE);
1323 if (ret) {
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -03001324 err("Could not load firmware (%s): %d\n", CX24120_FIRMWARE,
1325 ret);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001326 return ret;
1327 }
1328
1329 dev_dbg(&state->i2c->dev,
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001330 "Firmware found, size %d bytes (%02x %02x .. %02x %02x)\n",
Jemma Denson5afc9a22015-04-14 09:04:50 -03001331 (int)fw->size, /* firmware_size in bytes */
1332 fw->data[0], /* fw 1st byte */
1333 fw->data[1], /* fw 2d byte */
1334 fw->data[fw->size - 2], /* fw before last byte */
1335 fw->data[fw->size - 1]); /* fw last byte */
1336
Jemma Denson92443cd2015-05-08 16:57:56 -03001337 cx24120_test_rom(state);
Jemma Denson16687972015-05-08 17:26:34 -03001338 reg = cx24120_readreg(state, 0xfb) & 0xfe;
1339 cx24120_writereg(state, 0xfb, reg);
Jemma Denson92443cd2015-05-08 16:57:56 -03001340 cx24120_writereg(state, 0xe0, 0x76);
1341 cx24120_writereg(state, 0xf7, 0x81);
1342 cx24120_writereg(state, 0xf8, 0x00);
1343 cx24120_writereg(state, 0xf9, 0x00);
1344 cx24120_writeregs(state, 0xfa, fw->data, (fw->size - 1), 0x00);
1345 cx24120_writereg(state, 0xf7, 0xc0);
1346 cx24120_writereg(state, 0xe0, 0x00);
Jemma Denson16687972015-05-08 17:26:34 -03001347 reg = (fw->size - 2) & 0x00ff;
1348 cx24120_writereg(state, 0xf8, reg);
1349 reg = ((fw->size - 2) >> 8) & 0x00ff;
1350 cx24120_writereg(state, 0xf9, reg);
Jemma Denson92443cd2015-05-08 16:57:56 -03001351 cx24120_writereg(state, 0xf7, 0x00);
1352 cx24120_writereg(state, 0xdc, 0x00);
1353 cx24120_writereg(state, 0xdc, 0x07);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001354 msleep(500);
1355
1356 /* Check final byte matches final byte of firmware */
Jemma Denson16687972015-05-08 17:26:34 -03001357 reg = cx24120_readreg(state, 0xe1);
1358 if (reg == fw->data[fw->size - 1]) {
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001359 dev_dbg(&state->i2c->dev, "Firmware uploaded successfully\n");
Jemma Denson41336012015-05-08 17:36:19 -03001360 ret = 0;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001361 } else {
1362 err("Firmware upload failed. Last byte returned=0x%x\n", ret);
Jemma Denson41336012015-05-08 17:36:19 -03001363 ret = -EREMOTEIO;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001364 }
Jemma Denson92443cd2015-05-08 16:57:56 -03001365 cx24120_writereg(state, 0xdc, 0x00);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001366 release_firmware(fw);
Jemma Denson41336012015-05-08 17:36:19 -03001367 if (ret != 0)
1368 return ret;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001369
Jemma Denson5afc9a22015-04-14 09:04:50 -03001370 /* Start tuner */
1371 cmd.id = CMD_START_TUNER;
1372 cmd.len = 3;
1373 cmd.arg[0] = 0x00;
1374 cmd.arg[1] = 0x00;
1375 cmd.arg[2] = 0x00;
1376
1377 if (cx24120_message_send(state, &cmd) != 0) {
1378 err("Error tuner start! :(\n");
1379 return -EREMOTEIO;
1380 }
1381
1382 /* Set VCO */
Jemma Densonc84251b2015-05-03 08:55:15 -03001383 ret = cx24120_set_vco(state);
1384 if (ret != 0) {
Jemma Denson5afc9a22015-04-14 09:04:50 -03001385 err("Error set VCO! :(\n");
Jemma Densonc84251b2015-05-03 08:55:15 -03001386 return ret;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001387 }
1388
Jemma Denson5afc9a22015-04-14 09:04:50 -03001389 /* set bandwidth */
1390 cmd.id = CMD_BANDWIDTH;
1391 cmd.len = 12;
1392 cmd.arg[0] = 0x00;
1393 cmd.arg[1] = 0x00;
1394 cmd.arg[2] = 0x00;
1395 cmd.arg[3] = 0x00;
1396 cmd.arg[4] = 0x05;
1397 cmd.arg[5] = 0x02;
1398 cmd.arg[6] = 0x02;
1399 cmd.arg[7] = 0x00;
1400 cmd.arg[8] = 0x05;
1401 cmd.arg[9] = 0x02;
1402 cmd.arg[10] = 0x02;
1403 cmd.arg[11] = 0x00;
1404
1405 if (cx24120_message_send(state, &cmd)) {
1406 err("Error set bandwidth!\n");
1407 return -EREMOTEIO;
1408 }
1409
Jemma Denson16687972015-05-08 17:26:34 -03001410 reg = cx24120_readreg(state, 0xba);
1411 if (reg > 3) {
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001412 dev_dbg(&state->i2c->dev, "Reset-readreg 0xba: %x\n", ret);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001413 err("Error initialising tuner!\n");
1414 return -EREMOTEIO;
1415 }
1416
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001417 dev_dbg(&state->i2c->dev, "Tuner initialised correctly.\n");
Jemma Denson5afc9a22015-04-14 09:04:50 -03001418
1419 /* Initialise mpeg outputs */
Jemma Denson92443cd2015-05-08 16:57:56 -03001420 cx24120_writereg(state, 0xeb, 0x0a);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001421 if (cx24120_msg_mpeg_output_global_config(state, 0) ||
1422 cx24120_msg_mpeg_output_config(state, 0) ||
1423 cx24120_msg_mpeg_output_config(state, 1) ||
1424 cx24120_msg_mpeg_output_config(state, 2)) {
1425 err("Error initialising mpeg output. :(\n");
1426 return -EREMOTEIO;
1427 }
1428
Jemma Densonddcb2522015-05-19 15:17:23 -03001429 /* Set size of BER window */
1430 cmd.id = CMD_BER_CTRL;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001431 cmd.len = 3;
1432 cmd.arg[0] = 0x00;
Jemma Densonddcb2522015-05-19 15:17:23 -03001433 cmd.arg[1] = CX24120_BER_WINDOW;
1434 cmd.arg[2] = CX24120_BER_WINDOW;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001435 if (cx24120_message_send(state, &cmd)) {
Jemma Densonddcb2522015-05-19 15:17:23 -03001436 err("Error setting ber window\n");
Jemma Denson5afc9a22015-04-14 09:04:50 -03001437 return -EREMOTEIO;
1438 }
1439
Jemma Denson5afc9a22015-04-14 09:04:50 -03001440 /* Firmware CMD 35: Get firmware version */
1441 cmd.id = CMD_FWVERSION;
1442 cmd.len = 1;
1443 for (i = 0; i < 4; i++) {
1444 cmd.arg[0] = i;
1445 ret = cx24120_message_send(state, &cmd);
1446 if (ret != 0)
1447 return ret;
1448 vers[i] = cx24120_readreg(state, CX24120_REG_MAILBOX);
1449 }
1450 info("FW version %i.%i.%i.%i\n", vers[0], vers[1], vers[2], vers[3]);
1451
Jemma Densond3cf06b2015-05-05 17:10:13 -03001452 /* init stats here in order signal app which stats are supported */
1453 c->strength.len = 1;
1454 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Jemma Denson3b5eb502015-05-08 15:52:45 -03001455 c->cnr.len = 1;
1456 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Jemma Densonddcb2522015-05-19 15:17:23 -03001457 c->post_bit_error.len = 1;
1458 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1459 c->post_bit_count.len = 1;
1460 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Jemma Denson80e97102015-05-19 15:52:46 -03001461 c->block_error.len = 1;
1462 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1463 c->block_count.len = 1;
1464 c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1465
Jemma Densond3cf06b2015-05-05 17:10:13 -03001466
Jemma Denson5afc9a22015-04-14 09:04:50 -03001467 state->cold_init = 1;
1468 return 0;
1469}
1470
Jemma Denson5afc9a22015-04-14 09:04:50 -03001471static int cx24120_tune(struct dvb_frontend *fe, bool re_tune,
Patrick Boettcher1ff2e8e2015-04-28 13:39:20 -03001472 unsigned int mode_flags, unsigned int *delay,
1473 fe_status_t *status)
Jemma Denson5afc9a22015-04-14 09:04:50 -03001474{
1475 struct cx24120_state *state = fe->demodulator_priv;
1476 int ret;
1477
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001478 dev_dbg(&state->i2c->dev, "(%d)\n", re_tune);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001479
1480 /* TODO: Do we need to set delay? */
1481
1482 if (re_tune) {
1483 ret = cx24120_set_frontend(fe);
1484 if (ret)
1485 return ret;
1486 }
1487
1488 return cx24120_read_status(fe, status);
1489}
1490
Jemma Denson5afc9a22015-04-14 09:04:50 -03001491static int cx24120_get_algo(struct dvb_frontend *fe)
1492{
1493 return DVBFE_ALGO_HW;
1494}
1495
Jemma Denson5afc9a22015-04-14 09:04:50 -03001496static int cx24120_sleep(struct dvb_frontend *fe)
1497{
1498 return 0;
1499}
1500
Jemma Denson5afc9a22015-04-14 09:04:50 -03001501static int cx24120_get_frontend(struct dvb_frontend *fe)
1502{
1503 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1504 struct cx24120_state *state = fe->demodulator_priv;
1505 u8 freq1, freq2, freq3;
1506
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001507 dev_dbg(&state->i2c->dev, "\n");
Jemma Denson5afc9a22015-04-14 09:04:50 -03001508
1509 /* don't return empty data if we're not tuned in */
Jemma Denson14626122015-05-05 17:18:11 -03001510 if ((state->fe_status & FE_HAS_LOCK) == 0)
Jemma Denson5afc9a22015-04-14 09:04:50 -03001511 return 0;
1512
1513 /* Get frequency */
1514 freq1 = cx24120_readreg(state, CX24120_REG_FREQ1);
1515 freq2 = cx24120_readreg(state, CX24120_REG_FREQ2);
1516 freq3 = cx24120_readreg(state, CX24120_REG_FREQ3);
1517 c->frequency = (freq3 << 16) | (freq2 << 8) | freq1;
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001518 dev_dbg(&state->i2c->dev, "frequency = %d\n", c->frequency);
Jemma Denson5afc9a22015-04-14 09:04:50 -03001519
1520 /* Get modulation, fec, pilot */
1521 cx24120_get_fec(fe);
1522
1523 return 0;
1524}
1525
Jemma Denson5afc9a22015-04-14 09:04:50 -03001526static void cx24120_release(struct dvb_frontend *fe)
1527{
1528 struct cx24120_state *state = fe->demodulator_priv;
1529
Jemma Denson2f3f07f2015-05-08 16:34:31 -03001530 dev_dbg(&state->i2c->dev, "Clear state structure\n");
Jemma Denson5afc9a22015-04-14 09:04:50 -03001531 kfree(state);
1532}
1533
Jemma Denson5afc9a22015-04-14 09:04:50 -03001534static int cx24120_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1535{
1536 struct cx24120_state *state = fe->demodulator_priv;
Jemma Denson80e97102015-05-19 15:52:46 -03001537 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Jemma Denson5afc9a22015-04-14 09:04:50 -03001538
Jemma Denson80e97102015-05-19 15:52:46 -03001539 if (c->block_error.stat[0].scale != FE_SCALE_COUNTER) {
1540 *ucblocks = 0;
1541 return 0;
1542 }
Jemma Denson5afc9a22015-04-14 09:04:50 -03001543
Jemma Denson80e97102015-05-19 15:52:46 -03001544 *ucblocks = c->block_error.stat[0].uvalue - state->per_prev;
1545 state->per_prev = c->block_error.stat[0].uvalue;
1546
Jemma Denson5afc9a22015-04-14 09:04:50 -03001547 return 0;
1548}
1549
Jemma Denson5afc9a22015-04-14 09:04:50 -03001550static struct dvb_frontend_ops cx24120_ops = {
1551 .delsys = { SYS_DVBS, SYS_DVBS2 },
1552 .info = {
1553 .name = "Conexant CX24120/CX24118",
1554 .frequency_min = 950000,
1555 .frequency_max = 2150000,
1556 .frequency_stepsize = 1011, /* kHz for QPSK frontends */
1557 .frequency_tolerance = 5000,
1558 .symbol_rate_min = 1000000,
1559 .symbol_rate_max = 45000000,
1560 .caps = FE_CAN_INVERSION_AUTO |
1561 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1562 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
1563 FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1564 FE_CAN_2G_MODULATION |
1565 FE_CAN_QPSK | FE_CAN_RECOVER
1566 },
1567 .release = cx24120_release,
1568
1569 .init = cx24120_init,
1570 .sleep = cx24120_sleep,
1571
1572 .tune = cx24120_tune,
1573 .get_frontend_algo = cx24120_get_algo,
1574 .set_frontend = cx24120_set_frontend,
1575
1576 .get_frontend = cx24120_get_frontend,
1577 .read_status = cx24120_read_status,
1578 .read_ber = cx24120_read_ber,
1579 .read_signal_strength = cx24120_read_signal_strength,
1580 .read_snr = cx24120_read_snr,
1581 .read_ucblocks = cx24120_read_ucblocks,
1582
1583 .diseqc_send_master_cmd = cx24120_send_diseqc_msg,
1584
1585 .diseqc_send_burst = cx24120_diseqc_send_burst,
1586 .set_tone = cx24120_set_tone,
1587 .set_voltage = cx24120_set_voltage,
1588};
1589
1590MODULE_DESCRIPTION("DVB Frontend module for Conexant CX24120/CX24118 hardware");
1591MODULE_AUTHOR("Jemma Denson");
1592MODULE_LICENSE("GPL");