aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/dvb
diff options
context:
space:
mode:
authorAndreas Regel <andreas.regel@gmx.de>2010-01-05 17:19:43 -0500
committerMauro Carvalho Chehab <mchehab@redhat.com>2010-02-26 13:10:36 -0500
commit97f7a2ae1a8a1e44a481456375efced75210e5eb (patch)
tree0acc51538d65350219254dc06af3e2dc66f82520 /drivers/media/dvb
parentb79c6df705e02b5dcb0e9360a15b28373813fec1 (diff)
V4L/DVB (13975): [STV090x] Added internal structure with shared settings and data.
As the STV0900 features two demodulation paths in one chip there is some information used by both instances of the driver when used in dual mode. This information is now shared in an internal structure referenced by I2C adapter and address. Do initialisation of the demodulator only once when used in dual mode. Moved global mutex demod_lock to internal structure. Moved dev_ver and mclk to internal structure. Removed unused tuner_refclk from stv090x_state. Signed-off-by: Andreas Regel <andreas.regel@gmx.de> Signed-off-by: Manu Abraham <manu@linuxtv.org> Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'drivers/media/dvb')
-rw-r--r--drivers/media/dvb/frontends/stv090x.c289
-rw-r--r--drivers/media/dvb/frontends/stv090x.h2
-rw-r--r--drivers/media/dvb/frontends/stv090x_priv.h16
-rw-r--r--drivers/media/dvb/ttpci/budget.c1
4 files changed, 216 insertions, 92 deletions
diff --git a/drivers/media/dvb/frontends/stv090x.c b/drivers/media/dvb/frontends/stv090x.c
index e57581d14ccb..9a817a69ed30 100644
--- a/drivers/media/dvb/frontends/stv090x.c
+++ b/drivers/media/dvb/frontends/stv090x.c
@@ -37,7 +37,82 @@
37static unsigned int verbose; 37static unsigned int verbose;
38module_param(verbose, int, 0644); 38module_param(verbose, int, 0644);
39 39
40struct mutex demod_lock; 40/* internal params node */
41struct stv090x_dev {
42 /* pointer for internal params, one for each pair of demods */
43 struct stv090x_internal *internal;
44 struct stv090x_dev *next_dev;
45};
46
47/* first internal params */
48static struct stv090x_dev *stv090x_first_dev;
49
50/* find chip by i2c adapter and i2c address */
51static struct stv090x_dev *find_dev(struct i2c_adapter *i2c_adap,
52 u8 i2c_addr)
53{
54 struct stv090x_dev *temp_dev = stv090x_first_dev;
55
56 /*
57 Search of the last stv0900 chip or
58 find it by i2c adapter and i2c address */
59 while ((temp_dev != NULL) &&
60 ((temp_dev->internal->i2c_adap != i2c_adap) ||
61 (temp_dev->internal->i2c_addr != i2c_addr))) {
62
63 temp_dev = temp_dev->next_dev;
64 }
65
66 return temp_dev;
67}
68
69/* deallocating chip */
70static void remove_dev(struct stv090x_internal *internal)
71{
72 struct stv090x_dev *prev_dev = stv090x_first_dev;
73 struct stv090x_dev *del_dev = find_dev(internal->i2c_adap,
74 internal->i2c_addr);
75
76 if (del_dev != NULL) {
77 if (del_dev == stv090x_first_dev) {
78 stv090x_first_dev = del_dev->next_dev;
79 } else {
80 while (prev_dev->next_dev != del_dev)
81 prev_dev = prev_dev->next_dev;
82
83 prev_dev->next_dev = del_dev->next_dev;
84 }
85
86 kfree(del_dev);
87 }
88}
89
90/* allocating new chip */
91static struct stv090x_dev *append_internal(struct stv090x_internal *internal)
92{
93 struct stv090x_dev *new_dev;
94 struct stv090x_dev *temp_dev;
95
96 new_dev = kmalloc(sizeof(struct stv090x_dev), GFP_KERNEL);
97 if (new_dev != NULL) {
98 new_dev->internal = internal;
99 new_dev->next_dev = NULL;
100
101 /* append to list */
102 if (stv090x_first_dev == NULL) {
103 stv090x_first_dev = new_dev;
104 } else {
105 temp_dev = stv090x_first_dev;
106 while (temp_dev->next_dev != NULL)
107 temp_dev = temp_dev->next_dev;
108
109 temp_dev->next_dev = new_dev;
110 }
111 }
112
113 return new_dev;
114}
115
41 116
42/* DVBS1 and DSS C/N Lookup table */ 117/* DVBS1 and DSS C/N Lookup table */
43static const struct stv090x_tab stv090x_s1cn_tab[] = { 118static const struct stv090x_tab stv090x_s1cn_tab[] = {
@@ -755,13 +830,13 @@ static int stv090x_set_srate(struct stv090x_state *state, u32 srate)
755 830
756 if (srate > 60000000) { 831 if (srate > 60000000) {
757 sym = (srate << 4); /* SR * 2^16 / master_clk */ 832 sym = (srate << 4); /* SR * 2^16 / master_clk */
758 sym /= (state->mclk >> 12); 833 sym /= (state->internal->mclk >> 12);
759 } else if (srate > 6000000) { 834 } else if (srate > 6000000) {
760 sym = (srate << 6); 835 sym = (srate << 6);
761 sym /= (state->mclk >> 10); 836 sym /= (state->internal->mclk >> 10);
762 } else { 837 } else {
763 sym = (srate << 9); 838 sym = (srate << 9);
764 sym /= (state->mclk >> 7); 839 sym /= (state->internal->mclk >> 7);
765 } 840 }
766 841
767 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0x7f) < 0) /* MSB */ 842 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0x7f) < 0) /* MSB */
@@ -782,13 +857,13 @@ static int stv090x_set_max_srate(struct stv090x_state *state, u32 clk, u32 srate
782 srate = 105 * (srate / 100); 857 srate = 105 * (srate / 100);
783 if (srate > 60000000) { 858 if (srate > 60000000) {
784 sym = (srate << 4); /* SR * 2^16 / master_clk */ 859 sym = (srate << 4); /* SR * 2^16 / master_clk */
785 sym /= (state->mclk >> 12); 860 sym /= (state->internal->mclk >> 12);
786 } else if (srate > 6000000) { 861 } else if (srate > 6000000) {
787 sym = (srate << 6); 862 sym = (srate << 6);
788 sym /= (state->mclk >> 10); 863 sym /= (state->internal->mclk >> 10);
789 } else { 864 } else {
790 sym = (srate << 9); 865 sym = (srate << 9);
791 sym /= (state->mclk >> 7); 866 sym /= (state->internal->mclk >> 7);
792 } 867 }
793 868
794 if (sym < 0x7fff) { 869 if (sym < 0x7fff) {
@@ -816,13 +891,13 @@ static int stv090x_set_min_srate(struct stv090x_state *state, u32 clk, u32 srate
816 srate = 95 * (srate / 100); 891 srate = 95 * (srate / 100);
817 if (srate > 60000000) { 892 if (srate > 60000000) {
818 sym = (srate << 4); /* SR * 2^16 / master_clk */ 893 sym = (srate << 4); /* SR * 2^16 / master_clk */
819 sym /= (state->mclk >> 12); 894 sym /= (state->internal->mclk >> 12);
820 } else if (srate > 6000000) { 895 } else if (srate > 6000000) {
821 sym = (srate << 6); 896 sym = (srate << 6);
822 sym /= (state->mclk >> 10); 897 sym /= (state->internal->mclk >> 10);
823 } else { 898 } else {
824 sym = (srate << 9); 899 sym = (srate << 9);
825 sym /= (state->mclk >> 7); 900 sym /= (state->internal->mclk >> 7);
826 } 901 }
827 902
828 if (STV090x_WRITE_DEMOD(state, SFRLOW1, ((sym >> 8) & 0x7f)) < 0) /* MSB */ 903 if (STV090x_WRITE_DEMOD(state, SFRLOW1, ((sym >> 8) & 0x7f)) < 0) /* MSB */
@@ -1103,21 +1178,21 @@ static int stv090x_vitclk_ctl(struct stv090x_state *state, int enable)
1103 1178
1104 switch (state->demod) { 1179 switch (state->demod) {
1105 case STV090x_DEMODULATOR_0: 1180 case STV090x_DEMODULATOR_0:
1106 mutex_lock(&demod_lock); 1181 mutex_lock(&state->internal->demod_lock);
1107 reg = stv090x_read_reg(state, STV090x_STOPCLK2); 1182 reg = stv090x_read_reg(state, STV090x_STOPCLK2);
1108 STV090x_SETFIELD(reg, STOP_CLKVIT1_FIELD, enable); 1183 STV090x_SETFIELD(reg, STOP_CLKVIT1_FIELD, enable);
1109 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0) 1184 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
1110 goto err; 1185 goto err;
1111 mutex_unlock(&demod_lock); 1186 mutex_unlock(&state->internal->demod_lock);
1112 break; 1187 break;
1113 1188
1114 case STV090x_DEMODULATOR_1: 1189 case STV090x_DEMODULATOR_1:
1115 mutex_lock(&demod_lock); 1190 mutex_lock(&state->internal->demod_lock);
1116 reg = stv090x_read_reg(state, STV090x_STOPCLK2); 1191 reg = stv090x_read_reg(state, STV090x_STOPCLK2);
1117 STV090x_SETFIELD(reg, STOP_CLKVIT2_FIELD, enable); 1192 STV090x_SETFIELD(reg, STOP_CLKVIT2_FIELD, enable);
1118 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0) 1193 if (stv090x_write_reg(state, STV090x_STOPCLK2, reg) < 0)
1119 goto err; 1194 goto err;
1120 mutex_unlock(&demod_lock); 1195 mutex_unlock(&state->internal->demod_lock);
1121 break; 1196 break;
1122 1197
1123 default: 1198 default:
@@ -1126,14 +1201,14 @@ static int stv090x_vitclk_ctl(struct stv090x_state *state, int enable)
1126 } 1201 }
1127 return 0; 1202 return 0;
1128err: 1203err:
1129 mutex_unlock(&demod_lock); 1204 mutex_unlock(&state->internal->demod_lock);
1130 dprintk(FE_ERROR, 1, "I/O error"); 1205 dprintk(FE_ERROR, 1, "I/O error");
1131 return -1; 1206 return -1;
1132} 1207}
1133 1208
1134static int stv090x_dvbs_track_crl(struct stv090x_state *state) 1209static int stv090x_dvbs_track_crl(struct stv090x_state *state)
1135{ 1210{
1136 if (state->dev_ver >= 0x30) { 1211 if (state->internal->dev_ver >= 0x30) {
1137 /* Set ACLC BCLC optimised value vs SR */ 1212 /* Set ACLC BCLC optimised value vs SR */
1138 if (state->srate >= 15000000) { 1213 if (state->srate >= 15000000) {
1139 if (STV090x_WRITE_DEMOD(state, ACLC, 0x2b) < 0) 1214 if (STV090x_WRITE_DEMOD(state, ACLC, 0x2b) < 0)
@@ -1215,7 +1290,7 @@ static int stv090x_delivery_search(struct stv090x_state *state)
1215 if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0) 1290 if (STV090x_WRITE_DEMOD(state, BCLC, 0x09) < 0)
1216 goto err; 1291 goto err;
1217 1292
1218 if (state->dev_ver <= 0x20) { 1293 if (state->internal->dev_ver <= 0x20) {
1219 /* enable S2 carrier loop */ 1294 /* enable S2 carrier loop */
1220 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0) 1295 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
1221 goto err; 1296 goto err;
@@ -1261,7 +1336,7 @@ static int stv090x_delivery_search(struct stv090x_state *state)
1261 if (stv090x_dvbs_track_crl(state) < 0) 1336 if (stv090x_dvbs_track_crl(state) < 0)
1262 goto err; 1337 goto err;
1263 1338
1264 if (state->dev_ver <= 0x20) { 1339 if (state->internal->dev_ver <= 0x20) {
1265 /* enable S2 carrier loop */ 1340 /* enable S2 carrier loop */
1266 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0) 1341 if (STV090x_WRITE_DEMOD(state, CAR2CFG, 0x26) < 0)
1267 goto err; 1342 goto err;
@@ -1308,7 +1383,7 @@ static int stv090x_start_search(struct stv090x_state *state)
1308 if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0) 1383 if (STV090x_WRITE_DEMOD(state, DMDISTATE, reg) < 0)
1309 goto err; 1384 goto err;
1310 1385
1311 if (state->dev_ver <= 0x20) { 1386 if (state->internal->dev_ver <= 0x20) {
1312 if (state->srate <= 5000000) { 1387 if (state->srate <= 5000000) {
1313 if (STV090x_WRITE_DEMOD(state, CARCFG, 0x44) < 0) 1388 if (STV090x_WRITE_DEMOD(state, CARCFG, 0x44) < 0)
1314 goto err; 1389 goto err;
@@ -1352,7 +1427,7 @@ static int stv090x_start_search(struct stv090x_state *state)
1352 * CFR max = +1MHz 1427 * CFR max = +1MHz
1353 */ 1428 */
1354 freq_abs = 1000 << 16; 1429 freq_abs = 1000 << 16;
1355 freq_abs /= (state->mclk / 1000); 1430 freq_abs /= (state->internal->mclk / 1000);
1356 freq = (s16) freq_abs; 1431 freq = (s16) freq_abs;
1357 } else { 1432 } else {
1358 /* COLD Start 1433 /* COLD Start
@@ -1362,7 +1437,7 @@ static int stv090x_start_search(struct stv090x_state *state)
1362 */ 1437 */
1363 freq_abs = (state->search_range / 2000) + 600; 1438 freq_abs = (state->search_range / 2000) + 600;
1364 freq_abs = freq_abs << 16; 1439 freq_abs = freq_abs << 16;
1365 freq_abs /= (state->mclk / 1000); 1440 freq_abs /= (state->internal->mclk / 1000);
1366 freq = (s16) freq_abs; 1441 freq = (s16) freq_abs;
1367 } 1442 }
1368 1443
@@ -1385,7 +1460,7 @@ static int stv090x_start_search(struct stv090x_state *state)
1385 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0) < 0) 1460 if (STV090x_WRITE_DEMOD(state, CFRINIT0, 0) < 0)
1386 goto err; 1461 goto err;
1387 1462
1388 if (state->dev_ver >= 0x20) { 1463 if (state->internal->dev_ver >= 0x20) {
1389 if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0) 1464 if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
1390 goto err; 1465 goto err;
1391 if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0) 1466 if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
@@ -1422,10 +1497,10 @@ static int stv090x_start_search(struct stv090x_state *state)
1422 if (STV090x_WRITE_DEMOD(state, RTC, 0x88) < 0) 1497 if (STV090x_WRITE_DEMOD(state, RTC, 0x88) < 0)
1423 goto err; 1498 goto err;
1424 1499
1425 if (state->dev_ver >= 0x20) { 1500 if (state->internal->dev_ver >= 0x20) {
1426 /*Frequency offset detector setting*/ 1501 /*Frequency offset detector setting*/
1427 if (state->srate < 2000000) { 1502 if (state->srate < 2000000) {
1428 if (state->dev_ver <= 0x20) { 1503 if (state->internal->dev_ver <= 0x20) {
1429 /* Cut 2 */ 1504 /* Cut 2 */
1430 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x39) < 0) 1505 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x39) < 0)
1431 goto err; 1506 goto err;
@@ -1516,7 +1591,7 @@ static int stv090x_get_agc2_min_level(struct stv090x_state *state)
1516 steps = 1; 1591 steps = 1;
1517 1592
1518 dir = 1; 1593 dir = 1;
1519 freq_step = (1000000 * 256) / (state->mclk / 256); 1594 freq_step = (1000000 * 256) / (state->internal->mclk / 256);
1520 freq_init = 0; 1595 freq_init = 0;
1521 1596
1522 for (i = 0; i < steps; i++) { 1597 for (i = 0; i < steps; i++) {
@@ -1587,7 +1662,7 @@ static u32 stv090x_srate_srch_coarse(struct stv090x_state *state)
1587 u32 srate_coarse = 0, agc2 = 0, car_step = 1200, reg; 1662 u32 srate_coarse = 0, agc2 = 0, car_step = 1200, reg;
1588 u32 agc2th; 1663 u32 agc2th;
1589 1664
1590 if (state->dev_ver >= 0x30) 1665 if (state->internal->dev_ver >= 0x30)
1591 agc2th = 0x2e00; 1666 agc2th = 0x2e00;
1592 else 1667 else
1593 agc2th = 0x1f00; 1668 agc2th = 0x1f00;
@@ -1623,13 +1698,13 @@ static u32 stv090x_srate_srch_coarse(struct stv090x_state *state)
1623 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x50) < 0) 1698 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x50) < 0)
1624 goto err; 1699 goto err;
1625 1700
1626 if (state->dev_ver >= 0x30) { 1701 if (state->internal->dev_ver >= 0x30) {
1627 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x99) < 0) 1702 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x99) < 0)
1628 goto err; 1703 goto err;
1629 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x98) < 0) 1704 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x98) < 0)
1630 goto err; 1705 goto err;
1631 1706
1632 } else if (state->dev_ver >= 0x20) { 1707 } else if (state->internal->dev_ver >= 0x20) {
1633 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x6a) < 0) 1708 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x6a) < 0)
1634 goto err; 1709 goto err;
1635 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x95) < 0) 1710 if (STV090x_WRITE_DEMOD(state, SFRSTEP, 0x95) < 0)
@@ -1681,7 +1756,7 @@ static u32 stv090x_srate_srch_coarse(struct stv090x_state *state)
1681 STV090x_READ_DEMOD(state, AGC2I0); 1756 STV090x_READ_DEMOD(state, AGC2I0);
1682 } 1757 }
1683 agc2 /= 10; 1758 agc2 /= 10;
1684 srate_coarse = stv090x_get_srate(state, state->mclk); 1759 srate_coarse = stv090x_get_srate(state, state->internal->mclk);
1685 cur_step++; 1760 cur_step++;
1686 dir *= -1; 1761 dir *= -1;
1687 if ((tmg_cpt >= 5) && (agc2 < agc2th) && 1762 if ((tmg_cpt >= 5) && (agc2 < agc2th) &&
@@ -1733,7 +1808,7 @@ static u32 stv090x_srate_srch_coarse(struct stv090x_state *state)
1733 if (!tmg_lock) 1808 if (!tmg_lock)
1734 srate_coarse = 0; 1809 srate_coarse = 0;
1735 else 1810 else
1736 srate_coarse = stv090x_get_srate(state, state->mclk); 1811 srate_coarse = stv090x_get_srate(state, state->internal->mclk);
1737 1812
1738 return srate_coarse; 1813 return srate_coarse;
1739err: 1814err:
@@ -1745,7 +1820,7 @@ static u32 stv090x_srate_srch_fine(struct stv090x_state *state)
1745{ 1820{
1746 u32 srate_coarse, freq_coarse, sym, reg; 1821 u32 srate_coarse, freq_coarse, sym, reg;
1747 1822
1748 srate_coarse = stv090x_get_srate(state, state->mclk); 1823 srate_coarse = stv090x_get_srate(state, state->internal->mclk);
1749 freq_coarse = STV090x_READ_DEMOD(state, CFR2) << 8; 1824 freq_coarse = STV090x_READ_DEMOD(state, CFR2) << 8;
1750 freq_coarse |= STV090x_READ_DEMOD(state, CFR1); 1825 freq_coarse |= STV090x_READ_DEMOD(state, CFR1);
1751 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */ 1826 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
@@ -1771,10 +1846,10 @@ static u32 stv090x_srate_srch_fine(struct stv090x_state *state)
1771 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0) 1846 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
1772 goto err; 1847 goto err;
1773 1848
1774 if (state->dev_ver >= 0x30) { 1849 if (state->internal->dev_ver >= 0x30) {
1775 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x79) < 0) 1850 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x79) < 0)
1776 goto err; 1851 goto err;
1777 } else if (state->dev_ver >= 0x20) { 1852 } else if (state->internal->dev_ver >= 0x20) {
1778 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0) 1853 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
1779 goto err; 1854 goto err;
1780 } 1855 }
@@ -1782,20 +1857,20 @@ static u32 stv090x_srate_srch_fine(struct stv090x_state *state)
1782 if (srate_coarse > 3000000) { 1857 if (srate_coarse > 3000000) {
1783 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */ 1858 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
1784 sym = (sym / 1000) * 65536; 1859 sym = (sym / 1000) * 65536;
1785 sym /= (state->mclk / 1000); 1860 sym /= (state->internal->mclk / 1000);
1786 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0) 1861 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
1787 goto err; 1862 goto err;
1788 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0) 1863 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
1789 goto err; 1864 goto err;
1790 sym = 10 * (srate_coarse / 13); /* SFRLOW = SFR - 30% */ 1865 sym = 10 * (srate_coarse / 13); /* SFRLOW = SFR - 30% */
1791 sym = (sym / 1000) * 65536; 1866 sym = (sym / 1000) * 65536;
1792 sym /= (state->mclk / 1000); 1867 sym /= (state->internal->mclk / 1000);
1793 if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0) 1868 if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
1794 goto err; 1869 goto err;
1795 if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0) 1870 if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
1796 goto err; 1871 goto err;
1797 sym = (srate_coarse / 1000) * 65536; 1872 sym = (srate_coarse / 1000) * 65536;
1798 sym /= (state->mclk / 1000); 1873 sym /= (state->internal->mclk / 1000);
1799 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0) 1874 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
1800 goto err; 1875 goto err;
1801 if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0) 1876 if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
@@ -1803,20 +1878,20 @@ static u32 stv090x_srate_srch_fine(struct stv090x_state *state)
1803 } else { 1878 } else {
1804 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */ 1879 sym = 13 * (srate_coarse / 10); /* SFRUP = SFR + 30% */
1805 sym = (sym / 100) * 65536; 1880 sym = (sym / 100) * 65536;
1806 sym /= (state->mclk / 100); 1881 sym /= (state->internal->mclk / 100);
1807 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0) 1882 if (STV090x_WRITE_DEMOD(state, SFRUP1, (sym >> 8) & 0x7f) < 0)
1808 goto err; 1883 goto err;
1809 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0) 1884 if (STV090x_WRITE_DEMOD(state, SFRUP0, sym & 0xff) < 0)
1810 goto err; 1885 goto err;
1811 sym = 10 * (srate_coarse / 14); /* SFRLOW = SFR - 30% */ 1886 sym = 10 * (srate_coarse / 14); /* SFRLOW = SFR - 30% */
1812 sym = (sym / 100) * 65536; 1887 sym = (sym / 100) * 65536;
1813 sym /= (state->mclk / 100); 1888 sym /= (state->internal->mclk / 100);
1814 if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0) 1889 if (STV090x_WRITE_DEMOD(state, SFRLOW1, (sym >> 8) & 0x7f) < 0)
1815 goto err; 1890 goto err;
1816 if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0) 1891 if (STV090x_WRITE_DEMOD(state, SFRLOW0, sym & 0xff) < 0)
1817 goto err; 1892 goto err;
1818 sym = (srate_coarse / 100) * 65536; 1893 sym = (srate_coarse / 100) * 65536;
1819 sym /= (state->mclk / 100); 1894 sym /= (state->internal->mclk / 100);
1820 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0) 1895 if (STV090x_WRITE_DEMOD(state, SFRINIT1, (sym >> 8) & 0xff) < 0)
1821 goto err; 1896 goto err;
1822 if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0) 1897 if (STV090x_WRITE_DEMOD(state, SFRINIT0, sym & 0xff) < 0)
@@ -1885,11 +1960,11 @@ static int stv090x_blind_search(struct stv090x_state *state)
1885 1960
1886 agc2 = stv090x_get_agc2_min_level(state); 1961 agc2 = stv090x_get_agc2_min_level(state);
1887 1962
1888 if (agc2 > STV090x_SEARCH_AGC2_TH(state->dev_ver)) { 1963 if (agc2 > STV090x_SEARCH_AGC2_TH(state->internal->dev_ver)) {
1889 lock = 0; 1964 lock = 0;
1890 } else { 1965 } else {
1891 1966
1892 if (state->dev_ver <= 0x20) { 1967 if (state->internal->dev_ver <= 0x20) {
1893 if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0) 1968 if (STV090x_WRITE_DEMOD(state, CARCFG, 0xc4) < 0)
1894 goto err; 1969 goto err;
1895 } else { 1970 } else {
@@ -1901,7 +1976,7 @@ static int stv090x_blind_search(struct stv090x_state *state)
1901 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0) 1976 if (STV090x_WRITE_DEMOD(state, RTCS2, 0x44) < 0)
1902 goto err; 1977 goto err;
1903 1978
1904 if (state->dev_ver >= 0x20) { 1979 if (state->internal->dev_ver >= 0x20) {
1905 if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0) 1980 if (STV090x_WRITE_DEMOD(state, EQUALCFG, 0x41) < 0)
1906 goto err; 1981 goto err;
1907 if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0) 1982 if (STV090x_WRITE_DEMOD(state, FFECFG, 0x41) < 0)
@@ -2146,13 +2221,13 @@ static int stv090x_get_loop_params(struct stv090x_state *state, s32 *freq_inc, s
2146 car_max = state->search_range / 1000; 2221 car_max = state->search_range / 1000;
2147 car_max += car_max / 10; 2222 car_max += car_max / 10;
2148 car_max = 65536 * (car_max / 2); 2223 car_max = 65536 * (car_max / 2);
2149 car_max /= (state->mclk / 1000); 2224 car_max /= (state->internal->mclk / 1000);
2150 2225
2151 if (car_max > 0x4000) 2226 if (car_max > 0x4000)
2152 car_max = 0x4000 ; /* maxcarrier should be<= +-1/4 Mclk */ 2227 car_max = 0x4000 ; /* maxcarrier should be<= +-1/4 Mclk */
2153 2228
2154 inc = srate; 2229 inc = srate;
2155 inc /= state->mclk / 1000; 2230 inc /= state->internal->mclk / 1000;
2156 inc *= 256; 2231 inc *= 256;
2157 inc *= 256; 2232 inc *= 256;
2158 inc /= 1000; 2233 inc /= 1000;
@@ -2213,7 +2288,7 @@ static int stv090x_chk_signal(struct stv090x_state *state)
2213 2288
2214 car_max += (car_max / 10); /* 10% margin */ 2289 car_max += (car_max / 10); /* 10% margin */
2215 car_max = (65536 * car_max / 2); 2290 car_max = (65536 * car_max / 2);
2216 car_max /= state->mclk / 1000; 2291 car_max /= state->internal->mclk / 1000;
2217 2292
2218 if (car_max > 0x4000) 2293 if (car_max > 0x4000)
2219 car_max = 0x4000; 2294 car_max = 0x4000;
@@ -2238,7 +2313,7 @@ static int stv090x_search_car_loop(struct stv090x_state *state, s32 inc, s32 tim
2238 car_max = state->search_range / 1000; 2313 car_max = state->search_range / 1000;
2239 car_max += (car_max / 10); 2314 car_max += (car_max / 10);
2240 car_max = (65536 * car_max / 2); 2315 car_max = (65536 * car_max / 2);
2241 car_max /= (state->mclk / 1000); 2316 car_max /= (state->internal->mclk / 1000);
2242 if (car_max > 0x4000) 2317 if (car_max > 0x4000)
2243 car_max = 0x4000; 2318 car_max = 0x4000;
2244 2319
@@ -2308,7 +2383,7 @@ static int stv090x_sw_algo(struct stv090x_state *state)
2308 case STV090x_SEARCH_DVBS1: 2383 case STV090x_SEARCH_DVBS1:
2309 case STV090x_SEARCH_DSS: 2384 case STV090x_SEARCH_DSS:
2310 /* accelerate the frequency detector */ 2385 /* accelerate the frequency detector */
2311 if (state->dev_ver >= 0x20) { 2386 if (state->internal->dev_ver >= 0x20) {
2312 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3B) < 0) 2387 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3B) < 0)
2313 goto err; 2388 goto err;
2314 } 2389 }
@@ -2319,7 +2394,7 @@ static int stv090x_sw_algo(struct stv090x_state *state)
2319 break; 2394 break;
2320 2395
2321 case STV090x_SEARCH_DVBS2: 2396 case STV090x_SEARCH_DVBS2:
2322 if (state->dev_ver >= 0x20) { 2397 if (state->internal->dev_ver >= 0x20) {
2323 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0) 2398 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2324 goto err; 2399 goto err;
2325 } 2400 }
@@ -2332,7 +2407,7 @@ static int stv090x_sw_algo(struct stv090x_state *state)
2332 case STV090x_SEARCH_AUTO: 2407 case STV090x_SEARCH_AUTO:
2333 default: 2408 default:
2334 /* accelerate the frequency detector */ 2409 /* accelerate the frequency detector */
2335 if (state->dev_ver >= 0x20) { 2410 if (state->internal->dev_ver >= 0x20) {
2336 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3b) < 0) 2411 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x3b) < 0)
2337 goto err; 2412 goto err;
2338 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0) 2413 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
@@ -2354,7 +2429,7 @@ static int stv090x_sw_algo(struct stv090x_state *state)
2354 /*run the SW search 2 times maximum*/ 2429 /*run the SW search 2 times maximum*/
2355 if (lock || no_signal || (trials == 2)) { 2430 if (lock || no_signal || (trials == 2)) {
2356 /*Check if the demod is not losing lock in DVBS2*/ 2431 /*Check if the demod is not losing lock in DVBS2*/
2357 if (state->dev_ver >= 0x20) { 2432 if (state->internal->dev_ver >= 0x20) {
2358 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0) 2433 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
2359 goto err; 2434 goto err;
2360 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0) 2435 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
@@ -2376,7 +2451,7 @@ static int stv090x_sw_algo(struct stv090x_state *state)
2376 /*FALSE lock, The demod is loosing lock */ 2451 /*FALSE lock, The demod is loosing lock */
2377 lock = 0; 2452 lock = 0;
2378 if (trials < 2) { 2453 if (trials < 2) {
2379 if (state->dev_ver >= 0x20) { 2454 if (state->internal->dev_ver >= 0x20) {
2380 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0) 2455 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x79) < 0)
2381 goto err; 2456 goto err;
2382 } 2457 }
@@ -2426,11 +2501,11 @@ static s32 stv090x_get_car_freq(struct stv090x_state *state, u32 mclk)
2426 derot |= STV090x_READ_DEMOD(state, CFR0); 2501 derot |= STV090x_READ_DEMOD(state, CFR0);
2427 2502
2428 derot = comp2(derot, 24); 2503 derot = comp2(derot, 24);
2429 int_1 = state->mclk >> 12; 2504 int_1 = mclk >> 12;
2430 int_2 = derot >> 12; 2505 int_2 = derot >> 12;
2431 2506
2432 /* carrier_frequency = MasterClock * Reg / 2^24 */ 2507 /* carrier_frequency = MasterClock * Reg / 2^24 */
2433 tmp_1 = state->mclk % 0x1000; 2508 tmp_1 = mclk % 0x1000;
2434 tmp_2 = derot % 0x1000; 2509 tmp_2 = derot % 0x1000;
2435 2510
2436 derot = (int_1 * int_2) + 2511 derot = (int_1 * int_2) +
@@ -2512,7 +2587,7 @@ static enum stv090x_signal_state stv090x_get_sig_params(struct stv090x_state *st
2512 if (stv090x_i2c_gate_ctrl(fe, 0) < 0) 2587 if (stv090x_i2c_gate_ctrl(fe, 0) < 0)
2513 goto err; 2588 goto err;
2514 2589
2515 offst_freq = stv090x_get_car_freq(state, state->mclk) / 1000; 2590 offst_freq = stv090x_get_car_freq(state, state->internal->mclk) / 1000;
2516 state->frequency += offst_freq; 2591 state->frequency += offst_freq;
2517 2592
2518 if (stv090x_get_viterbi(state) < 0) 2593 if (stv090x_get_viterbi(state) < 0)
@@ -2583,7 +2658,7 @@ static u8 stv090x_optimize_carloop(struct stv090x_state *state, enum stv090x_mod
2583 s32 i; 2658 s32 i;
2584 struct stv090x_long_frame_crloop *car_loop, *car_loop_qpsk_low, *car_loop_apsk_low; 2659 struct stv090x_long_frame_crloop *car_loop, *car_loop_qpsk_low, *car_loop_apsk_low;
2585 2660
2586 if (state->dev_ver == 0x20) { 2661 if (state->internal->dev_ver == 0x20) {
2587 car_loop = stv090x_s2_crl_cut20; 2662 car_loop = stv090x_s2_crl_cut20;
2588 car_loop_qpsk_low = stv090x_s2_lowqpsk_crl_cut20; 2663 car_loop_qpsk_low = stv090x_s2_lowqpsk_crl_cut20;
2589 car_loop_apsk_low = stv090x_s2_apsk_crl_cut20; 2664 car_loop_apsk_low = stv090x_s2_apsk_crl_cut20;
@@ -2704,7 +2779,7 @@ static u8 stv090x_optimize_carloop_short(struct stv090x_state *state)
2704 break; 2779 break;
2705 } 2780 }
2706 2781
2707 if (state->dev_ver >= 0x30) { 2782 if (state->internal->dev_ver >= 0x30) {
2708 /* Cut 3.0 and up */ 2783 /* Cut 3.0 and up */
2709 short_crl = stv090x_s2_short_crl_cut30; 2784 short_crl = stv090x_s2_short_crl_cut30;
2710 } else { 2785 } else {
@@ -2736,7 +2811,7 @@ static int stv090x_optimize_track(struct stv090x_state *state)
2736 s32 srate, pilots, aclc, f_1, f_0, i = 0, blind_tune = 0; 2811 s32 srate, pilots, aclc, f_1, f_0, i = 0, blind_tune = 0;
2737 u32 reg; 2812 u32 reg;
2738 2813
2739 srate = stv090x_get_srate(state, state->mclk); 2814 srate = stv090x_get_srate(state, state->internal->mclk);
2740 srate += stv090x_get_tmgoffst(state, srate); 2815 srate += stv090x_get_tmgoffst(state, srate);
2741 2816
2742 switch (state->delsys) { 2817 switch (state->delsys) {
@@ -2755,7 +2830,7 @@ static int stv090x_optimize_track(struct stv090x_state *state)
2755 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0) 2830 if (STV090x_WRITE_DEMOD(state, DEMOD, reg) < 0)
2756 goto err; 2831 goto err;
2757 2832
2758 if (state->dev_ver >= 0x30) { 2833 if (state->internal->dev_ver >= 0x30) {
2759 if (stv090x_get_viterbi(state) < 0) 2834 if (stv090x_get_viterbi(state) < 0)
2760 goto err; 2835 goto err;
2761 2836
@@ -2872,7 +2947,7 @@ static int stv090x_optimize_track(struct stv090x_state *state)
2872 goto err; 2947 goto err;
2873 } 2948 }
2874 2949
2875 if (state->dev_ver >= 0x20) { 2950 if (state->internal->dev_ver >= 0x20) {
2876 if ((state->search_mode == STV090x_SEARCH_DVBS1) || 2951 if ((state->search_mode == STV090x_SEARCH_DVBS1) ||
2877 (state->search_mode == STV090x_SEARCH_DSS) || 2952 (state->search_mode == STV090x_SEARCH_DSS) ||
2878 (state->search_mode == STV090x_SEARCH_AUTO)) { 2953 (state->search_mode == STV090x_SEARCH_AUTO)) {
@@ -2894,7 +2969,8 @@ static int stv090x_optimize_track(struct stv090x_state *state)
2894 if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x80) < 0) 2969 if (STV090x_WRITE_DEMOD(state, SFRLOW1, 0x80) < 0)
2895 goto err; 2970 goto err;
2896 2971
2897 if ((state->dev_ver >= 0x20) || (blind_tune == 1) || (state->srate < 10000000)) { 2972 if ((state->internal->dev_ver >= 0x20) || (blind_tune == 1) ||
2973 (state->srate < 10000000)) {
2898 /* update initial carrier freq with the found freq offset */ 2974 /* update initial carrier freq with the found freq offset */
2899 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0) 2975 if (STV090x_WRITE_DEMOD(state, CFRINIT1, f_1) < 0)
2900 goto err; 2976 goto err;
@@ -2902,7 +2978,7 @@ static int stv090x_optimize_track(struct stv090x_state *state)
2902 goto err; 2978 goto err;
2903 state->tuner_bw = stv090x_car_width(srate, state->rolloff) + 10000000; 2979 state->tuner_bw = stv090x_car_width(srate, state->rolloff) + 10000000;
2904 2980
2905 if ((state->dev_ver >= 0x20) || (blind_tune == 1)) { 2981 if ((state->internal->dev_ver >= 0x20) || (blind_tune == 1)) {
2906 2982
2907 if (state->algo != STV090x_WARM_SEARCH) { 2983 if (state->algo != STV090x_WARM_SEARCH) {
2908 2984
@@ -2954,7 +3030,7 @@ static int stv090x_optimize_track(struct stv090x_state *state)
2954 3030
2955 } 3031 }
2956 3032
2957 if (state->dev_ver >= 0x20) { 3033 if (state->internal->dev_ver >= 0x20) {
2958 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0) 3034 if (STV090x_WRITE_DEMOD(state, CARFREQ, 0x49) < 0)
2959 goto err; 3035 goto err;
2960 } 3036 }
@@ -3030,7 +3106,7 @@ static int stv090x_set_s2rolloff(struct stv090x_state *state)
3030{ 3106{
3031 u32 reg; 3107 u32 reg;
3032 3108
3033 if (state->dev_ver <= 0x20) { 3109 if (state->internal->dev_ver <= 0x20) {
3034 /* rolloff to auto mode if DVBS2 */ 3110 /* rolloff to auto mode if DVBS2 */
3035 reg = STV090x_READ_DEMOD(state, DEMOD); 3111 reg = STV090x_READ_DEMOD(state, DEMOD);
3036 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 0x00); 3112 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 0x00);
@@ -3066,7 +3142,7 @@ static enum stv090x_signal_state stv090x_algo(struct stv090x_state *state)
3066 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod stop */ 3142 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Demod stop */
3067 goto err; 3143 goto err;
3068 3144
3069 if (state->dev_ver >= 0x20) { 3145 if (state->internal->dev_ver >= 0x20) {
3070 if (state->srate > 5000000) { 3146 if (state->srate > 5000000) {
3071 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0) 3147 if (STV090x_WRITE_DEMOD(state, CORRELABS, 0x9e) < 0)
3072 goto err; 3148 goto err;
@@ -3106,7 +3182,7 @@ static enum stv090x_signal_state stv090x_algo(struct stv090x_state *state)
3106 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0) 3182 if (STV090x_WRITE_DEMOD(state, AGC2REF, 0x38) < 0)
3107 goto err; 3183 goto err;
3108 3184
3109 if (state->dev_ver >= 0x20) { 3185 if (state->internal->dev_ver >= 0x20) {
3110 if (STV090x_WRITE_DEMOD(state, KREFTMG, 0x5a) < 0) 3186 if (STV090x_WRITE_DEMOD(state, KREFTMG, 0x5a) < 0)
3111 goto err; 3187 goto err;
3112 if (state->algo == STV090x_COLD_SEARCH) 3188 if (state->algo == STV090x_COLD_SEARCH)
@@ -3124,9 +3200,11 @@ static enum stv090x_signal_state stv090x_algo(struct stv090x_state *state)
3124 if (stv090x_set_srate(state, state->srate) < 0) 3200 if (stv090x_set_srate(state, state->srate) < 0)
3125 goto err; 3201 goto err;
3126 3202
3127 if (stv090x_set_max_srate(state, state->mclk, state->srate) < 0) 3203 if (stv090x_set_max_srate(state, state->internal->mclk,
3204 state->srate) < 0)
3128 goto err; 3205 goto err;
3129 if (stv090x_set_min_srate(state, state->mclk, state->srate) < 0) 3206 if (stv090x_set_min_srate(state, state->internal->mclk,
3207 state->srate) < 0)
3130 goto err; 3208 goto err;
3131 3209
3132 if (state->srate >= 10000000) 3210 if (state->srate >= 10000000)
@@ -3198,7 +3276,7 @@ static enum stv090x_signal_state stv090x_algo(struct stv090x_state *state)
3198 reg = STV090x_READ_DEMOD(state, DEMOD); 3276 reg = STV090x_READ_DEMOD(state, DEMOD);
3199 STV090x_SETFIELD_Px(reg, SPECINV_CONTROL_FIELD, state->inversion); 3277 STV090x_SETFIELD_Px(reg, SPECINV_CONTROL_FIELD, state->inversion);
3200 3278
3201 if (state->dev_ver <= 0x20) { 3279 if (state->internal->dev_ver <= 0x20) {
3202 /* rolloff to auto mode if DVBS2 */ 3280 /* rolloff to auto mode if DVBS2 */
3203 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 1); 3281 STV090x_SETFIELD_Px(reg, MANUAL_SXROLLOFF_FIELD, 1);
3204 } else { 3282 } else {
@@ -3242,7 +3320,7 @@ static enum stv090x_signal_state stv090x_algo(struct stv090x_state *state)
3242 if ((lock) && (signal_state == STV090x_RANGEOK)) { /* signal within Range */ 3320 if ((lock) && (signal_state == STV090x_RANGEOK)) { /* signal within Range */
3243 stv090x_optimize_track(state); 3321 stv090x_optimize_track(state);
3244 3322
3245 if (state->dev_ver >= 0x20) { 3323 if (state->internal->dev_ver >= 0x20) {
3246 /* >= Cut 2.0 :release TS reset after 3324 /* >= Cut 2.0 :release TS reset after
3247 * demod lock and optimized Tracking 3325 * demod lock and optimized Tracking
3248 */ 3326 */
@@ -3774,6 +3852,15 @@ static void stv090x_release(struct dvb_frontend *fe)
3774{ 3852{
3775 struct stv090x_state *state = fe->demodulator_priv; 3853 struct stv090x_state *state = fe->demodulator_priv;
3776 3854
3855 state->internal->num_used--;
3856 if (state->internal->num_used <= 0) {
3857
3858 dprintk(FE_ERROR, 1, "Actually removing");
3859
3860 remove_dev(state->internal);
3861 kfree(state->internal);
3862 }
3863
3777 kfree(state); 3864 kfree(state);
3778} 3865}
3779 3866
@@ -3905,10 +3992,10 @@ static int stv090x_set_mclk(struct stv090x_state *state, u32 mclk, u32 clk)
3905 if (stv090x_write_reg(state, STV090x_NCOARSE, reg) < 0) 3992 if (stv090x_write_reg(state, STV090x_NCOARSE, reg) < 0)
3906 goto err; 3993 goto err;
3907 3994
3908 state->mclk = stv090x_get_mclk(state); 3995 state->internal->mclk = stv090x_get_mclk(state);
3909 3996
3910 /*Set the DiseqC frequency to 22KHz */ 3997 /*Set the DiseqC frequency to 22KHz */
3911 div = state->mclk / 704000; 3998 div = state->internal->mclk / 704000;
3912 if (STV090x_WRITE_DEMOD(state, F22TX, div) < 0) 3999 if (STV090x_WRITE_DEMOD(state, F22TX, div) < 0)
3913 goto err; 4000 goto err;
3914 if (STV090x_WRITE_DEMOD(state, F22RX, div) < 0) 4001 if (STV090x_WRITE_DEMOD(state, F22RX, div) < 0)
@@ -3924,7 +4011,7 @@ static int stv090x_set_tspath(struct stv090x_state *state)
3924{ 4011{
3925 u32 reg; 4012 u32 reg;
3926 4013
3927 if (state->dev_ver >= 0x20) { 4014 if (state->internal->dev_ver >= 0x20) {
3928 switch (state->config->ts1_mode) { 4015 switch (state->config->ts1_mode) {
3929 case STV090x_TSMODE_PARALLEL_PUNCTURED: 4016 case STV090x_TSMODE_PARALLEL_PUNCTURED:
3930 case STV090x_TSMODE_DVBCI: 4017 case STV090x_TSMODE_DVBCI:
@@ -4192,16 +4279,26 @@ static int stv090x_setup(struct dvb_frontend *fe)
4192 } 4279 }
4193 4280
4194 /* STV090x init */ 4281 /* STV090x init */
4195 if (STV090x_WRITE_DEMOD(state, DMDISTATE, 0x5c) < 0) /* Stop Demod */ 4282
4283 /* Stop Demod */
4284 if (stv090x_write_reg(state, STV090x_P1_DMDISTATE, 0x5c) < 0)
4285 goto err;
4286 if (stv090x_write_reg(state, STV090x_P2_DMDISTATE, 0x5c) < 0)
4196 goto err; 4287 goto err;
4197 4288
4198 msleep(5); 4289 msleep(5);
4199 4290
4200 if (STV090x_WRITE_DEMOD(state, TNRCFG, 0x6c) < 0) /* check register ! (No Tuner Mode) */ 4291 /* Set No Tuner Mode */
4292 if (stv090x_write_reg(state, STV090x_P1_TNRCFG, 0x6c) < 0)
4293 goto err;
4294 if (stv090x_write_reg(state, STV090x_P2_TNRCFG, 0x6c) < 0)
4201 goto err; 4295 goto err;
4202 4296
4297 /* I2C repeater OFF */
4203 STV090x_SETFIELD_Px(reg, ENARPT_LEVEL_FIELD, config->repeater_level); 4298 STV090x_SETFIELD_Px(reg, ENARPT_LEVEL_FIELD, config->repeater_level);
4204 if (STV090x_WRITE_DEMOD(state, I2CRPT, reg) < 0) /* repeater OFF */ 4299 if (stv090x_write_reg(state, STV090x_P1_I2CRPT, reg) < 0)
4300 goto err;
4301 if (stv090x_write_reg(state, STV090x_P2_I2CRPT, reg) < 0)
4205 goto err; 4302 goto err;
4206 4303
4207 if (stv090x_write_reg(state, STV090x_NCOARSE, 0x13) < 0) /* set PLL divider */ 4304 if (stv090x_write_reg(state, STV090x_NCOARSE, 0x13) < 0) /* set PLL divider */
@@ -4220,8 +4317,8 @@ static int stv090x_setup(struct dvb_frontend *fe)
4220 goto err; 4317 goto err;
4221 } 4318 }
4222 4319
4223 state->dev_ver = stv090x_read_reg(state, STV090x_MID); 4320 state->internal->dev_ver = stv090x_read_reg(state, STV090x_MID);
4224 if (state->dev_ver >= 0x20) { 4321 if (state->internal->dev_ver >= 0x20) {
4225 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0) 4322 if (stv090x_write_reg(state, STV090x_TSGENERAL, 0x0c) < 0)
4226 goto err; 4323 goto err;
4227 4324
@@ -4232,15 +4329,15 @@ static int stv090x_setup(struct dvb_frontend *fe)
4232 goto err; 4329 goto err;
4233 } 4330 }
4234 4331
4235 } else if (state->dev_ver < 0x20) { 4332 } else if (state->internal->dev_ver < 0x20) {
4236 dprintk(FE_ERROR, 1, "ERROR: Unsupported Cut: 0x%02x!", 4333 dprintk(FE_ERROR, 1, "ERROR: Unsupported Cut: 0x%02x!",
4237 state->dev_ver); 4334 state->internal->dev_ver);
4238 4335
4239 goto err; 4336 goto err;
4240 } else if (state->dev_ver > 0x30) { 4337 } else if (state->internal->dev_ver > 0x30) {
4241 /* we shouldn't bail out from here */ 4338 /* we shouldn't bail out from here */
4242 dprintk(FE_ERROR, 1, "INFO: Cut: 0x%02x probably incomplete support!", 4339 dprintk(FE_ERROR, 1, "INFO: Cut: 0x%02x probably incomplete support!",
4243 state->dev_ver); 4340 state->internal->dev_ver);
4244 } 4341 }
4245 4342
4246 if (stv090x_write_reg(state, STV090x_TSTRES0, 0x80) < 0) 4343 if (stv090x_write_reg(state, STV090x_TSTRES0, 0x80) < 0)
@@ -4303,6 +4400,7 @@ struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
4303 enum stv090x_demodulator demod) 4400 enum stv090x_demodulator demod)
4304{ 4401{
4305 struct stv090x_state *state = NULL; 4402 struct stv090x_state *state = NULL;
4403 struct stv090x_dev *temp_int;
4306 4404
4307 state = kzalloc(sizeof (struct stv090x_state), GFP_KERNEL); 4405 state = kzalloc(sizeof (struct stv090x_state), GFP_KERNEL);
4308 if (state == NULL) 4406 if (state == NULL)
@@ -4318,8 +4416,29 @@ struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
4318 state->device = config->device; 4416 state->device = config->device;
4319 state->rolloff = STV090x_RO_35; /* default */ 4417 state->rolloff = STV090x_RO_35; /* default */
4320 4418
4321 if (state->demod == STV090x_DEMODULATOR_0) 4419 temp_int = find_dev(state->i2c,
4322 mutex_init(&demod_lock); 4420 state->config->address);
4421
4422 if ((temp_int != NULL) && (state->demod_mode == STV090x_DUAL)) {
4423 state->internal = temp_int->internal;
4424 state->internal->num_used++;
4425 dprintk(FE_INFO, 1, "Found Internal Structure!");
4426 dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x",
4427 state->device == STV0900 ? "STV0900" : "STV0903",
4428 demod,
4429 state->internal->dev_ver);
4430 return &state->frontend;
4431 } else {
4432 state->internal = kmalloc(sizeof(struct stv090x_internal),
4433 GFP_KERNEL);
4434 temp_int = append_internal(state->internal);
4435 state->internal->num_used = 1;
4436 state->internal->i2c_adap = state->i2c;
4437 state->internal->i2c_addr = state->config->address;
4438 dprintk(FE_INFO, 1, "Create New Internal Structure!");
4439 }
4440
4441 mutex_init(&state->internal->demod_lock);
4323 4442
4324 if (stv090x_sleep(&state->frontend) < 0) { 4443 if (stv090x_sleep(&state->frontend) < 0) {
4325 dprintk(FE_ERROR, 1, "Error putting device to sleep"); 4444 dprintk(FE_ERROR, 1, "Error putting device to sleep");
@@ -4335,10 +4454,10 @@ struct dvb_frontend *stv090x_attach(const struct stv090x_config *config,
4335 goto error; 4454 goto error;
4336 } 4455 }
4337 4456
4338 dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x\n", 4457 dprintk(FE_ERROR, 1, "Attaching %s demodulator(%d) Cut=0x%02x",
4339 state->device == STV0900 ? "STV0900" : "STV0903", 4458 state->device == STV0900 ? "STV0900" : "STV0903",
4340 demod, 4459 demod,
4341 state->dev_ver); 4460 state->internal->dev_ver);
4342 4461
4343 return &state->frontend; 4462 return &state->frontend;
4344 4463
diff --git a/drivers/media/dvb/frontends/stv090x.h b/drivers/media/dvb/frontends/stv090x.h
index b133807663ea..e009183ad772 100644
--- a/drivers/media/dvb/frontends/stv090x.h
+++ b/drivers/media/dvb/frontends/stv090x.h
@@ -68,8 +68,6 @@ struct stv090x_config {
68 u32 xtal; /* default: 8000000 */ 68 u32 xtal; /* default: 8000000 */
69 u8 address; /* default: 0x68 */ 69 u8 address; /* default: 0x68 */
70 70
71 u32 ref_clk; /* default: 16000000 FIXME to tuner config */
72
73 u8 ts1_mode; 71 u8 ts1_mode;
74 u8 ts2_mode; 72 u8 ts2_mode;
75 73
diff --git a/drivers/media/dvb/frontends/stv090x_priv.h b/drivers/media/dvb/frontends/stv090x_priv.h
index 5921a8d6c89f..d38542665b0a 100644
--- a/drivers/media/dvb/frontends/stv090x_priv.h
+++ b/drivers/media/dvb/frontends/stv090x_priv.h
@@ -230,11 +230,22 @@ struct stv090x_tab {
230 s32 read; 230 s32 read;
231}; 231};
232 232
233struct stv090x_internal {
234 struct i2c_adapter *i2c_adap;
235 u8 i2c_addr;
236
237 struct mutex demod_lock; /* Lock access to shared register */
238 s32 mclk; /* Masterclock Divider factor */
239 u32 dev_ver;
240
241 int num_used;
242};
243
233struct stv090x_state { 244struct stv090x_state {
234 enum stv090x_device device; 245 enum stv090x_device device;
235 enum stv090x_demodulator demod; 246 enum stv090x_demodulator demod;
236 enum stv090x_mode demod_mode; 247 enum stv090x_mode demod_mode;
237 u32 dev_ver; 248 struct stv090x_internal *internal;
238 249
239 struct i2c_adapter *i2c; 250 struct i2c_adapter *i2c;
240 const struct stv090x_config *config; 251 const struct stv090x_config *config;
@@ -256,11 +267,8 @@ struct stv090x_state {
256 u32 frequency; 267 u32 frequency;
257 u32 srate; 268 u32 srate;
258 269
259 s32 mclk; /* Masterclock Divider factor */
260 s32 tuner_bw; 270 s32 tuner_bw;
261 271
262 u32 tuner_refclk;
263
264 s32 search_range; 272 s32 search_range;
265 273
266 s32 DemodTimeout; 274 s32 DemodTimeout;
diff --git a/drivers/media/dvb/ttpci/budget.c b/drivers/media/dvb/ttpci/budget.c
index e48380c48990..d9aa9bd31b80 100644
--- a/drivers/media/dvb/ttpci/budget.c
+++ b/drivers/media/dvb/ttpci/budget.c
@@ -435,7 +435,6 @@ static struct stv090x_config tt1600_stv090x_config = {
435 435
436 .xtal = 27000000, 436 .xtal = 27000000,
437 .address = 0x68, 437 .address = 0x68,
438 .ref_clk = 27000000,
439 438
440 .ts1_mode = STV090x_TSMODE_DVBCI, 439 .ts1_mode = STV090x_TSMODE_DVBCI,
441 .ts2_mode = STV090x_TSMODE_SERIAL_CONTINUOUS, 440 .ts2_mode = STV090x_TSMODE_SERIAL_CONTINUOUS,