blob: cb36475e322b8aa816b27372163600d0adea1e21 [file] [log] [blame]
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 cx24110 - Single Chip Satellite Channel Receiver driver module
3
Johannes Stezenbacha8d995c2005-09-09 13:02:19 -07004 Copyright (C) 2002 Peter Hettkamp <peter.hettkamp@htp-tel.de> based on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 work
6 Copyright (C) 1999 Convergence Integrated Media GmbH <ralph@convergence.de>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23*/
24
25#include <linux/slab.h>
26#include <linux/kernel.h>
27#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
29
30#include "dvb_frontend.h"
31#include "cx24110.h"
32
33
34struct cx24110_state {
35
36 struct i2c_adapter* i2c;
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 const struct cx24110_config* config;
39
40 struct dvb_frontend frontend;
41
42 u32 lastber;
43 u32 lastbler;
44 u32 lastesn0;
45};
46
47static int debug;
48#define dprintk(args...) \
49 do { \
50 if (debug) printk(KERN_DEBUG "cx24110: " args); \
51 } while (0)
52
53static struct {u8 reg; u8 data;} cx24110_regdata[]=
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080054 /* Comments beginning with @ denote this value should
Michael Krufky50c25ff2006-01-09 15:25:34 -020055 be the default */
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080056 {{0x09,0x01}, /* SoftResetAll */
57 {0x09,0x00}, /* release reset */
58 {0x01,0xe8}, /* MSB of code rate 27.5MS/s */
59 {0x02,0x17}, /* middle byte " */
60 {0x03,0x29}, /* LSB " */
61 {0x05,0x03}, /* @ DVB mode, standard code rate 3/4 */
62 {0x06,0xa5}, /* @ PLL 60MHz */
63 {0x07,0x01}, /* @ Fclk, i.e. sampling clock, 60MHz */
64 {0x0a,0x00}, /* @ partial chip disables, do not set */
65 {0x0b,0x01}, /* set output clock in gapped mode, start signal low
Michael Krufky50c25ff2006-01-09 15:25:34 -020066 active for first byte */
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080067 {0x0c,0x11}, /* no parity bytes, large hold time, serial data out */
68 {0x0d,0x6f}, /* @ RS Sync/Unsync thresholds */
69 {0x10,0x40}, /* chip doc is misleading here: write bit 6 as 1
Michael Krufky50c25ff2006-01-09 15:25:34 -020070 to avoid starting the BER counter. Reset the
71 CRC test bit. Finite counting selected */
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080072 {0x15,0xff}, /* @ size of the limited time window for RS BER
Michael Krufky50c25ff2006-01-09 15:25:34 -020073 estimation. It is <value>*256 RS blocks, this
74 gives approx. 2.6 sec at 27.5MS/s, rate 3/4 */
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080075 {0x16,0x00}, /* @ enable all RS output ports */
76 {0x17,0x04}, /* @ time window allowed for the RS to sync */
77 {0x18,0xae}, /* @ allow all standard DVB code rates to be scanned
Michael Krufky50c25ff2006-01-09 15:25:34 -020078 for automatically */
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080079 /* leave the current code rate and normalization
Michael Krufky50c25ff2006-01-09 15:25:34 -020080 registers as they are after reset... */
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080081 {0x21,0x10}, /* @ during AutoAcq, search each viterbi setting
Michael Krufky50c25ff2006-01-09 15:25:34 -020082 only once */
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080083 {0x23,0x18}, /* @ size of the limited time window for Viterbi BER
Michael Krufky50c25ff2006-01-09 15:25:34 -020084 estimation. It is <value>*65536 channel bits, i.e.
85 approx. 38ms at 27.5MS/s, rate 3/4 */
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080086 {0x24,0x24}, /* do not trigger Viterbi CRC test. Finite count window */
87 /* leave front-end AGC parameters at default values */
88 /* leave decimation AGC parameters at default values */
89 {0x35,0x40}, /* disable all interrupts. They are not connected anyway */
90 {0x36,0xff}, /* clear all interrupt pending flags */
91 {0x37,0x00}, /* @ fully enable AutoAcqq state machine */
92 {0x38,0x07}, /* @ enable fade recovery, but not autostart AutoAcq */
93 /* leave the equalizer parameters on their default values */
94 /* leave the final AGC parameters on their default values */
95 {0x41,0x00}, /* @ MSB of front-end derotator frequency */
96 {0x42,0x00}, /* @ middle bytes " */
97 {0x43,0x00}, /* @ LSB " */
98 /* leave the carrier tracking loop parameters on default */
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020099 /* leave the bit timing loop parameters at default */
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800100 {0x56,0x4d}, /* set the filtune voltage to 2.7V, as recommended by */
101 /* the cx24108 data sheet for symbol rates above 15MS/s */
102 {0x57,0x00}, /* @ Filter sigma delta enabled, positive */
103 {0x61,0x95}, /* GPIO pins 1-4 have special function */
104 {0x62,0x05}, /* GPIO pin 5 has special function, pin 6 is GPIO */
105 {0x63,0x00}, /* All GPIO pins use CMOS output characteristics */
106 {0x64,0x20}, /* GPIO 6 is input, all others are outputs */
107 {0x6d,0x30}, /* tuner auto mode clock freq 62kHz */
108 {0x70,0x15}, /* use auto mode, tuner word is 21 bits long */
109 {0x73,0x00}, /* @ disable several demod bypasses */
110 {0x74,0x00}, /* @ " */
111 {0x75,0x00} /* @ " */
112 /* the remaining registers are for SEC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 };
114
115
116static int cx24110_writereg (struct cx24110_state* state, int reg, int data)
117{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800118 u8 buf [] = { reg, data };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
120 int err;
121
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800122 if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 dprintk ("%s: writereg error (err == %i, reg == 0x%02x,"
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300124 " data == 0x%02x)\n", __func__, err, reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return -EREMOTEIO;
126 }
127
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131static int cx24110_readreg (struct cx24110_state* state, u8 reg)
132{
133 int ret;
134 u8 b0 [] = { reg };
135 u8 b1 [] = { 0 };
136 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
137 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
138
139 ret = i2c_transfer(state->i2c, msg, 2);
140
141 if (ret != 2) return ret;
142
143 return b1[0];
144}
145
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300146static int cx24110_set_inversion(struct cx24110_state *state,
147 enum fe_spectral_inversion inversion)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149/* fixme (low): error handling */
150
151 switch (inversion) {
152 case INVERSION_OFF:
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800153 cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)|0x1);
154 /* AcqSpectrInvDis on. No idea why someone should want this */
155 cx24110_writereg(state,0x5,cx24110_readreg(state,0x5)&0xf7);
156 /* Initial value 0 at start of acq */
157 cx24110_writereg(state,0x22,cx24110_readreg(state,0x22)&0xef);
158 /* current value 0 */
159 /* The cx24110 manual tells us this reg is read-only.
160 But what the heck... set it ayways */
161 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 case INVERSION_ON:
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800163 cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)|0x1);
164 /* AcqSpectrInvDis on. No idea why someone should want this */
165 cx24110_writereg(state,0x5,cx24110_readreg(state,0x5)|0x08);
166 /* Initial value 1 at start of acq */
167 cx24110_writereg(state,0x22,cx24110_readreg(state,0x22)|0x10);
168 /* current value 1 */
169 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 case INVERSION_AUTO:
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800171 cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)&0xfe);
172 /* AcqSpectrInvDis off. Leave initial & current states as is */
173 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 default:
175 return -EINVAL;
176 }
177
178 return 0;
179}
180
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300181static int cx24110_set_fec(struct cx24110_state *state, enum fe_code_rate fec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Mauro Carvalho Chehabb8e64df2014-11-05 09:56:12 -0200183 static const int rate[FEC_AUTO] = {-1, 1, 2, 3, 5, 7, -1};
184 static const int g1[FEC_AUTO] = {-1, 0x01, 0x02, 0x05, 0x15, 0x45, -1};
185 static const int g2[FEC_AUTO] = {-1, 0x01, 0x03, 0x06, 0x1a, 0x7a, -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800187 /* Well, the AutoAcq engine of the cx24106 and 24110 automatically
188 searches all enabled viterbi rates, and can handle non-standard
189 rates as well. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Mauro Carvalho Chehabb8e64df2014-11-05 09:56:12 -0200191 if (fec > FEC_AUTO)
192 fec = FEC_AUTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Mauro Carvalho Chehab5eb28292014-11-05 09:57:23 -0200194 if (fec == FEC_AUTO) { /* (re-)establish AutoAcq behaviour */
195 cx24110_writereg(state, 0x37, cx24110_readreg(state, 0x37) & 0xdf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* clear AcqVitDis bit */
Mauro Carvalho Chehab5eb28292014-11-05 09:57:23 -0200197 cx24110_writereg(state, 0x18, 0xae);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* allow all DVB standard code rates */
Mauro Carvalho Chehab5eb28292014-11-05 09:57:23 -0200199 cx24110_writereg(state, 0x05, (cx24110_readreg(state, 0x05) & 0xf0) | 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* set nominal Viterbi rate 3/4 */
Mauro Carvalho Chehab5eb28292014-11-05 09:57:23 -0200201 cx24110_writereg(state, 0x22, (cx24110_readreg(state, 0x22) & 0xf0) | 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* set current Viterbi rate 3/4 */
Mauro Carvalho Chehab5eb28292014-11-05 09:57:23 -0200203 cx24110_writereg(state, 0x1a, 0x05);
204 cx24110_writereg(state, 0x1b, 0x06);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* set the puncture registers for code rate 3/4 */
206 return 0;
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800207 } else {
Mauro Carvalho Chehab5eb28292014-11-05 09:57:23 -0200208 cx24110_writereg(state, 0x37, cx24110_readreg(state, 0x37) | 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* set AcqVitDis bit */
Mauro Carvalho Chehaba2354732014-11-05 10:00:45 -0200210 if (rate[fec] < 0)
211 return -EINVAL;
212
213 cx24110_writereg(state, 0x05, (cx24110_readreg(state, 0x05) & 0xf0) | rate[fec]);
214 /* set nominal Viterbi rate */
215 cx24110_writereg(state, 0x22, (cx24110_readreg(state, 0x22) & 0xf0) | rate[fec]);
216 /* set current Viterbi rate */
217 cx24110_writereg(state, 0x1a, g1[fec]);
218 cx24110_writereg(state, 0x1b, g2[fec]);
219 /* not sure if this is the right way: I always used AutoAcq mode */
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return 0;
222}
223
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300224static enum fe_code_rate cx24110_get_fec(struct cx24110_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 int i;
227
228 i=cx24110_readreg(state,0x22)&0x0f;
229 if(!(i&0x08)) {
230 return FEC_1_2 + i - 1;
231 } else {
232/* fixme (low): a special code rate has been selected. In theory, we need to
233 return a denominator value, a numerator value, and a pair of puncture
234 maps to correctly describe this mode. But this should never happen in
235 practice, because it cannot be set by cx24110_get_fec. */
236 return FEC_NONE;
237 }
238}
239
240static int cx24110_set_symbolrate (struct cx24110_state* state, u32 srate)
241{
242/* fixme (low): add error handling */
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800243 u32 ratio;
244 u32 tmp, fclk, BDRI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800246 static const u32 bands[]={5000000UL,15000000UL,90999000UL/2};
247 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300249 dprintk("cx24110 debug: entering %s(%d)\n",__func__,srate);
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800250 if (srate>90999000UL/2)
251 srate=90999000UL/2;
252 if (srate<500000)
253 srate=500000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Ahmed S. Darwish0496daa72007-02-14 22:57:42 -0200255 for(i = 0; (i < ARRAY_SIZE(bands)) && (srate>bands[i]); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 ;
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800257 /* first, check which sample rate is appropriate: 45, 60 80 or 90 MHz,
258 and set the PLL accordingly (R07[1:0] Fclk, R06[7:4] PLLmult,
259 R06[3:0] PLLphaseDetGain */
260 tmp=cx24110_readreg(state,0x07)&0xfc;
261 if(srate<90999000UL/4) { /* sample rate 45MHz*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 cx24110_writereg(state,0x07,tmp);
263 cx24110_writereg(state,0x06,0x78);
264 fclk=90999000UL/2;
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800265 } else if(srate<60666000UL/2) { /* sample rate 60MHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 cx24110_writereg(state,0x07,tmp|0x1);
267 cx24110_writereg(state,0x06,0xa5);
268 fclk=60666000UL;
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800269 } else if(srate<80888000UL/2) { /* sample rate 80MHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 cx24110_writereg(state,0x07,tmp|0x2);
271 cx24110_writereg(state,0x06,0x87);
272 fclk=80888000UL;
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800273 } else { /* sample rate 90MHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 cx24110_writereg(state,0x07,tmp|0x3);
275 cx24110_writereg(state,0x06,0x78);
276 fclk=90999000UL;
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300277 }
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800278 dprintk("cx24110 debug: fclk %d Hz\n",fclk);
279 /* we need to divide two integers with approx. 27 bits in 32 bit
280 arithmetic giving a 25 bit result */
281 /* the maximum dividend is 90999000/2, 0x02b6446c, this number is
282 also the most complex divisor. Hence, the dividend has,
283 assuming 32bit unsigned arithmetic, 6 clear bits on top, the
284 divisor 2 unused bits at the bottom. Also, the quotient is
285 always less than 1/2. Borrowed from VES1893.c, of course */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800287 tmp=srate<<6;
288 BDRI=fclk>>2;
289 ratio=(tmp/BDRI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800291 tmp=(tmp%BDRI)<<8;
292 ratio=(ratio<<8)+(tmp/BDRI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800294 tmp=(tmp%BDRI)<<8;
295 ratio=(ratio<<8)+(tmp/BDRI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800297 tmp=(tmp%BDRI)<<1;
298 ratio=(ratio<<1)+(tmp/BDRI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800300 dprintk("srate= %d (range %d, up to %d)\n", srate,i,bands[i]);
301 dprintk("fclk = %d\n", fclk);
302 dprintk("ratio= %08x\n", ratio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800304 cx24110_writereg(state, 0x1, (ratio>>16)&0xff);
305 cx24110_writereg(state, 0x2, (ratio>>8)&0xff);
306 cx24110_writereg(state, 0x3, (ratio)&0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310}
311
lawrence rust2e4e98e2010-08-25 09:50:20 -0300312static int _cx24110_pll_write (struct dvb_frontend* fe, const u8 buf[], int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700314 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Andrew de Quinceyc10d14d2006-08-08 09:10:08 -0300316 if (len != 3)
317 return -EINVAL;
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/* tuner data is 21 bits long, must be left-aligned in data */
320/* tuner cx24108 is written through a dedicated 3wire interface on the demod chip */
321/* FIXME (low): add error handling, avoid infinite loops if HW fails... */
322
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800323 cx24110_writereg(state,0x6d,0x30); /* auto mode at 62kHz */
324 cx24110_writereg(state,0x70,0x15); /* auto mode 21 bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800326 /* if the auto tuner writer is still busy, clear it out */
327 while (cx24110_readreg(state,0x6d)&0x80)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 cx24110_writereg(state,0x72,0);
329
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800330 /* write the topmost 8 bits */
Andrew de Quinceyc10d14d2006-08-08 09:10:08 -0300331 cx24110_writereg(state,0x72,buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800333 /* wait for the send to be completed */
334 while ((cx24110_readreg(state,0x6d)&0xc0)==0x80)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 ;
336
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800337 /* send another 8 bytes */
Andrew de Quinceyc10d14d2006-08-08 09:10:08 -0300338 cx24110_writereg(state,0x72,buf[1]);
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800339 while ((cx24110_readreg(state,0x6d)&0xc0)==0x80)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 ;
341
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800342 /* and the topmost 5 bits of this byte */
Andrew de Quinceyc10d14d2006-08-08 09:10:08 -0300343 cx24110_writereg(state,0x72,buf[2]);
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800344 while ((cx24110_readreg(state,0x6d)&0xc0)==0x80)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 ;
346
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800347 /* now strobe the enable line once */
348 cx24110_writereg(state,0x6d,0x32);
349 cx24110_writereg(state,0x6d,0x30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
354static int cx24110_initfe(struct dvb_frontend* fe)
355{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700356 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/* fixme (low): error handling */
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800358 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300360 dprintk("%s: init chip\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Ahmed S. Darwish0496daa72007-02-14 22:57:42 -0200362 for(i = 0; i < ARRAY_SIZE(cx24110_regdata); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 cx24110_writereg(state, cx24110_regdata[i].reg, cx24110_regdata[i].data);
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Manu Abrahame7ac4642006-02-27 00:09:29 -0300366 return 0;
367}
368
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300369static int cx24110_set_voltage(struct dvb_frontend *fe,
370 enum fe_sec_voltage voltage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700372 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 switch (voltage) {
375 case SEC_VOLTAGE_13:
376 return cx24110_writereg(state,0x76,(cx24110_readreg(state,0x76)&0x3b)|0xc0);
377 case SEC_VOLTAGE_18:
378 return cx24110_writereg(state,0x76,(cx24110_readreg(state,0x76)&0x3b)|0x40);
379 default:
380 return -EINVAL;
Joe Perches2028c712013-10-08 20:29:08 -0300381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300384static int cx24110_diseqc_send_burst(struct dvb_frontend *fe,
385 enum fe_sec_mini_cmd burst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Johannes Stezenbachc589ebf2005-09-09 13:02:37 -0700387 int rv, bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct cx24110_state *state = fe->demodulator_priv;
Johannes Stezenbachc589ebf2005-09-09 13:02:37 -0700389 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (burst == SEC_MINI_A)
392 bit = 0x00;
393 else if (burst == SEC_MINI_B)
394 bit = 0x08;
395 else
396 return -EINVAL;
397
398 rv = cx24110_readreg(state, 0x77);
Adam Szalkowski296c7862005-09-09 13:02:35 -0700399 if (!(rv & 0x04))
400 cx24110_writereg(state, 0x77, rv | 0x04);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 rv = cx24110_readreg(state, 0x76);
403 cx24110_writereg(state, 0x76, ((rv & 0x90) | 0x40 | bit));
Johannes Stezenbachc589ebf2005-09-09 13:02:37 -0700404 timeout = jiffies + msecs_to_jiffies(100);
405 while (!time_after(jiffies, timeout) && !(cx24110_readreg(state, 0x76) & 0x40))
406 ; /* wait for LNB ready */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 return 0;
409}
410
411static int cx24110_send_diseqc_msg(struct dvb_frontend* fe,
412 struct dvb_diseqc_master_cmd *cmd)
413{
414 int i, rv;
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700415 struct cx24110_state *state = fe->demodulator_priv;
Johannes Stezenbachc589ebf2005-09-09 13:02:37 -0700416 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Manu Abraham1e7eb892006-02-27 00:09:25 -0300418 if (cmd->msg_len < 3 || cmd->msg_len > 6)
419 return -EINVAL; /* not implemented */
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 for (i = 0; i < cmd->msg_len; i++)
422 cx24110_writereg(state, 0x79 + i, cmd->msg[i]);
423
424 rv = cx24110_readreg(state, 0x77);
Adam Szalkowski296c7862005-09-09 13:02:35 -0700425 if (rv & 0x04) {
426 cx24110_writereg(state, 0x77, rv & ~0x04);
427 msleep(30); /* reportedly fixes switching problems */
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 rv = cx24110_readreg(state, 0x76);
431
432 cx24110_writereg(state, 0x76, ((rv & 0x90) | 0x40) | ((cmd->msg_len-3) & 3));
Johannes Stezenbachc589ebf2005-09-09 13:02:37 -0700433 timeout = jiffies + msecs_to_jiffies(100);
434 while (!time_after(jiffies, timeout) && !(cx24110_readreg(state, 0x76) & 0x40))
435 ; /* wait for LNB ready */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 return 0;
438}
439
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300440static int cx24110_read_status(struct dvb_frontend *fe,
441 enum fe_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700443 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 int sync = cx24110_readreg (state, 0x55);
446
447 *status = 0;
448
449 if (sync & 0x10)
450 *status |= FE_HAS_SIGNAL;
451
452 if (sync & 0x08)
453 *status |= FE_HAS_CARRIER;
454
455 sync = cx24110_readreg (state, 0x08);
456
457 if (sync & 0x40)
458 *status |= FE_HAS_VITERBI;
459
460 if (sync & 0x20)
461 *status |= FE_HAS_SYNC;
462
463 if ((sync & 0x60) == 0x60)
464 *status |= FE_HAS_LOCK;
465
466 return 0;
467}
468
469static int cx24110_read_ber(struct dvb_frontend* fe, u32* ber)
470{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700471 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /* fixme (maybe): value range is 16 bit. Scale? */
474 if(cx24110_readreg(state,0x24)&0x10) {
475 /* the Viterbi error counter has finished one counting window */
476 cx24110_writereg(state,0x24,0x04); /* select the ber reg */
477 state->lastber=cx24110_readreg(state,0x25)|
478 (cx24110_readreg(state,0x26)<<8);
479 cx24110_writereg(state,0x24,0x04); /* start new count window */
480 cx24110_writereg(state,0x24,0x14);
481 }
482 *ber = state->lastber;
483
484 return 0;
485}
486
487static int cx24110_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
488{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700489 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491/* no provision in hardware. Read the frontend AGC accumulator. No idea how to scale this, but I know it is 2s complement */
492 u8 signal = cx24110_readreg (state, 0x27)+128;
493 *signal_strength = (signal << 8) | signal;
494
495 return 0;
496}
497
498static int cx24110_read_snr(struct dvb_frontend* fe, u16* snr)
499{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700500 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /* no provision in hardware. Can be computed from the Es/N0 estimator, but I don't know how. */
503 if(cx24110_readreg(state,0x6a)&0x80) {
504 /* the Es/N0 error counter has finished one counting window */
505 state->lastesn0=cx24110_readreg(state,0x69)|
506 (cx24110_readreg(state,0x68)<<8);
507 cx24110_writereg(state,0x6a,0x84); /* start new count window */
508 }
509 *snr = state->lastesn0;
510
511 return 0;
512}
513
514static int cx24110_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
515{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700516 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 if(cx24110_readreg(state,0x10)&0x40) {
519 /* the RS error counter has finished one counting window */
520 cx24110_writereg(state,0x10,0x60); /* select the byer reg */
Hans Verkuil91e0cd42012-05-23 02:48:37 -0300521 (void)(cx24110_readreg(state, 0x12) |
Hans Verkuilfdf07b02012-04-20 08:04:48 -0300522 (cx24110_readreg(state, 0x13) << 8) |
Hans Verkuil91e0cd42012-05-23 02:48:37 -0300523 (cx24110_readreg(state, 0x14) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 cx24110_writereg(state,0x10,0x70); /* select the bler reg */
525 state->lastbler=cx24110_readreg(state,0x12)|
526 (cx24110_readreg(state,0x13)<<8)|
527 (cx24110_readreg(state,0x14)<<16);
528 cx24110_writereg(state,0x10,0x20); /* start new count window */
529 }
530 *ucblocks = state->lastbler;
531
532 return 0;
533}
534
Mauro Carvalho Chehab4be325c2011-12-22 16:44:14 -0300535static int cx24110_set_frontend(struct dvb_frontend *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700537 struct cx24110_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab4be325c2011-12-22 16:44:14 -0300538 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Andrew de Quincey81d8a8d2006-04-18 17:47:10 -0300539
Patrick Boettcherdea74862006-05-14 05:01:31 -0300540 if (fe->ops.tuner_ops.set_params) {
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -0300541 fe->ops.tuner_ops.set_params(fe);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300542 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey81d8a8d2006-04-18 17:47:10 -0300543 }
544
Mauro Carvalho Chehab4be325c2011-12-22 16:44:14 -0300545 cx24110_set_inversion(state, p->inversion);
546 cx24110_set_fec(state, p->fec_inner);
547 cx24110_set_symbolrate(state, p->symbol_rate);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300548 cx24110_writereg(state,0x04,0x05); /* start acquisition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 return 0;
551}
552
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300553static int cx24110_get_frontend(struct dvb_frontend *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300555 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700556 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 s32 afc; unsigned sclk;
558
559/* cannot read back tuner settings (freq). Need to have some private storage */
560
561 sclk = cx24110_readreg (state, 0x07) & 0x03;
562/* ok, real AFC (FEDR) freq. is afc/2^24*fsamp, fsamp=45/60/80/90MHz.
563 * Need 64 bit arithmetic. Is thiss possible in the kernel? */
564 if (sclk==0) sclk=90999000L/2L;
565 else if (sclk==1) sclk=60666000L;
566 else if (sclk==2) sclk=80888000L;
567 else sclk=90999000L;
568 sclk>>=8;
569 afc = sclk*(cx24110_readreg (state, 0x44)&0x1f)+
570 ((sclk*cx24110_readreg (state, 0x45))>>8)+
571 ((sclk*cx24110_readreg (state, 0x46))>>16);
572
573 p->frequency += afc;
574 p->inversion = (cx24110_readreg (state, 0x22) & 0x10) ?
575 INVERSION_ON : INVERSION_OFF;
Mauro Carvalho Chehab4be325c2011-12-22 16:44:14 -0300576 p->fec_inner = cx24110_get_fec(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 return 0;
579}
580
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300581static int cx24110_set_tone(struct dvb_frontend *fe,
582 enum fe_sec_tone_mode tone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700584 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 return cx24110_writereg(state,0x76,(cx24110_readreg(state,0x76)&~0x10)|(((tone==SEC_TONE_ON))?0x10:0));
587}
588
589static void cx24110_release(struct dvb_frontend* fe)
590{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700591 struct cx24110_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 kfree(state);
593}
594
595static struct dvb_frontend_ops cx24110_ops;
596
597struct dvb_frontend* cx24110_attach(const struct cx24110_config* config,
598 struct i2c_adapter* i2c)
599{
600 struct cx24110_state* state = NULL;
601 int ret;
602
603 /* allocate memory for the internal state */
Matthias Schwarzott084e24a2009-08-10 22:51:01 -0300604 state = kzalloc(sizeof(struct cx24110_state), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (state == NULL) goto error;
606
607 /* setup the state */
608 state->config = config;
609 state->i2c = i2c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 state->lastber = 0;
611 state->lastbler = 0;
612 state->lastesn0 = 0;
613
614 /* check if the demod is there */
615 ret = cx24110_readreg(state, 0x00);
616 if ((ret != 0x5a) && (ret != 0x69)) goto error;
617
618 /* create dvb_frontend */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300619 memcpy(&state->frontend.ops, &cx24110_ops, sizeof(struct dvb_frontend_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 state->frontend.demodulator_priv = state;
621 return &state->frontend;
622
623error:
624 kfree(state);
625 return NULL;
626}
627
628static struct dvb_frontend_ops cx24110_ops = {
Mauro Carvalho Chehab4be325c2011-12-22 16:44:14 -0300629 .delsys = { SYS_DVBS },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 .info = {
631 .name = "Conexant CX24110 DVB-S",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 .frequency_min = 950000,
633 .frequency_max = 2150000,
634 .frequency_stepsize = 1011, /* kHz for QPSK frontends */
635 .frequency_tolerance = 29500,
636 .symbol_rate_min = 1000000,
637 .symbol_rate_max = 45000000,
638 .caps = FE_CAN_INVERSION_AUTO |
639 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
640 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
641 FE_CAN_QPSK | FE_CAN_RECOVER
642 },
643
644 .release = cx24110_release,
645
646 .init = cx24110_initfe,
Andrew de Quinceyc10d14d2006-08-08 09:10:08 -0300647 .write = _cx24110_pll_write,
Mauro Carvalho Chehab4be325c2011-12-22 16:44:14 -0300648 .set_frontend = cx24110_set_frontend,
649 .get_frontend = cx24110_get_frontend,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 .read_status = cx24110_read_status,
651 .read_ber = cx24110_read_ber,
652 .read_signal_strength = cx24110_read_signal_strength,
653 .read_snr = cx24110_read_snr,
654 .read_ucblocks = cx24110_read_ucblocks,
655
656 .diseqc_send_master_cmd = cx24110_send_diseqc_msg,
657 .set_tone = cx24110_set_tone,
658 .set_voltage = cx24110_set_voltage,
659 .diseqc_send_burst = cx24110_diseqc_send_burst,
660};
661
662module_param(debug, int, 0644);
663MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
664
665MODULE_DESCRIPTION("Conexant CX24110 DVB-S Demodulator driver");
666MODULE_AUTHOR("Peter Hettkamp");
667MODULE_LICENSE("GPL");
668
669EXPORT_SYMBOL(cx24110_attach);