xref: /linux/drivers/media/dvb-frontends/stv0900_core.c (revision ead5d1f4d877e92c051e1a1ade623d0d30e71619)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
299277b38SIgor M. Liplianin /*
399277b38SIgor M. Liplianin  * stv0900_core.c
499277b38SIgor M. Liplianin  *
599277b38SIgor M. Liplianin  * Driver for ST STV0900 satellite demodulator IC.
699277b38SIgor M. Liplianin  *
799277b38SIgor M. Liplianin  * Copyright (C) ST Microelectronics.
899277b38SIgor M. Liplianin  * Copyright (C) 2009 NetUP Inc.
999277b38SIgor M. Liplianin  * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
1099277b38SIgor M. Liplianin  */
1199277b38SIgor M. Liplianin 
1299277b38SIgor M. Liplianin #include <linux/kernel.h>
1399277b38SIgor M. Liplianin #include <linux/module.h>
1499277b38SIgor M. Liplianin #include <linux/string.h>
1599277b38SIgor M. Liplianin #include <linux/slab.h>
1699277b38SIgor M. Liplianin #include <linux/i2c.h>
1799277b38SIgor M. Liplianin 
1899277b38SIgor M. Liplianin #include "stv0900.h"
1999277b38SIgor M. Liplianin #include "stv0900_reg.h"
2099277b38SIgor M. Liplianin #include "stv0900_priv.h"
2199277b38SIgor M. Liplianin #include "stv0900_init.h"
2299277b38SIgor M. Liplianin 
238171c205SIgor M. Liplianin int stvdebug = 1;
245a771cb1SRandy Dunlap module_param_named(debug, stvdebug, int, 0644);
2599277b38SIgor M. Liplianin 
2699277b38SIgor M. Liplianin /* internal params node */
2799277b38SIgor M. Liplianin struct stv0900_inode {
2899277b38SIgor M. Liplianin 	/* pointer for internal params, one for each pair of demods */
2999277b38SIgor M. Liplianin 	struct stv0900_internal		*internal;
3099277b38SIgor M. Liplianin 	struct stv0900_inode		*next_inode;
3199277b38SIgor M. Liplianin };
3299277b38SIgor M. Liplianin 
3399277b38SIgor M. Liplianin /* first internal params */
3499277b38SIgor M. Liplianin static struct stv0900_inode *stv0900_first_inode;
3599277b38SIgor M. Liplianin 
3699277b38SIgor M. Liplianin /* find chip by i2c adapter and i2c address */
3799277b38SIgor M. Liplianin static struct stv0900_inode *find_inode(struct i2c_adapter *i2c_adap,
3899277b38SIgor M. Liplianin 							u8 i2c_addr)
3999277b38SIgor M. Liplianin {
4099277b38SIgor M. Liplianin 	struct stv0900_inode *temp_chip = stv0900_first_inode;
4199277b38SIgor M. Liplianin 
4299277b38SIgor M. Liplianin 	if (temp_chip != NULL) {
4399277b38SIgor M. Liplianin 		/*
4499277b38SIgor M. Liplianin 		 Search of the last stv0900 chip or
4599277b38SIgor M. Liplianin 		 find it by i2c adapter and i2c address */
4699277b38SIgor M. Liplianin 		while ((temp_chip != NULL) &&
4799277b38SIgor M. Liplianin 			((temp_chip->internal->i2c_adap != i2c_adap) ||
4878175bf2SIgor M. Liplianin 			(temp_chip->internal->i2c_addr != i2c_addr)))
4999277b38SIgor M. Liplianin 
5099277b38SIgor M. Liplianin 			temp_chip = temp_chip->next_inode;
5178175bf2SIgor M. Liplianin 
5299277b38SIgor M. Liplianin 	}
5399277b38SIgor M. Liplianin 
5499277b38SIgor M. Liplianin 	return temp_chip;
5599277b38SIgor M. Liplianin }
5699277b38SIgor M. Liplianin 
5799277b38SIgor M. Liplianin /* deallocating chip */
5899277b38SIgor M. Liplianin static void remove_inode(struct stv0900_internal *internal)
5999277b38SIgor M. Liplianin {
6099277b38SIgor M. Liplianin 	struct stv0900_inode *prev_node = stv0900_first_inode;
6199277b38SIgor M. Liplianin 	struct stv0900_inode *del_node = find_inode(internal->i2c_adap,
6299277b38SIgor M. Liplianin 						internal->i2c_addr);
6399277b38SIgor M. Liplianin 
6499277b38SIgor M. Liplianin 	if (del_node != NULL) {
6599277b38SIgor M. Liplianin 		if (del_node == stv0900_first_inode) {
6699277b38SIgor M. Liplianin 			stv0900_first_inode = del_node->next_inode;
6799277b38SIgor M. Liplianin 		} else {
6899277b38SIgor M. Liplianin 			while (prev_node->next_inode != del_node)
6999277b38SIgor M. Liplianin 				prev_node = prev_node->next_inode;
7099277b38SIgor M. Liplianin 
7199277b38SIgor M. Liplianin 			if (del_node->next_inode == NULL)
7299277b38SIgor M. Liplianin 				prev_node->next_inode = NULL;
7399277b38SIgor M. Liplianin 			else
7499277b38SIgor M. Liplianin 				prev_node->next_inode =
7599277b38SIgor M. Liplianin 					prev_node->next_inode->next_inode;
7699277b38SIgor M. Liplianin 		}
7799277b38SIgor M. Liplianin 
7899277b38SIgor M. Liplianin 		kfree(del_node);
7999277b38SIgor M. Liplianin 	}
8099277b38SIgor M. Liplianin }
8199277b38SIgor M. Liplianin 
8299277b38SIgor M. Liplianin /* allocating new chip */
8399277b38SIgor M. Liplianin static struct stv0900_inode *append_internal(struct stv0900_internal *internal)
8499277b38SIgor M. Liplianin {
8599277b38SIgor M. Liplianin 	struct stv0900_inode *new_node = stv0900_first_inode;
8699277b38SIgor M. Liplianin 
8799277b38SIgor M. Liplianin 	if (new_node == NULL) {
8899277b38SIgor M. Liplianin 		new_node = kmalloc(sizeof(struct stv0900_inode), GFP_KERNEL);
8999277b38SIgor M. Liplianin 		stv0900_first_inode = new_node;
9099277b38SIgor M. Liplianin 	} else {
9199277b38SIgor M. Liplianin 		while (new_node->next_inode != NULL)
9299277b38SIgor M. Liplianin 			new_node = new_node->next_inode;
9399277b38SIgor M. Liplianin 
941e0c397dSIgor M. Liplianin 		new_node->next_inode = kmalloc(sizeof(struct stv0900_inode),
951e0c397dSIgor M. Liplianin 								GFP_KERNEL);
9699277b38SIgor M. Liplianin 		if (new_node->next_inode != NULL)
9799277b38SIgor M. Liplianin 			new_node = new_node->next_inode;
9899277b38SIgor M. Liplianin 		else
9999277b38SIgor M. Liplianin 			new_node = NULL;
10099277b38SIgor M. Liplianin 	}
10199277b38SIgor M. Liplianin 
10299277b38SIgor M. Liplianin 	if (new_node != NULL) {
10399277b38SIgor M. Liplianin 		new_node->internal = internal;
10499277b38SIgor M. Liplianin 		new_node->next_inode = NULL;
10599277b38SIgor M. Liplianin 	}
10699277b38SIgor M. Liplianin 
10799277b38SIgor M. Liplianin 	return new_node;
10899277b38SIgor M. Liplianin }
10999277b38SIgor M. Liplianin 
11099277b38SIgor M. Liplianin s32 ge2comp(s32 a, s32 width)
11199277b38SIgor M. Liplianin {
11299277b38SIgor M. Liplianin 	if (width == 32)
11399277b38SIgor M. Liplianin 		return a;
11499277b38SIgor M. Liplianin 	else
11599277b38SIgor M. Liplianin 		return (a >= (1 << (width - 1))) ? (a - (1 << width)) : a;
11699277b38SIgor M. Liplianin }
11799277b38SIgor M. Liplianin 
1181e0c397dSIgor M. Liplianin void stv0900_write_reg(struct stv0900_internal *intp, u16 reg_addr,
11999277b38SIgor M. Liplianin 								u8 reg_data)
12099277b38SIgor M. Liplianin {
12199277b38SIgor M. Liplianin 	u8 data[3];
12299277b38SIgor M. Liplianin 	int ret;
12399277b38SIgor M. Liplianin 	struct i2c_msg i2cmsg = {
1241e0c397dSIgor M. Liplianin 		.addr  = intp->i2c_addr,
12599277b38SIgor M. Liplianin 		.flags = 0,
12699277b38SIgor M. Liplianin 		.len   = 3,
12799277b38SIgor M. Liplianin 		.buf   = data,
12899277b38SIgor M. Liplianin 	};
12999277b38SIgor M. Liplianin 
13099277b38SIgor M. Liplianin 	data[0] = MSB(reg_addr);
13199277b38SIgor M. Liplianin 	data[1] = LSB(reg_addr);
13299277b38SIgor M. Liplianin 	data[2] = reg_data;
13399277b38SIgor M. Liplianin 
1341e0c397dSIgor M. Liplianin 	ret = i2c_transfer(intp->i2c_adap, &i2cmsg, 1);
13599277b38SIgor M. Liplianin 	if (ret != 1)
1368171c205SIgor M. Liplianin 		dprintk("%s: i2c error %d\n", __func__, ret);
13799277b38SIgor M. Liplianin }
13899277b38SIgor M. Liplianin 
1391e0c397dSIgor M. Liplianin u8 stv0900_read_reg(struct stv0900_internal *intp, u16 reg)
14099277b38SIgor M. Liplianin {
14199277b38SIgor M. Liplianin 	int ret;
14268191edeSAbylay Ospan 	u8 b0[] = { MSB(reg), LSB(reg) };
14368191edeSAbylay Ospan 	u8 buf = 0;
14468191edeSAbylay Ospan 	struct i2c_msg msg[] = {
14568191edeSAbylay Ospan 		{
1461e0c397dSIgor M. Liplianin 			.addr	= intp->i2c_addr,
14799277b38SIgor M. Liplianin 			.flags	= 0,
14868191edeSAbylay Ospan 			.buf = b0,
14999277b38SIgor M. Liplianin 			.len = 2,
15068191edeSAbylay Ospan 		}, {
1511e0c397dSIgor M. Liplianin 			.addr	= intp->i2c_addr,
15268191edeSAbylay Ospan 			.flags	= I2C_M_RD,
15368191edeSAbylay Ospan 			.buf = &buf,
15468191edeSAbylay Ospan 			.len = 1,
15568191edeSAbylay Ospan 		},
15699277b38SIgor M. Liplianin 	};
15799277b38SIgor M. Liplianin 
1581e0c397dSIgor M. Liplianin 	ret = i2c_transfer(intp->i2c_adap, msg, 2);
15968191edeSAbylay Ospan 	if (ret != 2)
1608171c205SIgor M. Liplianin 		dprintk("%s: i2c error %d, reg[0x%02x]\n",
16168191edeSAbylay Ospan 				__func__, ret, reg);
16299277b38SIgor M. Liplianin 
16368191edeSAbylay Ospan 	return buf;
16499277b38SIgor M. Liplianin }
16599277b38SIgor M. Liplianin 
166936c05e7SMárton Németh static void extract_mask_pos(u32 label, u8 *mask, u8 *pos)
16799277b38SIgor M. Liplianin {
16899277b38SIgor M. Liplianin 	u8 position = 0, i = 0;
16999277b38SIgor M. Liplianin 
17099277b38SIgor M. Liplianin 	(*mask) = label & 0xff;
17199277b38SIgor M. Liplianin 
17299277b38SIgor M. Liplianin 	while ((position == 0) && (i < 8)) {
17399277b38SIgor M. Liplianin 		position = ((*mask) >> i) & 0x01;
17499277b38SIgor M. Liplianin 		i++;
17599277b38SIgor M. Liplianin 	}
17699277b38SIgor M. Liplianin 
17799277b38SIgor M. Liplianin 	(*pos) = (i - 1);
17899277b38SIgor M. Liplianin }
17999277b38SIgor M. Liplianin 
1801e0c397dSIgor M. Liplianin void stv0900_write_bits(struct stv0900_internal *intp, u32 label, u8 val)
18199277b38SIgor M. Liplianin {
18299277b38SIgor M. Liplianin 	u8 reg, mask, pos;
18399277b38SIgor M. Liplianin 
1841e0c397dSIgor M. Liplianin 	reg = stv0900_read_reg(intp, (label >> 16) & 0xffff);
18599277b38SIgor M. Liplianin 	extract_mask_pos(label, &mask, &pos);
18699277b38SIgor M. Liplianin 
18799277b38SIgor M. Liplianin 	val = mask & (val << pos);
18899277b38SIgor M. Liplianin 
18999277b38SIgor M. Liplianin 	reg = (reg & (~mask)) | val;
1901e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, (label >> 16) & 0xffff, reg);
19199277b38SIgor M. Liplianin 
19299277b38SIgor M. Liplianin }
19399277b38SIgor M. Liplianin 
1941e0c397dSIgor M. Liplianin u8 stv0900_get_bits(struct stv0900_internal *intp, u32 label)
19599277b38SIgor M. Liplianin {
196*4e054aabSColin Ian King 	u8 val;
19799277b38SIgor M. Liplianin 	u8 mask, pos;
19899277b38SIgor M. Liplianin 
19999277b38SIgor M. Liplianin 	extract_mask_pos(label, &mask, &pos);
20099277b38SIgor M. Liplianin 
2011e0c397dSIgor M. Liplianin 	val = stv0900_read_reg(intp, label >> 16);
20299277b38SIgor M. Liplianin 	val = (val & mask) >> pos;
20399277b38SIgor M. Liplianin 
20499277b38SIgor M. Liplianin 	return val;
20599277b38SIgor M. Liplianin }
20699277b38SIgor M. Liplianin 
207936c05e7SMárton Németh static enum fe_stv0900_error stv0900_initialize(struct stv0900_internal *intp)
20899277b38SIgor M. Liplianin {
20999277b38SIgor M. Liplianin 	s32 i;
21099277b38SIgor M. Liplianin 
2111e0c397dSIgor M. Liplianin 	if (intp == NULL)
2121e0c397dSIgor M. Liplianin 		return STV0900_INVALID_HANDLE;
2131e0c397dSIgor M. Liplianin 
2141e0c397dSIgor M. Liplianin 	intp->chip_id = stv0900_read_reg(intp, R0900_MID);
2151e0c397dSIgor M. Liplianin 
2161e0c397dSIgor M. Liplianin 	if (intp->errs != STV0900_NO_ERROR)
2171e0c397dSIgor M. Liplianin 		return intp->errs;
2181e0c397dSIgor M. Liplianin 
21999277b38SIgor M. Liplianin 	/*Startup sequence*/
2201e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P1_DMDISTATE, 0x5c);
2211e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P2_DMDISTATE, 0x5c);
22299277b38SIgor M. Liplianin 	msleep(3);
2231e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P1_TNRCFG, 0x6c);
2241e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P2_TNRCFG, 0x6f);
2251e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P1_I2CRPT, 0x20);
2261e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P2_I2CRPT, 0x20);
2271e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_NCOARSE, 0x13);
2281e0c397dSIgor M. Liplianin 	msleep(3);
2291e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_I2CCFG, 0x08);
23099277b38SIgor M. Liplianin 
2311e0c397dSIgor M. Liplianin 	switch (intp->clkmode) {
23299277b38SIgor M. Liplianin 	case 0:
23399277b38SIgor M. Liplianin 	case 2:
2341e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_SYNTCTRL, 0x20
2351e0c397dSIgor M. Liplianin 				| intp->clkmode);
23699277b38SIgor M. Liplianin 		break;
23799277b38SIgor M. Liplianin 	default:
23899277b38SIgor M. Liplianin 		/* preserve SELOSCI bit */
2391e0c397dSIgor M. Liplianin 		i = 0x02 & stv0900_read_reg(intp, R0900_SYNTCTRL);
2401e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_SYNTCTRL, 0x20 | i);
24199277b38SIgor M. Liplianin 		break;
24299277b38SIgor M. Liplianin 	}
24399277b38SIgor M. Liplianin 
24499277b38SIgor M. Liplianin 	msleep(3);
2451e0c397dSIgor M. Liplianin 	for (i = 0; i < 181; i++)
2461e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, STV0900_InitVal[i][0],
2471e0c397dSIgor M. Liplianin 				STV0900_InitVal[i][1]);
24899277b38SIgor M. Liplianin 
2491e0c397dSIgor M. Liplianin 	if (stv0900_read_reg(intp, R0900_MID) >= 0x20) {
2501e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_TSGENERAL, 0x0c);
25199277b38SIgor M. Liplianin 		for (i = 0; i < 32; i++)
2521e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, STV0900_Cut20_AddOnVal[i][0],
2531e0c397dSIgor M. Liplianin 					STV0900_Cut20_AddOnVal[i][1]);
25499277b38SIgor M. Liplianin 	}
25599277b38SIgor M. Liplianin 
2561e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P1_FSPYCFG, 0x6c);
2571e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P2_FSPYCFG, 0x6c);
25899277b38SIgor M. Liplianin 
2591e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P1_PDELCTRL2, 0x01);
2601e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P2_PDELCTRL2, 0x21);
26199277b38SIgor M. Liplianin 
2621e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P1_PDELCTRL3, 0x20);
2631e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P2_PDELCTRL3, 0x20);
2641e0c397dSIgor M. Liplianin 
2651e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_TSTRES0, 0x80);
2661e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_TSTRES0, 0x00);
2671e0c397dSIgor M. Liplianin 
2681e0c397dSIgor M. Liplianin 	return STV0900_NO_ERROR;
26999277b38SIgor M. Liplianin }
27099277b38SIgor M. Liplianin 
271936c05e7SMárton Németh static u32 stv0900_get_mclk_freq(struct stv0900_internal *intp, u32 ext_clk)
27299277b38SIgor M. Liplianin {
273247d46b3SColin Ian King 	u32 mclk, div, ad_div;
27499277b38SIgor M. Liplianin 
2751e0c397dSIgor M. Liplianin 	div = stv0900_get_bits(intp, F0900_M_DIV);
2761e0c397dSIgor M. Liplianin 	ad_div = ((stv0900_get_bits(intp, F0900_SELX1RATIO) == 1) ? 4 : 6);
27799277b38SIgor M. Liplianin 
27899277b38SIgor M. Liplianin 	mclk = (div + 1) * ext_clk / ad_div;
27999277b38SIgor M. Liplianin 
2808171c205SIgor M. Liplianin 	dprintk("%s: Calculated Mclk = %d\n", __func__, mclk);
28199277b38SIgor M. Liplianin 
28299277b38SIgor M. Liplianin 	return mclk;
28399277b38SIgor M. Liplianin }
28499277b38SIgor M. Liplianin 
285936c05e7SMárton Németh static enum fe_stv0900_error stv0900_set_mclk(struct stv0900_internal *intp, u32 mclk)
28699277b38SIgor M. Liplianin {
28799277b38SIgor M. Liplianin 	u32 m_div, clk_sel;
28899277b38SIgor M. Liplianin 
2891e0c397dSIgor M. Liplianin 	if (intp == NULL)
2901e0c397dSIgor M. Liplianin 		return STV0900_INVALID_HANDLE;
2911e0c397dSIgor M. Liplianin 
2921e0c397dSIgor M. Liplianin 	if (intp->errs)
2931e0c397dSIgor M. Liplianin 		return STV0900_I2C_ERROR;
2941e0c397dSIgor M. Liplianin 
295b0f9bf36SWei Yongjun 	dprintk("%s: Mclk set to %d, Quartz = %d\n", __func__, mclk,
296b0f9bf36SWei Yongjun 			intp->quartz);
297b0f9bf36SWei Yongjun 
2981e0c397dSIgor M. Liplianin 	clk_sel = ((stv0900_get_bits(intp, F0900_SELX1RATIO) == 1) ? 4 : 6);
2991e0c397dSIgor M. Liplianin 	m_div = ((clk_sel * mclk) / intp->quartz) - 1;
3001e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, F0900_M_DIV, m_div);
3011e0c397dSIgor M. Liplianin 	intp->mclk = stv0900_get_mclk_freq(intp,
3021e0c397dSIgor M. Liplianin 					intp->quartz);
30399277b38SIgor M. Liplianin 
30499277b38SIgor M. Liplianin 	/*Set the DiseqC frequency to 22KHz */
30599277b38SIgor M. Liplianin 	/*
30699277b38SIgor M. Liplianin 		Formula:
30799277b38SIgor M. Liplianin 		DiseqC_TX_Freq= MasterClock/(32*F22TX_Reg)
30899277b38SIgor M. Liplianin 		DiseqC_RX_Freq= MasterClock/(32*F22RX_Reg)
30999277b38SIgor M. Liplianin 	*/
3101e0c397dSIgor M. Liplianin 	m_div = intp->mclk / 704000;
3111e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P1_F22TX, m_div);
3121e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P1_F22RX, m_div);
31399277b38SIgor M. Liplianin 
3141e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P2_F22TX, m_div);
3151e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P2_F22RX, m_div);
31699277b38SIgor M. Liplianin 
3171e0c397dSIgor M. Liplianin 	if ((intp->errs))
3181e0c397dSIgor M. Liplianin 		return STV0900_I2C_ERROR;
3191e0c397dSIgor M. Liplianin 
3201e0c397dSIgor M. Liplianin 	return STV0900_NO_ERROR;
32199277b38SIgor M. Liplianin }
32299277b38SIgor M. Liplianin 
323936c05e7SMárton Németh static u32 stv0900_get_err_count(struct stv0900_internal *intp, int cntr,
32499277b38SIgor M. Liplianin 					enum fe_stv0900_demod_num demod)
32599277b38SIgor M. Liplianin {
32699277b38SIgor M. Liplianin 	u32 lsb, msb, hsb, err_val;
32799277b38SIgor M. Liplianin 
32899277b38SIgor M. Liplianin 	switch (cntr) {
32999277b38SIgor M. Liplianin 	case 0:
33099277b38SIgor M. Liplianin 	default:
3311e0c397dSIgor M. Liplianin 		hsb = stv0900_get_bits(intp, ERR_CNT12);
3321e0c397dSIgor M. Liplianin 		msb = stv0900_get_bits(intp, ERR_CNT11);
3331e0c397dSIgor M. Liplianin 		lsb = stv0900_get_bits(intp, ERR_CNT10);
33499277b38SIgor M. Liplianin 		break;
33599277b38SIgor M. Liplianin 	case 1:
3361e0c397dSIgor M. Liplianin 		hsb = stv0900_get_bits(intp, ERR_CNT22);
3371e0c397dSIgor M. Liplianin 		msb = stv0900_get_bits(intp, ERR_CNT21);
3381e0c397dSIgor M. Liplianin 		lsb = stv0900_get_bits(intp, ERR_CNT20);
33999277b38SIgor M. Liplianin 		break;
34099277b38SIgor M. Liplianin 	}
34199277b38SIgor M. Liplianin 
34299277b38SIgor M. Liplianin 	err_val = (hsb << 16) + (msb << 8) + (lsb);
34399277b38SIgor M. Liplianin 
34499277b38SIgor M. Liplianin 	return err_val;
34599277b38SIgor M. Liplianin }
34699277b38SIgor M. Liplianin 
34799277b38SIgor M. Liplianin static int stv0900_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
34899277b38SIgor M. Liplianin {
34999277b38SIgor M. Liplianin 	struct stv0900_state *state = fe->demodulator_priv;
3501e0c397dSIgor M. Liplianin 	struct stv0900_internal *intp = state->internal;
35199277b38SIgor M. Liplianin 	enum fe_stv0900_demod_num demod = state->demod;
35299277b38SIgor M. Liplianin 
3531e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, I2CT_ON, enable);
35499277b38SIgor M. Liplianin 
35599277b38SIgor M. Liplianin 	return 0;
35699277b38SIgor M. Liplianin }
35799277b38SIgor M. Liplianin 
3581e0c397dSIgor M. Liplianin static void stv0900_set_ts_parallel_serial(struct stv0900_internal *intp,
35999277b38SIgor M. Liplianin 					enum fe_stv0900_clock_type path1_ts,
36099277b38SIgor M. Liplianin 					enum fe_stv0900_clock_type path2_ts)
36199277b38SIgor M. Liplianin {
36299277b38SIgor M. Liplianin 
3638171c205SIgor M. Liplianin 	dprintk("%s\n", __func__);
36499277b38SIgor M. Liplianin 
3651e0c397dSIgor M. Liplianin 	if (intp->chip_id >= 0x20) {
36699277b38SIgor M. Liplianin 		switch (path1_ts) {
36799277b38SIgor M. Liplianin 		case STV0900_PARALLEL_PUNCT_CLOCK:
36899277b38SIgor M. Liplianin 		case STV0900_DVBCI_CLOCK:
36999277b38SIgor M. Liplianin 			switch (path2_ts) {
37099277b38SIgor M. Liplianin 			case STV0900_SERIAL_PUNCT_CLOCK:
37199277b38SIgor M. Liplianin 			case STV0900_SERIAL_CONT_CLOCK:
37299277b38SIgor M. Liplianin 			default:
3731e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp, R0900_TSGENERAL,
37499277b38SIgor M. Liplianin 							0x00);
37599277b38SIgor M. Liplianin 				break;
37699277b38SIgor M. Liplianin 			case STV0900_PARALLEL_PUNCT_CLOCK:
37799277b38SIgor M. Liplianin 			case STV0900_DVBCI_CLOCK:
3781e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp, R0900_TSGENERAL,
37999277b38SIgor M. Liplianin 							0x06);
3801e0c397dSIgor M. Liplianin 				stv0900_write_bits(intp,
38199277b38SIgor M. Liplianin 						F0900_P1_TSFIFO_MANSPEED, 3);
3821e0c397dSIgor M. Liplianin 				stv0900_write_bits(intp,
38399277b38SIgor M. Liplianin 						F0900_P2_TSFIFO_MANSPEED, 0);
3841e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp,
38599277b38SIgor M. Liplianin 						R0900_P1_TSSPEED, 0x14);
3861e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp,
38799277b38SIgor M. Liplianin 						R0900_P2_TSSPEED, 0x28);
38899277b38SIgor M. Liplianin 				break;
38999277b38SIgor M. Liplianin 			}
39099277b38SIgor M. Liplianin 			break;
39199277b38SIgor M. Liplianin 		case STV0900_SERIAL_PUNCT_CLOCK:
39299277b38SIgor M. Liplianin 		case STV0900_SERIAL_CONT_CLOCK:
39399277b38SIgor M. Liplianin 		default:
39499277b38SIgor M. Liplianin 			switch (path2_ts) {
39599277b38SIgor M. Liplianin 			case STV0900_SERIAL_PUNCT_CLOCK:
39699277b38SIgor M. Liplianin 			case STV0900_SERIAL_CONT_CLOCK:
39799277b38SIgor M. Liplianin 			default:
3981e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp,
39999277b38SIgor M. Liplianin 						R0900_TSGENERAL, 0x0C);
40099277b38SIgor M. Liplianin 				break;
40199277b38SIgor M. Liplianin 			case STV0900_PARALLEL_PUNCT_CLOCK:
40299277b38SIgor M. Liplianin 			case STV0900_DVBCI_CLOCK:
4031e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp,
40499277b38SIgor M. Liplianin 						R0900_TSGENERAL, 0x0A);
4058171c205SIgor M. Liplianin 				dprintk("%s: 0x0a\n", __func__);
40699277b38SIgor M. Liplianin 				break;
40799277b38SIgor M. Liplianin 			}
40899277b38SIgor M. Liplianin 			break;
40999277b38SIgor M. Liplianin 		}
41099277b38SIgor M. Liplianin 	} else {
41199277b38SIgor M. Liplianin 		switch (path1_ts) {
41299277b38SIgor M. Liplianin 		case STV0900_PARALLEL_PUNCT_CLOCK:
41399277b38SIgor M. Liplianin 		case STV0900_DVBCI_CLOCK:
41499277b38SIgor M. Liplianin 			switch (path2_ts) {
41599277b38SIgor M. Liplianin 			case STV0900_SERIAL_PUNCT_CLOCK:
41699277b38SIgor M. Liplianin 			case STV0900_SERIAL_CONT_CLOCK:
41799277b38SIgor M. Liplianin 			default:
4181e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp, R0900_TSGENERAL1X,
41999277b38SIgor M. Liplianin 							0x10);
42099277b38SIgor M. Liplianin 				break;
42199277b38SIgor M. Liplianin 			case STV0900_PARALLEL_PUNCT_CLOCK:
42299277b38SIgor M. Liplianin 			case STV0900_DVBCI_CLOCK:
4231e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp, R0900_TSGENERAL1X,
42499277b38SIgor M. Liplianin 							0x16);
4251e0c397dSIgor M. Liplianin 				stv0900_write_bits(intp,
42699277b38SIgor M. Liplianin 						F0900_P1_TSFIFO_MANSPEED, 3);
4271e0c397dSIgor M. Liplianin 				stv0900_write_bits(intp,
42899277b38SIgor M. Liplianin 						F0900_P2_TSFIFO_MANSPEED, 0);
4291e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp, R0900_P1_TSSPEED,
43099277b38SIgor M. Liplianin 							0x14);
4311e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp, R0900_P2_TSSPEED,
43299277b38SIgor M. Liplianin 							0x28);
43399277b38SIgor M. Liplianin 				break;
43499277b38SIgor M. Liplianin 			}
43599277b38SIgor M. Liplianin 
43699277b38SIgor M. Liplianin 			break;
43799277b38SIgor M. Liplianin 		case STV0900_SERIAL_PUNCT_CLOCK:
43899277b38SIgor M. Liplianin 		case STV0900_SERIAL_CONT_CLOCK:
43999277b38SIgor M. Liplianin 		default:
44099277b38SIgor M. Liplianin 			switch (path2_ts) {
44199277b38SIgor M. Liplianin 			case STV0900_SERIAL_PUNCT_CLOCK:
44299277b38SIgor M. Liplianin 			case STV0900_SERIAL_CONT_CLOCK:
44399277b38SIgor M. Liplianin 			default:
4441e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp, R0900_TSGENERAL1X,
44599277b38SIgor M. Liplianin 							0x14);
44699277b38SIgor M. Liplianin 				break;
44799277b38SIgor M. Liplianin 			case STV0900_PARALLEL_PUNCT_CLOCK:
44899277b38SIgor M. Liplianin 			case STV0900_DVBCI_CLOCK:
4491e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp, R0900_TSGENERAL1X,
45099277b38SIgor M. Liplianin 							0x12);
4518171c205SIgor M. Liplianin 				dprintk("%s: 0x12\n", __func__);
45299277b38SIgor M. Liplianin 				break;
45399277b38SIgor M. Liplianin 			}
45499277b38SIgor M. Liplianin 
45599277b38SIgor M. Liplianin 			break;
45699277b38SIgor M. Liplianin 		}
45799277b38SIgor M. Liplianin 	}
45899277b38SIgor M. Liplianin 
45999277b38SIgor M. Liplianin 	switch (path1_ts) {
46099277b38SIgor M. Liplianin 	case STV0900_PARALLEL_PUNCT_CLOCK:
4611e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P1_TSFIFO_SERIAL, 0x00);
4621e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P1_TSFIFO_DVBCI, 0x00);
46399277b38SIgor M. Liplianin 		break;
46499277b38SIgor M. Liplianin 	case STV0900_DVBCI_CLOCK:
4651e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P1_TSFIFO_SERIAL, 0x00);
4661e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P1_TSFIFO_DVBCI, 0x01);
46799277b38SIgor M. Liplianin 		break;
46899277b38SIgor M. Liplianin 	case STV0900_SERIAL_PUNCT_CLOCK:
4691e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P1_TSFIFO_SERIAL, 0x01);
4701e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P1_TSFIFO_DVBCI, 0x00);
47199277b38SIgor M. Liplianin 		break;
47299277b38SIgor M. Liplianin 	case STV0900_SERIAL_CONT_CLOCK:
4731e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P1_TSFIFO_SERIAL, 0x01);
4741e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P1_TSFIFO_DVBCI, 0x01);
47599277b38SIgor M. Liplianin 		break;
47699277b38SIgor M. Liplianin 	default:
47799277b38SIgor M. Liplianin 		break;
47899277b38SIgor M. Liplianin 	}
47999277b38SIgor M. Liplianin 
48099277b38SIgor M. Liplianin 	switch (path2_ts) {
48199277b38SIgor M. Liplianin 	case STV0900_PARALLEL_PUNCT_CLOCK:
4821e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P2_TSFIFO_SERIAL, 0x00);
4831e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P2_TSFIFO_DVBCI, 0x00);
48499277b38SIgor M. Liplianin 		break;
48599277b38SIgor M. Liplianin 	case STV0900_DVBCI_CLOCK:
4861e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P2_TSFIFO_SERIAL, 0x00);
4871e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P2_TSFIFO_DVBCI, 0x01);
48899277b38SIgor M. Liplianin 		break;
48999277b38SIgor M. Liplianin 	case STV0900_SERIAL_PUNCT_CLOCK:
4901e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P2_TSFIFO_SERIAL, 0x01);
4911e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P2_TSFIFO_DVBCI, 0x00);
49299277b38SIgor M. Liplianin 		break;
49399277b38SIgor M. Liplianin 	case STV0900_SERIAL_CONT_CLOCK:
4941e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P2_TSFIFO_SERIAL, 0x01);
4951e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P2_TSFIFO_DVBCI, 0x01);
49699277b38SIgor M. Liplianin 		break;
49799277b38SIgor M. Liplianin 	default:
49899277b38SIgor M. Liplianin 		break;
49999277b38SIgor M. Liplianin 	}
50099277b38SIgor M. Liplianin 
5011e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, F0900_P2_RST_HWARE, 1);
5021e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, F0900_P2_RST_HWARE, 0);
5031e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, F0900_P1_RST_HWARE, 1);
5041e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, F0900_P1_RST_HWARE, 0);
50599277b38SIgor M. Liplianin }
50699277b38SIgor M. Liplianin 
50799277b38SIgor M. Liplianin void stv0900_set_tuner(struct dvb_frontend *fe, u32 frequency,
50899277b38SIgor M. Liplianin 							u32 bandwidth)
50999277b38SIgor M. Liplianin {
51099277b38SIgor M. Liplianin 	struct dvb_frontend_ops *frontend_ops = NULL;
51199277b38SIgor M. Liplianin 	struct dvb_tuner_ops *tuner_ops = NULL;
51299277b38SIgor M. Liplianin 
51399277b38SIgor M. Liplianin 	frontend_ops = &fe->ops;
51499277b38SIgor M. Liplianin 	tuner_ops = &frontend_ops->tuner_ops;
51599277b38SIgor M. Liplianin 
51699277b38SIgor M. Liplianin 	if (tuner_ops->set_frequency) {
51799277b38SIgor M. Liplianin 		if ((tuner_ops->set_frequency(fe, frequency)) < 0)
51899277b38SIgor M. Liplianin 			dprintk("%s: Invalid parameter\n", __func__);
51999277b38SIgor M. Liplianin 		else
52099277b38SIgor M. Liplianin 			dprintk("%s: Frequency=%d\n", __func__, frequency);
52199277b38SIgor M. Liplianin 
52299277b38SIgor M. Liplianin 	}
52399277b38SIgor M. Liplianin 
52499277b38SIgor M. Liplianin 	if (tuner_ops->set_bandwidth) {
52599277b38SIgor M. Liplianin 		if ((tuner_ops->set_bandwidth(fe, bandwidth)) < 0)
52699277b38SIgor M. Liplianin 			dprintk("%s: Invalid parameter\n", __func__);
52799277b38SIgor M. Liplianin 		else
52899277b38SIgor M. Liplianin 			dprintk("%s: Bandwidth=%d\n", __func__, bandwidth);
52999277b38SIgor M. Liplianin 
53099277b38SIgor M. Liplianin 	}
53199277b38SIgor M. Liplianin }
53299277b38SIgor M. Liplianin 
53399277b38SIgor M. Liplianin void stv0900_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
53499277b38SIgor M. Liplianin {
53599277b38SIgor M. Liplianin 	struct dvb_frontend_ops *frontend_ops = NULL;
53699277b38SIgor M. Liplianin 	struct dvb_tuner_ops *tuner_ops = NULL;
53799277b38SIgor M. Liplianin 
53899277b38SIgor M. Liplianin 	frontend_ops = &fe->ops;
53999277b38SIgor M. Liplianin 	tuner_ops = &frontend_ops->tuner_ops;
54099277b38SIgor M. Liplianin 
54199277b38SIgor M. Liplianin 	if (tuner_ops->set_bandwidth) {
54299277b38SIgor M. Liplianin 		if ((tuner_ops->set_bandwidth(fe, bandwidth)) < 0)
54399277b38SIgor M. Liplianin 			dprintk("%s: Invalid parameter\n", __func__);
54499277b38SIgor M. Liplianin 		else
54599277b38SIgor M. Liplianin 			dprintk("%s: Bandwidth=%d\n", __func__, bandwidth);
54699277b38SIgor M. Liplianin 
54799277b38SIgor M. Liplianin 	}
54899277b38SIgor M. Liplianin }
54999277b38SIgor M. Liplianin 
550cd79d33eSIgor M. Liplianin u32 stv0900_get_freq_auto(struct stv0900_internal *intp, int demod)
551cd79d33eSIgor M. Liplianin {
552cd79d33eSIgor M. Liplianin 	u32 freq, round;
553cd79d33eSIgor M. Liplianin 	/*	Formulat :
554cd79d33eSIgor M. Liplianin 	Tuner_Frequency(MHz)	= Regs / 64
555cd79d33eSIgor M. Liplianin 	Tuner_granularity(MHz)	= Regs / 2048
556cd79d33eSIgor M. Liplianin 	real_Tuner_Frequency	= Tuner_Frequency(MHz) - Tuner_granularity(MHz)
557cd79d33eSIgor M. Liplianin 	*/
558cd79d33eSIgor M. Liplianin 	freq = (stv0900_get_bits(intp, TUN_RFFREQ2) << 10) +
559cd79d33eSIgor M. Liplianin 		(stv0900_get_bits(intp, TUN_RFFREQ1) << 2) +
560cd79d33eSIgor M. Liplianin 		stv0900_get_bits(intp, TUN_RFFREQ0);
561cd79d33eSIgor M. Liplianin 
562cd79d33eSIgor M. Liplianin 	freq = (freq * 1000) / 64;
563cd79d33eSIgor M. Liplianin 
564cd79d33eSIgor M. Liplianin 	round = (stv0900_get_bits(intp, TUN_RFRESTE1) >> 2) +
565cd79d33eSIgor M. Liplianin 		stv0900_get_bits(intp, TUN_RFRESTE0);
566cd79d33eSIgor M. Liplianin 
567cd79d33eSIgor M. Liplianin 	round = (round * 1000) / 2048;
568cd79d33eSIgor M. Liplianin 
569cd79d33eSIgor M. Liplianin 	return freq + round;
570cd79d33eSIgor M. Liplianin }
571cd79d33eSIgor M. Liplianin 
572cd79d33eSIgor M. Liplianin void stv0900_set_tuner_auto(struct stv0900_internal *intp, u32 Frequency,
573cd79d33eSIgor M. Liplianin 						u32 Bandwidth, int demod)
574cd79d33eSIgor M. Liplianin {
575cd79d33eSIgor M. Liplianin 	u32 tunerFrequency;
576cd79d33eSIgor M. Liplianin 	/* Formulat:
577cd79d33eSIgor M. Liplianin 	Tuner_frequency_reg= Frequency(MHz)*64
578cd79d33eSIgor M. Liplianin 	*/
579cd79d33eSIgor M. Liplianin 	tunerFrequency = (Frequency * 64) / 1000;
580cd79d33eSIgor M. Liplianin 
581cd79d33eSIgor M. Liplianin 	stv0900_write_bits(intp, TUN_RFFREQ2, (tunerFrequency >> 10));
582cd79d33eSIgor M. Liplianin 	stv0900_write_bits(intp, TUN_RFFREQ1, (tunerFrequency >> 2) & 0xff);
583cd79d33eSIgor M. Liplianin 	stv0900_write_bits(intp, TUN_RFFREQ0, (tunerFrequency & 0x03));
584cd79d33eSIgor M. Liplianin 	/* Low Pass Filter = BW /2 (MHz)*/
585cd79d33eSIgor M. Liplianin 	stv0900_write_bits(intp, TUN_BW, Bandwidth / 2000000);
586cd79d33eSIgor M. Liplianin 	/* Tuner Write trig */
587cd79d33eSIgor M. Liplianin 	stv0900_write_reg(intp, TNRLD, 1);
588cd79d33eSIgor M. Liplianin }
589cd79d33eSIgor M. Liplianin 
5901e0c397dSIgor M. Liplianin static s32 stv0900_get_rf_level(struct stv0900_internal *intp,
59199277b38SIgor M. Liplianin 				const struct stv0900_table *lookup,
59299277b38SIgor M. Liplianin 				enum fe_stv0900_demod_num demod)
59399277b38SIgor M. Liplianin {
59499277b38SIgor M. Liplianin 	s32 agc_gain = 0,
59599277b38SIgor M. Liplianin 		imin,
59699277b38SIgor M. Liplianin 		imax,
59799277b38SIgor M. Liplianin 		i,
59899277b38SIgor M. Liplianin 		rf_lvl = 0;
59999277b38SIgor M. Liplianin 
6008171c205SIgor M. Liplianin 	dprintk("%s\n", __func__);
60199277b38SIgor M. Liplianin 
6021e0c397dSIgor M. Liplianin 	if ((lookup == NULL) || (lookup->size <= 0))
6031e0c397dSIgor M. Liplianin 		return 0;
60499277b38SIgor M. Liplianin 
6051e0c397dSIgor M. Liplianin 	agc_gain = MAKEWORD(stv0900_get_bits(intp, AGCIQ_VALUE1),
6061e0c397dSIgor M. Liplianin 				stv0900_get_bits(intp, AGCIQ_VALUE0));
607502cd96dSIgor M. Liplianin 
60899277b38SIgor M. Liplianin 	imin = 0;
60999277b38SIgor M. Liplianin 	imax = lookup->size - 1;
6101e0c397dSIgor M. Liplianin 	if (INRANGE(lookup->table[imin].regval, agc_gain,
6111e0c397dSIgor M. Liplianin 					lookup->table[imax].regval)) {
61299277b38SIgor M. Liplianin 		while ((imax - imin) > 1) {
61399277b38SIgor M. Liplianin 			i = (imax + imin) >> 1;
61499277b38SIgor M. Liplianin 
6151e0c397dSIgor M. Liplianin 			if (INRANGE(lookup->table[imin].regval,
6161e0c397dSIgor M. Liplianin 					agc_gain,
6171e0c397dSIgor M. Liplianin 					lookup->table[i].regval))
61899277b38SIgor M. Liplianin 				imax = i;
61999277b38SIgor M. Liplianin 			else
62099277b38SIgor M. Liplianin 				imin = i;
62199277b38SIgor M. Liplianin 		}
62299277b38SIgor M. Liplianin 
6231e0c397dSIgor M. Liplianin 		rf_lvl = (s32)agc_gain - lookup->table[imin].regval;
6241e0c397dSIgor M. Liplianin 		rf_lvl *= (lookup->table[imax].realval -
6251e0c397dSIgor M. Liplianin 				lookup->table[imin].realval);
6261e0c397dSIgor M. Liplianin 		rf_lvl /= (lookup->table[imax].regval -
6271e0c397dSIgor M. Liplianin 				lookup->table[imin].regval);
6281e0c397dSIgor M. Liplianin 		rf_lvl += lookup->table[imin].realval;
62999277b38SIgor M. Liplianin 	} else if (agc_gain > lookup->table[0].regval)
63099277b38SIgor M. Liplianin 		rf_lvl = 5;
63199277b38SIgor M. Liplianin 	else if (agc_gain < lookup->table[lookup->size-1].regval)
63299277b38SIgor M. Liplianin 		rf_lvl = -100;
63399277b38SIgor M. Liplianin 
6348171c205SIgor M. Liplianin 	dprintk("%s: RFLevel = %d\n", __func__, rf_lvl);
63599277b38SIgor M. Liplianin 
63699277b38SIgor M. Liplianin 	return rf_lvl;
63799277b38SIgor M. Liplianin }
63899277b38SIgor M. Liplianin 
63999277b38SIgor M. Liplianin static int stv0900_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
64099277b38SIgor M. Liplianin {
64199277b38SIgor M. Liplianin 	struct stv0900_state *state = fe->demodulator_priv;
64299277b38SIgor M. Liplianin 	struct stv0900_internal *internal = state->internal;
64399277b38SIgor M. Liplianin 	s32 rflevel = stv0900_get_rf_level(internal, &stv0900_rf,
64499277b38SIgor M. Liplianin 								state->demod);
64599277b38SIgor M. Liplianin 
646502cd96dSIgor M. Liplianin 	rflevel = (rflevel + 100) * (65535 / 70);
647502cd96dSIgor M. Liplianin 	if (rflevel < 0)
648502cd96dSIgor M. Liplianin 		rflevel = 0;
649502cd96dSIgor M. Liplianin 
650502cd96dSIgor M. Liplianin 	if (rflevel > 65535)
651502cd96dSIgor M. Liplianin 		rflevel = 65535;
652502cd96dSIgor M. Liplianin 
653502cd96dSIgor M. Liplianin 	*strength = rflevel;
65499277b38SIgor M. Liplianin 
65599277b38SIgor M. Liplianin 	return 0;
65699277b38SIgor M. Liplianin }
65799277b38SIgor M. Liplianin 
65899277b38SIgor M. Liplianin static s32 stv0900_carr_get_quality(struct dvb_frontend *fe,
65999277b38SIgor M. Liplianin 					const struct stv0900_table *lookup)
66099277b38SIgor M. Liplianin {
66199277b38SIgor M. Liplianin 	struct stv0900_state *state = fe->demodulator_priv;
6621e0c397dSIgor M. Liplianin 	struct stv0900_internal *intp = state->internal;
66399277b38SIgor M. Liplianin 	enum fe_stv0900_demod_num demod = state->demod;
66499277b38SIgor M. Liplianin 
66599277b38SIgor M. Liplianin 	s32	c_n = -100,
6661e0c397dSIgor M. Liplianin 		regval,
6671e0c397dSIgor M. Liplianin 		imin,
6681e0c397dSIgor M. Liplianin 		imax,
66999277b38SIgor M. Liplianin 		i,
67099277b38SIgor M. Liplianin 		noise_field1,
67199277b38SIgor M. Liplianin 		noise_field0;
67299277b38SIgor M. Liplianin 
6738171c205SIgor M. Liplianin 	dprintk("%s\n", __func__);
67499277b38SIgor M. Liplianin 
67599277b38SIgor M. Liplianin 	if (stv0900_get_standard(fe, demod) == STV0900_DVBS2_STANDARD) {
6761e0c397dSIgor M. Liplianin 		noise_field1 = NOSPLHT_NORMED1;
6771e0c397dSIgor M. Liplianin 		noise_field0 = NOSPLHT_NORMED0;
67899277b38SIgor M. Liplianin 	} else {
6791e0c397dSIgor M. Liplianin 		noise_field1 = NOSDATAT_NORMED1;
6801e0c397dSIgor M. Liplianin 		noise_field0 = NOSDATAT_NORMED0;
68199277b38SIgor M. Liplianin 	}
68299277b38SIgor M. Liplianin 
6831e0c397dSIgor M. Liplianin 	if (stv0900_get_bits(intp, LOCK_DEFINITIF)) {
68499277b38SIgor M. Liplianin 		if ((lookup != NULL) && lookup->size) {
68599277b38SIgor M. Liplianin 			regval = 0;
68699277b38SIgor M. Liplianin 			msleep(5);
68799277b38SIgor M. Liplianin 			for (i = 0; i < 16; i++) {
6881e0c397dSIgor M. Liplianin 				regval += MAKEWORD(stv0900_get_bits(intp,
68911a84143SIgor M. Liplianin 								noise_field1),
6901e0c397dSIgor M. Liplianin 						stv0900_get_bits(intp,
69111a84143SIgor M. Liplianin 								noise_field0));
69299277b38SIgor M. Liplianin 				msleep(1);
69399277b38SIgor M. Liplianin 			}
69499277b38SIgor M. Liplianin 
69599277b38SIgor M. Liplianin 			regval /= 16;
69699277b38SIgor M. Liplianin 			imin = 0;
69799277b38SIgor M. Liplianin 			imax = lookup->size - 1;
69811a84143SIgor M. Liplianin 			if (INRANGE(lookup->table[imin].regval,
69911a84143SIgor M. Liplianin 					regval,
70011a84143SIgor M. Liplianin 					lookup->table[imax].regval)) {
70199277b38SIgor M. Liplianin 				while ((imax - imin) > 1) {
70299277b38SIgor M. Liplianin 					i = (imax + imin) >> 1;
70311a84143SIgor M. Liplianin 					if (INRANGE(lookup->table[imin].regval,
70411a84143SIgor M. Liplianin 						    regval,
70511a84143SIgor M. Liplianin 						    lookup->table[i].regval))
70699277b38SIgor M. Liplianin 						imax = i;
70799277b38SIgor M. Liplianin 					else
70899277b38SIgor M. Liplianin 						imin = i;
70999277b38SIgor M. Liplianin 				}
71099277b38SIgor M. Liplianin 
71199277b38SIgor M. Liplianin 				c_n = ((regval - lookup->table[imin].regval)
71211a84143SIgor M. Liplianin 						* (lookup->table[imax].realval
71311a84143SIgor M. Liplianin 						- lookup->table[imin].realval)
71411a84143SIgor M. Liplianin 						/ (lookup->table[imax].regval
71511a84143SIgor M. Liplianin 						- lookup->table[imin].regval))
71699277b38SIgor M. Liplianin 						+ lookup->table[imin].realval;
71799277b38SIgor M. Liplianin 			} else if (regval < lookup->table[imin].regval)
71899277b38SIgor M. Liplianin 				c_n = 1000;
71999277b38SIgor M. Liplianin 		}
72099277b38SIgor M. Liplianin 	}
72199277b38SIgor M. Liplianin 
72299277b38SIgor M. Liplianin 	return c_n;
72399277b38SIgor M. Liplianin }
72499277b38SIgor M. Liplianin 
725ee1ebcfeSAbylay Ospan static int stv0900_read_ucblocks(struct dvb_frontend *fe, u32 * ucblocks)
726ee1ebcfeSAbylay Ospan {
727ee1ebcfeSAbylay Ospan 	struct stv0900_state *state = fe->demodulator_priv;
7281e0c397dSIgor M. Liplianin 	struct stv0900_internal *intp = state->internal;
729ee1ebcfeSAbylay Ospan 	enum fe_stv0900_demod_num demod = state->demod;
730ee1ebcfeSAbylay Ospan 	u8 err_val1, err_val0;
731ee1ebcfeSAbylay Ospan 	u32 header_err_val = 0;
732ee1ebcfeSAbylay Ospan 
733ee1ebcfeSAbylay Ospan 	*ucblocks = 0x0;
734ee1ebcfeSAbylay Ospan 	if (stv0900_get_standard(fe, demod) == STV0900_DVBS2_STANDARD) {
735ee1ebcfeSAbylay Ospan 		/* DVB-S2 delineator errors count */
736ee1ebcfeSAbylay Ospan 
737868c9a17SMauro Carvalho Chehab 		/* retrieving number for errnous headers */
7381e0c397dSIgor M. Liplianin 		err_val1 = stv0900_read_reg(intp, BBFCRCKO1);
7391e0c397dSIgor M. Liplianin 		err_val0 = stv0900_read_reg(intp, BBFCRCKO0);
740ee1ebcfeSAbylay Ospan 		header_err_val = (err_val1 << 8) | err_val0;
741ee1ebcfeSAbylay Ospan 
742868c9a17SMauro Carvalho Chehab 		/* retrieving number for errnous packets */
7431e0c397dSIgor M. Liplianin 		err_val1 = stv0900_read_reg(intp, UPCRCKO1);
7441e0c397dSIgor M. Liplianin 		err_val0 = stv0900_read_reg(intp, UPCRCKO0);
745ee1ebcfeSAbylay Ospan 		*ucblocks = (err_val1 << 8) | err_val0;
746ee1ebcfeSAbylay Ospan 		*ucblocks += header_err_val;
747ee1ebcfeSAbylay Ospan 	}
748ee1ebcfeSAbylay Ospan 
749ee1ebcfeSAbylay Ospan 	return 0;
750ee1ebcfeSAbylay Ospan }
751ee1ebcfeSAbylay Ospan 
75299277b38SIgor M. Liplianin static int stv0900_read_snr(struct dvb_frontend *fe, u16 *snr)
75399277b38SIgor M. Liplianin {
754502cd96dSIgor M. Liplianin 	s32 snrlcl = stv0900_carr_get_quality(fe,
75511a84143SIgor M. Liplianin 			(const struct stv0900_table *)&stv0900_s2_cn);
756502cd96dSIgor M. Liplianin 	snrlcl = (snrlcl + 30) * 384;
757502cd96dSIgor M. Liplianin 	if (snrlcl < 0)
758502cd96dSIgor M. Liplianin 		snrlcl = 0;
759502cd96dSIgor M. Liplianin 
760502cd96dSIgor M. Liplianin 	if (snrlcl > 65535)
761502cd96dSIgor M. Liplianin 		snrlcl = 65535;
762502cd96dSIgor M. Liplianin 
763502cd96dSIgor M. Liplianin 	*snr = snrlcl;
76499277b38SIgor M. Liplianin 
76599277b38SIgor M. Liplianin 	return 0;
76699277b38SIgor M. Liplianin }
76799277b38SIgor M. Liplianin 
7681e0c397dSIgor M. Liplianin static u32 stv0900_get_ber(struct stv0900_internal *intp,
76999277b38SIgor M. Liplianin 				enum fe_stv0900_demod_num demod)
77099277b38SIgor M. Liplianin {
77199277b38SIgor M. Liplianin 	u32 ber = 10000000, i;
77299277b38SIgor M. Liplianin 	s32 demod_state;
77399277b38SIgor M. Liplianin 
7741e0c397dSIgor M. Liplianin 	demod_state = stv0900_get_bits(intp, HEADER_MODE);
77599277b38SIgor M. Liplianin 
77699277b38SIgor M. Liplianin 	switch (demod_state) {
77799277b38SIgor M. Liplianin 	case STV0900_SEARCH:
77899277b38SIgor M. Liplianin 	case STV0900_PLH_DETECTED:
77999277b38SIgor M. Liplianin 	default:
78099277b38SIgor M. Liplianin 		ber = 10000000;
78199277b38SIgor M. Liplianin 		break;
78299277b38SIgor M. Liplianin 	case STV0900_DVBS_FOUND:
78399277b38SIgor M. Liplianin 		ber = 0;
78499277b38SIgor M. Liplianin 		for (i = 0; i < 5; i++) {
78599277b38SIgor M. Liplianin 			msleep(5);
7861e0c397dSIgor M. Liplianin 			ber += stv0900_get_err_count(intp, 0, demod);
78799277b38SIgor M. Liplianin 		}
78899277b38SIgor M. Liplianin 
78999277b38SIgor M. Liplianin 		ber /= 5;
7901e0c397dSIgor M. Liplianin 		if (stv0900_get_bits(intp, PRFVIT)) {
79199277b38SIgor M. Liplianin 			ber *= 9766;
79299277b38SIgor M. Liplianin 			ber = ber >> 13;
79399277b38SIgor M. Liplianin 		}
79499277b38SIgor M. Liplianin 
79599277b38SIgor M. Liplianin 		break;
79699277b38SIgor M. Liplianin 	case STV0900_DVBS2_FOUND:
79799277b38SIgor M. Liplianin 		ber = 0;
79899277b38SIgor M. Liplianin 		for (i = 0; i < 5; i++) {
79999277b38SIgor M. Liplianin 			msleep(5);
8001e0c397dSIgor M. Liplianin 			ber += stv0900_get_err_count(intp, 0, demod);
80199277b38SIgor M. Liplianin 		}
80299277b38SIgor M. Liplianin 
80399277b38SIgor M. Liplianin 		ber /= 5;
8041e0c397dSIgor M. Liplianin 		if (stv0900_get_bits(intp, PKTDELIN_LOCK)) {
80599277b38SIgor M. Liplianin 			ber *= 9766;
80699277b38SIgor M. Liplianin 			ber = ber >> 13;
80799277b38SIgor M. Liplianin 		}
80899277b38SIgor M. Liplianin 
80999277b38SIgor M. Liplianin 		break;
81099277b38SIgor M. Liplianin 	}
81199277b38SIgor M. Liplianin 
81299277b38SIgor M. Liplianin 	return ber;
81399277b38SIgor M. Liplianin }
81499277b38SIgor M. Liplianin 
81599277b38SIgor M. Liplianin static int stv0900_read_ber(struct dvb_frontend *fe, u32 *ber)
81699277b38SIgor M. Liplianin {
81799277b38SIgor M. Liplianin 	struct stv0900_state *state = fe->demodulator_priv;
81899277b38SIgor M. Liplianin 	struct stv0900_internal *internal = state->internal;
81999277b38SIgor M. Liplianin 
82099277b38SIgor M. Liplianin 	*ber = stv0900_get_ber(internal, state->demod);
82199277b38SIgor M. Liplianin 
82299277b38SIgor M. Liplianin 	return 0;
82399277b38SIgor M. Liplianin }
82499277b38SIgor M. Liplianin 
8251e0c397dSIgor M. Liplianin int stv0900_get_demod_lock(struct stv0900_internal *intp,
82699277b38SIgor M. Liplianin 			enum fe_stv0900_demod_num demod, s32 time_out)
82799277b38SIgor M. Liplianin {
82899277b38SIgor M. Liplianin 	s32 timer = 0,
8291e0c397dSIgor M. Liplianin 		lock = 0;
83099277b38SIgor M. Liplianin 
83199277b38SIgor M. Liplianin 	enum fe_stv0900_search_state	dmd_state;
83299277b38SIgor M. Liplianin 
83399277b38SIgor M. Liplianin 	while ((timer < time_out) && (lock == 0)) {
8341e0c397dSIgor M. Liplianin 		dmd_state = stv0900_get_bits(intp, HEADER_MODE);
83599277b38SIgor M. Liplianin 		dprintk("Demod State = %d\n", dmd_state);
83699277b38SIgor M. Liplianin 		switch (dmd_state) {
83799277b38SIgor M. Liplianin 		case STV0900_SEARCH:
83899277b38SIgor M. Liplianin 		case STV0900_PLH_DETECTED:
83999277b38SIgor M. Liplianin 		default:
84099277b38SIgor M. Liplianin 			lock = 0;
84199277b38SIgor M. Liplianin 			break;
84299277b38SIgor M. Liplianin 		case STV0900_DVBS2_FOUND:
84399277b38SIgor M. Liplianin 		case STV0900_DVBS_FOUND:
8441e0c397dSIgor M. Liplianin 			lock = stv0900_get_bits(intp, LOCK_DEFINITIF);
84599277b38SIgor M. Liplianin 			break;
84699277b38SIgor M. Liplianin 		}
84799277b38SIgor M. Liplianin 
84899277b38SIgor M. Liplianin 		if (lock == 0)
84999277b38SIgor M. Liplianin 			msleep(10);
85099277b38SIgor M. Liplianin 
85199277b38SIgor M. Liplianin 		timer += 10;
85299277b38SIgor M. Liplianin 	}
85399277b38SIgor M. Liplianin 
85499277b38SIgor M. Liplianin 	if (lock)
85599277b38SIgor M. Liplianin 		dprintk("DEMOD LOCK OK\n");
85699277b38SIgor M. Liplianin 	else
85799277b38SIgor M. Liplianin 		dprintk("DEMOD LOCK FAIL\n");
85899277b38SIgor M. Liplianin 
85999277b38SIgor M. Liplianin 	return lock;
86099277b38SIgor M. Liplianin }
86199277b38SIgor M. Liplianin 
8621e0c397dSIgor M. Liplianin void stv0900_stop_all_s2_modcod(struct stv0900_internal *intp,
86399277b38SIgor M. Liplianin 				enum fe_stv0900_demod_num demod)
86499277b38SIgor M. Liplianin {
86599277b38SIgor M. Liplianin 	s32 regflist,
86699277b38SIgor M. Liplianin 	i;
86799277b38SIgor M. Liplianin 
8688171c205SIgor M. Liplianin 	dprintk("%s\n", __func__);
86999277b38SIgor M. Liplianin 
8701e0c397dSIgor M. Liplianin 	regflist = MODCODLST0;
87199277b38SIgor M. Liplianin 
87299277b38SIgor M. Liplianin 	for (i = 0; i < 16; i++)
8731e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, regflist + i, 0xff);
87499277b38SIgor M. Liplianin }
87599277b38SIgor M. Liplianin 
8761e0c397dSIgor M. Liplianin void stv0900_activate_s2_modcod(struct stv0900_internal *intp,
87799277b38SIgor M. Liplianin 				enum fe_stv0900_demod_num demod)
87899277b38SIgor M. Liplianin {
87999277b38SIgor M. Liplianin 	u32 matype,
88099277b38SIgor M. Liplianin 		mod_code,
88199277b38SIgor M. Liplianin 		fmod,
88299277b38SIgor M. Liplianin 		reg_index,
88399277b38SIgor M. Liplianin 		field_index;
88499277b38SIgor M. Liplianin 
8858171c205SIgor M. Liplianin 	dprintk("%s\n", __func__);
88699277b38SIgor M. Liplianin 
8871e0c397dSIgor M. Liplianin 	if (intp->chip_id <= 0x11) {
88899277b38SIgor M. Liplianin 		msleep(5);
88999277b38SIgor M. Liplianin 
8901e0c397dSIgor M. Liplianin 		mod_code = stv0900_read_reg(intp, PLHMODCOD);
89199277b38SIgor M. Liplianin 		matype = mod_code & 0x3;
89299277b38SIgor M. Liplianin 		mod_code = (mod_code & 0x7f) >> 2;
89399277b38SIgor M. Liplianin 
8941e0c397dSIgor M. Liplianin 		reg_index = MODCODLSTF - mod_code / 2;
89599277b38SIgor M. Liplianin 		field_index = mod_code % 2;
89699277b38SIgor M. Liplianin 
89799277b38SIgor M. Liplianin 		switch (matype) {
89899277b38SIgor M. Liplianin 		case 0:
89999277b38SIgor M. Liplianin 		default:
90099277b38SIgor M. Liplianin 			fmod = 14;
90199277b38SIgor M. Liplianin 			break;
90299277b38SIgor M. Liplianin 		case 1:
90399277b38SIgor M. Liplianin 			fmod = 13;
90499277b38SIgor M. Liplianin 			break;
90599277b38SIgor M. Liplianin 		case 2:
90699277b38SIgor M. Liplianin 			fmod = 11;
90799277b38SIgor M. Liplianin 			break;
90899277b38SIgor M. Liplianin 		case 3:
90999277b38SIgor M. Liplianin 			fmod = 7;
91099277b38SIgor M. Liplianin 			break;
91199277b38SIgor M. Liplianin 		}
91299277b38SIgor M. Liplianin 
91399277b38SIgor M. Liplianin 		if ((INRANGE(STV0900_QPSK_12, mod_code, STV0900_8PSK_910))
91499277b38SIgor M. Liplianin 						&& (matype <= 1)) {
91599277b38SIgor M. Liplianin 			if (field_index == 0)
9161e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp, reg_index,
91799277b38SIgor M. Liplianin 							0xf0 | fmod);
91899277b38SIgor M. Liplianin 			else
9191e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp, reg_index,
92099277b38SIgor M. Liplianin 							(fmod << 4) | 0xf);
92199277b38SIgor M. Liplianin 		}
9221e0c397dSIgor M. Liplianin 
9231e0c397dSIgor M. Liplianin 	} else if (intp->chip_id >= 0x12) {
92499277b38SIgor M. Liplianin 		for (reg_index = 0; reg_index < 7; reg_index++)
9251e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, MODCODLST0 + reg_index, 0xff);
92699277b38SIgor M. Liplianin 
9271e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, MODCODLSTE, 0xff);
9281e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, MODCODLSTF, 0xcf);
92999277b38SIgor M. Liplianin 		for (reg_index = 0; reg_index < 8; reg_index++)
9301e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, MODCODLST7 + reg_index, 0xcc);
93199277b38SIgor M. Liplianin 
93299277b38SIgor M. Liplianin 
93399277b38SIgor M. Liplianin 	}
93499277b38SIgor M. Liplianin }
93599277b38SIgor M. Liplianin 
9361e0c397dSIgor M. Liplianin void stv0900_activate_s2_modcod_single(struct stv0900_internal *intp,
93799277b38SIgor M. Liplianin 					enum fe_stv0900_demod_num demod)
93899277b38SIgor M. Liplianin {
93999277b38SIgor M. Liplianin 	u32 reg_index;
94099277b38SIgor M. Liplianin 
9418171c205SIgor M. Liplianin 	dprintk("%s\n", __func__);
94299277b38SIgor M. Liplianin 
9431e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, MODCODLST0, 0xff);
9441e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, MODCODLST1, 0xf0);
9451e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, MODCODLSTF, 0x0f);
94699277b38SIgor M. Liplianin 	for (reg_index = 0; reg_index < 13; reg_index++)
9471e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, MODCODLST2 + reg_index, 0);
94899277b38SIgor M. Liplianin 
94999277b38SIgor M. Liplianin }
95099277b38SIgor M. Liplianin 
95199277b38SIgor M. Liplianin static enum dvbfe_algo stv0900_frontend_algo(struct dvb_frontend *fe)
95299277b38SIgor M. Liplianin {
95399277b38SIgor M. Liplianin 	return DVBFE_ALGO_CUSTOM;
95499277b38SIgor M. Liplianin }
95599277b38SIgor M. Liplianin 
9561e0c397dSIgor M. Liplianin void stv0900_start_search(struct stv0900_internal *intp,
95799277b38SIgor M. Liplianin 				enum fe_stv0900_demod_num demod)
95899277b38SIgor M. Liplianin {
9591e0c397dSIgor M. Liplianin 	u32 freq;
9601e0c397dSIgor M. Liplianin 	s16 freq_s16 ;
96199277b38SIgor M. Liplianin 
9621e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, DEMOD_MODE, 0x1f);
9631e0c397dSIgor M. Liplianin 	if (intp->chip_id == 0x10)
9641e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, CORRELEXP, 0xaa);
96599277b38SIgor M. Liplianin 
9661e0c397dSIgor M. Liplianin 	if (intp->chip_id < 0x20)
9671e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, CARHDR, 0x55);
96899277b38SIgor M. Liplianin 
9691e0c397dSIgor M. Liplianin 	if (intp->chip_id <= 0x20) {
9701e0c397dSIgor M. Liplianin 		if (intp->symbol_rate[0] <= 5000000) {
9711e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, CARCFG, 0x44);
9721e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, CFRUP1, 0x0f);
9731e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, CFRUP0, 0xff);
9741e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, CFRLOW1, 0xf0);
9751e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, CFRLOW0, 0x00);
9761e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, RTCS2, 0x68);
97799277b38SIgor M. Liplianin 		} else {
9781e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, CARCFG, 0xc4);
9791e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, RTCS2, 0x44);
98099277b38SIgor M. Liplianin 		}
98199277b38SIgor M. Liplianin 
9821e0c397dSIgor M. Liplianin 	} else { /*cut 3.0 above*/
9831e0c397dSIgor M. Liplianin 		if (intp->symbol_rate[demod] <= 5000000)
9841e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, RTCS2, 0x68);
98599277b38SIgor M. Liplianin 		else
9861e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, RTCS2, 0x44);
9871e0c397dSIgor M. Liplianin 
9881e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, CARCFG, 0x46);
9891e0c397dSIgor M. Liplianin 		if (intp->srch_algo[demod] == STV0900_WARM_START) {
9901e0c397dSIgor M. Liplianin 			freq = 1000 << 16;
9911e0c397dSIgor M. Liplianin 			freq /= (intp->mclk / 1000);
9921e0c397dSIgor M. Liplianin 			freq_s16 = (s16)freq;
9931e0c397dSIgor M. Liplianin 		} else {
9941e0c397dSIgor M. Liplianin 			freq = (intp->srch_range[demod] / 2000);
9951e0c397dSIgor M. Liplianin 			if (intp->symbol_rate[demod] <= 5000000)
9961e0c397dSIgor M. Liplianin 				freq += 80;
9971e0c397dSIgor M. Liplianin 			else
9981e0c397dSIgor M. Liplianin 				freq += 600;
9991e0c397dSIgor M. Liplianin 
10001e0c397dSIgor M. Liplianin 			freq = freq << 16;
10011e0c397dSIgor M. Liplianin 			freq /= (intp->mclk / 1000);
10021e0c397dSIgor M. Liplianin 			freq_s16 = (s16)freq;
100399277b38SIgor M. Liplianin 		}
100499277b38SIgor M. Liplianin 
10051e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, CFR_UP1, MSB(freq_s16));
10061e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, CFR_UP0, LSB(freq_s16));
10071e0c397dSIgor M. Liplianin 		freq_s16 *= (-1);
10081e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, CFR_LOW1, MSB(freq_s16));
10091e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, CFR_LOW0, LSB(freq_s16));
10101e0c397dSIgor M. Liplianin 	}
10111e0c397dSIgor M. Liplianin 
10121e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, CFRINIT1, 0);
10131e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, CFRINIT0, 0);
10141e0c397dSIgor M. Liplianin 
10151e0c397dSIgor M. Liplianin 	if (intp->chip_id >= 0x20) {
10161e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, EQUALCFG, 0x41);
10171e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, FFECFG, 0x41);
10181e0c397dSIgor M. Liplianin 
10191e0c397dSIgor M. Liplianin 		if ((intp->srch_standard[demod] == STV0900_SEARCH_DVBS1) ||
10201e0c397dSIgor M. Liplianin 			(intp->srch_standard[demod] == STV0900_SEARCH_DSS) ||
10211e0c397dSIgor M. Liplianin 			(intp->srch_standard[demod] == STV0900_AUTO_SEARCH)) {
10221e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, VITSCALE,
10231e0c397dSIgor M. Liplianin 								0x82);
10241e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, VAVSRVIT, 0x0);
10251e0c397dSIgor M. Liplianin 		}
10261e0c397dSIgor M. Liplianin 	}
10271e0c397dSIgor M. Liplianin 
10281e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, SFRSTEP, 0x00);
10291e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, TMGTHRISE, 0xe0);
10301e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, TMGTHFALL, 0xc0);
10311e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, SCAN_ENABLE, 0);
10321e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, CFR_AUTOSCAN, 0);
10331e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, S1S2_SEQUENTIAL, 0);
10341e0c397dSIgor M. Liplianin 	stv0900_write_reg(intp, RTC, 0x88);
10351e0c397dSIgor M. Liplianin 	if (intp->chip_id >= 0x20) {
10361e0c397dSIgor M. Liplianin 		if (intp->symbol_rate[demod] < 2000000) {
10371e0c397dSIgor M. Liplianin 			if (intp->chip_id <= 0x20)
10381e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp, CARFREQ, 0x39);
10391e0c397dSIgor M. Liplianin 			else  /*cut 3.0*/
10401e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp, CARFREQ, 0x89);
10411e0c397dSIgor M. Liplianin 
10421e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, CARHDR, 0x40);
10431e0c397dSIgor M. Liplianin 		} else if (intp->symbol_rate[demod] < 10000000) {
10441e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, CARFREQ, 0x4c);
10451e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, CARHDR, 0x20);
10461e0c397dSIgor M. Liplianin 		} else {
10471e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, CARFREQ, 0x4b);
10481e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, CARHDR, 0x20);
10491e0c397dSIgor M. Liplianin 		}
10501e0c397dSIgor M. Liplianin 
10511e0c397dSIgor M. Liplianin 	} else {
10521e0c397dSIgor M. Liplianin 		if (intp->symbol_rate[demod] < 10000000)
10531e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, CARFREQ, 0xef);
10541e0c397dSIgor M. Liplianin 		else
10551e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, CARFREQ, 0xed);
10561e0c397dSIgor M. Liplianin 	}
10571e0c397dSIgor M. Liplianin 
10581e0c397dSIgor M. Liplianin 	switch (intp->srch_algo[demod]) {
105999277b38SIgor M. Liplianin 	case STV0900_WARM_START:
10601e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, DMDISTATE, 0x1f);
10611e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, DMDISTATE, 0x18);
106299277b38SIgor M. Liplianin 		break;
106399277b38SIgor M. Liplianin 	case STV0900_COLD_START:
10641e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, DMDISTATE, 0x1f);
10651e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, DMDISTATE, 0x15);
106699277b38SIgor M. Liplianin 		break;
106799277b38SIgor M. Liplianin 	default:
106899277b38SIgor M. Liplianin 		break;
106999277b38SIgor M. Liplianin 	}
107099277b38SIgor M. Liplianin }
107199277b38SIgor M. Liplianin 
107299277b38SIgor M. Liplianin u8 stv0900_get_optim_carr_loop(s32 srate, enum fe_stv0900_modcode modcode,
107399277b38SIgor M. Liplianin 							s32 pilot, u8 chip_id)
107499277b38SIgor M. Liplianin {
107599277b38SIgor M. Liplianin 	u8 aclc_value = 0x29;
1076d079e36dSMauro Carvalho Chehab 	s32 i, cllas2_size;
10771e0c397dSIgor M. Liplianin 	const struct stv0900_car_loop_optim *cls2, *cllqs2, *cllas2;
107899277b38SIgor M. Liplianin 
10798171c205SIgor M. Liplianin 	dprintk("%s\n", __func__);
108099277b38SIgor M. Liplianin 
10811e0c397dSIgor M. Liplianin 	if (chip_id <= 0x12) {
10821e0c397dSIgor M. Liplianin 		cls2 = FE_STV0900_S2CarLoop;
10831e0c397dSIgor M. Liplianin 		cllqs2 = FE_STV0900_S2LowQPCarLoopCut30;
10841e0c397dSIgor M. Liplianin 		cllas2 = FE_STV0900_S2APSKCarLoopCut30;
1085d079e36dSMauro Carvalho Chehab 		cllas2_size = ARRAY_SIZE(FE_STV0900_S2APSKCarLoopCut30);
10861e0c397dSIgor M. Liplianin 	} else if (chip_id == 0x20) {
10871e0c397dSIgor M. Liplianin 		cls2 = FE_STV0900_S2CarLoopCut20;
10881e0c397dSIgor M. Liplianin 		cllqs2 = FE_STV0900_S2LowQPCarLoopCut20;
10891e0c397dSIgor M. Liplianin 		cllas2 = FE_STV0900_S2APSKCarLoopCut20;
1090d079e36dSMauro Carvalho Chehab 		cllas2_size = ARRAY_SIZE(FE_STV0900_S2APSKCarLoopCut20);
10911e0c397dSIgor M. Liplianin 	} else {
10921e0c397dSIgor M. Liplianin 		cls2 = FE_STV0900_S2CarLoopCut30;
10931e0c397dSIgor M. Liplianin 		cllqs2 = FE_STV0900_S2LowQPCarLoopCut30;
10941e0c397dSIgor M. Liplianin 		cllas2 = FE_STV0900_S2APSKCarLoopCut30;
1095d079e36dSMauro Carvalho Chehab 		cllas2_size = ARRAY_SIZE(FE_STV0900_S2APSKCarLoopCut30);
10961e0c397dSIgor M. Liplianin 	}
109799277b38SIgor M. Liplianin 
109899277b38SIgor M. Liplianin 	if (modcode < STV0900_QPSK_12) {
109999277b38SIgor M. Liplianin 		i = 0;
11001e0c397dSIgor M. Liplianin 		while ((i < 3) && (modcode != cllqs2[i].modcode))
110199277b38SIgor M. Liplianin 			i++;
110299277b38SIgor M. Liplianin 
110399277b38SIgor M. Liplianin 		if (i >= 3)
110499277b38SIgor M. Liplianin 			i = 2;
110599277b38SIgor M. Liplianin 	} else {
110699277b38SIgor M. Liplianin 		i = 0;
11071e0c397dSIgor M. Liplianin 		while ((i < 14) && (modcode != cls2[i].modcode))
110899277b38SIgor M. Liplianin 			i++;
110999277b38SIgor M. Liplianin 
111099277b38SIgor M. Liplianin 		if (i >= 14) {
111199277b38SIgor M. Liplianin 			i = 0;
11121e0c397dSIgor M. Liplianin 			while ((i < 11) && (modcode != cllas2[i].modcode))
111399277b38SIgor M. Liplianin 				i++;
111499277b38SIgor M. Liplianin 
111599277b38SIgor M. Liplianin 			if (i >= 11)
111699277b38SIgor M. Liplianin 				i = 10;
111799277b38SIgor M. Liplianin 		}
111899277b38SIgor M. Liplianin 	}
111999277b38SIgor M. Liplianin 
112099277b38SIgor M. Liplianin 	if (modcode <= STV0900_QPSK_25) {
112199277b38SIgor M. Liplianin 		if (pilot) {
112299277b38SIgor M. Liplianin 			if (srate <= 3000000)
11231e0c397dSIgor M. Liplianin 				aclc_value = cllqs2[i].car_loop_pilots_on_2;
112499277b38SIgor M. Liplianin 			else if (srate <= 7000000)
11251e0c397dSIgor M. Liplianin 				aclc_value = cllqs2[i].car_loop_pilots_on_5;
112699277b38SIgor M. Liplianin 			else if (srate <= 15000000)
11271e0c397dSIgor M. Liplianin 				aclc_value = cllqs2[i].car_loop_pilots_on_10;
112899277b38SIgor M. Liplianin 			else if (srate <= 25000000)
11291e0c397dSIgor M. Liplianin 				aclc_value = cllqs2[i].car_loop_pilots_on_20;
113099277b38SIgor M. Liplianin 			else
11311e0c397dSIgor M. Liplianin 				aclc_value = cllqs2[i].car_loop_pilots_on_30;
113299277b38SIgor M. Liplianin 		} else {
113399277b38SIgor M. Liplianin 			if (srate <= 3000000)
11341e0c397dSIgor M. Liplianin 				aclc_value = cllqs2[i].car_loop_pilots_off_2;
113599277b38SIgor M. Liplianin 			else if (srate <= 7000000)
11361e0c397dSIgor M. Liplianin 				aclc_value = cllqs2[i].car_loop_pilots_off_5;
113799277b38SIgor M. Liplianin 			else if (srate <= 15000000)
11381e0c397dSIgor M. Liplianin 				aclc_value = cllqs2[i].car_loop_pilots_off_10;
113999277b38SIgor M. Liplianin 			else if (srate <= 25000000)
11401e0c397dSIgor M. Liplianin 				aclc_value = cllqs2[i].car_loop_pilots_off_20;
114199277b38SIgor M. Liplianin 			else
11421e0c397dSIgor M. Liplianin 				aclc_value = cllqs2[i].car_loop_pilots_off_30;
114399277b38SIgor M. Liplianin 		}
114499277b38SIgor M. Liplianin 
114599277b38SIgor M. Liplianin 	} else if (modcode <= STV0900_8PSK_910) {
114699277b38SIgor M. Liplianin 		if (pilot) {
114799277b38SIgor M. Liplianin 			if (srate <= 3000000)
11481e0c397dSIgor M. Liplianin 				aclc_value = cls2[i].car_loop_pilots_on_2;
114999277b38SIgor M. Liplianin 			else if (srate <= 7000000)
11501e0c397dSIgor M. Liplianin 				aclc_value = cls2[i].car_loop_pilots_on_5;
115199277b38SIgor M. Liplianin 			else if (srate <= 15000000)
11521e0c397dSIgor M. Liplianin 				aclc_value = cls2[i].car_loop_pilots_on_10;
115399277b38SIgor M. Liplianin 			else if (srate <= 25000000)
11541e0c397dSIgor M. Liplianin 				aclc_value = cls2[i].car_loop_pilots_on_20;
115599277b38SIgor M. Liplianin 			else
11561e0c397dSIgor M. Liplianin 				aclc_value = cls2[i].car_loop_pilots_on_30;
115799277b38SIgor M. Liplianin 		} else {
115899277b38SIgor M. Liplianin 			if (srate <= 3000000)
11591e0c397dSIgor M. Liplianin 				aclc_value = cls2[i].car_loop_pilots_off_2;
116099277b38SIgor M. Liplianin 			else if (srate <= 7000000)
11611e0c397dSIgor M. Liplianin 				aclc_value = cls2[i].car_loop_pilots_off_5;
116299277b38SIgor M. Liplianin 			else if (srate <= 15000000)
11631e0c397dSIgor M. Liplianin 				aclc_value = cls2[i].car_loop_pilots_off_10;
116499277b38SIgor M. Liplianin 			else if (srate <= 25000000)
11651e0c397dSIgor M. Liplianin 				aclc_value = cls2[i].car_loop_pilots_off_20;
116699277b38SIgor M. Liplianin 			else
11671e0c397dSIgor M. Liplianin 				aclc_value = cls2[i].car_loop_pilots_off_30;
116899277b38SIgor M. Liplianin 		}
116999277b38SIgor M. Liplianin 
1170d079e36dSMauro Carvalho Chehab 	} else if (i < cllas2_size) {
117199277b38SIgor M. Liplianin 		if (srate <= 3000000)
11721e0c397dSIgor M. Liplianin 			aclc_value = cllas2[i].car_loop_pilots_on_2;
117399277b38SIgor M. Liplianin 		else if (srate <= 7000000)
11741e0c397dSIgor M. Liplianin 			aclc_value = cllas2[i].car_loop_pilots_on_5;
117599277b38SIgor M. Liplianin 		else if (srate <= 15000000)
11761e0c397dSIgor M. Liplianin 			aclc_value = cllas2[i].car_loop_pilots_on_10;
117799277b38SIgor M. Liplianin 		else if (srate <= 25000000)
11781e0c397dSIgor M. Liplianin 			aclc_value = cllas2[i].car_loop_pilots_on_20;
117999277b38SIgor M. Liplianin 		else
11801e0c397dSIgor M. Liplianin 			aclc_value = cllas2[i].car_loop_pilots_on_30;
118199277b38SIgor M. Liplianin 	}
118299277b38SIgor M. Liplianin 
118399277b38SIgor M. Liplianin 	return aclc_value;
118499277b38SIgor M. Liplianin }
118599277b38SIgor M. Liplianin 
11861e0c397dSIgor M. Liplianin u8 stv0900_get_optim_short_carr_loop(s32 srate,
11871e0c397dSIgor M. Liplianin 				enum fe_stv0900_modulation modulation,
11881e0c397dSIgor M. Liplianin 				u8 chip_id)
118999277b38SIgor M. Liplianin {
11901e0c397dSIgor M. Liplianin 	const struct stv0900_short_frames_car_loop_optim *s2scl;
11911e0c397dSIgor M. Liplianin 	const struct stv0900_short_frames_car_loop_optim_vs_mod *s2sclc30;
119299277b38SIgor M. Liplianin 	s32 mod_index = 0;
119399277b38SIgor M. Liplianin 	u8 aclc_value = 0x0b;
119499277b38SIgor M. Liplianin 
11958171c205SIgor M. Liplianin 	dprintk("%s\n", __func__);
119699277b38SIgor M. Liplianin 
11971e0c397dSIgor M. Liplianin 	s2scl = FE_STV0900_S2ShortCarLoop;
11981e0c397dSIgor M. Liplianin 	s2sclc30 = FE_STV0900_S2ShortCarLoopCut30;
11991e0c397dSIgor M. Liplianin 
120099277b38SIgor M. Liplianin 	switch (modulation) {
120199277b38SIgor M. Liplianin 	case STV0900_QPSK:
120299277b38SIgor M. Liplianin 	default:
120399277b38SIgor M. Liplianin 		mod_index = 0;
120499277b38SIgor M. Liplianin 		break;
120599277b38SIgor M. Liplianin 	case STV0900_8PSK:
120699277b38SIgor M. Liplianin 		mod_index = 1;
120799277b38SIgor M. Liplianin 		break;
120899277b38SIgor M. Liplianin 	case STV0900_16APSK:
120999277b38SIgor M. Liplianin 		mod_index = 2;
121099277b38SIgor M. Liplianin 		break;
121199277b38SIgor M. Liplianin 	case STV0900_32APSK:
121299277b38SIgor M. Liplianin 		mod_index = 3;
121399277b38SIgor M. Liplianin 		break;
121499277b38SIgor M. Liplianin 	}
121599277b38SIgor M. Liplianin 
12161e0c397dSIgor M. Liplianin 	if (chip_id >= 0x30) {
121799277b38SIgor M. Liplianin 		if (srate <= 3000000)
12181e0c397dSIgor M. Liplianin 			aclc_value = s2sclc30[mod_index].car_loop_2;
121999277b38SIgor M. Liplianin 		else if (srate <= 7000000)
12201e0c397dSIgor M. Liplianin 			aclc_value = s2sclc30[mod_index].car_loop_5;
122199277b38SIgor M. Liplianin 		else if (srate <= 15000000)
12221e0c397dSIgor M. Liplianin 			aclc_value = s2sclc30[mod_index].car_loop_10;
122399277b38SIgor M. Liplianin 		else if (srate <= 25000000)
12241e0c397dSIgor M. Liplianin 			aclc_value = s2sclc30[mod_index].car_loop_20;
122599277b38SIgor M. Liplianin 		else
12261e0c397dSIgor M. Liplianin 			aclc_value = s2sclc30[mod_index].car_loop_30;
122799277b38SIgor M. Liplianin 
12281e0c397dSIgor M. Liplianin 	} else if (chip_id >= 0x20) {
122999277b38SIgor M. Liplianin 		if (srate <= 3000000)
12301e0c397dSIgor M. Liplianin 			aclc_value = s2scl[mod_index].car_loop_cut20_2;
123199277b38SIgor M. Liplianin 		else if (srate <= 7000000)
12321e0c397dSIgor M. Liplianin 			aclc_value = s2scl[mod_index].car_loop_cut20_5;
123399277b38SIgor M. Liplianin 		else if (srate <= 15000000)
12341e0c397dSIgor M. Liplianin 			aclc_value = s2scl[mod_index].car_loop_cut20_10;
123599277b38SIgor M. Liplianin 		else if (srate <= 25000000)
12361e0c397dSIgor M. Liplianin 			aclc_value = s2scl[mod_index].car_loop_cut20_20;
123799277b38SIgor M. Liplianin 		else
12381e0c397dSIgor M. Liplianin 			aclc_value = s2scl[mod_index].car_loop_cut20_30;
123999277b38SIgor M. Liplianin 
12401e0c397dSIgor M. Liplianin 	} else {
12411e0c397dSIgor M. Liplianin 		if (srate <= 3000000)
12421e0c397dSIgor M. Liplianin 			aclc_value = s2scl[mod_index].car_loop_cut12_2;
12431e0c397dSIgor M. Liplianin 		else if (srate <= 7000000)
12441e0c397dSIgor M. Liplianin 			aclc_value = s2scl[mod_index].car_loop_cut12_5;
12451e0c397dSIgor M. Liplianin 		else if (srate <= 15000000)
12461e0c397dSIgor M. Liplianin 			aclc_value = s2scl[mod_index].car_loop_cut12_10;
12471e0c397dSIgor M. Liplianin 		else if (srate <= 25000000)
12481e0c397dSIgor M. Liplianin 			aclc_value = s2scl[mod_index].car_loop_cut12_20;
12491e0c397dSIgor M. Liplianin 		else
12501e0c397dSIgor M. Liplianin 			aclc_value = s2scl[mod_index].car_loop_cut12_30;
12511e0c397dSIgor M. Liplianin 
125299277b38SIgor M. Liplianin 	}
125399277b38SIgor M. Liplianin 
125499277b38SIgor M. Liplianin 	return aclc_value;
125599277b38SIgor M. Liplianin }
125699277b38SIgor M. Liplianin 
12571e0c397dSIgor M. Liplianin static
12581e0c397dSIgor M. Liplianin enum fe_stv0900_error stv0900_st_dvbs2_single(struct stv0900_internal *intp,
125999277b38SIgor M. Liplianin 					enum fe_stv0900_demod_mode LDPC_Mode,
126099277b38SIgor M. Liplianin 					enum fe_stv0900_demod_num demod)
126199277b38SIgor M. Liplianin {
12621e0c397dSIgor M. Liplianin 	s32 reg_ind;
126399277b38SIgor M. Liplianin 
12648171c205SIgor M. Liplianin 	dprintk("%s\n", __func__);
126599277b38SIgor M. Liplianin 
126699277b38SIgor M. Liplianin 	switch (LDPC_Mode) {
126799277b38SIgor M. Liplianin 	case STV0900_DUAL:
126899277b38SIgor M. Liplianin 	default:
12691e0c397dSIgor M. Liplianin 		if ((intp->demod_mode != STV0900_DUAL)
12701e0c397dSIgor M. Liplianin 			|| (stv0900_get_bits(intp, F0900_DDEMOD) != 1)) {
12711e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, R0900_GENCFG, 0x1d);
127299277b38SIgor M. Liplianin 
12731e0c397dSIgor M. Liplianin 			intp->demod_mode = STV0900_DUAL;
127499277b38SIgor M. Liplianin 
12751e0c397dSIgor M. Liplianin 			stv0900_write_bits(intp, F0900_FRESFEC, 1);
12761e0c397dSIgor M. Liplianin 			stv0900_write_bits(intp, F0900_FRESFEC, 0);
12771e0c397dSIgor M. Liplianin 
12781e0c397dSIgor M. Liplianin 			for (reg_ind = 0; reg_ind < 7; reg_ind++)
12791e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp,
12801e0c397dSIgor M. Liplianin 						R0900_P1_MODCODLST0 + reg_ind,
12811e0c397dSIgor M. Liplianin 						0xff);
12821e0c397dSIgor M. Liplianin 			for (reg_ind = 0; reg_ind < 8; reg_ind++)
12831e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp,
12841e0c397dSIgor M. Liplianin 						R0900_P1_MODCODLST7 + reg_ind,
12851e0c397dSIgor M. Liplianin 						0xcc);
12861e0c397dSIgor M. Liplianin 
12871e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, R0900_P1_MODCODLSTE, 0xff);
12881e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, R0900_P1_MODCODLSTF, 0xcf);
12891e0c397dSIgor M. Liplianin 
12901e0c397dSIgor M. Liplianin 			for (reg_ind = 0; reg_ind < 7; reg_ind++)
12911e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp,
12921e0c397dSIgor M. Liplianin 						R0900_P2_MODCODLST0 + reg_ind,
12931e0c397dSIgor M. Liplianin 						0xff);
12941e0c397dSIgor M. Liplianin 			for (reg_ind = 0; reg_ind < 8; reg_ind++)
12951e0c397dSIgor M. Liplianin 				stv0900_write_reg(intp,
12961e0c397dSIgor M. Liplianin 						R0900_P2_MODCODLST7 + reg_ind,
12971e0c397dSIgor M. Liplianin 						0xcc);
12981e0c397dSIgor M. Liplianin 
12991e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, R0900_P2_MODCODLSTE, 0xff);
13001e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, R0900_P2_MODCODLSTF, 0xcf);
130199277b38SIgor M. Liplianin 		}
130299277b38SIgor M. Liplianin 
130399277b38SIgor M. Liplianin 		break;
130499277b38SIgor M. Liplianin 	case STV0900_SINGLE:
13051e0c397dSIgor M. Liplianin 		if (demod == STV0900_DEMOD_2) {
13061e0c397dSIgor M. Liplianin 			stv0900_stop_all_s2_modcod(intp, STV0900_DEMOD_1);
13071e0c397dSIgor M. Liplianin 			stv0900_activate_s2_modcod_single(intp,
13081e0c397dSIgor M. Liplianin 							STV0900_DEMOD_2);
13091e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, R0900_GENCFG, 0x06);
13101e0c397dSIgor M. Liplianin 		} else {
13111e0c397dSIgor M. Liplianin 			stv0900_stop_all_s2_modcod(intp, STV0900_DEMOD_2);
13121e0c397dSIgor M. Liplianin 			stv0900_activate_s2_modcod_single(intp,
13131e0c397dSIgor M. Liplianin 							STV0900_DEMOD_1);
13141e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp, R0900_GENCFG, 0x04);
13151e0c397dSIgor M. Liplianin 		}
131699277b38SIgor M. Liplianin 
13171e0c397dSIgor M. Liplianin 		intp->demod_mode = STV0900_SINGLE;
131899277b38SIgor M. Liplianin 
13191e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_FRESFEC, 1);
13201e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_FRESFEC, 0);
13211e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P1_ALGOSWRST, 1);
13221e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P1_ALGOSWRST, 0);
13231e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P2_ALGOSWRST, 1);
13241e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P2_ALGOSWRST, 0);
132599277b38SIgor M. Liplianin 		break;
132699277b38SIgor M. Liplianin 	}
132799277b38SIgor M. Liplianin 
132849bc8962SMauro Carvalho Chehab 	return STV0900_NO_ERROR;
132999277b38SIgor M. Liplianin }
133099277b38SIgor M. Liplianin 
133199277b38SIgor M. Liplianin static enum fe_stv0900_error stv0900_init_internal(struct dvb_frontend *fe,
133299277b38SIgor M. Liplianin 					struct stv0900_init_params *p_init)
133399277b38SIgor M. Liplianin {
133499277b38SIgor M. Liplianin 	struct stv0900_state *state = fe->demodulator_priv;
133599277b38SIgor M. Liplianin 	enum fe_stv0900_error error = STV0900_NO_ERROR;
133699277b38SIgor M. Liplianin 	enum fe_stv0900_error demodError = STV0900_NO_ERROR;
13371e0c397dSIgor M. Liplianin 	struct stv0900_internal *intp = NULL;
1338f867c3f4SIgor M. Liplianin 	int selosci, i;
133999277b38SIgor M. Liplianin 
134099277b38SIgor M. Liplianin 	struct stv0900_inode *temp_int = find_inode(state->i2c_adap,
134199277b38SIgor M. Liplianin 						state->config->demod_address);
134299277b38SIgor M. Liplianin 
13438171c205SIgor M. Liplianin 	dprintk("%s\n", __func__);
134499277b38SIgor M. Liplianin 
134529372a8dSIgor M. Liplianin 	if ((temp_int != NULL) && (p_init->demod_mode == STV0900_DUAL)) {
134699277b38SIgor M. Liplianin 		state->internal = temp_int->internal;
134799277b38SIgor M. Liplianin 		(state->internal->dmds_used)++;
13488171c205SIgor M. Liplianin 		dprintk("%s: Find Internal Structure!\n", __func__);
134999277b38SIgor M. Liplianin 		return STV0900_NO_ERROR;
135099277b38SIgor M. Liplianin 	} else {
13511e0c397dSIgor M. Liplianin 		state->internal = kmalloc(sizeof(struct stv0900_internal),
13521e0c397dSIgor M. Liplianin 								GFP_KERNEL);
1353fb3ab105SRoel Kluin 		if (state->internal == NULL)
1354fb3ab105SRoel Kluin 			return STV0900_INVALID_HANDLE;
135599277b38SIgor M. Liplianin 		temp_int = append_internal(state->internal);
1356fb3ab105SRoel Kluin 		if (temp_int == NULL) {
1357fb3ab105SRoel Kluin 			kfree(state->internal);
1358fb3ab105SRoel Kluin 			state->internal = NULL;
1359fb3ab105SRoel Kluin 			return STV0900_INVALID_HANDLE;
1360fb3ab105SRoel Kluin 		}
136199277b38SIgor M. Liplianin 		state->internal->dmds_used = 1;
136299277b38SIgor M. Liplianin 		state->internal->i2c_adap = state->i2c_adap;
136399277b38SIgor M. Liplianin 		state->internal->i2c_addr = state->config->demod_address;
136499277b38SIgor M. Liplianin 		state->internal->clkmode = state->config->clkmode;
136599277b38SIgor M. Liplianin 		state->internal->errs = STV0900_NO_ERROR;
13668171c205SIgor M. Liplianin 		dprintk("%s: Create New Internal Structure!\n", __func__);
136799277b38SIgor M. Liplianin 	}
136899277b38SIgor M. Liplianin 
13691e0c397dSIgor M. Liplianin 	if (state->internal == NULL) {
13701e0c397dSIgor M. Liplianin 		error = STV0900_INVALID_HANDLE;
13711e0c397dSIgor M. Liplianin 		return error;
13721e0c397dSIgor M. Liplianin 	}
13731e0c397dSIgor M. Liplianin 
137499277b38SIgor M. Liplianin 	demodError = stv0900_initialize(state->internal);
137599277b38SIgor M. Liplianin 	if (demodError == STV0900_NO_ERROR) {
137699277b38SIgor M. Liplianin 			error = STV0900_NO_ERROR;
137799277b38SIgor M. Liplianin 	} else {
137899277b38SIgor M. Liplianin 		if (demodError == STV0900_INVALID_HANDLE)
137999277b38SIgor M. Liplianin 			error = STV0900_INVALID_HANDLE;
138099277b38SIgor M. Liplianin 		else
138199277b38SIgor M. Liplianin 			error = STV0900_I2C_ERROR;
138299277b38SIgor M. Liplianin 
138399277b38SIgor M. Liplianin 		return error;
138499277b38SIgor M. Liplianin 	}
138599277b38SIgor M. Liplianin 
13861e0c397dSIgor M. Liplianin 	intp = state->internal;
13871e0c397dSIgor M. Liplianin 
13881e0c397dSIgor M. Liplianin 	intp->demod_mode = p_init->demod_mode;
13891e0c397dSIgor M. Liplianin 	stv0900_st_dvbs2_single(intp, intp->demod_mode,	STV0900_DEMOD_1);
13901e0c397dSIgor M. Liplianin 	intp->chip_id = stv0900_read_reg(intp, R0900_MID);
13911e0c397dSIgor M. Liplianin 	intp->rolloff = p_init->rolloff;
13921e0c397dSIgor M. Liplianin 	intp->quartz = p_init->dmd_ref_clk;
13931e0c397dSIgor M. Liplianin 
13941e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, F0900_P1_ROLLOFF_CONTROL, p_init->rolloff);
13951e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, F0900_P2_ROLLOFF_CONTROL, p_init->rolloff);
13961e0c397dSIgor M. Liplianin 
13971e0c397dSIgor M. Liplianin 	intp->ts_config = p_init->ts_config;
13981e0c397dSIgor M. Liplianin 	if (intp->ts_config == NULL)
13991e0c397dSIgor M. Liplianin 		stv0900_set_ts_parallel_serial(intp,
14001e0c397dSIgor M. Liplianin 				p_init->path1_ts_clock,
14011e0c397dSIgor M. Liplianin 				p_init->path2_ts_clock);
14021e0c397dSIgor M. Liplianin 	else {
14031e0c397dSIgor M. Liplianin 		for (i = 0; intp->ts_config[i].addr != 0xffff; i++)
14041e0c397dSIgor M. Liplianin 			stv0900_write_reg(intp,
14051e0c397dSIgor M. Liplianin 					intp->ts_config[i].addr,
14061e0c397dSIgor M. Liplianin 					intp->ts_config[i].val);
14071e0c397dSIgor M. Liplianin 
14081e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P2_RST_HWARE, 1);
14091e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P2_RST_HWARE, 0);
14101e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P1_RST_HWARE, 1);
14111e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P1_RST_HWARE, 0);
14121e0c397dSIgor M. Liplianin 	}
14131e0c397dSIgor M. Liplianin 
1414cd79d33eSIgor M. Liplianin 	intp->tuner_type[0] = p_init->tuner1_type;
1415cd79d33eSIgor M. Liplianin 	intp->tuner_type[1] = p_init->tuner2_type;
1416cd79d33eSIgor M. Liplianin 	/* tuner init */
1417cd79d33eSIgor M. Liplianin 	switch (p_init->tuner1_type) {
1418cd79d33eSIgor M. Liplianin 	case 3: /*FE_AUTO_STB6100:*/
1419cd79d33eSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_P1_TNRCFG, 0x3c);
1420cd79d33eSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_P1_TNRCFG2, 0x86);
1421cd79d33eSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_P1_TNRCFG3, 0x18);
1422cd79d33eSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_P1_TNRXTAL, 27); /* 27MHz */
1423cd79d33eSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_P1_TNRSTEPS, 0x05);
1424cd79d33eSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_P1_TNRGAIN, 0x17);
1425cd79d33eSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_P1_TNRADJ, 0x1f);
1426cd79d33eSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_P1_TNRCTL2, 0x0);
1427cd79d33eSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P1_TUN_TYPE, 3);
1428cd79d33eSIgor M. Liplianin 		break;
1429cd79d33eSIgor M. Liplianin 	/* case FE_SW_TUNER: */
1430cd79d33eSIgor M. Liplianin 	default:
1431cd79d33eSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P1_TUN_TYPE, 6);
1432cd79d33eSIgor M. Liplianin 		break;
1433cd79d33eSIgor M. Liplianin 	}
1434cd79d33eSIgor M. Liplianin 
14351e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, F0900_P1_TUN_MADDRESS, p_init->tun1_maddress);
14361e0c397dSIgor M. Liplianin 	switch (p_init->tuner1_adc) {
14371e0c397dSIgor M. Liplianin 	case 1:
14381e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_TSTTNR1, 0x26);
14391e0c397dSIgor M. Liplianin 		break;
14401e0c397dSIgor M. Liplianin 	default:
14411e0c397dSIgor M. Liplianin 		break;
14421e0c397dSIgor M. Liplianin 	}
14431e0c397dSIgor M. Liplianin 
1444cd79d33eSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P1_TNRLD, 1); /* hw tuner */
1445cd79d33eSIgor M. Liplianin 
1446cd79d33eSIgor M. Liplianin 	/* tuner init */
1447cd79d33eSIgor M. Liplianin 	switch (p_init->tuner2_type) {
1448cd79d33eSIgor M. Liplianin 	case 3: /*FE_AUTO_STB6100:*/
1449cd79d33eSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_P2_TNRCFG, 0x3c);
1450cd79d33eSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_P2_TNRCFG2, 0x86);
1451cd79d33eSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_P2_TNRCFG3, 0x18);
1452cd79d33eSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_P2_TNRXTAL, 27); /* 27MHz */
1453cd79d33eSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_P2_TNRSTEPS, 0x05);
1454cd79d33eSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_P2_TNRGAIN, 0x17);
1455cd79d33eSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_P2_TNRADJ, 0x1f);
1456cd79d33eSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_P2_TNRCTL2, 0x0);
1457cd79d33eSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P2_TUN_TYPE, 3);
1458cd79d33eSIgor M. Liplianin 		break;
1459cd79d33eSIgor M. Liplianin 	/* case FE_SW_TUNER: */
1460cd79d33eSIgor M. Liplianin 	default:
1461cd79d33eSIgor M. Liplianin 		stv0900_write_bits(intp, F0900_P2_TUN_TYPE, 6);
1462cd79d33eSIgor M. Liplianin 		break;
1463cd79d33eSIgor M. Liplianin 	}
1464cd79d33eSIgor M. Liplianin 
14651e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, F0900_P2_TUN_MADDRESS, p_init->tun2_maddress);
14661e0c397dSIgor M. Liplianin 	switch (p_init->tuner2_adc) {
14671e0c397dSIgor M. Liplianin 	case 1:
14681e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_TSTTNR3, 0x26);
14691e0c397dSIgor M. Liplianin 		break;
14701e0c397dSIgor M. Liplianin 	default:
14711e0c397dSIgor M. Liplianin 		break;
14721e0c397dSIgor M. Liplianin 	}
14731e0c397dSIgor M. Liplianin 
1474cd79d33eSIgor M. Liplianin 	stv0900_write_reg(intp, R0900_P2_TNRLD, 1); /* hw tuner */
1475cd79d33eSIgor M. Liplianin 
14761e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, F0900_P1_TUN_IQSWAP, p_init->tun1_iq_inv);
14771e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, F0900_P2_TUN_IQSWAP, p_init->tun2_iq_inv);
14781e0c397dSIgor M. Liplianin 	stv0900_set_mclk(intp, 135000000);
14791e0c397dSIgor M. Liplianin 	msleep(3);
14801e0c397dSIgor M. Liplianin 
14811e0c397dSIgor M. Liplianin 	switch (intp->clkmode) {
14821e0c397dSIgor M. Liplianin 	case 0:
14831e0c397dSIgor M. Liplianin 	case 2:
14841e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_SYNTCTRL, 0x20 | intp->clkmode);
14851e0c397dSIgor M. Liplianin 		break;
14861e0c397dSIgor M. Liplianin 	default:
14871e0c397dSIgor M. Liplianin 		selosci = 0x02 & stv0900_read_reg(intp, R0900_SYNTCTRL);
14881e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, R0900_SYNTCTRL, 0x20 | selosci);
14891e0c397dSIgor M. Liplianin 		break;
14901e0c397dSIgor M. Liplianin 	}
14911e0c397dSIgor M. Liplianin 	msleep(3);
14921e0c397dSIgor M. Liplianin 
14931e0c397dSIgor M. Liplianin 	intp->mclk = stv0900_get_mclk_freq(intp, intp->quartz);
14941e0c397dSIgor M. Liplianin 	if (intp->errs)
14951e0c397dSIgor M. Liplianin 		error = STV0900_I2C_ERROR;
14961e0c397dSIgor M. Liplianin 
14971e0c397dSIgor M. Liplianin 	return error;
14981e0c397dSIgor M. Liplianin }
14991e0c397dSIgor M. Liplianin 
15001e0c397dSIgor M. Liplianin static int stv0900_status(struct stv0900_internal *intp,
150199277b38SIgor M. Liplianin 					enum fe_stv0900_demod_num demod)
150299277b38SIgor M. Liplianin {
150399277b38SIgor M. Liplianin 	enum fe_stv0900_search_state demod_state;
150499277b38SIgor M. Liplianin 	int locked = FALSE;
1505247cb142SAbylay Ospan 	u8 tsbitrate0_val, tsbitrate1_val;
1506247cb142SAbylay Ospan 	s32 bitrate;
150799277b38SIgor M. Liplianin 
15081e0c397dSIgor M. Liplianin 	demod_state = stv0900_get_bits(intp, HEADER_MODE);
150999277b38SIgor M. Liplianin 	switch (demod_state) {
151099277b38SIgor M. Liplianin 	case STV0900_SEARCH:
151199277b38SIgor M. Liplianin 	case STV0900_PLH_DETECTED:
151299277b38SIgor M. Liplianin 	default:
151399277b38SIgor M. Liplianin 		locked = FALSE;
151499277b38SIgor M. Liplianin 		break;
151599277b38SIgor M. Liplianin 	case STV0900_DVBS2_FOUND:
15161e0c397dSIgor M. Liplianin 		locked = stv0900_get_bits(intp, LOCK_DEFINITIF) &&
15171e0c397dSIgor M. Liplianin 				stv0900_get_bits(intp, PKTDELIN_LOCK) &&
15181e0c397dSIgor M. Liplianin 				stv0900_get_bits(intp, TSFIFO_LINEOK);
151999277b38SIgor M. Liplianin 		break;
152099277b38SIgor M. Liplianin 	case STV0900_DVBS_FOUND:
15211e0c397dSIgor M. Liplianin 		locked = stv0900_get_bits(intp, LOCK_DEFINITIF) &&
15221e0c397dSIgor M. Liplianin 				stv0900_get_bits(intp, LOCKEDVIT) &&
15231e0c397dSIgor M. Liplianin 				stv0900_get_bits(intp, TSFIFO_LINEOK);
152499277b38SIgor M. Liplianin 		break;
152599277b38SIgor M. Liplianin 	}
152699277b38SIgor M. Liplianin 
15271e0c397dSIgor M. Liplianin 	dprintk("%s: locked = %d\n", __func__, locked);
15281e0c397dSIgor M. Liplianin 
1529247cb142SAbylay Ospan 	if (stvdebug) {
1530247cb142SAbylay Ospan 		/* Print TS bitrate */
1531247cb142SAbylay Ospan 		tsbitrate0_val = stv0900_read_reg(intp, TSBITRATE0);
1532247cb142SAbylay Ospan 		tsbitrate1_val = stv0900_read_reg(intp, TSBITRATE1);
1533247cb142SAbylay Ospan 		/* Formula Bit rate = Mclk * px_tsfifo_bitrate / 16384 */
1534247cb142SAbylay Ospan 		bitrate = (stv0900_get_mclk_freq(intp, intp->quartz)/1000000)
1535247cb142SAbylay Ospan 			* (tsbitrate1_val << 8 | tsbitrate0_val);
1536247cb142SAbylay Ospan 		bitrate /= 16384;
1537247cb142SAbylay Ospan 		dprintk("TS bitrate = %d Mbit/sec\n", bitrate);
1538c2c1b415SPeter Senna Tschudin 	}
1539247cb142SAbylay Ospan 
154099277b38SIgor M. Liplianin 	return locked;
154199277b38SIgor M. Liplianin }
154299277b38SIgor M. Liplianin 
154373ec66c0SEvgeny Plehov static int stv0900_set_mis(struct stv0900_internal *intp,
154473ec66c0SEvgeny Plehov 				enum fe_stv0900_demod_num demod, int mis)
154573ec66c0SEvgeny Plehov {
154673ec66c0SEvgeny Plehov 	dprintk("%s\n", __func__);
154773ec66c0SEvgeny Plehov 
154873ec66c0SEvgeny Plehov 	if (mis < 0 || mis > 255) {
154973ec66c0SEvgeny Plehov 		dprintk("Disable MIS filtering\n");
155073ec66c0SEvgeny Plehov 		stv0900_write_bits(intp, FILTER_EN, 0);
155173ec66c0SEvgeny Plehov 	} else {
155273ec66c0SEvgeny Plehov 		dprintk("Enable MIS filtering - %d\n", mis);
155373ec66c0SEvgeny Plehov 		stv0900_write_bits(intp, FILTER_EN, 1);
155473ec66c0SEvgeny Plehov 		stv0900_write_reg(intp, ISIENTRY, mis);
155573ec66c0SEvgeny Plehov 		stv0900_write_reg(intp, ISIBITENA, 0xff);
155673ec66c0SEvgeny Plehov 	}
155773ec66c0SEvgeny Plehov 
155849bc8962SMauro Carvalho Chehab 	return STV0900_NO_ERROR;
155973ec66c0SEvgeny Plehov }
156073ec66c0SEvgeny Plehov 
156173ec66c0SEvgeny Plehov 
156241da5320SMauro Carvalho Chehab static enum dvbfe_search stv0900_search(struct dvb_frontend *fe)
156399277b38SIgor M. Liplianin {
156499277b38SIgor M. Liplianin 	struct stv0900_state *state = fe->demodulator_priv;
15651e0c397dSIgor M. Liplianin 	struct stv0900_internal *intp = state->internal;
15661e0c397dSIgor M. Liplianin 	enum fe_stv0900_demod_num demod = state->demod;
156799277b38SIgor M. Liplianin 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
156899277b38SIgor M. Liplianin 
156999277b38SIgor M. Liplianin 	struct stv0900_search_params p_search;
1570403c34f6SAbylay Ospan 	struct stv0900_signal_info p_result = intp->result[demod];
157199277b38SIgor M. Liplianin 
157299277b38SIgor M. Liplianin 	enum fe_stv0900_error error = STV0900_NO_ERROR;
157399277b38SIgor M. Liplianin 
15748171c205SIgor M. Liplianin 	dprintk("%s: ", __func__);
157599277b38SIgor M. Liplianin 
15761e0c397dSIgor M. Liplianin 	if (!(INRANGE(100000, c->symbol_rate, 70000000)))
15771e0c397dSIgor M. Liplianin 		return DVBFE_ALGO_SEARCH_FAILED;
15781e0c397dSIgor M. Liplianin 
1579b699c271SIgor M. Liplianin 	if (state->config->set_ts_params)
1580b699c271SIgor M. Liplianin 		state->config->set_ts_params(fe, 0);
1581b699c271SIgor M. Liplianin 
158273ec66c0SEvgeny Plehov 	stv0900_set_mis(intp, demod, c->stream_id);
158373ec66c0SEvgeny Plehov 
158499277b38SIgor M. Liplianin 	p_result.locked = FALSE;
15851e0c397dSIgor M. Liplianin 	p_search.path = demod;
158699277b38SIgor M. Liplianin 	p_search.frequency = c->frequency;
158799277b38SIgor M. Liplianin 	p_search.symbol_rate = c->symbol_rate;
158899277b38SIgor M. Liplianin 	p_search.search_range = 10000000;
158999277b38SIgor M. Liplianin 	p_search.fec = STV0900_FEC_UNKNOWN;
159099277b38SIgor M. Liplianin 	p_search.standard = STV0900_AUTO_SEARCH;
159199277b38SIgor M. Liplianin 	p_search.iq_inversion = STV0900_IQ_AUTO;
159299277b38SIgor M. Liplianin 	p_search.search_algo = STV0900_BLIND_SEARCH;
159338cdbce7SIgor M. Liplianin 	/* Speeds up DVB-S searching */
159438cdbce7SIgor M. Liplianin 	if (c->delivery_system == SYS_DVBS)
159538cdbce7SIgor M. Liplianin 		p_search.standard = STV0900_SEARCH_DVBS1;
159699277b38SIgor M. Liplianin 
15971e0c397dSIgor M. Liplianin 	intp->srch_standard[demod] = p_search.standard;
15981e0c397dSIgor M. Liplianin 	intp->symbol_rate[demod] = p_search.symbol_rate;
15991e0c397dSIgor M. Liplianin 	intp->srch_range[demod] = p_search.search_range;
16001e0c397dSIgor M. Liplianin 	intp->freq[demod] = p_search.frequency;
16011e0c397dSIgor M. Liplianin 	intp->srch_algo[demod] = p_search.search_algo;
16021e0c397dSIgor M. Liplianin 	intp->srch_iq_inv[demod] = p_search.iq_inversion;
16031e0c397dSIgor M. Liplianin 	intp->fec[demod] = p_search.fec;
160499277b38SIgor M. Liplianin 	if ((stv0900_algo(fe) == STV0900_RANGEOK) &&
16051e0c397dSIgor M. Liplianin 				(intp->errs == STV0900_NO_ERROR)) {
16061e0c397dSIgor M. Liplianin 		p_result.locked = intp->result[demod].locked;
16071e0c397dSIgor M. Liplianin 		p_result.standard = intp->result[demod].standard;
16081e0c397dSIgor M. Liplianin 		p_result.frequency = intp->result[demod].frequency;
16091e0c397dSIgor M. Liplianin 		p_result.symbol_rate = intp->result[demod].symbol_rate;
16101e0c397dSIgor M. Liplianin 		p_result.fec = intp->result[demod].fec;
16111e0c397dSIgor M. Liplianin 		p_result.modcode = intp->result[demod].modcode;
16121e0c397dSIgor M. Liplianin 		p_result.pilot = intp->result[demod].pilot;
16131e0c397dSIgor M. Liplianin 		p_result.frame_len = intp->result[demod].frame_len;
16141e0c397dSIgor M. Liplianin 		p_result.spectrum = intp->result[demod].spectrum;
16151e0c397dSIgor M. Liplianin 		p_result.rolloff = intp->result[demod].rolloff;
16161e0c397dSIgor M. Liplianin 		p_result.modulation = intp->result[demod].modulation;
161799277b38SIgor M. Liplianin 	} else {
161899277b38SIgor M. Liplianin 		p_result.locked = FALSE;
16191e0c397dSIgor M. Liplianin 		switch (intp->err[demod]) {
162099277b38SIgor M. Liplianin 		case STV0900_I2C_ERROR:
162199277b38SIgor M. Liplianin 			error = STV0900_I2C_ERROR;
162299277b38SIgor M. Liplianin 			break;
162399277b38SIgor M. Liplianin 		case STV0900_NO_ERROR:
162499277b38SIgor M. Liplianin 		default:
162599277b38SIgor M. Liplianin 			error = STV0900_SEARCH_FAILED;
162699277b38SIgor M. Liplianin 			break;
162799277b38SIgor M. Liplianin 		}
162899277b38SIgor M. Liplianin 	}
162999277b38SIgor M. Liplianin 
163099277b38SIgor M. Liplianin 	if ((p_result.locked == TRUE) && (error == STV0900_NO_ERROR)) {
16318171c205SIgor M. Liplianin 		dprintk("Search Success\n");
163299277b38SIgor M. Liplianin 		return DVBFE_ALGO_SEARCH_SUCCESS;
163399277b38SIgor M. Liplianin 	} else {
16348171c205SIgor M. Liplianin 		dprintk("Search Fail\n");
163599277b38SIgor M. Liplianin 		return DVBFE_ALGO_SEARCH_FAILED;
163699277b38SIgor M. Liplianin 	}
163799277b38SIgor M. Liplianin 
163899277b38SIgor M. Liplianin }
163999277b38SIgor M. Liplianin 
164099277b38SIgor M. Liplianin static int stv0900_read_status(struct dvb_frontend *fe, enum fe_status *status)
164199277b38SIgor M. Liplianin {
164299277b38SIgor M. Liplianin 	struct stv0900_state *state = fe->demodulator_priv;
164399277b38SIgor M. Liplianin 
164478175bf2SIgor M. Liplianin 	dprintk("%s: ", __func__);
164599277b38SIgor M. Liplianin 
164699277b38SIgor M. Liplianin 	if ((stv0900_status(state->internal, state->demod)) == TRUE) {
164799277b38SIgor M. Liplianin 		dprintk("DEMOD LOCK OK\n");
164899277b38SIgor M. Liplianin 		*status = FE_HAS_CARRIER
164999277b38SIgor M. Liplianin 			| FE_HAS_VITERBI
165099277b38SIgor M. Liplianin 			| FE_HAS_SYNC
165199277b38SIgor M. Liplianin 			| FE_HAS_LOCK;
1652fa8bae10SIgor M. Liplianin 		if (state->config->set_lock_led)
1653fa8bae10SIgor M. Liplianin 			state->config->set_lock_led(fe, 1);
1654fa8bae10SIgor M. Liplianin 	} else {
16553b30e0a8SAbylay Ospan 		*status = 0;
1656fa8bae10SIgor M. Liplianin 		if (state->config->set_lock_led)
1657fa8bae10SIgor M. Liplianin 			state->config->set_lock_led(fe, 0);
165899277b38SIgor M. Liplianin 		dprintk("DEMOD LOCK FAIL\n");
1659fa8bae10SIgor M. Liplianin 	}
166099277b38SIgor M. Liplianin 
166199277b38SIgor M. Liplianin 	return 0;
166299277b38SIgor M. Liplianin }
166399277b38SIgor M. Liplianin 
166499277b38SIgor M. Liplianin static int stv0900_stop_ts(struct dvb_frontend *fe, int stop_ts)
166599277b38SIgor M. Liplianin {
166699277b38SIgor M. Liplianin 
166799277b38SIgor M. Liplianin 	struct stv0900_state *state = fe->demodulator_priv;
16681e0c397dSIgor M. Liplianin 	struct stv0900_internal *intp = state->internal;
166999277b38SIgor M. Liplianin 	enum fe_stv0900_demod_num demod = state->demod;
167099277b38SIgor M. Liplianin 
167199277b38SIgor M. Liplianin 	if (stop_ts == TRUE)
16721e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, RST_HWARE, 1);
167399277b38SIgor M. Liplianin 	else
16741e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, RST_HWARE, 0);
167599277b38SIgor M. Liplianin 
167699277b38SIgor M. Liplianin 	return 0;
167799277b38SIgor M. Liplianin }
167899277b38SIgor M. Liplianin 
167999277b38SIgor M. Liplianin static int stv0900_diseqc_init(struct dvb_frontend *fe)
168099277b38SIgor M. Liplianin {
168199277b38SIgor M. Liplianin 	struct stv0900_state *state = fe->demodulator_priv;
16821e0c397dSIgor M. Liplianin 	struct stv0900_internal *intp = state->internal;
168399277b38SIgor M. Liplianin 	enum fe_stv0900_demod_num demod = state->demod;
168499277b38SIgor M. Liplianin 
16851e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, DISTX_MODE, state->config->diseqc_mode);
16861e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, DISEQC_RESET, 1);
16871e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, DISEQC_RESET, 0);
168899277b38SIgor M. Liplianin 
168999277b38SIgor M. Liplianin 	return 0;
169099277b38SIgor M. Liplianin }
169199277b38SIgor M. Liplianin 
169299277b38SIgor M. Liplianin static int stv0900_init(struct dvb_frontend *fe)
169399277b38SIgor M. Liplianin {
16948171c205SIgor M. Liplianin 	dprintk("%s\n", __func__);
169599277b38SIgor M. Liplianin 
169699277b38SIgor M. Liplianin 	stv0900_stop_ts(fe, 1);
169799277b38SIgor M. Liplianin 	stv0900_diseqc_init(fe);
169899277b38SIgor M. Liplianin 
169999277b38SIgor M. Liplianin 	return 0;
170099277b38SIgor M. Liplianin }
170199277b38SIgor M. Liplianin 
17021e0c397dSIgor M. Liplianin static int stv0900_diseqc_send(struct stv0900_internal *intp , u8 *data,
170399277b38SIgor M. Liplianin 				u32 NbData, enum fe_stv0900_demod_num demod)
170499277b38SIgor M. Liplianin {
170599277b38SIgor M. Liplianin 	s32 i = 0;
170699277b38SIgor M. Liplianin 
17071e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, DIS_PRECHARGE, 1);
170899277b38SIgor M. Liplianin 	while (i < NbData) {
17091e0c397dSIgor M. Liplianin 		while (stv0900_get_bits(intp, FIFO_FULL))
171099277b38SIgor M. Liplianin 			;/* checkpatch complains */
17111e0c397dSIgor M. Liplianin 		stv0900_write_reg(intp, DISTXDATA, data[i]);
171299277b38SIgor M. Liplianin 		i++;
171399277b38SIgor M. Liplianin 	}
171499277b38SIgor M. Liplianin 
17151e0c397dSIgor M. Liplianin 	stv0900_write_bits(intp, DIS_PRECHARGE, 0);
171699277b38SIgor M. Liplianin 	i = 0;
17171e0c397dSIgor M. Liplianin 	while ((stv0900_get_bits(intp, TX_IDLE) != 1) && (i < 10)) {
171899277b38SIgor M. Liplianin 		msleep(10);
171999277b38SIgor M. Liplianin 		i++;
172099277b38SIgor M. Liplianin 	}
172199277b38SIgor M. Liplianin 
172299277b38SIgor M. Liplianin 	return 0;
172399277b38SIgor M. Liplianin }
172499277b38SIgor M. Liplianin 
172599277b38SIgor M. Liplianin static int stv0900_send_master_cmd(struct dvb_frontend *fe,
172699277b38SIgor M. Liplianin 					struct dvb_diseqc_master_cmd *cmd)
172799277b38SIgor M. Liplianin {
172899277b38SIgor M. Liplianin 	struct stv0900_state *state = fe->demodulator_priv;
172999277b38SIgor M. Liplianin 
173099277b38SIgor M. Liplianin 	return stv0900_diseqc_send(state->internal,
173199277b38SIgor M. Liplianin 				cmd->msg,
173299277b38SIgor M. Liplianin 				cmd->msg_len,
173399277b38SIgor M. Liplianin 				state->demod);
173499277b38SIgor M. Liplianin }
173599277b38SIgor M. Liplianin 
17360df289a2SMauro Carvalho Chehab static int stv0900_send_burst(struct dvb_frontend *fe,
17370df289a2SMauro Carvalho Chehab 			      enum fe_sec_mini_cmd burst)
173899277b38SIgor M. Liplianin {
173999277b38SIgor M. Liplianin 	struct stv0900_state *state = fe->demodulator_priv;
17401e0c397dSIgor M. Liplianin 	struct stv0900_internal *intp = state->internal;
174199277b38SIgor M. Liplianin 	enum fe_stv0900_demod_num demod = state->demod;
17429329fb5bSAbylay Ospan 	u8 data;
174399277b38SIgor M. Liplianin 
174499277b38SIgor M. Liplianin 
174599277b38SIgor M. Liplianin 	switch (burst) {
174699277b38SIgor M. Liplianin 	case SEC_MINI_A:
17471e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, DISTX_MODE, 3);/* Unmodulated */
17489329fb5bSAbylay Ospan 		data = 0x00;
17491e0c397dSIgor M. Liplianin 		stv0900_diseqc_send(intp, &data, 1, state->demod);
175099277b38SIgor M. Liplianin 		break;
175199277b38SIgor M. Liplianin 	case SEC_MINI_B:
17521e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, DISTX_MODE, 2);/* Modulated */
17539329fb5bSAbylay Ospan 		data = 0xff;
17541e0c397dSIgor M. Liplianin 		stv0900_diseqc_send(intp, &data, 1, state->demod);
175599277b38SIgor M. Liplianin 		break;
175699277b38SIgor M. Liplianin 	}
175799277b38SIgor M. Liplianin 
175899277b38SIgor M. Liplianin 	return 0;
175999277b38SIgor M. Liplianin }
176099277b38SIgor M. Liplianin 
176199277b38SIgor M. Liplianin static int stv0900_recv_slave_reply(struct dvb_frontend *fe,
176299277b38SIgor M. Liplianin 				struct dvb_diseqc_slave_reply *reply)
176399277b38SIgor M. Liplianin {
176499277b38SIgor M. Liplianin 	struct stv0900_state *state = fe->demodulator_priv;
17651e0c397dSIgor M. Liplianin 	struct stv0900_internal *intp = state->internal;
17661e0c397dSIgor M. Liplianin 	enum fe_stv0900_demod_num demod = state->demod;
176799277b38SIgor M. Liplianin 	s32 i = 0;
176899277b38SIgor M. Liplianin 
176999277b38SIgor M. Liplianin 	reply->msg_len = 0;
177099277b38SIgor M. Liplianin 
17711e0c397dSIgor M. Liplianin 	while ((stv0900_get_bits(intp, RX_END) != 1) && (i < 10)) {
177299277b38SIgor M. Liplianin 		msleep(10);
177399277b38SIgor M. Liplianin 		i++;
177499277b38SIgor M. Liplianin 	}
177599277b38SIgor M. Liplianin 
17761e0c397dSIgor M. Liplianin 	if (stv0900_get_bits(intp, RX_END)) {
17771e0c397dSIgor M. Liplianin 		reply->msg_len = stv0900_get_bits(intp, FIFO_BYTENBR);
177899277b38SIgor M. Liplianin 
177999277b38SIgor M. Liplianin 		for (i = 0; i < reply->msg_len; i++)
17801e0c397dSIgor M. Liplianin 			reply->msg[i] = stv0900_read_reg(intp, DISRXDATA);
178199277b38SIgor M. Liplianin 	}
178299277b38SIgor M. Liplianin 
178399277b38SIgor M. Liplianin 	return 0;
178499277b38SIgor M. Liplianin }
178599277b38SIgor M. Liplianin 
17860df289a2SMauro Carvalho Chehab static int stv0900_set_tone(struct dvb_frontend *fe,
17870df289a2SMauro Carvalho Chehab 			    enum fe_sec_tone_mode toneoff)
178899277b38SIgor M. Liplianin {
178999277b38SIgor M. Liplianin 	struct stv0900_state *state = fe->demodulator_priv;
17901e0c397dSIgor M. Liplianin 	struct stv0900_internal *intp = state->internal;
179199277b38SIgor M. Liplianin 	enum fe_stv0900_demod_num demod = state->demod;
179299277b38SIgor M. Liplianin 
17939329fb5bSAbylay Ospan 	dprintk("%s: %s\n", __func__, ((toneoff == 0) ? "On" : "Off"));
179499277b38SIgor M. Liplianin 
17959329fb5bSAbylay Ospan 	switch (toneoff) {
17969329fb5bSAbylay Ospan 	case SEC_TONE_ON:
17979329fb5bSAbylay Ospan 		/*Set the DiseqC mode to 22Khz _continues_ tone*/
17981e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, DISTX_MODE, 0);
17991e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, DISEQC_RESET, 1);
180099277b38SIgor M. Liplianin 		/*release DiseqC reset to enable the 22KHz tone*/
18011e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, DISEQC_RESET, 0);
18029329fb5bSAbylay Ospan 		break;
18039329fb5bSAbylay Ospan 	case SEC_TONE_OFF:
18049329fb5bSAbylay Ospan 		/*return diseqc mode to config->diseqc_mode.
18059329fb5bSAbylay Ospan 		Usually it's without _continues_ tone */
18061e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, DISTX_MODE,
18079329fb5bSAbylay Ospan 				state->config->diseqc_mode);
180899277b38SIgor M. Liplianin 		/*maintain the DiseqC reset to disable the 22KHz tone*/
18091e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, DISEQC_RESET, 1);
18101e0c397dSIgor M. Liplianin 		stv0900_write_bits(intp, DISEQC_RESET, 0);
18119329fb5bSAbylay Ospan 		break;
18129329fb5bSAbylay Ospan 	default:
18139329fb5bSAbylay Ospan 		return -EINVAL;
181499277b38SIgor M. Liplianin 	}
181599277b38SIgor M. Liplianin 
181699277b38SIgor M. Liplianin 	return 0;
181799277b38SIgor M. Liplianin }
181899277b38SIgor M. Liplianin 
181999277b38SIgor M. Liplianin static void stv0900_release(struct dvb_frontend *fe)
182099277b38SIgor M. Liplianin {
182199277b38SIgor M. Liplianin 	struct stv0900_state *state = fe->demodulator_priv;
182299277b38SIgor M. Liplianin 
18238171c205SIgor M. Liplianin 	dprintk("%s\n", __func__);
182499277b38SIgor M. Liplianin 
1825fa8bae10SIgor M. Liplianin 	if (state->config->set_lock_led)
1826fa8bae10SIgor M. Liplianin 		state->config->set_lock_led(fe, 0);
1827fa8bae10SIgor M. Liplianin 
182899277b38SIgor M. Liplianin 	if ((--(state->internal->dmds_used)) <= 0) {
182999277b38SIgor M. Liplianin 
18308171c205SIgor M. Liplianin 		dprintk("%s: Actually removing\n", __func__);
183199277b38SIgor M. Liplianin 
183299277b38SIgor M. Liplianin 		remove_inode(state->internal);
183399277b38SIgor M. Liplianin 		kfree(state->internal);
183499277b38SIgor M. Liplianin 	}
183599277b38SIgor M. Liplianin 
183699277b38SIgor M. Liplianin 	kfree(state);
183799277b38SIgor M. Liplianin }
183899277b38SIgor M. Liplianin 
1839fa8bae10SIgor M. Liplianin static int stv0900_sleep(struct dvb_frontend *fe)
1840fa8bae10SIgor M. Liplianin {
1841fa8bae10SIgor M. Liplianin 	struct stv0900_state *state = fe->demodulator_priv;
1842fa8bae10SIgor M. Liplianin 
1843fa8bae10SIgor M. Liplianin 	dprintk("%s\n", __func__);
1844fa8bae10SIgor M. Liplianin 
1845fa8bae10SIgor M. Liplianin 	if (state->config->set_lock_led)
1846fa8bae10SIgor M. Liplianin 		state->config->set_lock_led(fe, 0);
1847fa8bae10SIgor M. Liplianin 
1848fa8bae10SIgor M. Liplianin 	return 0;
1849fa8bae10SIgor M. Liplianin }
1850fa8bae10SIgor M. Liplianin 
18517e3e68bcSMauro Carvalho Chehab static int stv0900_get_frontend(struct dvb_frontend *fe,
18527e3e68bcSMauro Carvalho Chehab 				struct dtv_frontend_properties *p)
1853403c34f6SAbylay Ospan {
1854403c34f6SAbylay Ospan 	struct stv0900_state *state = fe->demodulator_priv;
1855403c34f6SAbylay Ospan 	struct stv0900_internal *intp = state->internal;
1856403c34f6SAbylay Ospan 	enum fe_stv0900_demod_num demod = state->demod;
1857403c34f6SAbylay Ospan 	struct stv0900_signal_info p_result = intp->result[demod];
1858403c34f6SAbylay Ospan 
1859403c34f6SAbylay Ospan 	p->frequency = p_result.locked ? p_result.frequency : 0;
186038d945e0SMauro Carvalho Chehab 	p->symbol_rate = p_result.locked ? p_result.symbol_rate : 0;
1861403c34f6SAbylay Ospan 	return 0;
1862403c34f6SAbylay Ospan }
1863403c34f6SAbylay Ospan 
1864bd336e63SMax Kellermann static const struct dvb_frontend_ops stv0900_ops = {
186538d945e0SMauro Carvalho Chehab 	.delsys = { SYS_DVBS, SYS_DVBS2, SYS_DSS },
186699277b38SIgor M. Liplianin 	.info = {
186799277b38SIgor M. Liplianin 		.name			= "STV0900 frontend",
1868f1b1eabfSMauro Carvalho Chehab 		.frequency_min_hz	=  950 * MHz,
1869f1b1eabfSMauro Carvalho Chehab 		.frequency_max_hz	= 2150 * MHz,
1870f1b1eabfSMauro Carvalho Chehab 		.frequency_stepsize_hz	=  125 * kHz,
187199277b38SIgor M. Liplianin 		.symbol_rate_min	= 1000000,
187299277b38SIgor M. Liplianin 		.symbol_rate_max	= 45000000,
187399277b38SIgor M. Liplianin 		.symbol_rate_tolerance	= 500,
187499277b38SIgor M. Liplianin 		.caps			= FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
187599277b38SIgor M. Liplianin 					  FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 |
187699277b38SIgor M. Liplianin 					  FE_CAN_FEC_7_8 | FE_CAN_QPSK    |
187799277b38SIgor M. Liplianin 					  FE_CAN_2G_MODULATION |
187899277b38SIgor M. Liplianin 					  FE_CAN_FEC_AUTO
187999277b38SIgor M. Liplianin 	},
188099277b38SIgor M. Liplianin 	.release			= stv0900_release,
188199277b38SIgor M. Liplianin 	.init				= stv0900_init,
188238d945e0SMauro Carvalho Chehab 	.get_frontend                   = stv0900_get_frontend,
1883fa8bae10SIgor M. Liplianin 	.sleep				= stv0900_sleep,
188499277b38SIgor M. Liplianin 	.get_frontend_algo		= stv0900_frontend_algo,
188599277b38SIgor M. Liplianin 	.i2c_gate_ctrl			= stv0900_i2c_gate_ctrl,
188699277b38SIgor M. Liplianin 	.diseqc_send_master_cmd		= stv0900_send_master_cmd,
188799277b38SIgor M. Liplianin 	.diseqc_send_burst		= stv0900_send_burst,
188899277b38SIgor M. Liplianin 	.diseqc_recv_slave_reply	= stv0900_recv_slave_reply,
188999277b38SIgor M. Liplianin 	.set_tone			= stv0900_set_tone,
189099277b38SIgor M. Liplianin 	.search				= stv0900_search,
189199277b38SIgor M. Liplianin 	.read_status			= stv0900_read_status,
189299277b38SIgor M. Liplianin 	.read_ber			= stv0900_read_ber,
189399277b38SIgor M. Liplianin 	.read_signal_strength		= stv0900_read_signal_strength,
189499277b38SIgor M. Liplianin 	.read_snr			= stv0900_read_snr,
1895ee1ebcfeSAbylay Ospan 	.read_ucblocks                  = stv0900_read_ucblocks,
189699277b38SIgor M. Liplianin };
189799277b38SIgor M. Liplianin 
189899277b38SIgor M. Liplianin struct dvb_frontend *stv0900_attach(const struct stv0900_config *config,
189999277b38SIgor M. Liplianin 					struct i2c_adapter *i2c,
190099277b38SIgor M. Liplianin 					int demod)
190199277b38SIgor M. Liplianin {
190299277b38SIgor M. Liplianin 	struct stv0900_state *state = NULL;
190399277b38SIgor M. Liplianin 	struct stv0900_init_params init_params;
190499277b38SIgor M. Liplianin 	enum fe_stv0900_error err_stv0900;
190599277b38SIgor M. Liplianin 
190699277b38SIgor M. Liplianin 	state = kzalloc(sizeof(struct stv0900_state), GFP_KERNEL);
190799277b38SIgor M. Liplianin 	if (state == NULL)
190899277b38SIgor M. Liplianin 		goto error;
190999277b38SIgor M. Liplianin 
191099277b38SIgor M. Liplianin 	state->demod		= demod;
191199277b38SIgor M. Liplianin 	state->config		= config;
191299277b38SIgor M. Liplianin 	state->i2c_adap		= i2c;
191399277b38SIgor M. Liplianin 
191499277b38SIgor M. Liplianin 	memcpy(&state->frontend.ops, &stv0900_ops,
191599277b38SIgor M. Liplianin 			sizeof(struct dvb_frontend_ops));
191699277b38SIgor M. Liplianin 	state->frontend.demodulator_priv = state;
191799277b38SIgor M. Liplianin 
191899277b38SIgor M. Liplianin 	switch (demod) {
191999277b38SIgor M. Liplianin 	case 0:
192099277b38SIgor M. Liplianin 	case 1:
192199277b38SIgor M. Liplianin 		init_params.dmd_ref_clk		= config->xtal;
192229372a8dSIgor M. Liplianin 		init_params.demod_mode		= config->demod_mode;
192399277b38SIgor M. Liplianin 		init_params.rolloff		= STV0900_35;
192499277b38SIgor M. Liplianin 		init_params.path1_ts_clock	= config->path1_mode;
192599277b38SIgor M. Liplianin 		init_params.tun1_maddress	= config->tun1_maddress;
19261e0c397dSIgor M. Liplianin 		init_params.tun1_iq_inv		= STV0900_IQ_NORMAL;
192799277b38SIgor M. Liplianin 		init_params.tuner1_adc		= config->tun1_adc;
1928cd79d33eSIgor M. Liplianin 		init_params.tuner1_type		= config->tun1_type;
192999277b38SIgor M. Liplianin 		init_params.path2_ts_clock	= config->path2_mode;
1930f867c3f4SIgor M. Liplianin 		init_params.ts_config		= config->ts_config_regs;
193199277b38SIgor M. Liplianin 		init_params.tun2_maddress	= config->tun2_maddress;
193299277b38SIgor M. Liplianin 		init_params.tuner2_adc		= config->tun2_adc;
1933cd79d33eSIgor M. Liplianin 		init_params.tuner2_type		= config->tun2_type;
19341e0c397dSIgor M. Liplianin 		init_params.tun2_iq_inv		= STV0900_IQ_SWAPPED;
193599277b38SIgor M. Liplianin 
193699277b38SIgor M. Liplianin 		err_stv0900 = stv0900_init_internal(&state->frontend,
193799277b38SIgor M. Liplianin 							&init_params);
193899277b38SIgor M. Liplianin 
193999277b38SIgor M. Liplianin 		if (err_stv0900)
194099277b38SIgor M. Liplianin 			goto error;
194199277b38SIgor M. Liplianin 
194273ec66c0SEvgeny Plehov 		if (state->internal->chip_id >= 0x30)
194373ec66c0SEvgeny Plehov 			state->frontend.ops.info.caps |= FE_CAN_MULTISTREAM;
194473ec66c0SEvgeny Plehov 
194599277b38SIgor M. Liplianin 		break;
194699277b38SIgor M. Liplianin 	default:
194799277b38SIgor M. Liplianin 		goto error;
194899277b38SIgor M. Liplianin 		break;
194999277b38SIgor M. Liplianin 	}
195099277b38SIgor M. Liplianin 
195199277b38SIgor M. Liplianin 	dprintk("%s: Attaching STV0900 demodulator(%d) \n", __func__, demod);
195299277b38SIgor M. Liplianin 	return &state->frontend;
195399277b38SIgor M. Liplianin 
195499277b38SIgor M. Liplianin error:
195599277b38SIgor M. Liplianin 	dprintk("%s: Failed to attach STV0900 demodulator(%d) \n",
195699277b38SIgor M. Liplianin 		__func__, demod);
195799277b38SIgor M. Liplianin 	kfree(state);
195899277b38SIgor M. Liplianin 	return NULL;
195999277b38SIgor M. Liplianin }
196099277b38SIgor M. Liplianin EXPORT_SYMBOL(stv0900_attach);
196199277b38SIgor M. Liplianin 
196299277b38SIgor M. Liplianin MODULE_PARM_DESC(debug, "Set debug");
196399277b38SIgor M. Liplianin 
196499277b38SIgor M. Liplianin MODULE_AUTHOR("Igor M. Liplianin");
196599277b38SIgor M. Liplianin MODULE_DESCRIPTION("ST STV0900 frontend");
196699277b38SIgor M. Liplianin MODULE_LICENSE("GPL");
1967