1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun Driver for the Spase sp887x demodulator
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun /*
7*4882a593Smuzhiyun * This driver needs external firmware. Please use the command
8*4882a593Smuzhiyun * "<kerneldir>/scripts/get_dvb_firmware sp887x" to
9*4882a593Smuzhiyun * download/extract it, and then copy it to /usr/lib/hotplug/firmware
10*4882a593Smuzhiyun * or /lib/firmware (depending on configuration of firmware hotplug).
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun #define SP887X_DEFAULT_FIRMWARE "dvb-fe-sp887x.fw"
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <linux/init.h>
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/device.h>
17*4882a593Smuzhiyun #include <linux/firmware.h>
18*4882a593Smuzhiyun #include <linux/string.h>
19*4882a593Smuzhiyun #include <linux/slab.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #include <media/dvb_frontend.h>
22*4882a593Smuzhiyun #include "sp887x.h"
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun struct sp887x_state {
26*4882a593Smuzhiyun struct i2c_adapter* i2c;
27*4882a593Smuzhiyun const struct sp887x_config* config;
28*4882a593Smuzhiyun struct dvb_frontend frontend;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /* demodulator private data */
31*4882a593Smuzhiyun u8 initialised:1;
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun static int debug;
35*4882a593Smuzhiyun #define dprintk(args...) \
36*4882a593Smuzhiyun do { \
37*4882a593Smuzhiyun if (debug) printk(KERN_DEBUG "sp887x: " args); \
38*4882a593Smuzhiyun } while (0)
39*4882a593Smuzhiyun
i2c_writebytes(struct sp887x_state * state,u8 * buf,u8 len)40*4882a593Smuzhiyun static int i2c_writebytes (struct sp887x_state* state, u8 *buf, u8 len)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = len };
43*4882a593Smuzhiyun int err;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
46*4882a593Smuzhiyun printk ("%s: i2c write error (addr %02x, err == %i)\n",
47*4882a593Smuzhiyun __func__, state->config->demod_address, err);
48*4882a593Smuzhiyun return -EREMOTEIO;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun return 0;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun
sp887x_writereg(struct sp887x_state * state,u16 reg,u16 data)54*4882a593Smuzhiyun static int sp887x_writereg (struct sp887x_state* state, u16 reg, u16 data)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun u8 b0 [] = { reg >> 8 , reg & 0xff, data >> 8, data & 0xff };
57*4882a593Smuzhiyun struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 4 };
58*4882a593Smuzhiyun int ret;
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun if ((ret = i2c_transfer(state->i2c, &msg, 1)) != 1) {
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun * in case of soft reset we ignore ACK errors...
63*4882a593Smuzhiyun */
64*4882a593Smuzhiyun if (!(reg == 0xf1a && data == 0x000 &&
65*4882a593Smuzhiyun (ret == -EREMOTEIO || ret == -EFAULT)))
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun printk("%s: writereg error (reg %03x, data %03x, ret == %i)\n",
68*4882a593Smuzhiyun __func__, reg & 0xffff, data & 0xffff, ret);
69*4882a593Smuzhiyun return ret;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun return 0;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
sp887x_readreg(struct sp887x_state * state,u16 reg)76*4882a593Smuzhiyun static int sp887x_readreg (struct sp887x_state* state, u16 reg)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun u8 b0 [] = { reg >> 8 , reg & 0xff };
79*4882a593Smuzhiyun u8 b1 [2];
80*4882a593Smuzhiyun int ret;
81*4882a593Smuzhiyun struct i2c_msg msg[] = {{ .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 2 },
82*4882a593Smuzhiyun { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 2 }};
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun if ((ret = i2c_transfer(state->i2c, msg, 2)) != 2) {
85*4882a593Smuzhiyun printk("%s: readreg error (ret == %i)\n", __func__, ret);
86*4882a593Smuzhiyun return -1;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun return (((b1[0] << 8) | b1[1]) & 0xfff);
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun
sp887x_microcontroller_stop(struct sp887x_state * state)92*4882a593Smuzhiyun static void sp887x_microcontroller_stop (struct sp887x_state* state)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun dprintk("%s\n", __func__);
95*4882a593Smuzhiyun sp887x_writereg(state, 0xf08, 0x000);
96*4882a593Smuzhiyun sp887x_writereg(state, 0xf09, 0x000);
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /* microcontroller STOP */
99*4882a593Smuzhiyun sp887x_writereg(state, 0xf00, 0x000);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
sp887x_microcontroller_start(struct sp887x_state * state)102*4882a593Smuzhiyun static void sp887x_microcontroller_start (struct sp887x_state* state)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun dprintk("%s\n", __func__);
105*4882a593Smuzhiyun sp887x_writereg(state, 0xf08, 0x000);
106*4882a593Smuzhiyun sp887x_writereg(state, 0xf09, 0x000);
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /* microcontroller START */
109*4882a593Smuzhiyun sp887x_writereg(state, 0xf00, 0x001);
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
sp887x_setup_agc(struct sp887x_state * state)112*4882a593Smuzhiyun static void sp887x_setup_agc (struct sp887x_state* state)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun /* setup AGC parameters */
115*4882a593Smuzhiyun dprintk("%s\n", __func__);
116*4882a593Smuzhiyun sp887x_writereg(state, 0x33c, 0x054);
117*4882a593Smuzhiyun sp887x_writereg(state, 0x33b, 0x04c);
118*4882a593Smuzhiyun sp887x_writereg(state, 0x328, 0x000);
119*4882a593Smuzhiyun sp887x_writereg(state, 0x327, 0x005);
120*4882a593Smuzhiyun sp887x_writereg(state, 0x326, 0x001);
121*4882a593Smuzhiyun sp887x_writereg(state, 0x325, 0x001);
122*4882a593Smuzhiyun sp887x_writereg(state, 0x324, 0x001);
123*4882a593Smuzhiyun sp887x_writereg(state, 0x318, 0x050);
124*4882a593Smuzhiyun sp887x_writereg(state, 0x317, 0x3fe);
125*4882a593Smuzhiyun sp887x_writereg(state, 0x316, 0x001);
126*4882a593Smuzhiyun sp887x_writereg(state, 0x313, 0x005);
127*4882a593Smuzhiyun sp887x_writereg(state, 0x312, 0x002);
128*4882a593Smuzhiyun sp887x_writereg(state, 0x306, 0x000);
129*4882a593Smuzhiyun sp887x_writereg(state, 0x303, 0x000);
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun #define BLOCKSIZE 30
133*4882a593Smuzhiyun #define FW_SIZE 0x4000
134*4882a593Smuzhiyun /*
135*4882a593Smuzhiyun * load firmware and setup MPEG interface...
136*4882a593Smuzhiyun */
sp887x_initial_setup(struct dvb_frontend * fe,const struct firmware * fw)137*4882a593Smuzhiyun static int sp887x_initial_setup (struct dvb_frontend* fe, const struct firmware *fw)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun struct sp887x_state* state = fe->demodulator_priv;
140*4882a593Smuzhiyun u8 buf [BLOCKSIZE + 2];
141*4882a593Smuzhiyun int i;
142*4882a593Smuzhiyun int fw_size = fw->size;
143*4882a593Smuzhiyun const unsigned char *mem = fw->data;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun dprintk("%s\n", __func__);
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun /* ignore the first 10 bytes, then we expect 0x4000 bytes of firmware */
148*4882a593Smuzhiyun if (fw_size < FW_SIZE + 10)
149*4882a593Smuzhiyun return -ENODEV;
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun mem = fw->data + 10;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /* soft reset */
154*4882a593Smuzhiyun sp887x_writereg(state, 0xf1a, 0x000);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun sp887x_microcontroller_stop (state);
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun printk ("%s: firmware upload... ", __func__);
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun /* setup write pointer to -1 (end of memory) */
161*4882a593Smuzhiyun /* bit 0x8000 in address is set to enable 13bit mode */
162*4882a593Smuzhiyun sp887x_writereg(state, 0x8f08, 0x1fff);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /* dummy write (wrap around to start of memory) */
165*4882a593Smuzhiyun sp887x_writereg(state, 0x8f0a, 0x0000);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun for (i = 0; i < FW_SIZE; i += BLOCKSIZE) {
168*4882a593Smuzhiyun int c = BLOCKSIZE;
169*4882a593Smuzhiyun int err;
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun if (c > FW_SIZE - i)
172*4882a593Smuzhiyun c = FW_SIZE - i;
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /* bit 0x8000 in address is set to enable 13bit mode */
175*4882a593Smuzhiyun /* bit 0x4000 enables multibyte read/write transfers */
176*4882a593Smuzhiyun /* write register is 0xf0a */
177*4882a593Smuzhiyun buf[0] = 0xcf;
178*4882a593Smuzhiyun buf[1] = 0x0a;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun memcpy(&buf[2], mem + i, c);
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun if ((err = i2c_writebytes (state, buf, c+2)) < 0) {
183*4882a593Smuzhiyun printk ("failed.\n");
184*4882a593Smuzhiyun printk ("%s: i2c error (err == %i)\n", __func__, err);
185*4882a593Smuzhiyun return err;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun /* don't write RS bytes between packets */
190*4882a593Smuzhiyun sp887x_writereg(state, 0xc13, 0x001);
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun /* suppress clock if (!data_valid) */
193*4882a593Smuzhiyun sp887x_writereg(state, 0xc14, 0x000);
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun /* setup MPEG interface... */
196*4882a593Smuzhiyun sp887x_writereg(state, 0xc1a, 0x872);
197*4882a593Smuzhiyun sp887x_writereg(state, 0xc1b, 0x001);
198*4882a593Smuzhiyun sp887x_writereg(state, 0xc1c, 0x000); /* parallel mode (serial mode == 1) */
199*4882a593Smuzhiyun sp887x_writereg(state, 0xc1a, 0x871);
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun /* ADC mode, 2 for MT8872, 3 for SP8870/SP8871 */
202*4882a593Smuzhiyun sp887x_writereg(state, 0x301, 0x002);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun sp887x_setup_agc(state);
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /* bit 0x010: enable data valid signal */
207*4882a593Smuzhiyun sp887x_writereg(state, 0xd00, 0x010);
208*4882a593Smuzhiyun sp887x_writereg(state, 0x0d1, 0x000);
209*4882a593Smuzhiyun return 0;
210*4882a593Smuzhiyun };
211*4882a593Smuzhiyun
configure_reg0xc05(struct dtv_frontend_properties * p,u16 * reg0xc05)212*4882a593Smuzhiyun static int configure_reg0xc05(struct dtv_frontend_properties *p, u16 *reg0xc05)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun int known_parameters = 1;
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun *reg0xc05 = 0x000;
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun switch (p->modulation) {
219*4882a593Smuzhiyun case QPSK:
220*4882a593Smuzhiyun break;
221*4882a593Smuzhiyun case QAM_16:
222*4882a593Smuzhiyun *reg0xc05 |= (1 << 10);
223*4882a593Smuzhiyun break;
224*4882a593Smuzhiyun case QAM_64:
225*4882a593Smuzhiyun *reg0xc05 |= (2 << 10);
226*4882a593Smuzhiyun break;
227*4882a593Smuzhiyun case QAM_AUTO:
228*4882a593Smuzhiyun known_parameters = 0;
229*4882a593Smuzhiyun break;
230*4882a593Smuzhiyun default:
231*4882a593Smuzhiyun return -EINVAL;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun switch (p->hierarchy) {
235*4882a593Smuzhiyun case HIERARCHY_NONE:
236*4882a593Smuzhiyun break;
237*4882a593Smuzhiyun case HIERARCHY_1:
238*4882a593Smuzhiyun *reg0xc05 |= (1 << 7);
239*4882a593Smuzhiyun break;
240*4882a593Smuzhiyun case HIERARCHY_2:
241*4882a593Smuzhiyun *reg0xc05 |= (2 << 7);
242*4882a593Smuzhiyun break;
243*4882a593Smuzhiyun case HIERARCHY_4:
244*4882a593Smuzhiyun *reg0xc05 |= (3 << 7);
245*4882a593Smuzhiyun break;
246*4882a593Smuzhiyun case HIERARCHY_AUTO:
247*4882a593Smuzhiyun known_parameters = 0;
248*4882a593Smuzhiyun break;
249*4882a593Smuzhiyun default:
250*4882a593Smuzhiyun return -EINVAL;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun switch (p->code_rate_HP) {
254*4882a593Smuzhiyun case FEC_1_2:
255*4882a593Smuzhiyun break;
256*4882a593Smuzhiyun case FEC_2_3:
257*4882a593Smuzhiyun *reg0xc05 |= (1 << 3);
258*4882a593Smuzhiyun break;
259*4882a593Smuzhiyun case FEC_3_4:
260*4882a593Smuzhiyun *reg0xc05 |= (2 << 3);
261*4882a593Smuzhiyun break;
262*4882a593Smuzhiyun case FEC_5_6:
263*4882a593Smuzhiyun *reg0xc05 |= (3 << 3);
264*4882a593Smuzhiyun break;
265*4882a593Smuzhiyun case FEC_7_8:
266*4882a593Smuzhiyun *reg0xc05 |= (4 << 3);
267*4882a593Smuzhiyun break;
268*4882a593Smuzhiyun case FEC_AUTO:
269*4882a593Smuzhiyun known_parameters = 0;
270*4882a593Smuzhiyun break;
271*4882a593Smuzhiyun default:
272*4882a593Smuzhiyun return -EINVAL;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun if (known_parameters)
276*4882a593Smuzhiyun *reg0xc05 |= (2 << 1); /* use specified parameters */
277*4882a593Smuzhiyun else
278*4882a593Smuzhiyun *reg0xc05 |= (1 << 1); /* enable autoprobing */
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun return 0;
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun /*
284*4882a593Smuzhiyun * estimates division of two 24bit numbers,
285*4882a593Smuzhiyun * derived from the ves1820/stv0299 driver code
286*4882a593Smuzhiyun */
divide(int n,int d,int * quotient_i,int * quotient_f)287*4882a593Smuzhiyun static void divide (int n, int d, int *quotient_i, int *quotient_f)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun unsigned int q, r;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun r = (n % d) << 8;
292*4882a593Smuzhiyun q = (r / d);
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun if (quotient_i)
295*4882a593Smuzhiyun *quotient_i = q;
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun if (quotient_f) {
298*4882a593Smuzhiyun r = (r % d) << 8;
299*4882a593Smuzhiyun q = (q << 8) | (r / d);
300*4882a593Smuzhiyun r = (r % d) << 8;
301*4882a593Smuzhiyun *quotient_f = (q << 8) | (r / d);
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun
sp887x_correct_offsets(struct sp887x_state * state,struct dtv_frontend_properties * p,int actual_freq)305*4882a593Smuzhiyun static void sp887x_correct_offsets (struct sp887x_state* state,
306*4882a593Smuzhiyun struct dtv_frontend_properties *p,
307*4882a593Smuzhiyun int actual_freq)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun static const u32 srate_correction [] = { 1879617, 4544878, 8098561 };
310*4882a593Smuzhiyun int bw_index;
311*4882a593Smuzhiyun int freq_offset = actual_freq - p->frequency;
312*4882a593Smuzhiyun int sysclock = 61003; //[kHz]
313*4882a593Smuzhiyun int ifreq = 36000000;
314*4882a593Smuzhiyun int freq;
315*4882a593Smuzhiyun int frequency_shift;
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun switch (p->bandwidth_hz) {
318*4882a593Smuzhiyun default:
319*4882a593Smuzhiyun case 8000000:
320*4882a593Smuzhiyun bw_index = 0;
321*4882a593Smuzhiyun break;
322*4882a593Smuzhiyun case 7000000:
323*4882a593Smuzhiyun bw_index = 1;
324*4882a593Smuzhiyun break;
325*4882a593Smuzhiyun case 6000000:
326*4882a593Smuzhiyun bw_index = 2;
327*4882a593Smuzhiyun break;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun if (p->inversion == INVERSION_ON)
331*4882a593Smuzhiyun freq = ifreq - freq_offset;
332*4882a593Smuzhiyun else
333*4882a593Smuzhiyun freq = ifreq + freq_offset;
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun divide(freq / 333, sysclock, NULL, &frequency_shift);
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun if (p->inversion == INVERSION_ON)
338*4882a593Smuzhiyun frequency_shift = -frequency_shift;
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun /* sample rate correction */
341*4882a593Smuzhiyun sp887x_writereg(state, 0x319, srate_correction[bw_index] >> 12);
342*4882a593Smuzhiyun sp887x_writereg(state, 0x31a, srate_correction[bw_index] & 0xfff);
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun /* carrier offset correction */
345*4882a593Smuzhiyun sp887x_writereg(state, 0x309, frequency_shift >> 12);
346*4882a593Smuzhiyun sp887x_writereg(state, 0x30a, frequency_shift & 0xfff);
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun
sp887x_setup_frontend_parameters(struct dvb_frontend * fe)349*4882a593Smuzhiyun static int sp887x_setup_frontend_parameters(struct dvb_frontend *fe)
350*4882a593Smuzhiyun {
351*4882a593Smuzhiyun struct dtv_frontend_properties *p = &fe->dtv_property_cache;
352*4882a593Smuzhiyun struct sp887x_state* state = fe->demodulator_priv;
353*4882a593Smuzhiyun unsigned actual_freq;
354*4882a593Smuzhiyun int err;
355*4882a593Smuzhiyun u16 val, reg0xc05;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun if (p->bandwidth_hz != 8000000 &&
358*4882a593Smuzhiyun p->bandwidth_hz != 7000000 &&
359*4882a593Smuzhiyun p->bandwidth_hz != 6000000)
360*4882a593Smuzhiyun return -EINVAL;
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun if ((err = configure_reg0xc05(p, ®0xc05)))
363*4882a593Smuzhiyun return err;
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun sp887x_microcontroller_stop(state);
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun /* setup the PLL */
368*4882a593Smuzhiyun if (fe->ops.tuner_ops.set_params) {
369*4882a593Smuzhiyun fe->ops.tuner_ops.set_params(fe);
370*4882a593Smuzhiyun if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun if (fe->ops.tuner_ops.get_frequency) {
373*4882a593Smuzhiyun fe->ops.tuner_ops.get_frequency(fe, &actual_freq);
374*4882a593Smuzhiyun if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
375*4882a593Smuzhiyun } else {
376*4882a593Smuzhiyun actual_freq = p->frequency;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun /* read status reg in order to clear <pending irqs */
380*4882a593Smuzhiyun sp887x_readreg(state, 0x200);
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun sp887x_correct_offsets(state, p, actual_freq);
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun /* filter for 6/7/8 Mhz channel */
385*4882a593Smuzhiyun if (p->bandwidth_hz == 6000000)
386*4882a593Smuzhiyun val = 2;
387*4882a593Smuzhiyun else if (p->bandwidth_hz == 7000000)
388*4882a593Smuzhiyun val = 1;
389*4882a593Smuzhiyun else
390*4882a593Smuzhiyun val = 0;
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun sp887x_writereg(state, 0x311, val);
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun /* scan order: 2k first = 0, 8k first = 1 */
395*4882a593Smuzhiyun if (p->transmission_mode == TRANSMISSION_MODE_2K)
396*4882a593Smuzhiyun sp887x_writereg(state, 0x338, 0x000);
397*4882a593Smuzhiyun else
398*4882a593Smuzhiyun sp887x_writereg(state, 0x338, 0x001);
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun sp887x_writereg(state, 0xc05, reg0xc05);
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun if (p->bandwidth_hz == 6000000)
403*4882a593Smuzhiyun val = 2 << 3;
404*4882a593Smuzhiyun else if (p->bandwidth_hz == 7000000)
405*4882a593Smuzhiyun val = 3 << 3;
406*4882a593Smuzhiyun else
407*4882a593Smuzhiyun val = 0 << 3;
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun /* enable OFDM and SAW bits as lock indicators in sync register 0xf17,
410*4882a593Smuzhiyun * optimize algorithm for given bandwidth...
411*4882a593Smuzhiyun */
412*4882a593Smuzhiyun sp887x_writereg(state, 0xf14, 0x160 | val);
413*4882a593Smuzhiyun sp887x_writereg(state, 0xf15, 0x000);
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun sp887x_microcontroller_start(state);
416*4882a593Smuzhiyun return 0;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
sp887x_read_status(struct dvb_frontend * fe,enum fe_status * status)419*4882a593Smuzhiyun static int sp887x_read_status(struct dvb_frontend *fe, enum fe_status *status)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun struct sp887x_state* state = fe->demodulator_priv;
422*4882a593Smuzhiyun u16 snr12 = sp887x_readreg(state, 0xf16);
423*4882a593Smuzhiyun u16 sync0x200 = sp887x_readreg(state, 0x200);
424*4882a593Smuzhiyun u16 sync0xf17 = sp887x_readreg(state, 0xf17);
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun *status = 0;
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun if (snr12 > 0x00f)
429*4882a593Smuzhiyun *status |= FE_HAS_SIGNAL;
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun //if (sync0x200 & 0x004)
432*4882a593Smuzhiyun // *status |= FE_HAS_SYNC | FE_HAS_CARRIER;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun //if (sync0x200 & 0x008)
435*4882a593Smuzhiyun // *status |= FE_HAS_VITERBI;
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun if ((sync0xf17 & 0x00f) == 0x002) {
438*4882a593Smuzhiyun *status |= FE_HAS_LOCK;
439*4882a593Smuzhiyun *status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_CARRIER;
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun if (sync0x200 & 0x001) { /* tuner adjustment requested...*/
443*4882a593Smuzhiyun int steps = (sync0x200 >> 4) & 0x00f;
444*4882a593Smuzhiyun if (steps & 0x008)
445*4882a593Smuzhiyun steps = -steps;
446*4882a593Smuzhiyun dprintk("sp887x: implement tuner adjustment (%+i steps)!!\n",
447*4882a593Smuzhiyun steps);
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun return 0;
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun
sp887x_read_ber(struct dvb_frontend * fe,u32 * ber)453*4882a593Smuzhiyun static int sp887x_read_ber(struct dvb_frontend* fe, u32* ber)
454*4882a593Smuzhiyun {
455*4882a593Smuzhiyun struct sp887x_state* state = fe->demodulator_priv;
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun *ber = (sp887x_readreg(state, 0xc08) & 0x3f) |
458*4882a593Smuzhiyun (sp887x_readreg(state, 0xc07) << 6);
459*4882a593Smuzhiyun sp887x_writereg(state, 0xc08, 0x000);
460*4882a593Smuzhiyun sp887x_writereg(state, 0xc07, 0x000);
461*4882a593Smuzhiyun if (*ber >= 0x3fff0)
462*4882a593Smuzhiyun *ber = ~0;
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun return 0;
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun
sp887x_read_signal_strength(struct dvb_frontend * fe,u16 * strength)467*4882a593Smuzhiyun static int sp887x_read_signal_strength(struct dvb_frontend* fe, u16* strength)
468*4882a593Smuzhiyun {
469*4882a593Smuzhiyun struct sp887x_state* state = fe->demodulator_priv;
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun u16 snr12 = sp887x_readreg(state, 0xf16);
472*4882a593Smuzhiyun u32 signal = 3 * (snr12 << 4);
473*4882a593Smuzhiyun *strength = (signal < 0xffff) ? signal : 0xffff;
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun return 0;
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun
sp887x_read_snr(struct dvb_frontend * fe,u16 * snr)478*4882a593Smuzhiyun static int sp887x_read_snr(struct dvb_frontend* fe, u16* snr)
479*4882a593Smuzhiyun {
480*4882a593Smuzhiyun struct sp887x_state* state = fe->demodulator_priv;
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun u16 snr12 = sp887x_readreg(state, 0xf16);
483*4882a593Smuzhiyun *snr = (snr12 << 4) | (snr12 >> 8);
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun return 0;
486*4882a593Smuzhiyun }
487*4882a593Smuzhiyun
sp887x_read_ucblocks(struct dvb_frontend * fe,u32 * ucblocks)488*4882a593Smuzhiyun static int sp887x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
489*4882a593Smuzhiyun {
490*4882a593Smuzhiyun struct sp887x_state* state = fe->demodulator_priv;
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun *ucblocks = sp887x_readreg(state, 0xc0c);
493*4882a593Smuzhiyun if (*ucblocks == 0xfff)
494*4882a593Smuzhiyun *ucblocks = ~0;
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun return 0;
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun
sp887x_i2c_gate_ctrl(struct dvb_frontend * fe,int enable)499*4882a593Smuzhiyun static int sp887x_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
500*4882a593Smuzhiyun {
501*4882a593Smuzhiyun struct sp887x_state* state = fe->demodulator_priv;
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun if (enable) {
504*4882a593Smuzhiyun return sp887x_writereg(state, 0x206, 0x001);
505*4882a593Smuzhiyun } else {
506*4882a593Smuzhiyun return sp887x_writereg(state, 0x206, 0x000);
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun
sp887x_sleep(struct dvb_frontend * fe)510*4882a593Smuzhiyun static int sp887x_sleep(struct dvb_frontend* fe)
511*4882a593Smuzhiyun {
512*4882a593Smuzhiyun struct sp887x_state* state = fe->demodulator_priv;
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun /* tristate TS output and disable interface pins */
515*4882a593Smuzhiyun sp887x_writereg(state, 0xc18, 0x000);
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun return 0;
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun
sp887x_init(struct dvb_frontend * fe)520*4882a593Smuzhiyun static int sp887x_init(struct dvb_frontend* fe)
521*4882a593Smuzhiyun {
522*4882a593Smuzhiyun struct sp887x_state* state = fe->demodulator_priv;
523*4882a593Smuzhiyun const struct firmware *fw = NULL;
524*4882a593Smuzhiyun int ret;
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun if (!state->initialised) {
527*4882a593Smuzhiyun /* request the firmware, this will block until someone uploads it */
528*4882a593Smuzhiyun printk("sp887x: waiting for firmware upload (%s)...\n", SP887X_DEFAULT_FIRMWARE);
529*4882a593Smuzhiyun ret = state->config->request_firmware(fe, &fw, SP887X_DEFAULT_FIRMWARE);
530*4882a593Smuzhiyun if (ret) {
531*4882a593Smuzhiyun printk("sp887x: no firmware upload (timeout or file not found?)\n");
532*4882a593Smuzhiyun return ret;
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun ret = sp887x_initial_setup(fe, fw);
536*4882a593Smuzhiyun release_firmware(fw);
537*4882a593Smuzhiyun if (ret) {
538*4882a593Smuzhiyun printk("sp887x: writing firmware to device failed\n");
539*4882a593Smuzhiyun return ret;
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun printk("sp887x: firmware upload complete\n");
542*4882a593Smuzhiyun state->initialised = 1;
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun /* enable TS output and interface pins */
546*4882a593Smuzhiyun sp887x_writereg(state, 0xc18, 0x00d);
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun return 0;
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun
sp887x_get_tune_settings(struct dvb_frontend * fe,struct dvb_frontend_tune_settings * fesettings)551*4882a593Smuzhiyun static int sp887x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
552*4882a593Smuzhiyun {
553*4882a593Smuzhiyun fesettings->min_delay_ms = 350;
554*4882a593Smuzhiyun fesettings->step_size = 166666*2;
555*4882a593Smuzhiyun fesettings->max_drift = (166666*2)+1;
556*4882a593Smuzhiyun return 0;
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun
sp887x_release(struct dvb_frontend * fe)559*4882a593Smuzhiyun static void sp887x_release(struct dvb_frontend* fe)
560*4882a593Smuzhiyun {
561*4882a593Smuzhiyun struct sp887x_state* state = fe->demodulator_priv;
562*4882a593Smuzhiyun kfree(state);
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun static const struct dvb_frontend_ops sp887x_ops;
566*4882a593Smuzhiyun
sp887x_attach(const struct sp887x_config * config,struct i2c_adapter * i2c)567*4882a593Smuzhiyun struct dvb_frontend* sp887x_attach(const struct sp887x_config* config,
568*4882a593Smuzhiyun struct i2c_adapter* i2c)
569*4882a593Smuzhiyun {
570*4882a593Smuzhiyun struct sp887x_state* state = NULL;
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun /* allocate memory for the internal state */
573*4882a593Smuzhiyun state = kzalloc(sizeof(struct sp887x_state), GFP_KERNEL);
574*4882a593Smuzhiyun if (state == NULL) goto error;
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun /* setup the state */
577*4882a593Smuzhiyun state->config = config;
578*4882a593Smuzhiyun state->i2c = i2c;
579*4882a593Smuzhiyun state->initialised = 0;
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun /* check if the demod is there */
582*4882a593Smuzhiyun if (sp887x_readreg(state, 0x0200) < 0) goto error;
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun /* create dvb_frontend */
585*4882a593Smuzhiyun memcpy(&state->frontend.ops, &sp887x_ops, sizeof(struct dvb_frontend_ops));
586*4882a593Smuzhiyun state->frontend.demodulator_priv = state;
587*4882a593Smuzhiyun return &state->frontend;
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun error:
590*4882a593Smuzhiyun kfree(state);
591*4882a593Smuzhiyun return NULL;
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun static const struct dvb_frontend_ops sp887x_ops = {
595*4882a593Smuzhiyun .delsys = { SYS_DVBT },
596*4882a593Smuzhiyun .info = {
597*4882a593Smuzhiyun .name = "Spase SP887x DVB-T",
598*4882a593Smuzhiyun .frequency_min_hz = 50500 * kHz,
599*4882a593Smuzhiyun .frequency_max_hz = 858000 * kHz,
600*4882a593Smuzhiyun .frequency_stepsize_hz = 166666,
601*4882a593Smuzhiyun .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
602*4882a593Smuzhiyun FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
603*4882a593Smuzhiyun FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
604*4882a593Smuzhiyun FE_CAN_RECOVER
605*4882a593Smuzhiyun },
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun .release = sp887x_release,
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun .init = sp887x_init,
610*4882a593Smuzhiyun .sleep = sp887x_sleep,
611*4882a593Smuzhiyun .i2c_gate_ctrl = sp887x_i2c_gate_ctrl,
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun .set_frontend = sp887x_setup_frontend_parameters,
614*4882a593Smuzhiyun .get_tune_settings = sp887x_get_tune_settings,
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun .read_status = sp887x_read_status,
617*4882a593Smuzhiyun .read_ber = sp887x_read_ber,
618*4882a593Smuzhiyun .read_signal_strength = sp887x_read_signal_strength,
619*4882a593Smuzhiyun .read_snr = sp887x_read_snr,
620*4882a593Smuzhiyun .read_ucblocks = sp887x_read_ucblocks,
621*4882a593Smuzhiyun };
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun module_param(debug, int, 0644);
624*4882a593Smuzhiyun MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun MODULE_DESCRIPTION("Spase sp887x DVB-T demodulator driver");
627*4882a593Smuzhiyun MODULE_LICENSE("GPL");
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun EXPORT_SYMBOL(sp887x_attach);
630