blob: ee2fec87c2e643fbad942d6e448053d2f9f100e9 [file] [log] [blame]
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -03001/*
2 Driver for M88RS2000 demodulator and tuner
3
4 Copyright (C) 2012 Malcolm Priestley (tvboxspy@gmail.com)
5 Beta Driver
6
7 Include various calculation code from DS3000 driver.
8 Copyright (C) 2009 Konstantin Dimitrov.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
24*/
25#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/device.h>
28#include <linux/jiffies.h>
29#include <linux/string.h>
30#include <linux/slab.h>
31#include <linux/types.h>
32
33
34#include "dvb_frontend.h"
35#include "m88rs2000.h"
36
37struct m88rs2000_state {
38 struct i2c_adapter *i2c;
39 const struct m88rs2000_config *config;
40 struct dvb_frontend frontend;
41 u8 no_lock_count;
42 u32 tuner_frequency;
43 u32 symbol_rate;
44 fe_code_rate_t fec_inner;
45 u8 tuner_level;
46 int errmode;
47};
48
49static int m88rs2000_debug;
50
51module_param_named(debug, m88rs2000_debug, int, 0644);
52MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able)).");
53
54#define dprintk(level, args...) do { \
55 if (level & m88rs2000_debug) \
56 printk(KERN_DEBUG "m88rs2000-fe: " args); \
57} while (0)
58
59#define deb_info(args...) dprintk(0x01, args)
60#define info(format, arg...) \
61 printk(KERN_INFO "m88rs2000-fe: " format "\n" , ## arg)
62
Igor M. Liplianinb858c332012-12-28 19:40:33 -030063static int m88rs2000_writereg(struct m88rs2000_state *state,
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -030064 u8 reg, u8 data)
65{
66 int ret;
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -030067 u8 buf[] = { reg, data };
68 struct i2c_msg msg = {
Igor M. Liplianinb858c332012-12-28 19:40:33 -030069 .addr = state->config->demod_addr,
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -030070 .flags = 0,
71 .buf = buf,
72 .len = 2
73 };
74
75 ret = i2c_transfer(state->i2c, &msg, 1);
76
77 if (ret != 1)
78 deb_info("%s: writereg error (reg == 0x%02x, val == 0x%02x, "
79 "ret == %i)\n", __func__, reg, data, ret);
80
81 return (ret != 1) ? -EREMOTEIO : 0;
82}
83
Igor M. Liplianinb858c332012-12-28 19:40:33 -030084static u8 m88rs2000_readreg(struct m88rs2000_state *state, u8 reg)
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -030085{
86 int ret;
87 u8 b0[] = { reg };
88 u8 b1[] = { 0 };
Igor M. Liplianinb858c332012-12-28 19:40:33 -030089
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -030090 struct i2c_msg msg[] = {
91 {
Igor M. Liplianinb858c332012-12-28 19:40:33 -030092 .addr = state->config->demod_addr,
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -030093 .flags = 0,
94 .buf = b0,
95 .len = 1
96 }, {
Igor M. Liplianinb858c332012-12-28 19:40:33 -030097 .addr = state->config->demod_addr,
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -030098 .flags = I2C_M_RD,
99 .buf = b1,
100 .len = 1
101 }
102 };
103
104 ret = i2c_transfer(state->i2c, msg, 2);
105
106 if (ret != 2)
107 deb_info("%s: readreg error (reg == 0x%02x, ret == %i)\n",
108 __func__, reg, ret);
109
110 return b1[0];
111}
112
Malcolm Priestley06af15d2013-12-24 13:17:12 -0300113static u32 m88rs2000_get_mclk(struct dvb_frontend *fe)
114{
115 struct m88rs2000_state *state = fe->demodulator_priv;
116 u32 mclk;
117 u8 reg;
118 /* Must not be 0x00 or 0xff */
119 reg = m88rs2000_readreg(state, 0x86);
120 if (!reg || reg == 0xff)
121 return 0;
122
123 reg /= 2;
124 reg += 1;
125
126 mclk = (u32)(reg * RS2000_FE_CRYSTAL_KHZ + 28 / 2) / 28;
127
128 return mclk;
129}
130
131static int m88rs2000_set_carrieroffset(struct dvb_frontend *fe, s16 offset)
132{
133 struct m88rs2000_state *state = fe->demodulator_priv;
134 u32 mclk;
135 s32 tmp;
136 u8 reg;
137 int ret;
138
139 mclk = m88rs2000_get_mclk(fe);
140 if (!mclk)
141 return -EINVAL;
142
143 tmp = (offset * 4096 + (s32)mclk / 2) / (s32)mclk;
144 if (tmp < 0)
145 tmp += 4096;
146
147 /* Carrier Offset */
148 ret = m88rs2000_writereg(state, 0x9c, (u8)(tmp >> 4));
149
150 reg = m88rs2000_readreg(state, 0x9d);
151 reg &= 0xf;
152 reg |= (u8)(tmp & 0xf) << 4;
153
154 ret |= m88rs2000_writereg(state, 0x9d, reg);
155
156 return ret;
157}
158
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300159static int m88rs2000_set_symbolrate(struct dvb_frontend *fe, u32 srate)
160{
161 struct m88rs2000_state *state = fe->demodulator_priv;
162 int ret;
Malcolm Priestleydd4491d2013-12-24 13:18:46 -0300163 u64 temp;
164 u32 mclk;
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300165 u8 b[3];
166
167 if ((srate < 1000000) || (srate > 45000000))
168 return -EINVAL;
169
Malcolm Priestleydd4491d2013-12-24 13:18:46 -0300170 mclk = m88rs2000_get_mclk(fe);
171 if (!mclk)
172 return -EINVAL;
173
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300174 temp = srate / 1000;
Malcolm Priestleydd4491d2013-12-24 13:18:46 -0300175 temp *= 1 << 24;
176
177 do_div(temp, mclk);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300178
179 b[0] = (u8) (temp >> 16) & 0xff;
180 b[1] = (u8) (temp >> 8) & 0xff;
181 b[2] = (u8) temp & 0xff;
Malcolm Priestleydd4491d2013-12-24 13:18:46 -0300182
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300183 ret = m88rs2000_writereg(state, 0x93, b[2]);
184 ret |= m88rs2000_writereg(state, 0x94, b[1]);
185 ret |= m88rs2000_writereg(state, 0x95, b[0]);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300186
Malcolm Priestleydd4491d2013-12-24 13:18:46 -0300187 if (srate > 10000000)
188 ret |= m88rs2000_writereg(state, 0xa0, 0x20);
189 else
190 ret |= m88rs2000_writereg(state, 0xa0, 0x60);
191
192 ret |= m88rs2000_writereg(state, 0xa1, 0xe0);
193
194 if (srate > 12000000)
195 ret |= m88rs2000_writereg(state, 0xa3, 0x20);
196 else if (srate > 2800000)
197 ret |= m88rs2000_writereg(state, 0xa3, 0x98);
198 else
199 ret |= m88rs2000_writereg(state, 0xa3, 0x90);
200
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300201 deb_info("m88rs2000: m88rs2000_set_symbolrate\n");
202 return ret;
203}
204
205static int m88rs2000_send_diseqc_msg(struct dvb_frontend *fe,
206 struct dvb_diseqc_master_cmd *m)
207{
208 struct m88rs2000_state *state = fe->demodulator_priv;
209
210 int i;
211 u8 reg;
212 deb_info("%s\n", __func__);
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300213 m88rs2000_writereg(state, 0x9a, 0x30);
214 reg = m88rs2000_readreg(state, 0xb2);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300215 reg &= 0x3f;
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300216 m88rs2000_writereg(state, 0xb2, reg);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300217 for (i = 0; i < m->msg_len; i++)
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300218 m88rs2000_writereg(state, 0xb3 + i, m->msg[i]);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300219
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300220 reg = m88rs2000_readreg(state, 0xb1);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300221 reg &= 0x87;
222 reg |= ((m->msg_len - 1) << 3) | 0x07;
223 reg &= 0x7f;
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300224 m88rs2000_writereg(state, 0xb1, reg);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300225
226 for (i = 0; i < 15; i++) {
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300227 if ((m88rs2000_readreg(state, 0xb1) & 0x40) == 0x0)
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300228 break;
229 msleep(20);
230 }
231
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300232 reg = m88rs2000_readreg(state, 0xb1);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300233 if ((reg & 0x40) > 0x0) {
234 reg &= 0x7f;
235 reg |= 0x40;
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300236 m88rs2000_writereg(state, 0xb1, reg);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300237 }
238
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300239 reg = m88rs2000_readreg(state, 0xb2);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300240 reg &= 0x3f;
241 reg |= 0x80;
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300242 m88rs2000_writereg(state, 0xb2, reg);
243 m88rs2000_writereg(state, 0x9a, 0xb0);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300244
245
246 return 0;
247}
248
249static int m88rs2000_send_diseqc_burst(struct dvb_frontend *fe,
250 fe_sec_mini_cmd_t burst)
251{
252 struct m88rs2000_state *state = fe->demodulator_priv;
253 u8 reg0, reg1;
254 deb_info("%s\n", __func__);
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300255 m88rs2000_writereg(state, 0x9a, 0x30);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300256 msleep(50);
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300257 reg0 = m88rs2000_readreg(state, 0xb1);
258 reg1 = m88rs2000_readreg(state, 0xb2);
Malcolm Priestley593a2ce02012-03-14 16:31:26 -0300259 /* TODO complete this section */
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300260 m88rs2000_writereg(state, 0xb2, reg1);
261 m88rs2000_writereg(state, 0xb1, reg0);
262 m88rs2000_writereg(state, 0x9a, 0xb0);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300263
264 return 0;
265}
266
267static int m88rs2000_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
268{
269 struct m88rs2000_state *state = fe->demodulator_priv;
270 u8 reg0, reg1;
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300271 m88rs2000_writereg(state, 0x9a, 0x30);
272 reg0 = m88rs2000_readreg(state, 0xb1);
273 reg1 = m88rs2000_readreg(state, 0xb2);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300274
275 reg1 &= 0x3f;
276
277 switch (tone) {
278 case SEC_TONE_ON:
279 reg0 |= 0x4;
280 reg0 &= 0xbc;
Malcolm Priestley593a2ce02012-03-14 16:31:26 -0300281 break;
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300282 case SEC_TONE_OFF:
283 reg1 |= 0x80;
Malcolm Priestley593a2ce02012-03-14 16:31:26 -0300284 break;
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300285 default:
Malcolm Priestley593a2ce02012-03-14 16:31:26 -0300286 break;
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300287 }
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300288 m88rs2000_writereg(state, 0xb2, reg1);
289 m88rs2000_writereg(state, 0xb1, reg0);
290 m88rs2000_writereg(state, 0x9a, 0xb0);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300291 return 0;
292}
293
294struct inittab {
295 u8 cmd;
296 u8 reg;
297 u8 val;
298};
299
300struct inittab m88rs2000_setup[] = {
301 {DEMOD_WRITE, 0x9a, 0x30},
302 {DEMOD_WRITE, 0x00, 0x01},
303 {WRITE_DELAY, 0x19, 0x00},
304 {DEMOD_WRITE, 0x00, 0x00},
305 {DEMOD_WRITE, 0x9a, 0xb0},
306 {DEMOD_WRITE, 0x81, 0xc1},
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300307 {DEMOD_WRITE, 0x81, 0x81},
308 {DEMOD_WRITE, 0x86, 0xc6},
309 {DEMOD_WRITE, 0x9a, 0x30},
310 {DEMOD_WRITE, 0xf0, 0x22},
311 {DEMOD_WRITE, 0xf1, 0xbf},
312 {DEMOD_WRITE, 0xb0, 0x45},
Malcolm Priestley593a2ce02012-03-14 16:31:26 -0300313 {DEMOD_WRITE, 0xb2, 0x01}, /* set voltage pin always set 1*/
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300314 {DEMOD_WRITE, 0x9a, 0xb0},
315 {0xff, 0xaa, 0xff}
316};
317
318struct inittab m88rs2000_shutdown[] = {
319 {DEMOD_WRITE, 0x9a, 0x30},
320 {DEMOD_WRITE, 0xb0, 0x00},
321 {DEMOD_WRITE, 0xf1, 0x89},
322 {DEMOD_WRITE, 0x00, 0x01},
323 {DEMOD_WRITE, 0x9a, 0xb0},
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300324 {DEMOD_WRITE, 0x81, 0x81},
325 {0xff, 0xaa, 0xff}
326};
327
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300328struct inittab fe_reset[] = {
329 {DEMOD_WRITE, 0x00, 0x01},
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300330 {DEMOD_WRITE, 0x20, 0x81},
331 {DEMOD_WRITE, 0x21, 0x80},
332 {DEMOD_WRITE, 0x10, 0x33},
333 {DEMOD_WRITE, 0x11, 0x44},
334 {DEMOD_WRITE, 0x12, 0x07},
335 {DEMOD_WRITE, 0x18, 0x20},
336 {DEMOD_WRITE, 0x28, 0x04},
337 {DEMOD_WRITE, 0x29, 0x8e},
338 {DEMOD_WRITE, 0x3b, 0xff},
339 {DEMOD_WRITE, 0x32, 0x10},
340 {DEMOD_WRITE, 0x33, 0x02},
341 {DEMOD_WRITE, 0x34, 0x30},
342 {DEMOD_WRITE, 0x35, 0xff},
343 {DEMOD_WRITE, 0x38, 0x50},
344 {DEMOD_WRITE, 0x39, 0x68},
345 {DEMOD_WRITE, 0x3c, 0x7f},
346 {DEMOD_WRITE, 0x3d, 0x0f},
347 {DEMOD_WRITE, 0x45, 0x20},
348 {DEMOD_WRITE, 0x46, 0x24},
349 {DEMOD_WRITE, 0x47, 0x7c},
350 {DEMOD_WRITE, 0x48, 0x16},
351 {DEMOD_WRITE, 0x49, 0x04},
352 {DEMOD_WRITE, 0x4a, 0x01},
353 {DEMOD_WRITE, 0x4b, 0x78},
354 {DEMOD_WRITE, 0X4d, 0xd2},
355 {DEMOD_WRITE, 0x4e, 0x6d},
356 {DEMOD_WRITE, 0x50, 0x30},
357 {DEMOD_WRITE, 0x51, 0x30},
358 {DEMOD_WRITE, 0x54, 0x7b},
359 {DEMOD_WRITE, 0x56, 0x09},
360 {DEMOD_WRITE, 0x58, 0x59},
361 {DEMOD_WRITE, 0x59, 0x37},
362 {DEMOD_WRITE, 0x63, 0xfa},
363 {0xff, 0xaa, 0xff}
364};
365
366struct inittab fe_trigger[] = {
367 {DEMOD_WRITE, 0x97, 0x04},
368 {DEMOD_WRITE, 0x99, 0x77},
369 {DEMOD_WRITE, 0x9b, 0x64},
370 {DEMOD_WRITE, 0x9e, 0x00},
371 {DEMOD_WRITE, 0x9f, 0xf8},
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300372 {DEMOD_WRITE, 0x98, 0xff},
373 {DEMOD_WRITE, 0xc0, 0x0f},
374 {DEMOD_WRITE, 0x89, 0x01},
375 {DEMOD_WRITE, 0x00, 0x00},
376 {WRITE_DELAY, 0x0a, 0x00},
377 {DEMOD_WRITE, 0x00, 0x01},
378 {DEMOD_WRITE, 0x00, 0x00},
379 {DEMOD_WRITE, 0x9a, 0xb0},
380 {0xff, 0xaa, 0xff}
381};
382
383static int m88rs2000_tab_set(struct m88rs2000_state *state,
384 struct inittab *tab)
385{
386 int ret = 0;
387 u8 i;
388 if (tab == NULL)
389 return -EINVAL;
390
391 for (i = 0; i < 255; i++) {
392 switch (tab[i].cmd) {
393 case 0x01:
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300394 ret = m88rs2000_writereg(state, tab[i].reg,
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300395 tab[i].val);
396 break;
397 case 0x10:
398 if (tab[i].reg > 0)
399 mdelay(tab[i].reg);
400 break;
401 case 0xff:
402 if (tab[i].reg == 0xaa && tab[i].val == 0xff)
403 return 0;
404 case 0x00:
405 break;
406 default:
407 return -EINVAL;
408 }
409 if (ret < 0)
410 return -ENODEV;
411 }
412 return 0;
413}
414
415static int m88rs2000_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t volt)
416{
Igor M. Liplianin38431a92012-05-08 04:25:24 -0300417 struct m88rs2000_state *state = fe->demodulator_priv;
418 u8 data;
419
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300420 data = m88rs2000_readreg(state, 0xb2);
Igor M. Liplianin38431a92012-05-08 04:25:24 -0300421 data |= 0x03; /* bit0 V/H, bit1 off/on */
422
423 switch (volt) {
424 case SEC_VOLTAGE_18:
425 data &= ~0x03;
426 break;
427 case SEC_VOLTAGE_13:
428 data &= ~0x03;
429 data |= 0x01;
430 break;
431 case SEC_VOLTAGE_OFF:
432 break;
433 }
434
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300435 m88rs2000_writereg(state, 0xb2, data);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300436
437 return 0;
438}
439
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300440static int m88rs2000_init(struct dvb_frontend *fe)
441{
442 struct m88rs2000_state *state = fe->demodulator_priv;
443 int ret;
444
445 deb_info("m88rs2000: init chip\n");
446 /* Setup frontend from shutdown/cold */
Igor M. Liplianin081416e2012-05-08 04:08:04 -0300447 if (state->config->inittab)
448 ret = m88rs2000_tab_set(state,
449 (struct inittab *)state->config->inittab);
450 else
451 ret = m88rs2000_tab_set(state, m88rs2000_setup);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300452
453 return ret;
454}
455
456static int m88rs2000_sleep(struct dvb_frontend *fe)
457{
458 struct m88rs2000_state *state = fe->demodulator_priv;
459 int ret;
460 /* Shutdown the frondend */
461 ret = m88rs2000_tab_set(state, m88rs2000_shutdown);
462 return ret;
463}
464
465static int m88rs2000_read_status(struct dvb_frontend *fe, fe_status_t *status)
466{
467 struct m88rs2000_state *state = fe->demodulator_priv;
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300468 u8 reg = m88rs2000_readreg(state, 0x8c);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300469
470 *status = 0;
471
Malcolm Priestley7a9d6b42013-12-28 14:00:40 -0300472 if ((reg & 0xee) == 0xee) {
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300473 *status = FE_HAS_CARRIER | FE_HAS_SIGNAL | FE_HAS_VITERBI
Antti Palosaariff542982012-08-06 18:35:26 -0300474 | FE_HAS_SYNC | FE_HAS_LOCK;
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300475 if (state->config->set_ts_params)
476 state->config->set_ts_params(fe, CALL_IS_READ);
477 }
478 return 0;
479}
480
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300481static int m88rs2000_read_ber(struct dvb_frontend *fe, u32 *ber)
482{
Igor M. Liplianin38f78892012-12-28 19:40:16 -0300483 struct m88rs2000_state *state = fe->demodulator_priv;
484 u8 tmp0, tmp1;
485
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300486 m88rs2000_writereg(state, 0x9a, 0x30);
487 tmp0 = m88rs2000_readreg(state, 0xd8);
Igor M. Liplianin38f78892012-12-28 19:40:16 -0300488 if ((tmp0 & 0x10) != 0) {
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300489 m88rs2000_writereg(state, 0x9a, 0xb0);
Igor M. Liplianin38f78892012-12-28 19:40:16 -0300490 *ber = 0xffffffff;
491 return 0;
492 }
493
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300494 *ber = (m88rs2000_readreg(state, 0xd7) << 8) |
495 m88rs2000_readreg(state, 0xd6);
Igor M. Liplianin38f78892012-12-28 19:40:16 -0300496
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300497 tmp1 = m88rs2000_readreg(state, 0xd9);
498 m88rs2000_writereg(state, 0xd9, (tmp1 & ~7) | 4);
Igor M. Liplianin38f78892012-12-28 19:40:16 -0300499 /* needs twice */
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300500 m88rs2000_writereg(state, 0xd8, (tmp0 & ~8) | 0x30);
501 m88rs2000_writereg(state, 0xd8, (tmp0 & ~8) | 0x30);
502 m88rs2000_writereg(state, 0x9a, 0xb0);
Igor M. Liplianin38f78892012-12-28 19:40:16 -0300503
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300504 return 0;
505}
506
507static int m88rs2000_read_signal_strength(struct dvb_frontend *fe,
508 u16 *strength)
509{
Malcolm Priestleya0a030b2013-01-06 08:40:42 -0300510 if (fe->ops.tuner_ops.get_rf_strength)
511 fe->ops.tuner_ops.get_rf_strength(fe, strength);
512
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300513 return 0;
514}
515
516static int m88rs2000_read_snr(struct dvb_frontend *fe, u16 *snr)
517{
Igor M. Liplianin38f78892012-12-28 19:40:16 -0300518 struct m88rs2000_state *state = fe->demodulator_priv;
519
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300520 *snr = 512 * m88rs2000_readreg(state, 0x65);
Igor M. Liplianin38f78892012-12-28 19:40:16 -0300521
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300522 return 0;
523}
524
525static int m88rs2000_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
526{
Igor M. Liplianin38f78892012-12-28 19:40:16 -0300527 struct m88rs2000_state *state = fe->demodulator_priv;
528 u8 tmp;
529
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300530 *ucblocks = (m88rs2000_readreg(state, 0xd5) << 8) |
531 m88rs2000_readreg(state, 0xd4);
532 tmp = m88rs2000_readreg(state, 0xd8);
533 m88rs2000_writereg(state, 0xd8, tmp & ~0x20);
Igor M. Liplianin38f78892012-12-28 19:40:16 -0300534 /* needs two times */
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300535 m88rs2000_writereg(state, 0xd8, tmp | 0x20);
536 m88rs2000_writereg(state, 0xd8, tmp | 0x20);
Igor M. Liplianin38f78892012-12-28 19:40:16 -0300537
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300538 return 0;
539}
540
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300541static int m88rs2000_set_fec(struct m88rs2000_state *state,
542 fe_code_rate_t fec)
543{
Malcolm Priestley49c44802013-12-28 14:02:44 -0300544 u8 fec_set, reg;
545 int ret;
546
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300547 switch (fec) {
Malcolm Priestley49c44802013-12-28 14:02:44 -0300548 case FEC_1_2:
549 fec_set = 0x8;
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300550 break;
551 case FEC_2_3:
Malcolm Priestley49c44802013-12-28 14:02:44 -0300552 fec_set = 0x10;
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300553 break;
554 case FEC_3_4:
Malcolm Priestley49c44802013-12-28 14:02:44 -0300555 fec_set = 0x20;
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300556 break;
557 case FEC_5_6:
Malcolm Priestley49c44802013-12-28 14:02:44 -0300558 fec_set = 0x40;
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300559 break;
560 case FEC_7_8:
Malcolm Priestley49c44802013-12-28 14:02:44 -0300561 fec_set = 0x80;
562 break;
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300563 case FEC_AUTO:
564 default:
Malcolm Priestley49c44802013-12-28 14:02:44 -0300565 fec_set = 0x0;
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300566 }
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300567
Malcolm Priestley49c44802013-12-28 14:02:44 -0300568 reg = m88rs2000_readreg(state, 0x70);
569 reg &= 0x7;
570 ret = m88rs2000_writereg(state, 0x70, reg | fec_set);
571
572 ret |= m88rs2000_writereg(state, 0x76, 0x8);
573
574 return ret;
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300575}
576
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300577static fe_code_rate_t m88rs2000_get_fec(struct m88rs2000_state *state)
578{
579 u8 reg;
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300580 m88rs2000_writereg(state, 0x9a, 0x30);
581 reg = m88rs2000_readreg(state, 0x76);
582 m88rs2000_writereg(state, 0x9a, 0xb0);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300583
Malcolm Priestleya6d8e682013-12-28 14:04:51 -0300584 reg &= 0xf0;
585 reg >>= 5;
586
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300587 switch (reg) {
Malcolm Priestleya6d8e682013-12-28 14:04:51 -0300588 case 0x4:
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300589 return FEC_1_2;
Malcolm Priestleya6d8e682013-12-28 14:04:51 -0300590 case 0x3:
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300591 return FEC_2_3;
Malcolm Priestleya6d8e682013-12-28 14:04:51 -0300592 case 0x2:
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300593 return FEC_3_4;
Malcolm Priestleya6d8e682013-12-28 14:04:51 -0300594 case 0x1:
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300595 return FEC_5_6;
Malcolm Priestleya6d8e682013-12-28 14:04:51 -0300596 case 0x0:
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300597 return FEC_7_8;
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300598 default:
599 break;
600 }
601
602 return FEC_AUTO;
603}
604
605static int m88rs2000_set_frontend(struct dvb_frontend *fe)
606{
607 struct m88rs2000_state *state = fe->demodulator_priv;
608 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
609 fe_status_t status;
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300610 int i, ret = 0;
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300611 u32 tuner_freq;
Malcolm Priestley06af15d2013-12-24 13:17:12 -0300612 s16 offset = 0;
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300613 u8 reg;
614
615 state->no_lock_count = 0;
616
617 if (c->delivery_system != SYS_DVBS) {
618 deb_info("%s: unsupported delivery "
619 "system selected (%d)\n",
620 __func__, c->delivery_system);
621 return -EOPNOTSUPP;
622 }
623
624 /* Set Tuner */
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300625 if (fe->ops.tuner_ops.set_params)
626 ret = fe->ops.tuner_ops.set_params(fe);
627
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300628 if (ret < 0)
629 return -ENODEV;
630
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300631 if (fe->ops.tuner_ops.get_frequency)
632 ret = fe->ops.tuner_ops.get_frequency(fe, &tuner_freq);
633
634 if (ret < 0)
635 return -ENODEV;
636
Malcolm Priestley06af15d2013-12-24 13:17:12 -0300637 offset = (s16)((s32)tuner_freq - c->frequency);
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300638
Malcolm Priestley06af15d2013-12-24 13:17:12 -0300639 /* default mclk value 96.4285 * 2 * 1000 = 192857 */
640 if (((c->frequency % 192857) >= (192857 - 3000)) ||
641 (c->frequency % 192857) <= 3000)
642 ret = m88rs2000_writereg(state, 0x86, 0xc2);
643 else
644 ret = m88rs2000_writereg(state, 0x86, 0xc6);
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300645
Malcolm Priestley06af15d2013-12-24 13:17:12 -0300646 ret |= m88rs2000_set_carrieroffset(fe, offset);
647 if (ret < 0)
648 return -ENODEV;
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300649
Malcolm Priestleydd4491d2013-12-24 13:18:46 -0300650 /* Reset demod by symbol rate */
651 if (c->symbol_rate > 27500000)
652 ret = m88rs2000_writereg(state, 0xf1, 0xa4);
653 else
654 ret = m88rs2000_writereg(state, 0xf1, 0xbf);
655
656 ret |= m88rs2000_tab_set(state, fe_reset);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300657 if (ret < 0)
658 return -ENODEV;
659
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300660 /* Set FEC */
Malcolm Priestley49c44802013-12-28 14:02:44 -0300661 ret = m88rs2000_set_fec(state, c->fec_inner);
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300662 ret |= m88rs2000_writereg(state, 0x85, 0x1);
663 ret |= m88rs2000_writereg(state, 0x8a, 0xbf);
664 ret |= m88rs2000_writereg(state, 0x8d, 0x1e);
665 ret |= m88rs2000_writereg(state, 0x90, 0xf1);
666 ret |= m88rs2000_writereg(state, 0x91, 0x08);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300667
668 if (ret < 0)
669 return -ENODEV;
670
671 /* Set Symbol Rate */
672 ret = m88rs2000_set_symbolrate(fe, c->symbol_rate);
673 if (ret < 0)
674 return -ENODEV;
675
676 /* Set up Demod */
677 ret = m88rs2000_tab_set(state, fe_trigger);
678 if (ret < 0)
679 return -ENODEV;
680
681 for (i = 0; i < 25; i++) {
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300682 reg = m88rs2000_readreg(state, 0x8c);
Malcolm Priestley7a9d6b42013-12-28 14:00:40 -0300683 if ((reg & 0xee) == 0xee) {
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300684 status = FE_HAS_LOCK;
685 break;
686 }
687 state->no_lock_count++;
Malcolm Priestleye58c11f2012-05-14 16:43:50 -0300688 if (state->no_lock_count == 15) {
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300689 reg = m88rs2000_readreg(state, 0x70);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300690 reg ^= 0x4;
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300691 m88rs2000_writereg(state, 0x70, reg);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300692 state->no_lock_count = 0;
693 }
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300694 msleep(20);
695 }
696
697 if (status & FE_HAS_LOCK) {
698 state->fec_inner = m88rs2000_get_fec(state);
699 /* Uknown suspect SNR level */
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300700 reg = m88rs2000_readreg(state, 0x65);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300701 }
702
703 state->tuner_frequency = c->frequency;
704 state->symbol_rate = c->symbol_rate;
705 return 0;
706}
707
708static int m88rs2000_get_frontend(struct dvb_frontend *fe)
709{
710 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
711 struct m88rs2000_state *state = fe->demodulator_priv;
712 c->fec_inner = state->fec_inner;
713 c->frequency = state->tuner_frequency;
714 c->symbol_rate = state->symbol_rate;
715 return 0;
716}
717
718static int m88rs2000_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
719{
720 struct m88rs2000_state *state = fe->demodulator_priv;
721
722 if (enable)
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300723 m88rs2000_writereg(state, 0x81, 0x84);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300724 else
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300725 m88rs2000_writereg(state, 0x81, 0x81);
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300726 udelay(10);
727 return 0;
728}
729
730static void m88rs2000_release(struct dvb_frontend *fe)
731{
732 struct m88rs2000_state *state = fe->demodulator_priv;
733 kfree(state);
734}
735
736static struct dvb_frontend_ops m88rs2000_ops = {
737 .delsys = { SYS_DVBS },
738 .info = {
739 .name = "M88RS2000 DVB-S",
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300740 .frequency_min = 950000,
741 .frequency_max = 2150000,
742 .frequency_stepsize = 1000, /* kHz for QPSK frontends */
743 .frequency_tolerance = 5000,
744 .symbol_rate_min = 1000000,
745 .symbol_rate_max = 45000000,
746 .symbol_rate_tolerance = 500, /* ppm */
747 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
748 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
Malcolm Priestley3c8023a2014-02-25 23:05:39 -0300749 FE_CAN_QPSK | FE_CAN_INVERSION_AUTO |
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300750 FE_CAN_FEC_AUTO
751 },
752
753 .release = m88rs2000_release,
754 .init = m88rs2000_init,
755 .sleep = m88rs2000_sleep,
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300756 .i2c_gate_ctrl = m88rs2000_i2c_gate_ctrl,
757 .read_status = m88rs2000_read_status,
758 .read_ber = m88rs2000_read_ber,
759 .read_signal_strength = m88rs2000_read_signal_strength,
760 .read_snr = m88rs2000_read_snr,
761 .read_ucblocks = m88rs2000_read_ucblocks,
762 .diseqc_send_master_cmd = m88rs2000_send_diseqc_msg,
763 .diseqc_send_burst = m88rs2000_send_diseqc_burst,
764 .set_tone = m88rs2000_set_tone,
765 .set_voltage = m88rs2000_set_voltage,
766
767 .set_frontend = m88rs2000_set_frontend,
768 .get_frontend = m88rs2000_get_frontend,
769};
770
771struct dvb_frontend *m88rs2000_attach(const struct m88rs2000_config *config,
772 struct i2c_adapter *i2c)
773{
774 struct m88rs2000_state *state = NULL;
775
776 /* allocate memory for the internal state */
777 state = kzalloc(sizeof(struct m88rs2000_state), GFP_KERNEL);
778 if (state == NULL)
779 goto error;
780
781 /* setup the state */
782 state->config = config;
783 state->i2c = i2c;
784 state->tuner_frequency = 0;
785 state->symbol_rate = 0;
786 state->fec_inner = 0;
787
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300788 /* create dvb_frontend */
789 memcpy(&state->frontend.ops, &m88rs2000_ops,
790 sizeof(struct dvb_frontend_ops));
791 state->frontend.demodulator_priv = state;
792 return &state->frontend;
793
794error:
795 kfree(state);
796
797 return NULL;
798}
799EXPORT_SYMBOL(m88rs2000_attach);
800
801MODULE_DESCRIPTION("M88RS2000 DVB-S Demodulator driver");
802MODULE_AUTHOR("Malcolm Priestley tvboxspy@gmail.com");
803MODULE_LICENSE("GPL");
Malcolm Priestley593a2ce02012-03-14 16:31:26 -0300804MODULE_VERSION("1.13");
Malcolm Priestleyae8dc8ee2012-03-07 18:11:03 -0300805