1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 	STB0899 Multistandard Frontend driver
4 	Copyright (C) Manu Abraham (abraham.manu@gmail.com)
5 
6 	Copyright (C) ST Microelectronics
7 
8 */
9 
10 #include <linux/init.h>
11 #include <linux/jiffies.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16 
17 #include <linux/dvb/frontend.h>
18 #include <media/dvb_frontend.h>
19 
20 #include "stb0899_drv.h"
21 #include "stb0899_priv.h"
22 #include "stb0899_reg.h"
23 
24 /* Max transfer size done by I2C transfer functions */
25 #define MAX_XFER_SIZE  64
26 
27 static unsigned int verbose = 0;//1;
28 module_param(verbose, int, 0644);
29 
30 /* C/N in dB/10, NIRM/NIRL */
31 static const struct stb0899_tab stb0899_cn_tab[] = {
32 	{ 200,	2600 },
33 	{ 190,	2700 },
34 	{ 180,	2860 },
35 	{ 170,	3020 },
36 	{ 160,	3210 },
37 	{ 150,	3440 },
38 	{ 140,	3710 },
39 	{ 130,	4010 },
40 	{ 120,	4360 },
41 	{ 110,	4740 },
42 	{ 100,	5190 },
43 	{ 90,	5670 },
44 	{ 80,	6200 },
45 	{ 70,	6770 },
46 	{ 60,	7360 },
47 	{ 50,	7970 },
48 	{ 40,	8250 },
49 	{ 30,	9000 },
50 	{ 20,	9450 },
51 	{ 15,	9600 },
52 };
53 
54 /* DVB-S AGCIQ_VALUE vs. signal level in dBm/10.
55  * As measured, connected to a modulator.
56  * -8.0 to -50.0 dBm directly connected,
57  * -52.0 to -74.8 with extra attenuation.
58  * Cut-off to AGCIQ_VALUE = 0x80 below -74.8dBm.
59  * Crude linear extrapolation below -84.8dBm and above -8.0dBm.
60  */
61 static const struct stb0899_tab stb0899_dvbsrf_tab[] = {
62 	{ -750,	-128 },
63 	{ -748,	 -94 },
64 	{ -745,	 -92 },
65 	{ -735,	 -90 },
66 	{ -720,	 -87 },
67 	{ -670,	 -77 },
68 	{ -640,	 -70 },
69 	{ -610,	 -62 },
70 	{ -600,	 -60 },
71 	{ -590,	 -56 },
72 	{ -560,	 -41 },
73 	{ -540,	 -25 },
74 	{ -530,	 -17 },
75 	{ -520,	 -11 },
76 	{ -500,	   1 },
77 	{ -490,	   6 },
78 	{ -480,	  10 },
79 	{ -440,	  22 },
80 	{ -420,	  27 },
81 	{ -400,	  31 },
82 	{ -380,	  34 },
83 	{ -340,	  40 },
84 	{ -320,	  43 },
85 	{ -280,	  48 },
86 	{ -250,	  52 },
87 	{ -230,	  55 },
88 	{ -180,	  61 },
89 	{ -140,	  66 },
90 	{  -90,	  73 },
91 	{  -80,	  74 },
92 	{  500,	 127 }
93 };
94 
95 /* DVB-S2 IF_AGC_GAIN vs. signal level in dBm/10.
96  * As measured, connected to a modulator.
97  * -8.0 to -50.1 dBm directly connected,
98  * -53.0 to -76.6 with extra attenuation.
99  * Cut-off to IF_AGC_GAIN = 0x3fff below -76.6dBm.
100  * Crude linear extrapolation below -76.6dBm and above -8.0dBm.
101  */
102 static const struct stb0899_tab stb0899_dvbs2rf_tab[] = {
103 	{  700,	    0 },
104 	{  -80,	 3217 },
105 	{ -150,	 3893 },
106 	{ -190,	 4217 },
107 	{ -240,	 4621 },
108 	{ -280,	 4945 },
109 	{ -320,	 5273 },
110 	{ -350,	 5545 },
111 	{ -370,	 5741 },
112 	{ -410,	 6147 },
113 	{ -450,	 6671 },
114 	{ -490,	 7413 },
115 	{ -501,	 7665 },
116 	{ -530,	 8767 },
117 	{ -560,	10219 },
118 	{ -580,	10939 },
119 	{ -590,	11518 },
120 	{ -600,	11723 },
121 	{ -650,	12659 },
122 	{ -690,	13219 },
123 	{ -730,	13645 },
124 	{ -750,	13909 },
125 	{ -766,	14153 },
126 	{ -950,	16383 }
127 };
128 
129 /* DVB-S2 Es/N0 quant in dB/100 vs read value * 100*/
130 static struct stb0899_tab stb0899_quant_tab[] = {
131 	{    0,	    0 },
132 	{    0,	  100 },
133 	{  600,	  200 },
134 	{  950,	  299 },
135 	{ 1200,	  398 },
136 	{ 1400,	  501 },
137 	{ 1560,	  603 },
138 	{ 1690,	  700 },
139 	{ 1810,	  804 },
140 	{ 1910,	  902 },
141 	{ 2000,	 1000 },
142 	{ 2080,	 1096 },
143 	{ 2160,	 1202 },
144 	{ 2230,	 1303 },
145 	{ 2350,	 1496 },
146 	{ 2410,	 1603 },
147 	{ 2460,	 1698 },
148 	{ 2510,	 1799 },
149 	{ 2600,	 1995 },
150 	{ 2650,	 2113 },
151 	{ 2690,  2213 },
152 	{ 2720,	 2291 },
153 	{ 2760,	 2399 },
154 	{ 2800,	 2512 },
155 	{ 2860,	 2692 },
156 	{ 2930,	 2917 },
157 	{ 2960,	 3020 },
158 	{ 3010,	 3199 },
159 	{ 3040,	 3311 },
160 	{ 3060,	 3388 },
161 	{ 3120,	 3631 },
162 	{ 3190,	 3936 },
163 	{ 3400,	 5012 },
164 	{ 3610,	 6383 },
165 	{ 3800,	 7943 },
166 	{ 4210,	12735 },
167 	{ 4500,	17783 },
168 	{ 4690,	22131 },
169 	{ 4810,	25410 }
170 };
171 
172 /* DVB-S2 Es/N0 estimate in dB/100 vs read value */
173 static struct stb0899_tab stb0899_est_tab[] = {
174 	{    0,	     0 },
175 	{    0,	     1 },
176 	{  301,	     2 },
177 	{ 1204,	    16 },
178 	{ 1806,	    64 },
179 	{ 2408,	   256 },
180 	{ 2709,	   512 },
181 	{ 3010,	  1023 },
182 	{ 3311,	  2046 },
183 	{ 3612,	  4093 },
184 	{ 3823,	  6653 },
185 	{ 3913,	  8185 },
186 	{ 4010,	 10233 },
187 	{ 4107,	 12794 },
188 	{ 4214,	 16368 },
189 	{ 4266,	 18450 },
190 	{ 4311,	 20464 },
191 	{ 4353,	 22542 },
192 	{ 4391,	 24604 },
193 	{ 4425,	 26607 },
194 	{ 4457,	 28642 },
195 	{ 4487,	 30690 },
196 	{ 4515,	 32734 },
197 	{ 4612,	 40926 },
198 	{ 4692,	 49204 },
199 	{ 4816,	 65464 },
200 	{ 4913,	 81846 },
201 	{ 4993,	 98401 },
202 	{ 5060,	114815 },
203 	{ 5118,	131220 },
204 	{ 5200,	158489 },
205 	{ 5300,	199526 },
206 	{ 5400,	251189 },
207 	{ 5500,	316228 },
208 	{ 5600,	398107 },
209 	{ 5720,	524807 },
210 	{ 5721,	526017 },
211 };
212 
_stb0899_read_reg(struct stb0899_state * state,unsigned int reg)213 static int _stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
214 {
215 	int ret;
216 
217 	u8 b0[] = { reg >> 8, reg & 0xff };
218 	u8 buf;
219 
220 	struct i2c_msg msg[] = {
221 		{
222 			.addr	= state->config->demod_address,
223 			.flags	= 0,
224 			.buf	= b0,
225 			.len	= 2
226 		},{
227 			.addr	= state->config->demod_address,
228 			.flags	= I2C_M_RD,
229 			.buf	= &buf,
230 			.len	= 1
231 		}
232 	};
233 
234 	ret = i2c_transfer(state->i2c, msg, 2);
235 	if (ret != 2) {
236 		if (ret != -ERESTARTSYS)
237 			dprintk(state->verbose, FE_ERROR, 1,
238 				"Read error, Reg=[0x%02x], Status=%d",
239 				reg, ret);
240 
241 		return ret < 0 ? ret : -EREMOTEIO;
242 	}
243 	if (unlikely(*state->verbose >= FE_DEBUGREG))
244 		dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
245 			reg, buf);
246 
247 	return (unsigned int)buf;
248 }
249 
stb0899_read_reg(struct stb0899_state * state,unsigned int reg)250 int stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
251 {
252 	int result;
253 
254 	result = _stb0899_read_reg(state, reg);
255 	/*
256 	 * Bug ID 9:
257 	 * access to 0xf2xx/0xf6xx
258 	 * must be followed by read from 0xf2ff/0xf6ff.
259 	 */
260 	if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
261 	    (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
262 		_stb0899_read_reg(state, (reg | 0x00ff));
263 
264 	return result;
265 }
266 
_stb0899_read_s2reg(struct stb0899_state * state,u32 stb0899_i2cdev,u32 stb0899_base_addr,u16 stb0899_reg_offset)267 u32 _stb0899_read_s2reg(struct stb0899_state *state,
268 			u32 stb0899_i2cdev,
269 			u32 stb0899_base_addr,
270 			u16 stb0899_reg_offset)
271 {
272 	int status;
273 	u32 data;
274 	u8 buf[7] = { 0 };
275 	u16 tmpaddr;
276 
277 	u8 buf_0[] = {
278 		GETBYTE(stb0899_i2cdev, BYTE1),		/* 0xf3	S2 Base Address (MSB)	*/
279 		GETBYTE(stb0899_i2cdev, BYTE0),		/* 0xfc	S2 Base Address (LSB)	*/
280 		GETBYTE(stb0899_base_addr, BYTE0),	/* 0x00	Base Address (LSB)	*/
281 		GETBYTE(stb0899_base_addr, BYTE1),	/* 0x04	Base Address (LSB)	*/
282 		GETBYTE(stb0899_base_addr, BYTE2),	/* 0x00	Base Address (MSB)	*/
283 		GETBYTE(stb0899_base_addr, BYTE3),	/* 0x00	Base Address (MSB)	*/
284 	};
285 	u8 buf_1[] = {
286 		0x00,	/* 0xf3	Reg Offset	*/
287 		0x00,	/* 0x44	Reg Offset	*/
288 	};
289 
290 	struct i2c_msg msg_0 = {
291 		.addr	= state->config->demod_address,
292 		.flags	= 0,
293 		.buf	= buf_0,
294 		.len	= 6
295 	};
296 
297 	struct i2c_msg msg_1 = {
298 		.addr	= state->config->demod_address,
299 		.flags	= 0,
300 		.buf	= buf_1,
301 		.len	= 2
302 	};
303 
304 	struct i2c_msg msg_r = {
305 		.addr	= state->config->demod_address,
306 		.flags	= I2C_M_RD,
307 		.buf	= buf,
308 		.len	= 4
309 	};
310 
311 	tmpaddr = stb0899_reg_offset & 0xff00;
312 	if (!(stb0899_reg_offset & 0x8))
313 		tmpaddr = stb0899_reg_offset | 0x20;
314 
315 	buf_1[0] = GETBYTE(tmpaddr, BYTE1);
316 	buf_1[1] = GETBYTE(tmpaddr, BYTE0);
317 
318 	status = i2c_transfer(state->i2c, &msg_0, 1);
319 	if (status < 1) {
320 		if (status != -ERESTARTSYS)
321 			printk(KERN_ERR "%s ERR(1), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
322 			       __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
323 
324 		goto err;
325 	}
326 
327 	/* Dummy	*/
328 	status = i2c_transfer(state->i2c, &msg_1, 1);
329 	if (status < 1)
330 		goto err;
331 
332 	status = i2c_transfer(state->i2c, &msg_r, 1);
333 	if (status < 1)
334 		goto err;
335 
336 	buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
337 	buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
338 
339 	/* Actual	*/
340 	status = i2c_transfer(state->i2c, &msg_1, 1);
341 	if (status < 1) {
342 		if (status != -ERESTARTSYS)
343 			printk(KERN_ERR "%s ERR(2), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
344 			       __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
345 		goto err;
346 	}
347 
348 	status = i2c_transfer(state->i2c, &msg_r, 1);
349 	if (status < 1) {
350 		if (status != -ERESTARTSYS)
351 			printk(KERN_ERR "%s ERR(3), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
352 			       __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
353 		return status < 0 ? status : -EREMOTEIO;
354 	}
355 
356 	data = MAKEWORD32(buf[3], buf[2], buf[1], buf[0]);
357 	if (unlikely(*state->verbose >= FE_DEBUGREG))
358 		printk(KERN_DEBUG "%s Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
359 		       __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, data);
360 
361 	return data;
362 
363 err:
364 	return status < 0 ? status : -EREMOTEIO;
365 }
366 
stb0899_write_s2reg(struct stb0899_state * state,u32 stb0899_i2cdev,u32 stb0899_base_addr,u16 stb0899_reg_offset,u32 stb0899_data)367 int stb0899_write_s2reg(struct stb0899_state *state,
368 			u32 stb0899_i2cdev,
369 			u32 stb0899_base_addr,
370 			u16 stb0899_reg_offset,
371 			u32 stb0899_data)
372 {
373 	int status;
374 
375 	/* Base Address Setup	*/
376 	u8 buf_0[] = {
377 		GETBYTE(stb0899_i2cdev, BYTE1),		/* 0xf3	S2 Base Address (MSB)	*/
378 		GETBYTE(stb0899_i2cdev, BYTE0),		/* 0xfc	S2 Base Address (LSB)	*/
379 		GETBYTE(stb0899_base_addr, BYTE0),	/* 0x00	Base Address (LSB)	*/
380 		GETBYTE(stb0899_base_addr, BYTE1),	/* 0x04	Base Address (LSB)	*/
381 		GETBYTE(stb0899_base_addr, BYTE2),	/* 0x00	Base Address (MSB)	*/
382 		GETBYTE(stb0899_base_addr, BYTE3),	/* 0x00	Base Address (MSB)	*/
383 	};
384 	u8 buf_1[] = {
385 		0x00,	/* 0xf3	Reg Offset	*/
386 		0x00,	/* 0x44	Reg Offset	*/
387 		0x00,	/* data			*/
388 		0x00,	/* data			*/
389 		0x00,	/* data			*/
390 		0x00,	/* data			*/
391 	};
392 
393 	struct i2c_msg msg_0 = {
394 		.addr	= state->config->demod_address,
395 		.flags	= 0,
396 		.buf	= buf_0,
397 		.len	= 6
398 	};
399 
400 	struct i2c_msg msg_1 = {
401 		.addr	= state->config->demod_address,
402 		.flags	= 0,
403 		.buf	= buf_1,
404 		.len	= 6
405 	};
406 
407 	buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
408 	buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
409 	buf_1[2] = GETBYTE(stb0899_data, BYTE0);
410 	buf_1[3] = GETBYTE(stb0899_data, BYTE1);
411 	buf_1[4] = GETBYTE(stb0899_data, BYTE2);
412 	buf_1[5] = GETBYTE(stb0899_data, BYTE3);
413 
414 	if (unlikely(*state->verbose >= FE_DEBUGREG))
415 		printk(KERN_DEBUG "%s Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
416 		       __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data);
417 
418 	status = i2c_transfer(state->i2c, &msg_0, 1);
419 	if (unlikely(status < 1)) {
420 		if (status != -ERESTARTSYS)
421 			printk(KERN_ERR "%s ERR (1), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
422 			       __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
423 		goto err;
424 	}
425 	status = i2c_transfer(state->i2c, &msg_1, 1);
426 	if (unlikely(status < 1)) {
427 		if (status != -ERESTARTSYS)
428 			printk(KERN_ERR "%s ERR (2), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
429 			       __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
430 
431 		return status < 0 ? status : -EREMOTEIO;
432 	}
433 
434 	return 0;
435 
436 err:
437 	return status < 0 ? status : -EREMOTEIO;
438 }
439 
stb0899_read_regs(struct stb0899_state * state,unsigned int reg,u8 * buf,u32 count)440 int stb0899_read_regs(struct stb0899_state *state, unsigned int reg, u8 *buf, u32 count)
441 {
442 	int status;
443 
444 	u8 b0[] = { reg >> 8, reg & 0xff };
445 
446 	struct i2c_msg msg[] = {
447 		{
448 			.addr	= state->config->demod_address,
449 			.flags	= 0,
450 			.buf	= b0,
451 			.len	= 2
452 		},{
453 			.addr	= state->config->demod_address,
454 			.flags	= I2C_M_RD,
455 			.buf	= buf,
456 			.len	= count
457 		}
458 	};
459 
460 	status = i2c_transfer(state->i2c, msg, 2);
461 	if (status != 2) {
462 		if (status != -ERESTARTSYS)
463 			printk(KERN_ERR "%s Read error, Reg=[0x%04x], Count=%u, Status=%d\n",
464 			       __func__, reg, count, status);
465 		goto err;
466 	}
467 	/*
468 	 * Bug ID 9:
469 	 * access to 0xf2xx/0xf6xx
470 	 * must be followed by read from 0xf2ff/0xf6ff.
471 	 */
472 	if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
473 	    (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
474 		_stb0899_read_reg(state, (reg | 0x00ff));
475 
476 	dprintk(state->verbose, FE_DEBUGREG, 1,
477 		"%s [0x%04x]: %*ph", __func__, reg, count, buf);
478 
479 	return 0;
480 err:
481 	return status < 0 ? status : -EREMOTEIO;
482 }
483 
stb0899_write_regs(struct stb0899_state * state,unsigned int reg,u8 * data,u32 count)484 int stb0899_write_regs(struct stb0899_state *state, unsigned int reg, u8 *data, u32 count)
485 {
486 	int ret;
487 	u8 buf[MAX_XFER_SIZE];
488 	struct i2c_msg i2c_msg = {
489 		.addr	= state->config->demod_address,
490 		.flags	= 0,
491 		.buf	= buf,
492 		.len	= 2 + count
493 	};
494 
495 	if (2 + count > sizeof(buf)) {
496 		printk(KERN_WARNING
497 		       "%s: i2c wr reg=%04x: len=%d is too big!\n",
498 		       KBUILD_MODNAME, reg, count);
499 		return -EINVAL;
500 	}
501 
502 	buf[0] = reg >> 8;
503 	buf[1] = reg & 0xff;
504 	memcpy(&buf[2], data, count);
505 
506 	dprintk(state->verbose, FE_DEBUGREG, 1,
507 		"%s [0x%04x]: %*ph", __func__, reg, count, data);
508 	ret = i2c_transfer(state->i2c, &i2c_msg, 1);
509 
510 	/*
511 	 * Bug ID 9:
512 	 * access to 0xf2xx/0xf6xx
513 	 * must be followed by read from 0xf2ff/0xf6ff.
514 	 */
515 	if ((((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
516 		stb0899_read_reg(state, (reg | 0x00ff));
517 
518 	if (ret != 1) {
519 		if (ret != -ERESTARTSYS)
520 			dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
521 				reg, data[0], count, ret);
522 		return ret < 0 ? ret : -EREMOTEIO;
523 	}
524 
525 	return 0;
526 }
527 
stb0899_write_reg(struct stb0899_state * state,unsigned int reg,u8 data)528 int stb0899_write_reg(struct stb0899_state *state, unsigned int reg, u8 data)
529 {
530 	u8 tmp = data;
531 	return stb0899_write_regs(state, reg, &tmp, 1);
532 }
533 
534 /*
535  * stb0899_get_mclk
536  * Get STB0899 master clock frequency
537  * ExtClk: external clock frequency (Hz)
538  */
stb0899_get_mclk(struct stb0899_state * state)539 static u32 stb0899_get_mclk(struct stb0899_state *state)
540 {
541 	u32 mclk = 0, div = 0;
542 
543 	div = stb0899_read_reg(state, STB0899_NCOARSE);
544 	mclk = (div + 1) * state->config->xtal_freq / 6;
545 	dprintk(state->verbose, FE_DEBUG, 1, "div=%d, mclk=%d", div, mclk);
546 
547 	return mclk;
548 }
549 
550 /*
551  * stb0899_set_mclk
552  * Set STB0899 master Clock frequency
553  * Mclk: demodulator master clock
554  * ExtClk: external clock frequency (Hz)
555  */
stb0899_set_mclk(struct stb0899_state * state,u32 Mclk)556 static void stb0899_set_mclk(struct stb0899_state *state, u32 Mclk)
557 {
558 	struct stb0899_internal *internal = &state->internal;
559 	u8 mdiv = 0;
560 
561 	dprintk(state->verbose, FE_DEBUG, 1, "state->config=%p", state->config);
562 	mdiv = ((6 * Mclk) / state->config->xtal_freq) - 1;
563 	dprintk(state->verbose, FE_DEBUG, 1, "mdiv=%d", mdiv);
564 
565 	stb0899_write_reg(state, STB0899_NCOARSE, mdiv);
566 	internal->master_clk = stb0899_get_mclk(state);
567 
568 	dprintk(state->verbose, FE_DEBUG, 1, "MasterCLOCK=%d", internal->master_clk);
569 }
570 
stb0899_postproc(struct stb0899_state * state,u8 ctl,int enable)571 static int stb0899_postproc(struct stb0899_state *state, u8 ctl, int enable)
572 {
573 	struct stb0899_config *config		= state->config;
574 	const struct stb0899_postproc *postproc	= config->postproc;
575 
576 	/* post process event */
577 	if (postproc) {
578 		if (enable) {
579 			if (postproc[ctl].level == STB0899_GPIOPULLUP)
580 				stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
581 			else
582 				stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
583 		} else {
584 			if (postproc[ctl].level == STB0899_GPIOPULLUP)
585 				stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
586 			else
587 				stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
588 		}
589 	}
590 	return 0;
591 }
592 
stb0899_detach(struct dvb_frontend * fe)593 static void stb0899_detach(struct dvb_frontend *fe)
594 {
595 	struct stb0899_state *state = fe->demodulator_priv;
596 
597 	/* post process event */
598 	stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
599 }
600 
stb0899_release(struct dvb_frontend * fe)601 static void stb0899_release(struct dvb_frontend *fe)
602 {
603 	struct stb0899_state *state = fe->demodulator_priv;
604 
605 	dprintk(state->verbose, FE_DEBUG, 1, "Release Frontend");
606 	kfree(state);
607 }
608 
609 /*
610  * stb0899_get_alpha
611  * return: rolloff
612  */
stb0899_get_alpha(struct stb0899_state * state)613 static int stb0899_get_alpha(struct stb0899_state *state)
614 {
615 	u8 mode_coeff;
616 
617 	mode_coeff = stb0899_read_reg(state, STB0899_DEMOD);
618 
619 	if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1)
620 		return 20;
621 	else
622 		return 35;
623 }
624 
625 /*
626  * stb0899_init_calc
627  */
stb0899_init_calc(struct stb0899_state * state)628 static void stb0899_init_calc(struct stb0899_state *state)
629 {
630 	struct stb0899_internal *internal = &state->internal;
631 	int master_clk;
632 	u8 agc[2];
633 	u32 reg;
634 
635 	/* Read registers (in burst mode)	*/
636 	stb0899_read_regs(state, STB0899_AGC1REF, agc, 2); /* AGC1R and AGC2O	*/
637 
638 	/* Initial calculations	*/
639 	master_clk			= stb0899_get_mclk(state);
640 	internal->t_agc1		= 0;
641 	internal->t_agc2		= 0;
642 	internal->master_clk		= master_clk;
643 	internal->mclk			= master_clk / 65536L;
644 	internal->rolloff		= stb0899_get_alpha(state);
645 
646 	/* DVBS2 Initial calculations	*/
647 	/* Set AGC value to the middle	*/
648 	internal->agc_gain		= 8154;
649 	reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
650 	STB0899_SETFIELD_VAL(IF_GAIN_INIT, reg, internal->agc_gain);
651 	stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
652 
653 	reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA);
654 	internal->rrc_alpha		= STB0899_GETFIELD(RRC_ALPHA, reg);
655 
656 	internal->center_freq		= 0;
657 	internal->av_frame_coarse	= 10;
658 	internal->av_frame_fine		= 20;
659 	internal->step_size		= 2;
660 /*
661 	if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO))
662 		pParams->IQLocked = 0;
663 	else
664 		pParams->IQLocked = 1;
665 */
666 }
667 
stb0899_wait_diseqc_fifo_empty(struct stb0899_state * state,int timeout)668 static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout)
669 {
670 	u8 reg = 0;
671 	unsigned long start = jiffies;
672 
673 	while (1) {
674 		reg = stb0899_read_reg(state, STB0899_DISSTATUS);
675 		if (!STB0899_GETFIELD(FIFOFULL, reg))
676 			break;
677 		if (time_after(jiffies, start + timeout)) {
678 			dprintk(state->verbose, FE_ERROR, 1, "timed out !!");
679 			return -ETIMEDOUT;
680 		}
681 	}
682 
683 	return 0;
684 }
685 
stb0899_send_diseqc_msg(struct dvb_frontend * fe,struct dvb_diseqc_master_cmd * cmd)686 static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
687 {
688 	struct stb0899_state *state = fe->demodulator_priv;
689 	u8 reg, i;
690 
691 	if (cmd->msg_len > sizeof(cmd->msg))
692 		return -EINVAL;
693 
694 	/* enable FIFO precharge	*/
695 	reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
696 	STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 1);
697 	stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
698 	for (i = 0; i < cmd->msg_len; i++) {
699 		/* wait for FIFO empty	*/
700 		if (stb0899_wait_diseqc_fifo_empty(state, 100) < 0)
701 			return -ETIMEDOUT;
702 
703 		stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]);
704 	}
705 	reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
706 	STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0);
707 	stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
708 	msleep(100);
709 	return 0;
710 }
711 
stb0899_wait_diseqc_rxidle(struct stb0899_state * state,int timeout)712 static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout)
713 {
714 	u8 reg = 0;
715 	unsigned long start = jiffies;
716 
717 	while (!STB0899_GETFIELD(RXEND, reg)) {
718 		reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
719 		if (time_after(jiffies, start + timeout)) {
720 			dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
721 			return -ETIMEDOUT;
722 		}
723 		msleep(10);
724 	}
725 
726 	return 0;
727 }
728 
stb0899_recv_slave_reply(struct dvb_frontend * fe,struct dvb_diseqc_slave_reply * reply)729 static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
730 {
731 	struct stb0899_state *state = fe->demodulator_priv;
732 	u8 reg, length = 0, i;
733 	int result;
734 
735 	if (stb0899_wait_diseqc_rxidle(state, 100) < 0)
736 		return -ETIMEDOUT;
737 
738 	reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
739 	if (STB0899_GETFIELD(RXEND, reg)) {
740 
741 		reg = stb0899_read_reg(state, STB0899_DISRX_ST1);
742 		length = STB0899_GETFIELD(FIFOBYTENBR, reg);
743 
744 		if (length > sizeof (reply->msg)) {
745 			result = -EOVERFLOW;
746 			goto exit;
747 		}
748 		reply->msg_len = length;
749 
750 		/* extract data */
751 		for (i = 0; i < length; i++)
752 			reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO);
753 	}
754 
755 	return 0;
756 exit:
757 
758 	return result;
759 }
760 
stb0899_wait_diseqc_txidle(struct stb0899_state * state,int timeout)761 static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout)
762 {
763 	u8 reg = 0;
764 	unsigned long start = jiffies;
765 
766 	while (!STB0899_GETFIELD(TXIDLE, reg)) {
767 		reg = stb0899_read_reg(state, STB0899_DISSTATUS);
768 		if (time_after(jiffies, start + timeout)) {
769 			dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
770 			return -ETIMEDOUT;
771 		}
772 		msleep(10);
773 	}
774 	return 0;
775 }
776 
stb0899_send_diseqc_burst(struct dvb_frontend * fe,enum fe_sec_mini_cmd burst)777 static int stb0899_send_diseqc_burst(struct dvb_frontend *fe,
778 				     enum fe_sec_mini_cmd burst)
779 {
780 	struct stb0899_state *state = fe->demodulator_priv;
781 	u8 reg, old_state;
782 
783 	/* wait for diseqc idle	*/
784 	if (stb0899_wait_diseqc_txidle(state, 100) < 0)
785 		return -ETIMEDOUT;
786 
787 	reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
788 	old_state = reg;
789 	/* set to burst mode	*/
790 	STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x03);
791 	STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x01);
792 	stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
793 	switch (burst) {
794 	case SEC_MINI_A:
795 		/* unmodulated	*/
796 		stb0899_write_reg(state, STB0899_DISFIFO, 0x00);
797 		break;
798 	case SEC_MINI_B:
799 		/* modulated	*/
800 		stb0899_write_reg(state, STB0899_DISFIFO, 0xff);
801 		break;
802 	}
803 	reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
804 	STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x00);
805 	stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
806 	/* wait for diseqc idle	*/
807 	if (stb0899_wait_diseqc_txidle(state, 100) < 0)
808 		return -ETIMEDOUT;
809 
810 	/* restore state	*/
811 	stb0899_write_reg(state, STB0899_DISCNTRL1, old_state);
812 
813 	return 0;
814 }
815 
stb0899_diseqc_init(struct stb0899_state * state)816 static int stb0899_diseqc_init(struct stb0899_state *state)
817 {
818 /*
819 	struct dvb_diseqc_slave_reply rx_data;
820 */
821 	u8 f22_tx, reg;
822 
823 	u32 mclk, tx_freq = 22000;/* count = 0, i; */
824 	reg = stb0899_read_reg(state, STB0899_DISCNTRL2);
825 	STB0899_SETFIELD_VAL(ONECHIP_TRX, reg, 0);
826 	stb0899_write_reg(state, STB0899_DISCNTRL2, reg);
827 
828 	/* disable Tx spy	*/
829 	reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
830 	STB0899_SETFIELD_VAL(DISEQCRESET, reg, 1);
831 	stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
832 
833 	reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
834 	STB0899_SETFIELD_VAL(DISEQCRESET, reg, 0);
835 	stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
836 
837 	mclk = stb0899_get_mclk(state);
838 	f22_tx = mclk / (tx_freq * 32);
839 	stb0899_write_reg(state, STB0899_DISF22, f22_tx); /* DiSEqC Tx freq	*/
840 	state->rx_freq = 20000;
841 
842 	return 0;
843 }
844 
stb0899_sleep(struct dvb_frontend * fe)845 static int stb0899_sleep(struct dvb_frontend *fe)
846 {
847 	struct stb0899_state *state = fe->demodulator_priv;
848 /*
849 	u8 reg;
850 */
851 	dprintk(state->verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))");
852 	/* post process event */
853 	stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
854 
855 	return 0;
856 }
857 
stb0899_wakeup(struct dvb_frontend * fe)858 static int stb0899_wakeup(struct dvb_frontend *fe)
859 {
860 	int rc;
861 	struct stb0899_state *state = fe->demodulator_priv;
862 
863 	if ((rc = stb0899_write_reg(state, STB0899_SYNTCTRL, STB0899_SELOSCI)))
864 		return rc;
865 	/* Activate all clocks; DVB-S2 registers are inaccessible otherwise. */
866 	if ((rc = stb0899_write_reg(state, STB0899_STOPCLK1, 0x00)))
867 		return rc;
868 	if ((rc = stb0899_write_reg(state, STB0899_STOPCLK2, 0x00)))
869 		return rc;
870 
871 	/* post process event */
872 	stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 1);
873 
874 	return 0;
875 }
876 
stb0899_init(struct dvb_frontend * fe)877 static int stb0899_init(struct dvb_frontend *fe)
878 {
879 	int i;
880 	struct stb0899_state *state = fe->demodulator_priv;
881 	struct stb0899_config *config = state->config;
882 
883 	dprintk(state->verbose, FE_DEBUG, 1, "Initializing STB0899 ... ");
884 
885 	/* init device		*/
886 	dprintk(state->verbose, FE_DEBUG, 1, "init device");
887 	for (i = 0; config->init_dev[i].address != 0xffff; i++)
888 		stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data);
889 
890 	dprintk(state->verbose, FE_DEBUG, 1, "init S2 demod");
891 	/* init S2 demod	*/
892 	for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++)
893 		stb0899_write_s2reg(state, STB0899_S2DEMOD,
894 				    config->init_s2_demod[i].base_address,
895 				    config->init_s2_demod[i].offset,
896 				    config->init_s2_demod[i].data);
897 
898 	dprintk(state->verbose, FE_DEBUG, 1, "init S1 demod");
899 	/* init S1 demod	*/
900 	for (i = 0; config->init_s1_demod[i].address != 0xffff; i++)
901 		stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data);
902 
903 	dprintk(state->verbose, FE_DEBUG, 1, "init S2 FEC");
904 	/* init S2 fec		*/
905 	for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++)
906 		stb0899_write_s2reg(state, STB0899_S2FEC,
907 				    config->init_s2_fec[i].base_address,
908 				    config->init_s2_fec[i].offset,
909 				    config->init_s2_fec[i].data);
910 
911 	dprintk(state->verbose, FE_DEBUG, 1, "init TST");
912 	/* init test		*/
913 	for (i = 0; config->init_tst[i].address != 0xffff; i++)
914 		stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data);
915 
916 	stb0899_init_calc(state);
917 	stb0899_diseqc_init(state);
918 
919 	return 0;
920 }
921 
stb0899_table_lookup(const struct stb0899_tab * tab,int max,int val)922 static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val)
923 {
924 	int res = 0;
925 	int min = 0, med;
926 
927 	if (val < tab[min].read)
928 		res = tab[min].real;
929 	else if (val >= tab[max].read)
930 		res = tab[max].real;
931 	else {
932 		while ((max - min) > 1) {
933 			med = (max + min) / 2;
934 			if (val >= tab[min].read && val < tab[med].read)
935 				max = med;
936 			else
937 				min = med;
938 		}
939 		res = ((val - tab[min].read) *
940 		       (tab[max].real - tab[min].real) /
941 		       (tab[max].read - tab[min].read)) +
942 			tab[min].real;
943 	}
944 
945 	return res;
946 }
947 
stb0899_read_signal_strength(struct dvb_frontend * fe,u16 * strength)948 static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
949 {
950 	struct stb0899_state *state		= fe->demodulator_priv;
951 	struct stb0899_internal *internal	= &state->internal;
952 
953 	int val;
954 	u32 reg;
955 	*strength = 0;
956 	switch (state->delsys) {
957 	case SYS_DVBS:
958 	case SYS_DSS:
959 		if (internal->lock) {
960 			reg  = stb0899_read_reg(state, STB0899_VSTATUS);
961 			if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
962 
963 				reg = stb0899_read_reg(state, STB0899_AGCIQIN);
964 				val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg);
965 
966 				*strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val);
967 				*strength += 750;
968 				dprintk(state->verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm",
969 					val & 0xff, *strength);
970 			}
971 		}
972 		break;
973 	case SYS_DVBS2:
974 		if (internal->lock) {
975 			reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_GAIN);
976 			val = STB0899_GETFIELD(IF_AGC_GAIN, reg);
977 
978 			*strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val);
979 			*strength += 950;
980 			dprintk(state->verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm",
981 				val & 0x3fff, *strength);
982 		}
983 		break;
984 	default:
985 		dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
986 		return -EINVAL;
987 	}
988 
989 	return 0;
990 }
991 
stb0899_read_snr(struct dvb_frontend * fe,u16 * snr)992 static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr)
993 {
994 	struct stb0899_state *state		= fe->demodulator_priv;
995 	struct stb0899_internal *internal	= &state->internal;
996 
997 	unsigned int val, quant, quantn = -1, est, estn = -1;
998 	u8 buf[2];
999 	u32 reg;
1000 
1001 	*snr = 0;
1002 	reg  = stb0899_read_reg(state, STB0899_VSTATUS);
1003 	switch (state->delsys) {
1004 	case SYS_DVBS:
1005 	case SYS_DSS:
1006 		if (internal->lock) {
1007 			if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1008 
1009 				stb0899_read_regs(state, STB0899_NIRM, buf, 2);
1010 				val = MAKEWORD16(buf[0], buf[1]);
1011 
1012 				*snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val);
1013 				dprintk(state->verbose, FE_DEBUG, 1, "NIR = 0x%02x%02x = %u, C/N = %d * 0.1 dBm\n",
1014 					buf[0], buf[1], val, *snr);
1015 			}
1016 		}
1017 		break;
1018 	case SYS_DVBS2:
1019 		if (internal->lock) {
1020 			reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
1021 			quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg);
1022 			reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
1023 			est = STB0899_GETFIELD(ESN0_EST, reg);
1024 			if (est == 1)
1025 				val = 301; /* C/N = 30.1 dB */
1026 			else if (est == 2)
1027 				val = 270; /* C/N = 27.0 dB */
1028 			else {
1029 				/* quantn = 100 * log(quant^2) */
1030 				quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100);
1031 				/* estn = 100 * log(est) */
1032 				estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est);
1033 				/* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */
1034 				val = (quantn - estn) / 10;
1035 			}
1036 			*snr = val;
1037 			dprintk(state->verbose, FE_DEBUG, 1, "Es/N0 quant = %d (%d) estimate = %u (%d), C/N = %d * 0.1 dBm",
1038 				quant, quantn, est, estn, val);
1039 		}
1040 		break;
1041 	default:
1042 		dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1043 		return -EINVAL;
1044 	}
1045 
1046 	return 0;
1047 }
1048 
stb0899_read_status(struct dvb_frontend * fe,enum fe_status * status)1049 static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status)
1050 {
1051 	struct stb0899_state *state		= fe->demodulator_priv;
1052 	struct stb0899_internal *internal	= &state->internal;
1053 	u8 reg;
1054 	*status = 0;
1055 
1056 	switch (state->delsys) {
1057 	case SYS_DVBS:
1058 	case SYS_DSS:
1059 		dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS");
1060 		if (internal->lock) {
1061 			reg  = stb0899_read_reg(state, STB0899_VSTATUS);
1062 			if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1063 				dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK");
1064 				*status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_LOCK;
1065 
1066 				reg = stb0899_read_reg(state, STB0899_PLPARM);
1067 				if (STB0899_GETFIELD(VITCURPUN, reg)) {
1068 					dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC");
1069 					*status |= FE_HAS_VITERBI | FE_HAS_SYNC;
1070 					/* post process event */
1071 					stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
1072 				}
1073 			}
1074 		}
1075 		break;
1076 	case SYS_DVBS2:
1077 		dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2");
1078 		if (internal->lock) {
1079 			reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
1080 			if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) {
1081 				*status |= FE_HAS_CARRIER;
1082 				dprintk(state->verbose, FE_DEBUG, 1,
1083 					"UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER");
1084 
1085 				reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
1086 				if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) {
1087 					*status |= FE_HAS_LOCK;
1088 					dprintk(state->verbose, FE_DEBUG, 1,
1089 						"Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK");
1090 
1091 				}
1092 				if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) {
1093 					*status |= FE_HAS_VITERBI;
1094 					dprintk(state->verbose, FE_DEBUG, 1,
1095 						"Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI");
1096 				}
1097 				if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) {
1098 					*status |= FE_HAS_SYNC;
1099 					dprintk(state->verbose, FE_DEBUG, 1,
1100 						"Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC");
1101 					/* post process event */
1102 					stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
1103 				}
1104 			}
1105 		}
1106 		break;
1107 	default:
1108 		dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1109 		return -EINVAL;
1110 	}
1111 	return 0;
1112 }
1113 
1114 /*
1115  * stb0899_get_error
1116  * viterbi error for DVB-S/DSS
1117  * packet error for DVB-S2
1118  * Bit Error Rate or Packet Error Rate * 10 ^ 7
1119  */
stb0899_read_ber(struct dvb_frontend * fe,u32 * ber)1120 static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber)
1121 {
1122 	struct stb0899_state *state		= fe->demodulator_priv;
1123 	struct stb0899_internal *internal	= &state->internal;
1124 
1125 	u8  lsb, msb;
1126 
1127 	*ber = 0;
1128 
1129 	switch (state->delsys) {
1130 	case SYS_DVBS:
1131 	case SYS_DSS:
1132 		if (internal->lock) {
1133 			lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1134 			msb = stb0899_read_reg(state, STB0899_ECNT1M);
1135 			*ber = MAKEWORD16(msb, lsb);
1136 			/* Viterbi Check	*/
1137 			if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) {
1138 				/* Error Rate		*/
1139 				*ber *= 9766;
1140 				/* ber = ber * 10 ^ 7	*/
1141 				*ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1142 				*ber /= 8;
1143 			}
1144 		}
1145 		break;
1146 	case SYS_DVBS2:
1147 		if (internal->lock) {
1148 			lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1149 			msb = stb0899_read_reg(state, STB0899_ECNT1M);
1150 			*ber = MAKEWORD16(msb, lsb);
1151 			/* ber = ber * 10 ^ 7	*/
1152 			*ber *= 10000000;
1153 			*ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1154 		}
1155 		break;
1156 	default:
1157 		dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1158 		return -EINVAL;
1159 	}
1160 
1161 	return 0;
1162 }
1163 
stb0899_set_voltage(struct dvb_frontend * fe,enum fe_sec_voltage voltage)1164 static int stb0899_set_voltage(struct dvb_frontend *fe,
1165 			       enum fe_sec_voltage voltage)
1166 {
1167 	struct stb0899_state *state = fe->demodulator_priv;
1168 
1169 	switch (voltage) {
1170 	case SEC_VOLTAGE_13:
1171 		stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1172 		stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1173 		stb0899_write_reg(state, STB0899_GPIO02CFG, 0x00);
1174 		break;
1175 	case SEC_VOLTAGE_18:
1176 		stb0899_write_reg(state, STB0899_GPIO00CFG, 0x02);
1177 		stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1178 		stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1179 		break;
1180 	case SEC_VOLTAGE_OFF:
1181 		stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1182 		stb0899_write_reg(state, STB0899_GPIO01CFG, 0x82);
1183 		stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1184 		break;
1185 	default:
1186 		return -EINVAL;
1187 	}
1188 
1189 	return 0;
1190 }
1191 
stb0899_set_tone(struct dvb_frontend * fe,enum fe_sec_tone_mode tone)1192 static int stb0899_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
1193 {
1194 	struct stb0899_state *state = fe->demodulator_priv;
1195 	struct stb0899_internal *internal = &state->internal;
1196 
1197 	u8 div, reg;
1198 
1199 	/* wait for diseqc idle	*/
1200 	if (stb0899_wait_diseqc_txidle(state, 100) < 0)
1201 		return -ETIMEDOUT;
1202 
1203 	switch (tone) {
1204 	case SEC_TONE_ON:
1205 		div = (internal->master_clk / 100) / 5632;
1206 		div = (div + 5) / 10;
1207 		stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x66);
1208 		reg = stb0899_read_reg(state, STB0899_ACRPRESC);
1209 		STB0899_SETFIELD_VAL(ACRPRESC, reg, 0x03);
1210 		stb0899_write_reg(state, STB0899_ACRPRESC, reg);
1211 		stb0899_write_reg(state, STB0899_ACRDIV1, div);
1212 		break;
1213 	case SEC_TONE_OFF:
1214 		stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x20);
1215 		break;
1216 	default:
1217 		return -EINVAL;
1218 	}
1219 	return 0;
1220 }
1221 
stb0899_i2c_gate_ctrl(struct dvb_frontend * fe,int enable)1222 int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
1223 {
1224 	int i2c_stat;
1225 	struct stb0899_state *state = fe->demodulator_priv;
1226 
1227 	i2c_stat = stb0899_read_reg(state, STB0899_I2CRPT);
1228 	if (i2c_stat < 0)
1229 		goto err;
1230 
1231 	if (enable) {
1232 		dprintk(state->verbose, FE_DEBUG, 1, "Enabling I2C Repeater ...");
1233 		i2c_stat |=  STB0899_I2CTON;
1234 		if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1235 			goto err;
1236 	} else {
1237 		dprintk(state->verbose, FE_DEBUG, 1, "Disabling I2C Repeater ...");
1238 		i2c_stat &= ~STB0899_I2CTON;
1239 		if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1240 			goto err;
1241 	}
1242 	return 0;
1243 err:
1244 	dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater control failed");
1245 	return -EREMOTEIO;
1246 }
1247 
1248 
CONVERT32(u32 x,char * str)1249 static inline void CONVERT32(u32 x, char *str)
1250 {
1251 	*str++	= (x >> 24) & 0xff;
1252 	*str++	= (x >> 16) & 0xff;
1253 	*str++	= (x >>  8) & 0xff;
1254 	*str++	= (x >>  0) & 0xff;
1255 	*str	= '\0';
1256 }
1257 
stb0899_get_dev_id(struct stb0899_state * state)1258 static int stb0899_get_dev_id(struct stb0899_state *state)
1259 {
1260 	u8 chip_id, release;
1261 	u16 id;
1262 	u32 demod_ver = 0, fec_ver = 0;
1263 	char demod_str[5] = { 0 };
1264 	char fec_str[5] = { 0 };
1265 
1266 	id = stb0899_read_reg(state, STB0899_DEV_ID);
1267 	dprintk(state->verbose, FE_DEBUG, 1, "ID reg=[0x%02x]", id);
1268 	chip_id = STB0899_GETFIELD(CHIP_ID, id);
1269 	release = STB0899_GETFIELD(CHIP_REL, id);
1270 
1271 	dprintk(state->verbose, FE_ERROR, 1, "Device ID=[%d], Release=[%d]",
1272 		chip_id, release);
1273 
1274 	CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CORE_ID), (char *)&demod_str);
1275 
1276 	demod_ver = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_VERSION_ID);
1277 	dprintk(state->verbose, FE_ERROR, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str, demod_ver);
1278 	CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC, FEC_CORE_ID_REG), (char *)&fec_str);
1279 	fec_ver = STB0899_READ_S2REG(STB0899_S2FEC, FEC_VER_ID_REG);
1280 	if (! (chip_id > 0)) {
1281 		dprintk(state->verbose, FE_ERROR, 1, "couldn't find a STB 0899");
1282 
1283 		return -ENODEV;
1284 	}
1285 	dprintk(state->verbose, FE_ERROR, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str, fec_ver);
1286 
1287 	return 0;
1288 }
1289 
stb0899_set_delivery(struct stb0899_state * state)1290 static void stb0899_set_delivery(struct stb0899_state *state)
1291 {
1292 	u8 reg;
1293 	u8 stop_clk[2];
1294 
1295 	stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1);
1296 	stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2);
1297 
1298 	switch (state->delsys) {
1299 	case SYS_DVBS:
1300 		dprintk(state->verbose, FE_DEBUG, 1, "Delivery System -- DVB-S");
1301 		/* FECM/Viterbi ON	*/
1302 		reg = stb0899_read_reg(state, STB0899_FECM);
1303 		STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1304 		STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1305 		stb0899_write_reg(state, STB0899_FECM, reg);
1306 
1307 		stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1308 		stb0899_write_reg(state, STB0899_TSULC, 0x40);
1309 		stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1310 		stb0899_write_reg(state, STB0899_TSLPL, 0x12);
1311 
1312 		reg = stb0899_read_reg(state, STB0899_TSTRES);
1313 		STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1314 		stb0899_write_reg(state, STB0899_TSTRES, reg);
1315 
1316 		STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1317 		STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1318 		STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1319 
1320 		STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1321 		STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1322 
1323 		STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1);
1324 		STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1325 
1326 		STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1327 		break;
1328 	case SYS_DVBS2:
1329 		/* FECM/Viterbi OFF	*/
1330 		reg = stb0899_read_reg(state, STB0899_FECM);
1331 		STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1332 		STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0);
1333 		stb0899_write_reg(state, STB0899_FECM, reg);
1334 
1335 		stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1336 		stb0899_write_reg(state, STB0899_TSULC, 0x42);
1337 		stb0899_write_reg(state, STB0899_RSLLC, 0x40);
1338 		stb0899_write_reg(state, STB0899_TSLPL, 0x02);
1339 
1340 		reg = stb0899_read_reg(state, STB0899_TSTRES);
1341 		STB0899_SETFIELD_VAL(FRESLDPC, reg, 0);
1342 		stb0899_write_reg(state, STB0899_TSTRES, reg);
1343 
1344 		STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1345 		STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 0);
1346 		STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0);
1347 
1348 		STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0);
1349 		STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0);
1350 
1351 		STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0);
1352 		STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1353 
1354 		STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0);
1355 		break;
1356 	case SYS_DSS:
1357 		/* FECM/Viterbi ON	*/
1358 		reg = stb0899_read_reg(state, STB0899_FECM);
1359 		STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1);
1360 		STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1361 		stb0899_write_reg(state, STB0899_FECM, reg);
1362 
1363 		stb0899_write_reg(state, STB0899_RSULC, 0xa1);
1364 		stb0899_write_reg(state, STB0899_TSULC, 0x61);
1365 		stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1366 
1367 		reg = stb0899_read_reg(state, STB0899_TSTRES);
1368 		STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1369 		stb0899_write_reg(state, STB0899_TSTRES, reg);
1370 
1371 		STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1372 		STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1373 		STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1374 
1375 		STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1376 		STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1377 
1378 		STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1379 
1380 		STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1381 		break;
1382 	default:
1383 		dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
1384 		break;
1385 	}
1386 	STB0899_SETFIELD_VAL(STOP_CKADCI108, stop_clk[0], 0);
1387 	stb0899_write_regs(state, STB0899_STOPCLK1, stop_clk, 2);
1388 }
1389 
1390 /*
1391  * stb0899_set_iterations
1392  * set the LDPC iteration scale function
1393  */
stb0899_set_iterations(struct stb0899_state * state)1394 static void stb0899_set_iterations(struct stb0899_state *state)
1395 {
1396 	struct stb0899_internal *internal = &state->internal;
1397 	struct stb0899_config *config = state->config;
1398 
1399 	s32 iter_scale;
1400 	u32 reg;
1401 
1402 	iter_scale = 17 * (internal->master_clk / 1000);
1403 	iter_scale += 410000;
1404 	iter_scale /= (internal->srate / 1000000);
1405 	iter_scale /= 1000;
1406 
1407 	if (iter_scale > config->ldpc_max_iter)
1408 		iter_scale = config->ldpc_max_iter;
1409 
1410 	reg = STB0899_READ_S2REG(STB0899_S2FEC, MAX_ITER);
1411 	STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale);
1412 	stb0899_write_s2reg(state, STB0899_S2FEC, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg);
1413 }
1414 
stb0899_search(struct dvb_frontend * fe)1415 static enum dvbfe_search stb0899_search(struct dvb_frontend *fe)
1416 {
1417 	struct stb0899_state *state = fe->demodulator_priv;
1418 	struct stb0899_params *i_params = &state->params;
1419 	struct stb0899_internal *internal = &state->internal;
1420 	struct stb0899_config *config = state->config;
1421 	struct dtv_frontend_properties *props = &fe->dtv_property_cache;
1422 
1423 	u32 SearchRange, gain;
1424 
1425 	i_params->freq	= props->frequency;
1426 	i_params->srate = props->symbol_rate;
1427 	state->delsys = props->delivery_system;
1428 	dprintk(state->verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
1429 
1430 	SearchRange = 10000000;
1431 	dprintk(state->verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate);
1432 	/* checking Search Range is meaningless for a fixed 3 Mhz			*/
1433 	if (INRANGE(i_params->srate, 1000000, 45000000)) {
1434 		dprintk(state->verbose, FE_DEBUG, 1, "Parameters IN RANGE");
1435 		stb0899_set_delivery(state);
1436 
1437 		if (state->config->tuner_set_rfsiggain) {
1438 			if (internal->srate > 15000000)
1439 				gain =  8; /* 15Mb < srate < 45Mb, gain = 8dB	*/
1440 			else if (internal->srate > 5000000)
1441 				gain = 12; /*  5Mb < srate < 15Mb, gain = 12dB	*/
1442 			else
1443 				gain = 14; /*  1Mb < srate <  5Mb, gain = 14db	*/
1444 			state->config->tuner_set_rfsiggain(fe, gain);
1445 		}
1446 
1447 		if (i_params->srate <= 5000000)
1448 			stb0899_set_mclk(state, config->lo_clk);
1449 		else
1450 			stb0899_set_mclk(state, config->hi_clk);
1451 
1452 		switch (state->delsys) {
1453 		case SYS_DVBS:
1454 		case SYS_DSS:
1455 			dprintk(state->verbose, FE_DEBUG, 1, "DVB-S delivery system");
1456 			internal->freq	= i_params->freq;
1457 			internal->srate	= i_params->srate;
1458 			/*
1459 			 * search = user search range +
1460 			 *	    500Khz +
1461 			 *	    2 * Tuner_step_size +
1462 			 *	    10% of the symbol rate
1463 			 */
1464 			internal->srch_range	= SearchRange + 1500000 + (i_params->srate / 5);
1465 			internal->derot_percent	= 30;
1466 
1467 			/* What to do for tuners having no bandwidth setup ?	*/
1468 			/* enable tuner I/O */
1469 			stb0899_i2c_gate_ctrl(&state->frontend, 1);
1470 
1471 			if (state->config->tuner_set_bandwidth)
1472 				state->config->tuner_set_bandwidth(fe, (13 * (stb0899_carr_width(state) + SearchRange)) / 10);
1473 			if (state->config->tuner_get_bandwidth)
1474 				state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1475 
1476 			/* disable tuner I/O */
1477 			stb0899_i2c_gate_ctrl(&state->frontend, 0);
1478 
1479 			/* Set DVB-S1 AGC		*/
1480 			stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11);
1481 
1482 			/* Run the search algorithm	*/
1483 			dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S search algo ..");
1484 			if (stb0899_dvbs_algo(state)	== RANGEOK) {
1485 				internal->lock		= 1;
1486 				dprintk(state->verbose, FE_DEBUG, 1,
1487 					"-------------------------------------> DVB-S LOCK !");
1488 
1489 //				stb0899_write_reg(state, STB0899_ERRCTRL1, 0x3d); /* Viterbi Errors	*/
1490 //				internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1491 //				internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1492 //				dprintk(state->verbose, FE_DEBUG, 1, "VSTATUS=0x%02x", internal->v_status);
1493 //				dprintk(state->verbose, FE_DEBUG, 1, "ERR_CTRL=0x%02x", internal->err_ctrl);
1494 
1495 				return DVBFE_ALGO_SEARCH_SUCCESS;
1496 			} else {
1497 				internal->lock		= 0;
1498 
1499 				return DVBFE_ALGO_SEARCH_FAILED;
1500 			}
1501 			break;
1502 		case SYS_DVBS2:
1503 			internal->freq			= i_params->freq;
1504 			internal->srate			= i_params->srate;
1505 			internal->srch_range		= SearchRange;
1506 
1507 			/* enable tuner I/O */
1508 			stb0899_i2c_gate_ctrl(&state->frontend, 1);
1509 
1510 			if (state->config->tuner_set_bandwidth)
1511 				state->config->tuner_set_bandwidth(fe, (stb0899_carr_width(state) + SearchRange));
1512 			if (state->config->tuner_get_bandwidth)
1513 				state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1514 
1515 			/* disable tuner I/O */
1516 			stb0899_i2c_gate_ctrl(&state->frontend, 0);
1517 
1518 //			pParams->SpectralInv		= pSearch->IQ_Inversion;
1519 
1520 			/* Set DVB-S2 AGC		*/
1521 			stb0899_write_reg(state, STB0899_AGCRFCFG, 0x1c);
1522 
1523 			/* Set IterScale =f(MCLK,SYMB)	*/
1524 			stb0899_set_iterations(state);
1525 
1526 			/* Run the search algorithm	*/
1527 			dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S2 search algo ..");
1528 			if (stb0899_dvbs2_algo(state)	== DVBS2_FEC_LOCK) {
1529 				internal->lock		= 1;
1530 				dprintk(state->verbose, FE_DEBUG, 1,
1531 					"-------------------------------------> DVB-S2 LOCK !");
1532 
1533 //				stb0899_write_reg(state, STB0899_ERRCTRL1, 0xb6); /* Packet Errors	*/
1534 //				internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1535 //				internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1536 
1537 				return DVBFE_ALGO_SEARCH_SUCCESS;
1538 			} else {
1539 				internal->lock		= 0;
1540 
1541 				return DVBFE_ALGO_SEARCH_FAILED;
1542 			}
1543 			break;
1544 		default:
1545 			dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
1546 			return DVBFE_ALGO_SEARCH_INVALID;
1547 		}
1548 	}
1549 
1550 	return DVBFE_ALGO_SEARCH_ERROR;
1551 }
1552 
stb0899_get_frontend(struct dvb_frontend * fe,struct dtv_frontend_properties * p)1553 static int stb0899_get_frontend(struct dvb_frontend *fe,
1554 				struct dtv_frontend_properties *p)
1555 {
1556 	struct stb0899_state *state		= fe->demodulator_priv;
1557 	struct stb0899_internal *internal	= &state->internal;
1558 
1559 	dprintk(state->verbose, FE_DEBUG, 1, "Get params");
1560 	p->symbol_rate = internal->srate;
1561 	p->frequency = internal->freq;
1562 
1563 	return 0;
1564 }
1565 
stb0899_frontend_algo(struct dvb_frontend * fe)1566 static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe)
1567 {
1568 	return DVBFE_ALGO_CUSTOM;
1569 }
1570 
1571 static const struct dvb_frontend_ops stb0899_ops = {
1572 	.delsys = { SYS_DVBS, SYS_DVBS2, SYS_DSS },
1573 	.info = {
1574 		.name			= "STB0899 Multistandard",
1575 		.frequency_min_hz	=  950 * MHz,
1576 		.frequency_max_hz	= 2150 * MHz,
1577 		.symbol_rate_min	=  5000000,
1578 		.symbol_rate_max	= 45000000,
1579 
1580 		.caps			= FE_CAN_INVERSION_AUTO	|
1581 					  FE_CAN_FEC_AUTO	|
1582 					  FE_CAN_2G_MODULATION	|
1583 					  FE_CAN_QPSK
1584 	},
1585 
1586 	.detach				= stb0899_detach,
1587 	.release			= stb0899_release,
1588 	.init				= stb0899_init,
1589 	.sleep				= stb0899_sleep,
1590 //	.wakeup				= stb0899_wakeup,
1591 
1592 	.i2c_gate_ctrl			= stb0899_i2c_gate_ctrl,
1593 
1594 	.get_frontend_algo		= stb0899_frontend_algo,
1595 	.search				= stb0899_search,
1596 	.get_frontend                   = stb0899_get_frontend,
1597 
1598 
1599 	.read_status			= stb0899_read_status,
1600 	.read_snr			= stb0899_read_snr,
1601 	.read_signal_strength		= stb0899_read_signal_strength,
1602 	.read_ber			= stb0899_read_ber,
1603 
1604 	.set_voltage			= stb0899_set_voltage,
1605 	.set_tone			= stb0899_set_tone,
1606 
1607 	.diseqc_send_master_cmd		= stb0899_send_diseqc_msg,
1608 	.diseqc_recv_slave_reply	= stb0899_recv_slave_reply,
1609 	.diseqc_send_burst		= stb0899_send_diseqc_burst,
1610 };
1611 
stb0899_attach(struct stb0899_config * config,struct i2c_adapter * i2c)1612 struct dvb_frontend *stb0899_attach(struct stb0899_config *config, struct i2c_adapter *i2c)
1613 {
1614 	struct stb0899_state *state = NULL;
1615 
1616 	state = kzalloc(sizeof (struct stb0899_state), GFP_KERNEL);
1617 	if (state == NULL)
1618 		goto error;
1619 
1620 	state->verbose				= &verbose;
1621 	state->config				= config;
1622 	state->i2c				= i2c;
1623 	state->frontend.ops			= stb0899_ops;
1624 	state->frontend.demodulator_priv	= state;
1625 	/* use configured inversion as default -- we'll later autodetect inversion */
1626 	state->internal.inversion		= config->inversion;
1627 
1628 	stb0899_wakeup(&state->frontend);
1629 	if (stb0899_get_dev_id(state) == -ENODEV) {
1630 		printk("%s: Exiting .. !\n", __func__);
1631 		goto error;
1632 	}
1633 
1634 	printk("%s: Attaching STB0899 \n", __func__);
1635 	return &state->frontend;
1636 
1637 error:
1638 	kfree(state);
1639 	return NULL;
1640 }
1641 EXPORT_SYMBOL(stb0899_attach);
1642 MODULE_PARM_DESC(verbose, "Set Verbosity level");
1643 MODULE_AUTHOR("Manu Abraham");
1644 MODULE_DESCRIPTION("STB0899 Multi-Std frontend");
1645 MODULE_LICENSE("GPL");
1646