2 * Driver for Micronas drx397xD demodulator
4 * Copyright (C) 2007 Henk Vergonet <Henk.Vergonet@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; If not, see <http://www.gnu.org/licenses/>.
20 #define DEBUG /* uncomment if you want debugging output */
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/init.h>
25 #include <linux/device.h>
26 #include <linux/delay.h>
27 #include <linux/string.h>
28 #include <linux/firmware.h>
29 #include <asm/div64.h>
31 #include "dvb_frontend.h"
34 static const char mod_name[] = "drx397xD";
36 #define MAX_CLOCK_DRIFT 200 /* maximal 200 PPM allowed */
42 #define _FW_ENTRY(a, b, c) b
43 #include "drx397xD_fw.h"
47 struct drx397xD_state {
48 struct i2c_adapter *i2c;
49 struct dvb_frontend frontend;
50 struct drx397xD_config config;
53 u32 bandwidth_parm; /* internal bandwidth conversions */
54 u32 f_osc; /* w90: actual osc frequency [Hz] */
58 static const char *blob_name[] = {
59 #define _BLOB_ENTRY(a, b) a
60 #include "drx397xD_fw.h"
64 #define _BLOB_ENTRY(a, b) b
65 #include "drx397xD_fw.h"
70 const struct firmware *file;
73 const u8 *data[ARRAY_SIZE(blob_name)];
75 #define _FW_ENTRY(a, b, c) { \
78 .lock = __RW_LOCK_UNLOCKED(fw[c].lock), \
81 #include "drx397xD_fw.h"
84 /* use only with writer lock aquired */
85 static void _drx_release_fw(struct drx397xD_state *s, enum fw_ix ix)
87 memset(&fw[ix].data[0], 0, sizeof(fw[0].data));
89 release_firmware(fw[ix].file);
92 static void drx_release_fw(struct drx397xD_state *s)
94 enum fw_ix ix = s->chip_rev;
96 pr_debug("%s\n", __func__);
98 write_lock(&fw[ix].lock);
101 if (fw[ix].refcnt == 0)
102 _drx_release_fw(s, ix);
104 write_unlock(&fw[ix].lock);
107 static int drx_load_fw(struct drx397xD_state *s, enum fw_ix ix)
111 int i = 0, j, rc = -EINVAL;
113 pr_debug("%s\n", __func__);
115 if (ix < 0 || ix >= ARRAY_SIZE(fw))
119 write_lock(&fw[ix].lock);
124 memset(&fw[ix].data[0], 0, sizeof(fw[0].data));
126 if (request_firmware(&fw[ix].file, fw[ix].name, &s->i2c->dev) != 0) {
127 printk(KERN_ERR "%s: Firmware \"%s\" not available\n",
128 mod_name, fw[ix].name);
133 if (!fw[ix].file->data || fw[ix].file->size < 10)
136 data = fw[ix].file->data;
137 size = fw[ix].file->size;
139 if (data[i++] != 2) /* check firmware version */
144 case 0x00: /* bytecode */
148 case 0x01: /* reset */
149 case 0x02: /* sleep */
152 case 0xfe: /* name */
153 len = strnlen(&data[i], size - i);
154 if (i + len + 1 >= size)
156 if (data[i + len + 1] != 0)
158 for (j = 0; j < ARRAY_SIZE(blob_name); j++) {
159 if (strcmp(blob_name[j], &data[i]) == 0) {
160 fw[ix].data[j] = &data[i + len + 1];
161 pr_debug("Loading %s\n", blob_name[j]);
166 case 0xff: /* file terminator */
177 printk(KERN_ERR "%s: Firmware is corrupt\n", mod_name);
179 _drx_release_fw(s, ix);
183 write_unlock(&fw[ix].lock);
189 static int write_fw(struct drx397xD_state *s, enum blob_ix ix)
192 int len, rc = 0, i = 0;
193 struct i2c_msg msg = {
194 .addr = s->config.demod_address,
198 if (ix < 0 || ix >= ARRAY_SIZE(blob_name)) {
199 pr_debug("%s drx_fw_ix_t out of range\n", __func__);
202 pr_debug("%s %s\n", __func__, blob_name[ix]);
204 read_lock(&fw[s->chip_rev].lock);
205 data = fw[s->chip_rev].data[ix];
213 case 0: /* bytecode */
216 msg.buf = (__u8 *) &data[i];
217 if (i2c_transfer(s->i2c, &msg, 1) != 1) {
232 read_unlock(&fw[s->chip_rev].lock);
237 /* Function is not endian safe, use the RD16 wrapper below */
238 static int _read16(struct drx397xD_state *s, __le32 i2c_adr)
243 struct i2c_msg msg[2] = {
245 .addr = s->config.demod_address,
250 .addr = s->config.demod_address,
257 *(__le32 *) a = i2c_adr;
259 rc = i2c_transfer(s->i2c, msg, 2);
263 return le16_to_cpu(v);
266 /* Function is not endian safe, use the WR16.. wrappers below */
267 static int _write16(struct drx397xD_state *s, __le32 i2c_adr, __le16 val)
271 struct i2c_msg msg = {
272 .addr = s->config.demod_address,
278 *(__le32 *)a = i2c_adr;
279 *(__le16 *)&a[4] = val;
281 rc = i2c_transfer(s->i2c, &msg, 1);
288 #define WR16(ss, adr, val) \
289 _write16(ss, I2C_ADR_C0(adr), cpu_to_le16(val))
290 #define WR16_E0(ss, adr, val) \
291 _write16(ss, I2C_ADR_E0(adr), cpu_to_le16(val))
292 #define RD16(ss, adr) \
293 _read16(ss, I2C_ADR_C0(adr))
295 #define EXIT_RC(cmd) \
296 if ((rc = (cmd)) < 0) \
300 static int PLL_Set(struct drx397xD_state *s,
301 struct dvb_frontend_parameters *fep, int *df_tuner)
303 struct dvb_frontend *fe = &s->frontend;
304 u32 f_tuner, f = fep->frequency;
307 pr_debug("%s\n", __func__);
309 if ((f > s->frontend.ops.tuner_ops.info.frequency_max) ||
310 (f < s->frontend.ops.tuner_ops.info.frequency_min))
314 if (!s->frontend.ops.tuner_ops.set_params ||
315 !s->frontend.ops.tuner_ops.get_frequency)
318 rc = s->frontend.ops.tuner_ops.set_params(fe, fep);
322 rc = s->frontend.ops.tuner_ops.get_frequency(fe, &f_tuner);
326 *df_tuner = f_tuner - f;
327 pr_debug("%s requested %d [Hz] tuner %d [Hz]\n", __func__, f,
333 /* Demodulator helper functions */
334 static int SC_WaitForReady(struct drx397xD_state *s)
339 pr_debug("%s\n", __func__);
342 rc = RD16(s, 0x820043);
350 static int SC_SendCommand(struct drx397xD_state *s, int cmd)
354 pr_debug("%s\n", __func__);
356 WR16(s, 0x820043, cmd);
358 rc = RD16(s, 0x820042);
359 if ((rc & 0xffff) == 0xffff)
365 static int HI_Command(struct drx397xD_state *s, u16 cmd)
369 pr_debug("%s\n", __func__);
371 rc = WR16(s, 0x420032, cmd);
376 rc = RD16(s, 0x420032);
378 rc = RD16(s, 0x420031);
388 static int HI_CfgCommand(struct drx397xD_state *s)
391 pr_debug("%s\n", __func__);
393 WR16(s, 0x420033, 0x3973);
394 WR16(s, 0x420034, s->config.w50); /* code 4, log 4 */
395 WR16(s, 0x420035, s->config.w52); /* code 15, log 9 */
396 WR16(s, 0x420036, s->config.demod_address << 1);
397 WR16(s, 0x420037, s->config.w56); /* code (set_i2c ?? initX 1 ), log 1 */
398 /* WR16(s, 0x420033, 0x3973); */
399 if ((s->config.w56 & 8) == 0)
400 return HI_Command(s, 3);
402 return WR16(s, 0x420032, 0x3);
405 static const u8 fastIncrDecLUT_15273[] = {
406 0x0e, 0x0f, 0x0f, 0x10, 0x11, 0x12, 0x12, 0x13, 0x14,
407 0x15, 0x16, 0x17, 0x18, 0x1a, 0x1b, 0x1c, 0x1d, 0x1f
410 static const u8 slowIncrDecLUT_15272[] = {
414 static int SetCfgIfAgc(struct drx397xD_state *s, struct drx397xD_CfgIfAgc *agc)
420 int quot, rem, i, rc = -EINVAL;
422 pr_debug("%s\n", __func__);
424 if (agc->w04 > 0x3ff)
428 EXIT_RC(RD16(s, 0x0c20010));
430 EXIT_RC(WR16(s, 0x0c20010, rc));
431 return WR16(s, 0x0c20030, agc->w04 & 0x7ff);
445 EXIT_RC(RD16(s, 0x0c20010));
447 EXIT_RC(WR16(s, 0x0c20010, rc));
449 EXIT_RC(WR16(s, 0x0c20025, (w06 >> 1) & 0x1ff));
450 EXIT_RC(WR16(s, 0x0c20031, (w0A - w08) >> 1));
451 EXIT_RC(WR16(s, 0x0c20032, ((w0A + w08) >> 1) - 0x1ff));
462 EXIT_RC(WR16(s, 0x0c20024, quot));
464 i = fastIncrDecLUT_15273[rem / 8];
465 EXIT_RC(WR16(s, 0x0c2002d, i));
466 EXIT_RC(WR16(s, 0x0c2002e, i));
468 i = slowIncrDecLUT_15272[rem / 28];
469 EXIT_RC(WR16(s, 0x0c2002b, i));
470 rc = WR16(s, 0x0c2002c, i);
475 static int SetCfgRfAgc(struct drx397xD_state *s, struct drx397xD_CfgRfAgc *agc)
481 pr_debug("%s %d 0x%x 0x%x\n", __func__, agc->d00, w04, w06);
491 EXIT_RC(WR16(s, 0x0c20036, w04));
493 EXIT_RC(WR16(s, 0x0c20015, s->config.w9C));
494 EXIT_RC(RD16(s, 0x0c20010));
496 EXIT_RC(WR16(s, 0x0c20010, rc));
497 EXIT_RC(RD16(s, 0x0c20013));
503 EXIT_RC(WR16(s, 0x0c20015, s->config.w9C));
504 EXIT_RC(RD16(s, 0x0c20010));
507 EXIT_RC(WR16(s, 0x0c20010, rc));
508 EXIT_RC(WR16(s, 0x0c20051, (w06 >> 4) & 0x3f));
509 EXIT_RC(RD16(s, 0x0c20013));
514 EXIT_RC(WR16(s, 0x0c20015, s->config.w9C));
515 EXIT_RC(RD16(s, 0x0c20010));
517 EXIT_RC(WR16(s, 0x0c20010, rc));
519 EXIT_RC(WR16(s, 0x0c20036, 0));
521 EXIT_RC(RD16(s, 0x0c20013));
524 rc = WR16(s, 0x0c20013, rc);
530 static int GetLockStatus(struct drx397xD_state *s, int *lockstat)
536 rc = RD16(s, 0x082004b);
540 if (s->config.d60 != 2)
552 static int CorrectSysClockDeviation(struct drx397xD_state *s)
558 pr_debug("%s\n", __func__);
560 if (s->config.d5C == 0) {
561 EXIT_RC(WR16(s, 0x08200e8, 0x010));
562 EXIT_RC(WR16(s, 0x08200e9, 0x113));
566 if (s->config.d5C != 1)
569 rc = RD16(s, 0x0820048);
571 rc = GetLockStatus(s, &lockstat);
574 if ((lockstat & 1) == 0)
577 EXIT_RC(WR16(s, 0x0420033, 0x200));
578 EXIT_RC(WR16(s, 0x0420034, 0xc5));
579 EXIT_RC(WR16(s, 0x0420035, 0x10));
580 EXIT_RC(WR16(s, 0x0420036, 0x1));
581 EXIT_RC(WR16(s, 0x0420037, 0xa));
582 EXIT_RC(HI_Command(s, 6));
583 EXIT_RC(RD16(s, 0x0420040));
585 EXIT_RC(RD16(s, 0x0420041));
593 if (!s->bandwidth_parm)
596 /* round & convert to Hz */
597 clk = ((u64) (clk + 0x800000) * s->bandwidth_parm + (1 << 20)) >> 21;
598 clk_limit = s->config.f_osc * MAX_CLOCK_DRIFT / 1000;
600 if (clk - s->config.f_osc * 1000 + clk_limit <= 2 * clk_limit) {
602 pr_debug("%s: osc %d %d [Hz]\n", __func__,
603 s->config.f_osc * 1000, clk - s->config.f_osc * 1000);
605 rc = WR16(s, 0x08200e8, 0);
611 static int ConfigureMPEGOutput(struct drx397xD_state *s, int type)
615 pr_debug("%s\n", __func__);
618 if (s->config.w98 == 0) {
625 if (s->config.w9A == 0)
630 EXIT_RC(WR16(s, 0x2150045, 0));
631 EXIT_RC(WR16(s, 0x2150010, si));
632 EXIT_RC(WR16(s, 0x2150011, bp));
633 rc = WR16(s, 0x2150012, (type == 0 ? 0xfff : 0));
639 static int drx_tune(struct drx397xD_state *s,
640 struct dvb_frontend_parameters *fep)
646 u32 edi = 0, ebx = 0, ebp = 0, edx = 0;
647 u16 v20 = 0, v1E = 0, v16 = 0, v14 = 0, v12 = 0, v10 = 0, v0E = 0;
649 int rc, df_tuner = 0;
651 pr_debug("%s %d\n", __func__, s->config.d60);
653 if (s->config.d60 != 2)
655 rc = CorrectSysClockDeviation(s);
660 rc = ConfigureMPEGOutput(s, 0);
665 rc = PLL_Set(s, fep, &df_tuner);
667 printk(KERN_ERR "Error in pll_set\n");
672 a = rc = RD16(s, 0x2150016);
675 b = rc = RD16(s, 0x2150010);
678 c = rc = RD16(s, 0x2150034);
681 d = rc = RD16(s, 0x2150035);
684 rc = WR16(s, 0x2150014, c);
685 rc = WR16(s, 0x2150015, d);
686 rc = WR16(s, 0x2150010, 0);
687 rc = WR16(s, 0x2150000, 2);
688 rc = WR16(s, 0x2150036, 0x0fff);
689 rc = WR16(s, 0x2150016, a);
691 rc = WR16(s, 0x2150010, 2);
692 rc = WR16(s, 0x2150007, 0);
693 rc = WR16(s, 0x2150000, 1);
694 rc = WR16(s, 0x2110000, 0);
695 rc = WR16(s, 0x0800000, 0);
696 rc = WR16(s, 0x2800000, 0);
697 rc = WR16(s, 0x2110010, 0x664);
699 rc = write_fw(s, DRXD_ResetECRAM);
700 rc = WR16(s, 0x2110000, 1);
702 rc = write_fw(s, DRXD_InitSC);
706 rc = SetCfgIfAgc(s, &s->config.ifagc);
710 rc = SetCfgRfAgc(s, &s->config.rfagc);
714 if (fep->u.ofdm.transmission_mode != TRANSMISSION_MODE_2K)
716 switch (fep->u.ofdm.transmission_mode) {
717 case TRANSMISSION_MODE_8K:
719 if (s->chip_rev == DRXD_FW_B1)
722 rc = WR16(s, 0x2010010, 0);
731 if (s->chip_rev == DRXD_FW_B1)
734 rc = WR16(s, 0x2010010, 1);
743 switch (fep->u.ofdm.guard_interval) {
744 case GUARD_INTERVAL_1_4:
747 case GUARD_INTERVAL_1_8:
750 case GUARD_INTERVAL_1_16:
753 case GUARD_INTERVAL_1_32:
769 switch (fep->u.ofdm.hierarchy_information) {
772 if (s->chip_rev == DRXD_FW_B1)
774 rc = WR16(s, 0x1c10047, 1);
777 rc = WR16(s, 0x2010012, 1);
792 if (s->chip_rev == DRXD_FW_B1)
794 rc = WR16(s, 0x1c10047, 2);
797 rc = WR16(s, 0x2010012, 2);
812 if (s->chip_rev == DRXD_FW_B1)
814 rc = WR16(s, 0x1c10047, 3);
817 rc = WR16(s, 0x2010012, 3);
832 if (s->chip_rev == DRXD_FW_B1)
834 rc = WR16(s, 0x1c10047, 0);
837 rc = WR16(s, 0x2010012, 0);
840 /* QPSK QAM16 QAM64 */
841 ebx = 0x19f; /* 62 */
842 ebp = 0x1fb; /* 15 */
843 v20 = 0x16a; /* 62 */
844 v1E = 0x195; /* 62 */
845 v16 = 0x1bb; /* 15 */
846 v14 = 0x1ef; /* 15 */
852 switch (fep->u.ofdm.constellation) {
856 if (s->chip_rev == DRXD_FW_B1)
859 rc = WR16(s, 0x1c10046, 0);
862 rc = WR16(s, 0x2010011, 0);
865 rc = WR16(s, 0x201001a, 0x10);
868 rc = WR16(s, 0x201001b, 0);
871 rc = WR16(s, 0x201001c, 0);
874 rc = WR16(s, 0x1c10062, v20);
877 rc = WR16(s, 0x1c1002a, v1C);
880 rc = WR16(s, 0x1c10015, v16);
883 rc = WR16(s, 0x1c10016, v12);
889 if (s->chip_rev == DRXD_FW_B1)
892 rc = WR16(s, 0x1c10046, 1);
895 rc = WR16(s, 0x2010011, 1);
898 rc = WR16(s, 0x201001a, 0x10);
901 rc = WR16(s, 0x201001b, 4);
904 rc = WR16(s, 0x201001c, 0);
907 rc = WR16(s, 0x1c10062, v1E);
910 rc = WR16(s, 0x1c1002a, v1A);
913 rc = WR16(s, 0x1c10015, v14);
916 rc = WR16(s, 0x1c10016, v10);
922 rc = WR16(s, 0x1c10046, 2);
925 rc = WR16(s, 0x2010011, 2);
928 rc = WR16(s, 0x201001a, 0x20);
931 rc = WR16(s, 0x201001b, 8);
934 rc = WR16(s, 0x201001c, 2);
937 rc = WR16(s, 0x1c10062, ebx);
940 rc = WR16(s, 0x1c1002a, v18);
943 rc = WR16(s, 0x1c10015, ebp);
946 rc = WR16(s, 0x1c10016, v0E);
952 if (s->config.s20d24 == 1) {
953 rc = WR16(s, 0x2010013, 0);
955 rc = WR16(s, 0x2010013, 1);
959 switch (fep->u.ofdm.code_rate_HP) {
963 if (s->chip_rev == DRXD_FW_B1)
965 rc = WR16(s, 0x2090011, 0);
969 if (s->chip_rev == DRXD_FW_B1)
971 rc = WR16(s, 0x2090011, 1);
975 if (s->chip_rev == DRXD_FW_B1)
977 rc = WR16(s, 0x2090011, 2);
979 case FEC_5_6: /* 5 */
981 if (s->chip_rev == DRXD_FW_B1)
983 rc = WR16(s, 0x2090011, 3);
985 case FEC_7_8: /* 7 */
987 if (s->chip_rev == DRXD_FW_B1)
989 rc = WR16(s, 0x2090011, 4);
995 switch (fep->u.ofdm.bandwidth) {
999 case BANDWIDTH_8_MHZ: /* 0 */
1000 case BANDWIDTH_AUTO:
1001 rc = WR16(s, 0x0c2003f, 0x32);
1002 s->bandwidth_parm = ebx = 0x8b8249;
1005 case BANDWIDTH_7_MHZ:
1006 rc = WR16(s, 0x0c2003f, 0x3b);
1007 s->bandwidth_parm = ebx = 0x7a1200;
1010 case BANDWIDTH_6_MHZ:
1011 rc = WR16(s, 0x0c2003f, 0x47);
1012 s->bandwidth_parm = ebx = 0x68a1b6;
1020 rc = WR16(s, 0x08200ec, edx);
1024 rc = RD16(s, 0x0820050);
1027 rc = WR16(s, 0x0820050, rc);
1030 /* Configure bandwidth specific factor */
1031 ebx = div64_u64(((u64) (s->f_osc) << 21) + (ebx >> 1),
1032 (u64)ebx) - 0x800000;
1033 EXIT_RC(WR16(s, 0x0c50010, ebx & 0xffff));
1034 EXIT_RC(WR16(s, 0x0c50011, ebx >> 16));
1036 /* drx397xD oscillator calibration */
1037 ebx = div64_u64(((u64) (s->config.f_if + df_tuner) << 28) +
1038 (s->f_osc >> 1), (u64)s->f_osc);
1041 if (fep->inversion == INVERSION_ON)
1042 ebx = 0x10000000 - ebx;
1044 EXIT_RC(WR16(s, 0x0c30010, ebx & 0xffff));
1045 EXIT_RC(WR16(s, 0x0c30011, ebx >> 16));
1047 EXIT_RC(WR16(s, 0x0800000, 1));
1048 EXIT_RC(RD16(s, 0x0800000));
1051 EXIT_RC(SC_WaitForReady(s));
1052 EXIT_RC(WR16(s, 0x0820042, 0));
1053 EXIT_RC(WR16(s, 0x0820041, v22));
1054 EXIT_RC(WR16(s, 0x0820040, edi));
1055 EXIT_RC(SC_SendCommand(s, 3));
1057 rc = RD16(s, 0x0800000);
1060 WR16(s, 0x0820042, 0);
1061 WR16(s, 0x0820041, 1);
1062 WR16(s, 0x0820040, 1);
1063 SC_SendCommand(s, 1);
1066 rc = WR16(s, 0x2150000, 2);
1067 rc = WR16(s, 0x2150016, a);
1068 rc = WR16(s, 0x2150010, 4);
1069 rc = WR16(s, 0x2150036, 0);
1070 rc = WR16(s, 0x2150000, 1);
1077 /*******************************************************************************
1079 ******************************************************************************/
1081 static int drx397x_init(struct dvb_frontend *fe)
1083 struct drx397xD_state *s = fe->demodulator_priv;
1086 pr_debug("%s\n", __func__);
1088 s->config.rfagc.d00 = 2; /* 0x7c */
1089 s->config.rfagc.w04 = 0;
1090 s->config.rfagc.w06 = 0x3ff;
1092 s->config.ifagc.d00 = 0; /* 0x68 */
1093 s->config.ifagc.w04 = 0;
1094 s->config.ifagc.w06 = 140;
1095 s->config.ifagc.w08 = 0;
1096 s->config.ifagc.w0A = 0x3ff;
1097 s->config.ifagc.w0C = 0x388;
1099 /* for signal strenght calculations */
1100 s->config.ss76 = 820;
1101 s->config.ss78 = 2200;
1102 s->config.ss7A = 150;
1108 s->config.f_if = 42800000; /* d14: intermediate frequency [Hz] */
1109 s->config.f_osc = 48000; /* s66 : oscillator frequency [kHz] */
1110 s->config.w92 = 12000;
1112 s->config.w9C = 0x000e;
1113 s->config.w9E = 0x0000;
1115 /* ConfigureMPEGOutput params */
1120 /* get chip revision */
1121 rc = RD16(s, 0x2410019);
1126 printk(KERN_INFO "%s: chip revision A2\n", mod_name);
1127 rc = drx_load_fw(s, DRXD_FW_A2);
1130 rc = (rc >> 12) - 3;
1133 s->flags |= F_SET_0D4h;
1136 s->flags |= F_SET_0D0h;
1142 s->flags |= F_SET_0D4h;
1147 printk(KERN_INFO "%s: chip revision B1.%d\n", mod_name, rc);
1148 rc = drx_load_fw(s, DRXD_FW_B1);
1153 rc = WR16(s, 0x0420033, 0x3973);
1157 rc = HI_Command(s, 2);
1161 if (s->chip_rev == DRXD_FW_A2) {
1162 rc = WR16(s, 0x043012d, 0x47F);
1166 rc = WR16_E0(s, 0x0400000, 0);
1170 if (s->config.w92 > 20000 || s->config.w92 % 4000) {
1171 printk(KERN_ERR "%s: invalid osc frequency\n", mod_name);
1176 rc = WR16(s, 0x2410010, 1);
1179 rc = WR16(s, 0x2410011, 0x15);
1182 rc = WR16(s, 0x2410012, s->config.w92 / 4000);
1186 rc = WR16(s, 0x2410015, 2);
1190 rc = WR16(s, 0x2410017, 0x3973);
1194 s->f_osc = s->config.f_osc * 1000; /* initial estimator */
1198 rc = HI_CfgCommand(s);
1202 rc = write_fw(s, DRXD_InitAtomicRead);
1206 if (s->chip_rev == DRXD_FW_A2) {
1207 rc = WR16(s, 0x2150013, 0);
1212 rc = WR16_E0(s, 0x0400002, 0);
1215 rc = WR16(s, 0x0400002, 0);
1219 if (s->chip_rev == DRXD_FW_A2) {
1220 rc = write_fw(s, DRXD_ResetCEFR);
1224 rc = write_fw(s, DRXD_microcode);
1228 s->config.w9C = 0x0e;
1229 if (s->flags & F_SET_0D0h) {
1231 rc = RD16(s, 0x0c20010);
1233 goto write_DRXD_InitFE_1;
1236 rc = WR16(s, 0x0c20010, rc);
1238 goto write_DRXD_InitFE_1;
1240 rc = RD16(s, 0x0c20011);
1242 goto write_DRXD_InitFE_1;
1245 rc = WR16(s, 0x0c20011, rc);
1247 goto write_DRXD_InitFE_1;
1249 rc = WR16(s, 0x0c20012, 1);
1252 write_DRXD_InitFE_1:
1254 rc = write_fw(s, DRXD_InitFE_1);
1259 if (s->chip_rev == DRXD_FW_B1) {
1260 if (s->flags & F_SET_0D0h)
1263 if (s->flags & F_SET_0D0h)
1267 rc = WR16(s, 0x0C20012, rc);
1271 rc = WR16(s, 0x0C20013, s->config.w9E);
1274 rc = WR16(s, 0x0C20015, s->config.w9C);
1278 rc = write_fw(s, DRXD_InitFE_2);
1281 rc = write_fw(s, DRXD_InitFT);
1284 rc = write_fw(s, DRXD_InitCP);
1287 rc = write_fw(s, DRXD_InitCE);
1290 rc = write_fw(s, DRXD_InitEQ);
1293 rc = write_fw(s, DRXD_InitEC);
1296 rc = write_fw(s, DRXD_InitSC);
1300 rc = SetCfgIfAgc(s, &s->config.ifagc);
1304 rc = SetCfgRfAgc(s, &s->config.rfagc);
1308 rc = ConfigureMPEGOutput(s, 1);
1309 rc = WR16(s, 0x08201fe, 0x0017);
1310 rc = WR16(s, 0x08201ff, 0x0101);
1320 static int drx397x_get_frontend(struct dvb_frontend *fe,
1321 struct dvb_frontend_parameters *params)
1326 static int drx397x_set_frontend(struct dvb_frontend *fe,
1327 struct dvb_frontend_parameters *params)
1329 struct drx397xD_state *s = fe->demodulator_priv;
1331 s->config.s20d24 = 1;
1333 return drx_tune(s, params);
1336 static int drx397x_get_tune_settings(struct dvb_frontend *fe,
1337 struct dvb_frontend_tune_settings
1340 fe_tune_settings->min_delay_ms = 10000;
1341 fe_tune_settings->step_size = 0;
1342 fe_tune_settings->max_drift = 0;
1347 static int drx397x_read_status(struct dvb_frontend *fe, fe_status_t *status)
1349 struct drx397xD_state *s = fe->demodulator_priv;
1352 GetLockStatus(s, &lockstat);
1356 CorrectSysClockDeviation(s);
1357 ConfigureMPEGOutput(s, 1);
1358 *status = FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
1361 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
1366 static int drx397x_read_ber(struct dvb_frontend *fe, unsigned int *ber)
1373 static int drx397x_read_snr(struct dvb_frontend *fe, u16 *snr)
1380 static int drx397x_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
1382 struct drx397xD_state *s = fe->demodulator_priv;
1385 if (s->config.ifagc.d00 == 2) {
1389 rc = RD16(s, 0x0c20035);
1395 /* Signal strength is calculated using the following formula:
1397 * a = 2200 * 150 / (2200 + 150);
1398 * a = a * 3300 / (a + 820);
1399 * b = 2200 * 3300 / (2200 + 820);
1400 * c = (((b-a) * rc) >> 10 + a) << 4;
1401 * strength = ~c & 0xffff;
1403 * The following does the same but with less rounding errors:
1405 *strength = ~(7720 + (rc * 30744 >> 10));
1410 static int drx397x_read_ucblocks(struct dvb_frontend *fe,
1411 unsigned int *ucblocks)
1418 static int drx397x_sleep(struct dvb_frontend *fe)
1423 static void drx397x_release(struct dvb_frontend *fe)
1425 struct drx397xD_state *s = fe->demodulator_priv;
1426 printk(KERN_INFO "%s: release demodulator\n", mod_name);
1434 static struct dvb_frontend_ops drx397x_ops = {
1437 .name = "Micronas DRX397xD DVB-T Frontend",
1439 .frequency_min = 47125000,
1440 .frequency_max = 855250000,
1441 .frequency_stepsize = 166667,
1442 .frequency_tolerance = 0,
1443 .caps = /* 0x0C01B2EAE */
1444 FE_CAN_FEC_1_2 | /* = 0x2, */
1445 FE_CAN_FEC_2_3 | /* = 0x4, */
1446 FE_CAN_FEC_3_4 | /* = 0x8, */
1447 FE_CAN_FEC_5_6 | /* = 0x20, */
1448 FE_CAN_FEC_7_8 | /* = 0x80, */
1449 FE_CAN_FEC_AUTO | /* = 0x200, */
1450 FE_CAN_QPSK | /* = 0x400, */
1451 FE_CAN_QAM_16 | /* = 0x800, */
1452 FE_CAN_QAM_64 | /* = 0x2000, */
1453 FE_CAN_QAM_AUTO | /* = 0x10000, */
1454 FE_CAN_TRANSMISSION_MODE_AUTO | /* = 0x20000, */
1455 FE_CAN_GUARD_INTERVAL_AUTO | /* = 0x80000, */
1456 FE_CAN_HIERARCHY_AUTO | /* = 0x100000, */
1457 FE_CAN_RECOVER | /* = 0x40000000, */
1458 FE_CAN_MUTE_TS /* = 0x80000000 */
1461 .release = drx397x_release,
1462 .init = drx397x_init,
1463 .sleep = drx397x_sleep,
1465 .set_frontend = drx397x_set_frontend,
1466 .get_tune_settings = drx397x_get_tune_settings,
1467 .get_frontend = drx397x_get_frontend,
1469 .read_status = drx397x_read_status,
1470 .read_snr = drx397x_read_snr,
1471 .read_signal_strength = drx397x_read_signal_strength,
1472 .read_ber = drx397x_read_ber,
1473 .read_ucblocks = drx397x_read_ucblocks,
1476 struct dvb_frontend *drx397xD_attach(const struct drx397xD_config *config,
1477 struct i2c_adapter *i2c)
1479 struct drx397xD_state *state;
1481 /* allocate memory for the internal state */
1482 state = kzalloc(sizeof(struct drx397xD_state), GFP_KERNEL);
1486 /* setup the state */
1488 memcpy(&state->config, config, sizeof(struct drx397xD_config));
1490 /* check if the demod is there */
1491 if (RD16(state, 0x2410019) < 0)
1494 /* create dvb_frontend */
1495 memcpy(&state->frontend.ops, &drx397x_ops,
1496 sizeof(struct dvb_frontend_ops));
1497 state->frontend.demodulator_priv = state;
1499 return &state->frontend;
1505 EXPORT_SYMBOL(drx397xD_attach);
1507 MODULE_DESCRIPTION("Micronas DRX397xD DVB-T Frontend");
1508 MODULE_AUTHOR("Henk Vergonet");
1509 MODULE_LICENSE("GPL");