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) 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] */
57 /*******************************************************************************
59 ******************************************************************************/
61 static const char *blob_name[] = {
62 #define _BLOB_ENTRY(a, b) a
63 #include "drx397xD_fw.h"
66 typedef enum blob_ix {
67 #define _BLOB_ENTRY(a, b) b
68 #include "drx397xD_fw.h"
73 const struct firmware *file;
76 const u8 *data[ARRAY_SIZE(blob_name)];
78 #define _FW_ENTRY(a, b) { \
81 .lock = RW_LOCK_UNLOCKED, \
84 #include "drx397xD_fw.h"
87 /* use only with writer lock aquired */
88 static void _drx_release_fw(struct drx397xD_state *s, fw_ix_t ix)
90 memset(&fw[ix].data[0], 0, sizeof(fw[0].data));
92 release_firmware(fw[ix].file);
95 static void drx_release_fw(struct drx397xD_state *s)
97 fw_ix_t ix = s->chip_rev;
99 pr_debug("%s\n", __func__);
101 write_lock(&fw[ix].lock);
104 if (fw[ix].refcnt == 0)
105 _drx_release_fw(s, ix);
107 write_unlock(&fw[ix].lock);
110 static int drx_load_fw(struct drx397xD_state *s, fw_ix_t ix)
114 int i = 0, j, rc = -EINVAL;
116 pr_debug("%s\n", __func__);
118 if (ix < 0 || ix >= ARRAY_SIZE(fw))
122 write_lock(&fw[ix].lock);
127 memset(&fw[ix].data[0], 0, sizeof(fw[0].data));
129 if (request_firmware(&fw[ix].file, fw[ix].name, &s->i2c->dev) != 0) {
130 printk(KERN_ERR "%s: Firmware \"%s\" not available\n",
131 mod_name, fw[ix].name);
136 if (!fw[ix].file->data || fw[ix].file->size < 10)
139 data = fw[ix].file->data;
140 size = fw[ix].file->size;
142 if (data[i++] != 2) /* check firmware version */
147 case 0x00: /* bytecode */
151 case 0x01: /* reset */
152 case 0x02: /* sleep */
155 case 0xfe: /* name */
156 len = strnlen(&data[i], size - i);
157 if (i + len + 1 >= size)
159 if (data[i + len + 1] != 0)
161 for (j = 0; j < ARRAY_SIZE(blob_name); j++) {
162 if (strcmp(blob_name[j], &data[i]) == 0) {
163 fw[ix].data[j] = &data[i + len + 1];
164 pr_debug("Loading %s\n", blob_name[j]);
169 case 0xff: /* file terminator */
179 printk(KERN_ERR "%s: Firmware is corrupt\n", mod_name);
181 _drx_release_fw(s, ix);
185 write_unlock(&fw[ix].lock);
189 /*******************************************************************************
191 ******************************************************************************/
193 static int write_fw(struct drx397xD_state *s, blob_ix_t ix)
195 struct i2c_msg msg = {.addr = s->config.demod_address,.flags = 0 };
197 int len, rc = 0, i = 0;
199 if (ix < 0 || ix >= ARRAY_SIZE(blob_name)) {
200 pr_debug("%s drx_fw_ix_t out of range\n", __func__);
203 pr_debug("%s %s\n", __func__, blob_name[ix]);
205 read_lock(&fw[s->chip_rev].lock);
206 data = fw[s->chip_rev].data[ix];
214 case 0: /* bytecode */
217 msg.buf = (__u8 *) &data[i];
218 if (i2c_transfer(s->i2c, &msg, 1) != 1) {
233 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, u32 i2c_adr)
243 struct i2c_msg msg[2] = {
245 .addr = s->config.demod_address,
251 .addr = s->config.demod_address,
258 *(u32 *) a = i2c_adr;
260 rc = i2c_transfer(s->i2c, msg, 2);
264 return le16_to_cpu(v);
267 /* Function is not endian safe, use the WR16.. wrappers below */
268 static int _write16(struct drx397xD_state *s, u32 i2c_adr, u16 val)
272 struct i2c_msg msg = {
273 .addr = s->config.demod_address,
279 *(u32 *) a = i2c_adr;
280 *(u16 *) & a[4] = val;
282 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 ) if ( (rc = (cmd)) < 0) goto exit_rc
297 /*******************************************************************************
299 ******************************************************************************/
301 static int PLL_Set(struct drx397xD_state *s,
302 struct dvb_frontend_parameters *fep, int *df_tuner)
304 struct dvb_frontend *fe = &s->frontend;
305 u32 f_tuner, f = fep->frequency;
308 pr_debug("%s\n", __func__);
310 if ((f > s->frontend.ops.tuner_ops.info.frequency_max) ||
311 (f < s->frontend.ops.tuner_ops.info.frequency_min))
315 if (!s->frontend.ops.tuner_ops.set_params ||
316 !s->frontend.ops.tuner_ops.get_frequency)
319 rc = s->frontend.ops.tuner_ops.set_params(fe, fep);
323 rc = s->frontend.ops.tuner_ops.get_frequency(fe, &f_tuner);
327 *df_tuner = f_tuner - f;
328 pr_debug("%s requested %d [Hz] tuner %d [Hz]\n", __func__, f,
334 /*******************************************************************************
335 * Demodulator helper functions
336 ******************************************************************************/
338 static int SC_WaitForReady(struct drx397xD_state *s)
343 pr_debug("%s\n", __func__);
346 rc = RD16(s, 0x820043);
353 static int SC_SendCommand(struct drx397xD_state *s, int cmd)
357 pr_debug("%s\n", __func__);
359 WR16(s, 0x820043, cmd);
361 rc = RD16(s, 0x820042);
362 if ((rc & 0xffff) == 0xffff)
367 static int HI_Command(struct drx397xD_state *s, u16 cmd)
371 pr_debug("%s\n", __func__);
373 rc = WR16(s, 0x420032, cmd);
378 rc = RD16(s, 0x420032);
380 rc = RD16(s, 0x420031);
389 static int HI_CfgCommand(struct drx397xD_state *s)
392 pr_debug("%s\n", __func__);
394 WR16(s, 0x420033, 0x3973);
395 WR16(s, 0x420034, s->config.w50); // code 4, log 4
396 WR16(s, 0x420035, s->config.w52); // code 15, log 9
397 WR16(s, 0x420036, s->config.demod_address << 1);
398 WR16(s, 0x420037, s->config.w56); // code (set_i2c ?? initX 1 ), log 1
399 // WR16(s, 0x420033, 0x3973);
400 if ((s->config.w56 & 8) == 0)
401 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);
529 static int GetLockStatus(struct drx397xD_state *s, int *lockstat)
535 rc = RD16(s, 0x082004b);
539 if (s->config.d60 != 2)
551 static int CorrectSysClockDeviation(struct drx397xD_state *s)
557 pr_debug("%s\n", __func__);
559 if (s->config.d5C == 0) {
560 EXIT_RC(WR16(s, 0x08200e8, 0x010));
561 EXIT_RC(WR16(s, 0x08200e9, 0x113));
565 if (s->config.d5C != 1)
568 rc = RD16(s, 0x0820048);
570 rc = GetLockStatus(s, &lockstat);
573 if ((lockstat & 1) == 0)
576 EXIT_RC(WR16(s, 0x0420033, 0x200));
577 EXIT_RC(WR16(s, 0x0420034, 0xc5));
578 EXIT_RC(WR16(s, 0x0420035, 0x10));
579 EXIT_RC(WR16(s, 0x0420036, 0x1));
580 EXIT_RC(WR16(s, 0x0420037, 0xa));
581 EXIT_RC(HI_Command(s, 6));
582 EXIT_RC(RD16(s, 0x0420040));
584 EXIT_RC(RD16(s, 0x0420041));
592 if (!s->bandwidth_parm)
595 /* round & convert to Hz */
596 clk = ((u64) (clk + 0x800000) * s->bandwidth_parm + (1 << 20)) >> 21;
597 clk_limit = s->config.f_osc * MAX_CLOCK_DRIFT / 1000;
599 if (clk - s->config.f_osc * 1000 + clk_limit <= 2 * clk_limit) {
601 pr_debug("%s: osc %d %d [Hz]\n", __func__,
602 s->config.f_osc * 1000, clk - s->config.f_osc * 1000);
604 rc = WR16(s, 0x08200e8, 0);
609 static int ConfigureMPEGOutput(struct drx397xD_state *s, int type)
613 pr_debug("%s\n", __func__);
616 if (s->config.w98 == 0) {
623 if (s->config.w9A == 0) {
629 EXIT_RC(WR16(s, 0x2150045, 0));
630 EXIT_RC(WR16(s, 0x2150010, si));
631 EXIT_RC(WR16(s, 0x2150011, bp));
632 rc = WR16(s, 0x2150012, (type == 0 ? 0xfff : 0));
637 static int drx_tune(struct drx397xD_state *s,
638 struct dvb_frontend_parameters *fep)
644 u32 edi = 0, ebx = 0, ebp = 0, edx = 0;
645 u16 v20 = 0, v1E = 0, v16 = 0, v14 = 0, v12 = 0, v10 = 0, v0E = 0;
649 pr_debug("%s %d\n", __func__, s->config.d60);
651 if (s->config.d60 != 2)
653 rc = CorrectSysClockDeviation(s);
658 rc = ConfigureMPEGOutput(s, 0);
663 rc = PLL_Set(s, fep, &df_tuner);
665 printk(KERN_ERR "Error in pll_set\n");
670 a = rc = RD16(s, 0x2150016);
673 b = rc = RD16(s, 0x2150010);
676 c = rc = RD16(s, 0x2150034);
679 d = rc = RD16(s, 0x2150035);
682 rc = WR16(s, 0x2150014, c);
683 rc = WR16(s, 0x2150015, d);
684 rc = WR16(s, 0x2150010, 0);
685 rc = WR16(s, 0x2150000, 2);
686 rc = WR16(s, 0x2150036, 0x0fff);
687 rc = WR16(s, 0x2150016, a);
689 rc = WR16(s, 0x2150010, 2);
690 rc = WR16(s, 0x2150007, 0);
691 rc = WR16(s, 0x2150000, 1);
692 rc = WR16(s, 0x2110000, 0);
693 rc = WR16(s, 0x0800000, 0);
694 rc = WR16(s, 0x2800000, 0);
695 rc = WR16(s, 0x2110010, 0x664);
697 rc = write_fw(s, DRXD_ResetECRAM);
698 rc = WR16(s, 0x2110000, 1);
700 rc = write_fw(s, DRXD_InitSC);
704 rc = SetCfgIfAgc(s, &s->config.ifagc);
708 rc = SetCfgRfAgc(s, &s->config.rfagc);
712 if (fep->u.ofdm.transmission_mode != TRANSMISSION_MODE_2K)
714 switch (fep->u.ofdm.transmission_mode) {
715 case TRANSMISSION_MODE_8K:
717 if (s->chip_rev == DRXD_FW_B1)
720 rc = WR16(s, 0x2010010, 0);
729 if (s->chip_rev == DRXD_FW_B1)
732 rc = WR16(s, 0x2010010, 1);
741 switch (fep->u.ofdm.guard_interval) {
742 case GUARD_INTERVAL_1_4:
745 case GUARD_INTERVAL_1_8:
748 case GUARD_INTERVAL_1_16:
751 case GUARD_INTERVAL_1_32:
767 switch (fep->u.ofdm.hierarchy_information) {
770 if (s->chip_rev == DRXD_FW_B1)
772 rc = WR16(s, 0x1c10047, 1);
775 rc = WR16(s, 0x2010012, 1);
790 if (s->chip_rev == DRXD_FW_B1)
792 rc = WR16(s, 0x1c10047, 2);
795 rc = WR16(s, 0x2010012, 2);
810 if (s->chip_rev == DRXD_FW_B1)
812 rc = WR16(s, 0x1c10047, 3);
815 rc = WR16(s, 0x2010012, 3);
830 if (s->chip_rev == DRXD_FW_B1)
832 rc = WR16(s, 0x1c10047, 0);
835 rc = WR16(s, 0x2010012, 0);
850 switch (fep->u.ofdm.constellation) {
854 if (s->chip_rev == DRXD_FW_B1)
857 rc = WR16(s, 0x1c10046, 0);
860 rc = WR16(s, 0x2010011, 0);
863 rc = WR16(s, 0x201001a, 0x10);
866 rc = WR16(s, 0x201001b, 0);
869 rc = WR16(s, 0x201001c, 0);
872 rc = WR16(s, 0x1c10062, v20);
875 rc = WR16(s, 0x1c1002a, v1C);
878 rc = WR16(s, 0x1c10015, v16);
881 rc = WR16(s, 0x1c10016, v12);
887 if (s->chip_rev == DRXD_FW_B1)
890 rc = WR16(s, 0x1c10046, 1);
893 rc = WR16(s, 0x2010011, 1);
896 rc = WR16(s, 0x201001a, 0x10);
899 rc = WR16(s, 0x201001b, 4);
902 rc = WR16(s, 0x201001c, 0);
905 rc = WR16(s, 0x1c10062, v1E);
908 rc = WR16(s, 0x1c1002a, v1A);
911 rc = WR16(s, 0x1c10015, v14);
914 rc = WR16(s, 0x1c10016, v10);
920 rc = WR16(s, 0x1c10046, 2);
923 rc = WR16(s, 0x2010011, 2);
926 rc = WR16(s, 0x201001a, 0x20);
929 rc = WR16(s, 0x201001b, 8);
932 rc = WR16(s, 0x201001c, 2);
935 rc = WR16(s, 0x1c10062, ebx);
938 rc = WR16(s, 0x1c1002a, v18);
941 rc = WR16(s, 0x1c10015, ebp);
944 rc = WR16(s, 0x1c10016, v0E);
950 if (s->config.s20d24 == 1) {
951 rc = WR16(s, 0x2010013, 0);
953 rc = WR16(s, 0x2010013, 1);
957 switch (fep->u.ofdm.code_rate_HP) {
961 if (s->chip_rev == DRXD_FW_B1)
963 rc = WR16(s, 0x2090011, 0);
967 if (s->chip_rev == DRXD_FW_B1)
969 rc = WR16(s, 0x2090011, 1);
973 if (s->chip_rev == DRXD_FW_B1)
975 rc = WR16(s, 0x2090011, 2);
977 case FEC_5_6: /* 5 */
979 if (s->chip_rev == DRXD_FW_B1)
981 rc = WR16(s, 0x2090011, 3);
983 case FEC_7_8: /* 7 */
985 if (s->chip_rev == DRXD_FW_B1)
987 rc = WR16(s, 0x2090011, 4);
993 switch (fep->u.ofdm.bandwidth) {
997 case BANDWIDTH_8_MHZ: /* 0 */
999 rc = WR16(s, 0x0c2003f, 0x32);
1000 s->bandwidth_parm = ebx = 0x8b8249; // 9142857
1003 case BANDWIDTH_7_MHZ:
1004 rc = WR16(s, 0x0c2003f, 0x3b);
1005 s->bandwidth_parm = ebx = 0x7a1200; // 8000000
1008 case BANDWIDTH_6_MHZ:
1009 rc = WR16(s, 0x0c2003f, 0x47);
1010 s->bandwidth_parm = ebx = 0x68a1b6; // 6857142
1018 rc = WR16(s, 0x08200ec, edx);
1022 rc = RD16(s, 0x0820050);
1025 rc = WR16(s, 0x0820050, rc);
1028 /* Configure bandwidth specific factor */
1029 ebx = div64_u64(((u64) (s->f_osc) << 21) + (ebx >> 1),
1030 (u64)ebx) - 0x800000;
1031 EXIT_RC(WR16(s, 0x0c50010, ebx & 0xffff));
1032 EXIT_RC(WR16(s, 0x0c50011, ebx >> 16));
1034 /* drx397xD oscillator calibration */
1035 ebx = div64_u64(((u64) (s->config.f_if + df_tuner) << 28) +
1036 (s->f_osc >> 1), (u64)s->f_osc);
1039 if (fep->inversion == INVERSION_ON)
1040 ebx = 0x10000000 - ebx;
1042 EXIT_RC(WR16(s, 0x0c30010, ebx & 0xffff));
1043 EXIT_RC(WR16(s, 0x0c30011, ebx >> 16));
1045 EXIT_RC(WR16(s, 0x0800000, 1));
1046 EXIT_RC(RD16(s, 0x0800000));
1049 EXIT_RC(SC_WaitForReady(s));
1050 EXIT_RC(WR16(s, 0x0820042, 0));
1051 EXIT_RC(WR16(s, 0x0820041, v22));
1052 EXIT_RC(WR16(s, 0x0820040, edi));
1053 EXIT_RC(SC_SendCommand(s, 3));
1055 rc = RD16(s, 0x0800000);
1058 WR16(s, 0x0820042, 0);
1059 WR16(s, 0x0820041, 1);
1060 WR16(s, 0x0820040, 1);
1061 SC_SendCommand(s, 1);
1063 // rc = WR16(s, 0x2150000, 1);
1064 // if (rc < 0) goto exit_rc;
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);
1076 /*******************************************************************************
1078 ******************************************************************************/
1080 static int drx397x_init(struct dvb_frontend *fe)
1082 struct drx397xD_state *s = fe->demodulator_priv;
1085 pr_debug("%s\n", __func__);
1087 s->config.rfagc.d00 = 2; /* 0x7c */
1088 s->config.rfagc.w04 = 0;
1089 s->config.rfagc.w06 = 0x3ff;
1091 s->config.ifagc.d00 = 0; /* 0x68 */
1092 s->config.ifagc.w04 = 0;
1093 s->config.ifagc.w06 = 140;
1094 s->config.ifagc.w08 = 0;
1095 s->config.ifagc.w0A = 0x3ff;
1096 s->config.ifagc.w0C = 0x388;
1098 /* for signal strenght calculations */
1099 s->config.ss76 = 820;
1100 s->config.ss78 = 2200;
1101 s->config.ss7A = 150;
1105 s->config.w52 = 9; // 0xf;
1107 s->config.f_if = 42800000; /* d14: intermediate frequency [Hz] */
1108 s->config.f_osc = 48000; /* s66 : oscillator frequency [kHz] */
1109 s->config.w92 = 12000; // 20000;
1111 s->config.w9C = 0x000e;
1112 s->config.w9E = 0x0000;
1114 /* ConfigureMPEGOutput params */
1116 s->config.w98 = 1; // 0;
1119 /* get chip revision */
1120 rc = RD16(s, 0x2410019);
1125 printk(KERN_INFO "%s: chip revision A2\n", mod_name);
1126 rc = drx_load_fw(s, DRXD_FW_A2);
1129 rc = (rc >> 12) - 3;
1132 s->flags |= F_SET_0D4h;
1135 s->flags |= F_SET_0D0h;
1141 s->flags |= F_SET_0D4h;
1146 printk(KERN_INFO "%s: chip revision B1.%d\n", mod_name, rc);
1147 rc = drx_load_fw(s, DRXD_FW_B1);
1152 rc = WR16(s, 0x0420033, 0x3973);
1156 rc = HI_Command(s, 2);
1160 if (s->chip_rev == DRXD_FW_A2) {
1161 rc = WR16(s, 0x043012d, 0x47F);
1165 rc = WR16_E0(s, 0x0400000, 0);
1169 if (s->config.w92 > 20000 || s->config.w92 % 4000) {
1170 printk(KERN_ERR "%s: invalid osc frequency\n", mod_name);
1175 rc = WR16(s, 0x2410010, 1);
1178 rc = WR16(s, 0x2410011, 0x15);
1181 rc = WR16(s, 0x2410012, s->config.w92 / 4000);
1185 rc = WR16(s, 0x2410015, 2);
1189 rc = WR16(s, 0x2410017, 0x3973);
1193 s->f_osc = s->config.f_osc * 1000; /* initial estimator */
1197 rc = HI_CfgCommand(s);
1201 rc = write_fw(s, DRXD_InitAtomicRead);
1205 if (s->chip_rev == DRXD_FW_A2) {
1206 rc = WR16(s, 0x2150013, 0);
1211 rc = WR16_E0(s, 0x0400002, 0);
1214 rc = WR16(s, 0x0400002, 0);
1218 if (s->chip_rev == DRXD_FW_A2) {
1219 rc = write_fw(s, DRXD_ResetCEFR);
1223 rc = write_fw(s, DRXD_microcode);
1227 s->config.w9C = 0x0e;
1228 if (s->flags & F_SET_0D0h) {
1230 rc = RD16(s, 0x0c20010);
1232 goto write_DRXD_InitFE_1;
1235 rc = WR16(s, 0x0c20010, rc);
1237 goto write_DRXD_InitFE_1;
1239 rc = RD16(s, 0x0c20011);
1241 goto write_DRXD_InitFE_1;
1244 rc = WR16(s, 0x0c20011, rc);
1246 goto write_DRXD_InitFE_1;
1248 rc = WR16(s, 0x0c20012, 1);
1251 write_DRXD_InitFE_1:
1253 rc = write_fw(s, DRXD_InitFE_1);
1258 if (s->chip_rev == DRXD_FW_B1) {
1259 if (s->flags & F_SET_0D0h)
1262 if (s->flags & F_SET_0D0h)
1266 rc = WR16(s, 0x0C20012, rc);
1270 rc = WR16(s, 0x0C20013, s->config.w9E);
1273 rc = WR16(s, 0x0C20015, s->config.w9C);
1277 rc = write_fw(s, DRXD_InitFE_2);
1280 rc = write_fw(s, DRXD_InitFT);
1283 rc = write_fw(s, DRXD_InitCP);
1286 rc = write_fw(s, DRXD_InitCE);
1289 rc = write_fw(s, DRXD_InitEQ);
1292 rc = write_fw(s, DRXD_InitEC);
1295 rc = write_fw(s, DRXD_InitSC);
1299 rc = SetCfgIfAgc(s, &s->config.ifagc);
1303 rc = SetCfgRfAgc(s, &s->config.rfagc);
1307 rc = ConfigureMPEGOutput(s, 1);
1308 rc = WR16(s, 0x08201fe, 0x0017);
1309 rc = WR16(s, 0x08201ff, 0x0101);
1318 static int drx397x_get_frontend(struct dvb_frontend *fe,
1319 struct dvb_frontend_parameters *params)
1324 static int drx397x_set_frontend(struct dvb_frontend *fe,
1325 struct dvb_frontend_parameters *params)
1327 struct drx397xD_state *s = fe->demodulator_priv;
1329 s->config.s20d24 = 1; // 0;
1330 return drx_tune(s, params);
1333 static int drx397x_get_tune_settings(struct dvb_frontend *fe,
1334 struct dvb_frontend_tune_settings
1337 fe_tune_settings->min_delay_ms = 10000;
1338 fe_tune_settings->step_size = 0;
1339 fe_tune_settings->max_drift = 0;
1343 static int drx397x_read_status(struct dvb_frontend *fe, fe_status_t * status)
1345 struct drx397xD_state *s = fe->demodulator_priv;
1348 GetLockStatus(s, &lockstat);
1350 // if (lockstat & 1)
1351 // CorrectSysClockDeviation(s);
1355 CorrectSysClockDeviation(s);
1356 ConfigureMPEGOutput(s, 1);
1357 *status = FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
1360 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
1366 static int drx397x_read_ber(struct dvb_frontend *fe, unsigned int *ber)
1372 static int drx397x_read_snr(struct dvb_frontend *fe, u16 * snr)
1378 static int drx397x_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
1380 struct drx397xD_state *s = fe->demodulator_priv;
1383 if (s->config.ifagc.d00 == 2) {
1387 rc = RD16(s, 0x0c20035);
1393 /* Signal strength is calculated using the following formula:
1395 * a = 2200 * 150 / (2200 + 150);
1396 * a = a * 3300 / (a + 820);
1397 * b = 2200 * 3300 / (2200 + 820);
1398 * c = (((b-a) * rc) >> 10 + a) << 4;
1399 * strength = ~c & 0xffff;
1401 * The following does the same but with less rounding errors:
1403 *strength = ~(7720 + (rc * 30744 >> 10));
1407 static int drx397x_read_ucblocks(struct dvb_frontend *fe,
1408 unsigned int *ucblocks)
1414 static int drx397x_sleep(struct dvb_frontend *fe)
1419 static void drx397x_release(struct dvb_frontend *fe)
1421 struct drx397xD_state *s = fe->demodulator_priv;
1422 printk(KERN_INFO "%s: release demodulator\n", mod_name);
1430 static struct dvb_frontend_ops drx397x_ops = {
1433 .name = "Micronas DRX397xD DVB-T Frontend",
1435 .frequency_min = 47125000,
1436 .frequency_max = 855250000,
1437 .frequency_stepsize = 166667,
1438 .frequency_tolerance = 0,
1439 .caps = /* 0x0C01B2EAE */
1440 FE_CAN_FEC_1_2 | // = 0x2,
1441 FE_CAN_FEC_2_3 | // = 0x4,
1442 FE_CAN_FEC_3_4 | // = 0x8,
1443 FE_CAN_FEC_5_6 | // = 0x20,
1444 FE_CAN_FEC_7_8 | // = 0x80,
1445 FE_CAN_FEC_AUTO | // = 0x200,
1446 FE_CAN_QPSK | // = 0x400,
1447 FE_CAN_QAM_16 | // = 0x800,
1448 FE_CAN_QAM_64 | // = 0x2000,
1449 FE_CAN_QAM_AUTO | // = 0x10000,
1450 FE_CAN_TRANSMISSION_MODE_AUTO | // = 0x20000,
1451 FE_CAN_GUARD_INTERVAL_AUTO | // = 0x80000,
1452 FE_CAN_HIERARCHY_AUTO | // = 0x100000,
1453 FE_CAN_RECOVER | // = 0x40000000,
1454 FE_CAN_MUTE_TS // = 0x80000000
1457 .release = drx397x_release,
1458 .init = drx397x_init,
1459 .sleep = drx397x_sleep,
1461 .set_frontend = drx397x_set_frontend,
1462 .get_tune_settings = drx397x_get_tune_settings,
1463 .get_frontend = drx397x_get_frontend,
1465 .read_status = drx397x_read_status,
1466 .read_snr = drx397x_read_snr,
1467 .read_signal_strength = drx397x_read_signal_strength,
1468 .read_ber = drx397x_read_ber,
1469 .read_ucblocks = drx397x_read_ucblocks,
1472 struct dvb_frontend *drx397xD_attach(const struct drx397xD_config *config,
1473 struct i2c_adapter *i2c)
1475 struct drx397xD_state *s = NULL;
1477 /* allocate memory for the internal state */
1478 s = kzalloc(sizeof(struct drx397xD_state), GFP_KERNEL);
1482 /* setup the state */
1484 memcpy(&s->config, config, sizeof(struct drx397xD_config));
1486 /* check if the demod is there */
1487 if (RD16(s, 0x2410019) < 0)
1490 /* create dvb_frontend */
1491 memcpy(&s->frontend.ops, &drx397x_ops, sizeof(struct dvb_frontend_ops));
1492 s->frontend.demodulator_priv = s;
1494 return &s->frontend;
1500 MODULE_DESCRIPTION("Micronas DRX397xD DVB-T Frontend");
1501 MODULE_AUTHOR("Henk Vergonet");
1502 MODULE_LICENSE("GPL");
1504 EXPORT_SYMBOL(drx397xD_attach);