mt2063.c 140.6 KB
Newer Older
1

2 3 4 5
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
6 7 8

#include "mt2063.h"

9
static unsigned int verbose;
10 11
module_param(verbose, int, 0644);

12 13
/* Internal structures and types */

14
/* FIXME: we probably don't need these new FE get/set property types for tuner */
15 16 17 18
#define DVBFE_TUNER_OPEN			99
#define DVBFE_TUNER_SOFTWARE_SHUTDOWN		100
#define DVBFE_TUNER_CLEAR_POWER_MASKBITS	101

19
/* FIXME: Those two error codes need conversion*/
20 21 22 23
/*  Error:  Upconverter PLL is not locked  */
#define MT2063_UPC_UNLOCK                   (0x80000002)
/*  Error:  Downconverter PLL is not locked  */
#define MT2063_DNC_UNLOCK                   (0x80000004)
24

25
/*  Info: Unavoidable LO-related spur may be present in the output  */
26
#define MT2063_SPUR_PRESENT_ERR             (0x00800000)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

/*  Info: Mask of bits used for # of LO-related spurs that were avoided during tuning  */
#define MT2063_SPUR_CNT_MASK                (0x001f0000)
#define MT2063_SPUR_SHIFT                   (16)

/*  Info: Upconverter frequency is out of range (may be reason for MT_UPC_UNLOCK) */
#define MT2063_UPC_RANGE                    (0x04000000)

/*  Info: Downconverter frequency is out of range (may be reason for MT_DPC_UNLOCK) */
#define MT2063_DNC_RANGE                    (0x08000000)

#define MAX_UDATA         (4294967295)	/*  max value storable in u32   */

#define MT2063_TUNER_CNT               (1)	/*  total num of MicroTuner tuners  */
#define MT2063_I2C (0xC0)

43 44 45 46
/*
 *  Data Types
 */

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
/*
 *  Constant defining the version of the following structure
 *  and therefore the API for this code.
 *
 *  When compiling the tuner driver, the preprocessor will
 *  check against this version number to make sure that
 *  it matches the version that the tuner driver knows about.
 */

/* DECT Frequency Avoidance */
#define MT2063_DECT_AVOID_US_FREQS      0x00000001

#define MT2063_DECT_AVOID_EURO_FREQS    0x00000002

#define MT2063_EXCLUDE_US_DECT_FREQUENCIES(s) (((s) & MT2063_DECT_AVOID_US_FREQS) != 0)

#define MT2063_EXCLUDE_EURO_DECT_FREQUENCIES(s) (((s) & MT2063_DECT_AVOID_EURO_FREQS) != 0)

enum MT2063_DECT_Avoid_Type {
	MT2063_NO_DECT_AVOIDANCE = 0,				/* Do not create DECT exclusion zones.     */
	MT2063_AVOID_US_DECT = MT2063_DECT_AVOID_US_FREQS,	/* Avoid US DECT frequencies.              */
	MT2063_AVOID_EURO_DECT = MT2063_DECT_AVOID_EURO_FREQS,	/* Avoid European DECT frequencies.        */
	MT2063_AVOID_BOTH					/* Avoid both regions. Not typically used. */
};

#define MT2063_MAX_ZONES 48

struct MT2063_ExclZone_t {
	u32 min_;
	u32 max_;
	struct MT2063_ExclZone_t *next_;
};

/*
 *  Structure of data needed for Spur Avoidance
 */
struct MT2063_AvoidSpursData_t {
	u32 nAS_Algorithm;
	u32 f_ref;
	u32 f_in;
	u32 f_LO1;
	u32 f_if1_Center;
	u32 f_if1_Request;
	u32 f_if1_bw;
	u32 f_LO2;
	u32 f_out;
	u32 f_out_bw;
	u32 f_LO1_Step;
	u32 f_LO2_Step;
	u32 f_LO1_FracN_Avoid;
	u32 f_LO2_FracN_Avoid;
	u32 f_zif_bw;
	u32 f_min_LO_Separation;
	u32 maxH1;
	u32 maxH2;
	enum MT2063_DECT_Avoid_Type avoidDECT;
	u32 bSpurPresent;
	u32 bSpurAvoided;
	u32 nSpursFound;
	u32 nZones;
	struct MT2063_ExclZone_t *freeZones;
	struct MT2063_ExclZone_t *usedZones;
	struct MT2063_ExclZone_t MT2063_ExclZones[MT2063_MAX_ZONES];
};

/*
 *  Parameter for function MT2063_SetPowerMask that specifies the power down
 *  of various sections of the MT2063.
 */
enum MT2063_Mask_Bits {
	MT2063_REG_SD = 0x0040,		/* Shutdown regulator                 */
	MT2063_SRO_SD = 0x0020,		/* Shutdown SRO                       */
	MT2063_AFC_SD = 0x0010,		/* Shutdown AFC A/D                   */
	MT2063_PD_SD = 0x0002,		/* Enable power detector shutdown     */
	MT2063_PDADC_SD = 0x0001,	/* Enable power detector A/D shutdown */
	MT2063_VCO_SD = 0x8000,		/* Enable VCO shutdown                */
	MT2063_LTX_SD = 0x4000,		/* Enable LTX shutdown                */
	MT2063_LT1_SD = 0x2000,		/* Enable LT1 shutdown                */
	MT2063_LNA_SD = 0x1000,		/* Enable LNA shutdown                */
	MT2063_UPC_SD = 0x0800,		/* Enable upconverter shutdown        */
	MT2063_DNC_SD = 0x0400,		/* Enable downconverter shutdown      */
	MT2063_VGA_SD = 0x0200,		/* Enable VGA shutdown                */
	MT2063_AMP_SD = 0x0100,		/* Enable AMP shutdown                */
	MT2063_ALL_SD = 0xFF73,		/* All shutdown bits for this tuner   */
	MT2063_NONE_SD = 0x0000		/* No shutdown bits                   */
};

/*
 *  Parameter for function MT2063_GetParam & MT2063_SetParam that
 *  specifies the tuning algorithm parameter to be read/written.
 */
enum MT2063_Param {
	/*  tuner address                                  set by MT2063_Open() */
	MT2063_IC_ADDR,

	/*  max number of MT2063 tuners     set by MT_TUNER_CNT in mt_userdef.h */
	MT2063_MAX_OPEN,

	/*  current number of open MT2063 tuners           set by MT2063_Open() */
	MT2063_NUM_OPEN,

	/*  crystal frequency                            (default: 16000000 Hz) */
	MT2063_SRO_FREQ,

	/*  min tuning step size                            (default: 50000 Hz) */
	MT2063_STEPSIZE,

	/*  input center frequency                         set by MT2063_Tune() */
	MT2063_INPUT_FREQ,

	/*  LO1 Frequency                                  set by MT2063_Tune() */
	MT2063_LO1_FREQ,

	/*  LO1 minimum step size                          (default: 250000 Hz) */
	MT2063_LO1_STEPSIZE,

	/*  LO1 FracN keep-out region                      (default: 999999 Hz) */
	MT2063_LO1_FRACN_AVOID_PARAM,

	/*  Current 1st IF in use                          set by MT2063_Tune() */
	MT2063_IF1_ACTUAL,

	/*  Requested 1st IF                               set by MT2063_Tune() */
	MT2063_IF1_REQUEST,

	/*  Center of 1st IF SAW filter                (default: 1218000000 Hz) */
	MT2063_IF1_CENTER,

	/*  Bandwidth of 1st IF SAW filter               (default: 20000000 Hz) */
	MT2063_IF1_BW,

	/*  zero-IF bandwidth                             (default: 2000000 Hz) */
	MT2063_ZIF_BW,

	/*  LO2 Frequency                                  set by MT2063_Tune() */
	MT2063_LO2_FREQ,

	/*  LO2 minimum step size                           (default: 50000 Hz) */
	MT2063_LO2_STEPSIZE,

	/*  LO2 FracN keep-out region                      (default: 374999 Hz) */
	MT2063_LO2_FRACN_AVOID,

	/*  output center frequency                        set by MT2063_Tune() */
	MT2063_OUTPUT_FREQ,

	/*  output bandwidth                               set by MT2063_Tune() */
	MT2063_OUTPUT_BW,

	/*  min inter-tuner LO separation                 (default: 1000000 Hz) */
	MT2063_LO_SEPARATION,

	/*  ID of avoid-spurs algorithm in use            compile-time constant */
	MT2063_AS_ALG,

	/*  max # of intra-tuner harmonics                       (default: 15)  */
	MT2063_MAX_HARM1,

	/*  max # of inter-tuner harmonics                        (default: 7)  */
	MT2063_MAX_HARM2,

	/*  # of 1st IF exclusion zones used               set by MT2063_Tune() */
	MT2063_EXCL_ZONES,

	/*  # of spurs found/avoided                       set by MT2063_Tune() */
	MT2063_NUM_SPURS,

	/*  >0 spurs avoided                               set by MT2063_Tune() */
	MT2063_SPUR_AVOIDED,

	/*  >0 spurs in output (mathematically)            set by MT2063_Tune() */
	MT2063_SPUR_PRESENT,

	/* Receiver Mode for some parameters. 1 is DVB-T                        */
	MT2063_RCVR_MODE,

	/* directly set LNA attenuation, parameter is value to set              */
	MT2063_ACLNA,

	/* maximum LNA attenuation, parameter is value to set                   */
	MT2063_ACLNA_MAX,

	/* directly set ATN attenuation.  Paremeter is value to set.            */
	MT2063_ACRF,

	/* maxium ATN attenuation.  Paremeter is value to set.                  */
	MT2063_ACRF_MAX,

	/* directly set FIF attenuation.  Paremeter is value to set.            */
	MT2063_ACFIF,

	/* maxium FIF attenuation.  Paremeter is value to set.                  */
	MT2063_ACFIF_MAX,

	/*  LNA Rin                                                             */
	MT2063_LNA_RIN,

	/*  Power Detector LNA level target                                     */
	MT2063_LNA_TGT,

	/*  Power Detector 1 level                                              */
	MT2063_PD1,

	/*  Power Detector 1 level target                                       */
	MT2063_PD1_TGT,

	/*  Power Detector 2 level                                              */
	MT2063_PD2,

	/*  Power Detector 2 level target                                       */
	MT2063_PD2_TGT,

	/*  Selects, which DNC is activ                                         */
	MT2063_DNC_OUTPUT_ENABLE,

	/*  VGA gain code                                                       */
	MT2063_VGAGC,

	/*  VGA bias current                                                    */
	MT2063_VGAOI,

	/*  TAGC, determins the speed of the AGC                                */
	MT2063_TAGC,

	/*  AMP gain code                                                       */
	MT2063_AMPGC,

	/* Control setting to avoid DECT freqs         (default: MT_AVOID_BOTH) */
	MT2063_AVOID_DECT,

	/* Cleartune filter selection: 0 - by IC (default), 1 - by software     */
	MT2063_CTFILT_SW,

	MT2063_EOP		/*  last entry in enumerated list         */
};

/*
 *  Parameter for selecting tuner mode
 */
enum MT2063_RCVR_MODES {
	MT2063_CABLE_QAM = 0,	/* Digital cable              */
	MT2063_CABLE_ANALOG,	/* Analog cable               */
	MT2063_OFFAIR_COFDM,	/* Digital offair             */
	MT2063_OFFAIR_COFDM_SAWLESS,	/* Digital offair without SAW */
	MT2063_OFFAIR_ANALOG,	/* Analog offair              */
	MT2063_OFFAIR_8VSB,	/* Analog offair              */
	MT2063_NUM_RCVR_MODES
};

/*
 *  Possible values for MT2063_DNC_OUTPUT
 */
enum MT2063_DNC_Output_Enable {
	MT2063_DNC_NONE = 0,
	MT2063_DNC_1,
	MT2063_DNC_2,
	MT2063_DNC_BOTH
};

/*
**  Two-wire serial bus subaddresses of the tuner registers.
**  Also known as the tuner's register addresses.
*/
enum MT2063_Register_Offsets {
	MT2063_REG_PART_REV = 0,	/*  0x00: Part/Rev Code         */
	MT2063_REG_LO1CQ_1,		/*  0x01: LO1C Queued Byte 1    */
	MT2063_REG_LO1CQ_2,		/*  0x02: LO1C Queued Byte 2    */
	MT2063_REG_LO2CQ_1,		/*  0x03: LO2C Queued Byte 1    */
	MT2063_REG_LO2CQ_2,		/*  0x04: LO2C Queued Byte 2    */
	MT2063_REG_LO2CQ_3,		/*  0x05: LO2C Queued Byte 3    */
	MT2063_REG_RSVD_06,		/*  0x06: Reserved              */
	MT2063_REG_LO_STATUS,		/*  0x07: LO Status             */
	MT2063_REG_FIFFC,		/*  0x08: FIFF Center           */
	MT2063_REG_CLEARTUNE,		/*  0x09: ClearTune Filter      */
	MT2063_REG_ADC_OUT,		/*  0x0A: ADC_OUT               */
	MT2063_REG_LO1C_1,		/*  0x0B: LO1C Byte 1           */
	MT2063_REG_LO1C_2,		/*  0x0C: LO1C Byte 2           */
	MT2063_REG_LO2C_1,		/*  0x0D: LO2C Byte 1           */
	MT2063_REG_LO2C_2,		/*  0x0E: LO2C Byte 2           */
	MT2063_REG_LO2C_3,		/*  0x0F: LO2C Byte 3           */
	MT2063_REG_RSVD_10,		/*  0x10: Reserved              */
	MT2063_REG_PWR_1,		/*  0x11: PWR Byte 1            */
	MT2063_REG_PWR_2,		/*  0x12: PWR Byte 2            */
	MT2063_REG_TEMP_STATUS,		/*  0x13: Temp Status           */
	MT2063_REG_XO_STATUS,		/*  0x14: Crystal Status        */
	MT2063_REG_RF_STATUS,		/*  0x15: RF Attn Status        */
	MT2063_REG_FIF_STATUS,		/*  0x16: FIF Attn Status       */
	MT2063_REG_LNA_OV,		/*  0x17: LNA Attn Override     */
	MT2063_REG_RF_OV,		/*  0x18: RF Attn Override      */
	MT2063_REG_FIF_OV,		/*  0x19: FIF Attn Override     */
	MT2063_REG_LNA_TGT,		/*  0x1A: Reserved              */
	MT2063_REG_PD1_TGT,		/*  0x1B: Pwr Det 1 Target      */
	MT2063_REG_PD2_TGT,		/*  0x1C: Pwr Det 2 Target      */
	MT2063_REG_RSVD_1D,		/*  0x1D: Reserved              */
	MT2063_REG_RSVD_1E,		/*  0x1E: Reserved              */
	MT2063_REG_RSVD_1F,		/*  0x1F: Reserved              */
	MT2063_REG_RSVD_20,		/*  0x20: Reserved              */
	MT2063_REG_BYP_CTRL,		/*  0x21: Bypass Control        */
	MT2063_REG_RSVD_22,		/*  0x22: Reserved              */
	MT2063_REG_RSVD_23,		/*  0x23: Reserved              */
	MT2063_REG_RSVD_24,		/*  0x24: Reserved              */
	MT2063_REG_RSVD_25,		/*  0x25: Reserved              */
	MT2063_REG_RSVD_26,		/*  0x26: Reserved              */
	MT2063_REG_RSVD_27,		/*  0x27: Reserved              */
	MT2063_REG_FIFF_CTRL,		/*  0x28: FIFF Control          */
	MT2063_REG_FIFF_OFFSET,		/*  0x29: FIFF Offset           */
	MT2063_REG_CTUNE_CTRL,		/*  0x2A: Reserved              */
	MT2063_REG_CTUNE_OV,		/*  0x2B: Reserved              */
	MT2063_REG_CTRL_2C,		/*  0x2C: Reserved              */
	MT2063_REG_FIFF_CTRL2,		/*  0x2D: Fiff Control          */
	MT2063_REG_RSVD_2E,		/*  0x2E: Reserved              */
	MT2063_REG_DNC_GAIN,		/*  0x2F: DNC Control           */
	MT2063_REG_VGA_GAIN,		/*  0x30: VGA Gain Ctrl         */
	MT2063_REG_RSVD_31,		/*  0x31: Reserved              */
	MT2063_REG_TEMP_SEL,		/*  0x32: Temperature Selection */
	MT2063_REG_RSVD_33,		/*  0x33: Reserved              */
	MT2063_REG_RSVD_34,		/*  0x34: Reserved              */
	MT2063_REG_RSVD_35,		/*  0x35: Reserved              */
	MT2063_REG_RSVD_36,		/*  0x36: Reserved              */
	MT2063_REG_RSVD_37,		/*  0x37: Reserved              */
	MT2063_REG_RSVD_38,		/*  0x38: Reserved              */
	MT2063_REG_RSVD_39,		/*  0x39: Reserved              */
	MT2063_REG_RSVD_3A,		/*  0x3A: Reserved              */
	MT2063_REG_RSVD_3B,		/*  0x3B: Reserved              */
	MT2063_REG_RSVD_3C,		/*  0x3C: Reserved              */
	MT2063_REG_END_REGS
};

enum MTTune_atv_standard {
	MTTUNEA_UNKNOWN = 0,
	MTTUNEA_PAL_B,
	MTTUNEA_PAL_G,
	MTTUNEA_PAL_I,
	MTTUNEA_PAL_L,
	MTTUNEA_PAL_MN,
	MTTUNEA_PAL_DK,
	MTTUNEA_DIGITAL,
	MTTUNEA_FMRADIO,
	MTTUNEA_DVBC,
	MTTUNEA_DVBT
};


struct mt2063_state {
	struct i2c_adapter *i2c;

	const struct mt2063_config *config;
	struct dvb_tuner_ops ops;
	struct dvb_frontend *frontend;
	struct tuner_state status;
	bool MT2063_init;

	enum MTTune_atv_standard tv_type;
	u32 frequency;
	u32 srate;
	u32 bandwidth;
	u32 reference;
404 405 406 407 408 409 410 411 412

	u32 tuner_id;
	struct MT2063_AvoidSpursData_t AS_Data;
	u32 f_IF1_actual;
	u32 rcvr_mode;
	u32 ctfilt_sw;
	u32 CTFiltMax[31];
	u32 num_regs;
	u8 reg[MT2063_REG_END_REGS];
413
};
414

415 416 417
/* Prototypes */
static void MT2063_AddExclZone(struct MT2063_AvoidSpursData_t *pAS_Info,
                        u32 f_min, u32 f_max);
418 419 420 421 422 423
static u32 MT2063_ReInit(struct mt2063_state *state);
static u32 MT2063_Close(struct mt2063_state *state);
static u32 MT2063_GetReg(struct mt2063_state *state, u8 reg, u8 * val);
static u32 MT2063_GetParam(struct mt2063_state *state, enum MT2063_Param param, u32 * pValue);
static u32 MT2063_SetReg(struct mt2063_state *state, u8 reg, u8 val);
static u32 MT2063_SetParam(struct mt2063_state *state, enum MT2063_Param param,
424
			   enum MT2063_DNC_Output_Enable nValue);
425

426 427 428
/*****************/
/* From drivers/media/common/tuners/mt2063_cfg.h */

429
unsigned int mt2063_setTune(struct dvb_frontend *fe, u32 f_in,
430 431
			    u32 bw_in,
			    enum MTTune_atv_standard tv_type)
432 433 434 435
{
	struct dvb_frontend_ops *frontend_ops = NULL;
	struct dvb_tuner_ops *tuner_ops = NULL;
	struct tuner_state t_state;
436
	struct mt2063_state *state = fe->tuner_priv;
437 438 439 440
	int err = 0;

	t_state.frequency = f_in;
	t_state.bandwidth = bw_in;
441
	state->tv_type = tv_type;
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
	if (&fe->ops)
		frontend_ops = &fe->ops;
	if (&frontend_ops->tuner_ops)
		tuner_ops = &frontend_ops->tuner_ops;
	if (tuner_ops->set_state) {
		if ((err =
		     tuner_ops->set_state(fe, DVBFE_TUNER_FREQUENCY,
					  &t_state)) < 0) {
			printk("%s: Invalid parameter\n", __func__);
			return err;
		}
	}

	return err;
}

458
unsigned int mt2063_lockStatus(struct dvb_frontend *fe)
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
{
	struct dvb_frontend_ops *frontend_ops = &fe->ops;
	struct dvb_tuner_ops *tuner_ops = &frontend_ops->tuner_ops;
	struct tuner_state t_state;
	int err = 0;

	if (&fe->ops)
		frontend_ops = &fe->ops;
	if (&frontend_ops->tuner_ops)
		tuner_ops = &frontend_ops->tuner_ops;
	if (tuner_ops->get_state) {
		if ((err =
		     tuner_ops->get_state(fe, DVBFE_TUNER_REFCLOCK,
					  &t_state)) < 0) {
			printk("%s: Invalid parameter\n", __func__);
			return err;
		}
	}
	return err;
}

480
unsigned int tuner_MT2063_Open(struct dvb_frontend *fe)
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
{
	struct dvb_frontend_ops *frontend_ops = &fe->ops;
	struct dvb_tuner_ops *tuner_ops = &frontend_ops->tuner_ops;
	struct tuner_state t_state;
	int err = 0;

	if (&fe->ops)
		frontend_ops = &fe->ops;
	if (&frontend_ops->tuner_ops)
		tuner_ops = &frontend_ops->tuner_ops;
	if (tuner_ops->set_state) {
		if ((err =
		     tuner_ops->set_state(fe, DVBFE_TUNER_OPEN,
					  &t_state)) < 0) {
			printk("%s: Invalid parameter\n", __func__);
			return err;
		}
	}

	return err;
}

503
unsigned int tuner_MT2063_SoftwareShutdown(struct dvb_frontend *fe)
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
{
	struct dvb_frontend_ops *frontend_ops = &fe->ops;
	struct dvb_tuner_ops *tuner_ops = &frontend_ops->tuner_ops;
	struct tuner_state t_state;
	int err = 0;

	if (&fe->ops)
		frontend_ops = &fe->ops;
	if (&frontend_ops->tuner_ops)
		tuner_ops = &frontend_ops->tuner_ops;
	if (tuner_ops->set_state) {
		if ((err =
		     tuner_ops->set_state(fe, DVBFE_TUNER_SOFTWARE_SHUTDOWN,
					  &t_state)) < 0) {
			printk("%s: Invalid parameter\n", __func__);
			return err;
		}
	}

	return err;
}

526
unsigned int tuner_MT2063_ClearPowerMaskBits(struct dvb_frontend *fe)
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
{
	struct dvb_frontend_ops *frontend_ops = &fe->ops;
	struct dvb_tuner_ops *tuner_ops = &frontend_ops->tuner_ops;
	struct tuner_state t_state;
	int err = 0;

	if (&fe->ops)
		frontend_ops = &fe->ops;
	if (&frontend_ops->tuner_ops)
		tuner_ops = &frontend_ops->tuner_ops;
	if (tuner_ops->set_state) {
		if ((err =
		     tuner_ops->set_state(fe, DVBFE_TUNER_CLEAR_POWER_MASKBITS,
					  &t_state)) < 0) {
			printk("%s: Invalid parameter\n", __func__);
			return err;
		}
	}

	return err;
}

/*****************/

551 552
//i2c operation
static int mt2063_writeregs(struct mt2063_state *state, u8 reg1,
553
			    u8 *data, int len)
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
{
	int ret;
	u8 buf[60];		/* = { reg1, data }; */

	struct i2c_msg msg = {
		.addr = state->config->tuner_address,
		.flags = 0,
		.buf = buf,
		.len = len + 1
	};

	msg.buf[0] = reg1;
	memcpy(msg.buf + 1, data, len);

	//printk("mt2063_writeregs state->i2c=%p\n", state->i2c);
	ret = i2c_transfer(state->i2c, &msg, 1);

	if (ret < 0)
		printk("mt2063_writeregs error ret=%d\n", ret);

	return ret;
}

static int mt2063_read_regs(struct mt2063_state *state, u8 reg1, u8 * b, u8 len)
{
	int ret;
	u8 b0[] = { reg1 };
	struct i2c_msg msg[] = {
		{
		 .addr = state->config->tuner_address,
		 .flags = I2C_M_RD,
		 .buf = b0,
		 .len = 1}, {
			     .addr = state->config->tuner_address,
			     .flags = I2C_M_RD,
			     .buf = b,
			     .len = len}
	};

	//printk("mt2063_read_regs state->i2c=%p\n", state->i2c);
	ret = i2c_transfer(state->i2c, msg, 2);
	if (ret < 0)
		printk("mt2063_readregs error ret=%d\n", ret);

	return ret;
}

//context of mt2063_userdef.c   <Henry> ======================================
//#################################################################
//=================================================================
/*****************************************************************************
**
**  Name: MT_WriteSub
**
**  Description:    Write values to device using a two-wire serial bus.
**
**  Parameters:     hUserData  - User-specific I/O parameter that was
**                               passed to tuner's Open function.
**                  addr       - device serial bus address  (value passed
**                               as parameter to MTxxxx_Open)
**                  subAddress - serial bus sub-address (Register Address)
**                  pData      - pointer to the Data to be written to the
**                               device
**                  cnt        - number of bytes/registers to be written
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_COMM_ERR      - Serial bus communications error
**                      user-defined
**
**  Notes:          This is a callback function that is called from the
**                  the tuning algorithm.  You MUST provide code for this
**                  function to write data using the tuner's 2-wire serial
**                  bus.
**
**                  The hUserData parameter is a user-specific argument.
**                  If additional arguments are needed for the user's
**                  serial bus read/write functions, this argument can be
**                  used to supply the necessary information.
**                  The hUserData parameter is initialized in the tuner's Open
**                  function.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   03-25-2004    DAD    Original
**
*****************************************************************************/
643 644
static u32 MT2063_WriteSub(struct mt2063_state *state,
			   u8 subAddress, u8 *pData, u32 cnt)
645
{
646
	u32 status = 0;	/* Status to be returned        */
647 648
	struct dvb_frontend *fe = state->frontend;

649 650 651 652 653 654 655 656 657
	/*
	 **  ToDo:  Add code here to implement a serial-bus write
	 **         operation to the MTxxxx tuner.  If successful,
	 **         return MT_OK.
	 */

	fe->ops.i2c_gate_ctrl(fe, 1);	//I2C bypass drxk3926 close i2c bridge

	if (mt2063_writeregs(state, subAddress, pData, cnt) < 0) {
658
		status = -EINVAL;
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
	}
	fe->ops.i2c_gate_ctrl(fe, 0);	//I2C bypass drxk3926 close i2c bridge

	return (status);
}

/*****************************************************************************
**
**  Name: MT_ReadSub
**
**  Description:    Read values from device using a two-wire serial bus.
**
**  Parameters:     hUserData  - User-specific I/O parameter that was
**                               passed to tuner's Open function.
**                  addr       - device serial bus address  (value passed
**                               as parameter to MTxxxx_Open)
**                  subAddress - serial bus sub-address (Register Address)
**                  pData      - pointer to the Data to be written to the
**                               device
**                  cnt        - number of bytes/registers to be written
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_COMM_ERR      - Serial bus communications error
**                      user-defined
**
**  Notes:          This is a callback function that is called from the
**                  the tuning algorithm.  You MUST provide code for this
**                  function to read data using the tuner's 2-wire serial
**                  bus.
**
**                  The hUserData parameter is a user-specific argument.
**                  If additional arguments are needed for the user's
**                  serial bus read/write functions, this argument can be
**                  used to supply the necessary information.
**                  The hUserData parameter is initialized in the tuner's Open
**                  function.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   03-25-2004    DAD    Original
**
*****************************************************************************/
704 705
static u32 MT2063_ReadSub(struct mt2063_state *state,
			   u8 subAddress, u8 *pData, u32 cnt)
706
{
707 708 709 710
	u32 status = 0;	/* Status to be returned        */
	struct dvb_frontend *fe = state->frontend;
	u32 i = 0;

711 712 713 714 715 716 717 718 719
	/*
	 **  ToDo:  Add code here to implement a serial-bus read
	 **         operation to the MTxxxx tuner.  If successful,
	 **         return MT_OK.
	 */
	fe->ops.i2c_gate_ctrl(fe, 1);	//I2C bypass drxk3926 close i2c bridge

	for (i = 0; i < cnt; i++) {
		if (mt2063_read_regs(state, subAddress + i, pData + i, 1) < 0) {
720
			status = -EINVAL;
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
			break;
		}
	}

	fe->ops.i2c_gate_ctrl(fe, 0);	//I2C bypass drxk3926 close i2c bridge

	return (status);
}

/*****************************************************************************
**
**  Name: MT_Sleep
**
**  Description:    Delay execution for "nMinDelayTime" milliseconds
**
**  Parameters:     hUserData     - User-specific I/O parameter that was
**                                  passed to tuner's Open function.
**                  nMinDelayTime - Delay time in milliseconds
**
**  Returns:        None.
**
**  Notes:          This is a callback function that is called from the
**                  the tuning algorithm.  You MUST provide code that
**                  blocks execution for the specified period of time.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   03-25-2004    DAD    Original
**
*****************************************************************************/
753
static int MT2063_Sleep(struct dvb_frontend *fe)
754 755 756 757 758
{
	/*
	 **  ToDo:  Add code here to implement a OS blocking
	 **         for a period of "nMinDelayTime" milliseconds.
	 */
759 760 761
	msleep(10);

	return 0;
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
}

//end of mt2063_userdef.c
//=================================================================
//#################################################################
//=================================================================

//context of mt2063_spuravoid.c <Henry> ======================================
//#################################################################
//=================================================================

/*****************************************************************************
**
**  Name: mt_spuravoid.c
**
**  Description:    Microtune spur avoidance software module.
**                  Supports Microtune tuner drivers.
**
**  CVS ID:         $Id: mt_spuravoid.c,v 1.3 2008/06/26 15:39:52 software Exp $
**  CVS Source:     $Source: /export/home/cvsroot/software/tuners/MT2063/mt_spuravoid.c,v $
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   082   03-25-2005    JWS    Original multi-tuner support - requires
**                              MTxxxx_CNT declarations
**   096   04-06-2005    DAD    Ver 1.11: Fix divide by 0 error if maxH==0.
**   094   04-06-2005    JWS    Ver 1.11 Added uceil and ufloor to get rid
**                              of compiler warnings
**   N/A   04-07-2005    DAD    Ver 1.13: Merged single- and multi-tuner spur
**                              avoidance into a single module.
**   103   01-31-2005    DAD    Ver 1.14: In MT_AddExclZone(), if the range
**                              (f_min, f_max) < 0, ignore the entry.
**   115   03-23-2007    DAD    Fix declaration of spur due to truncation
**                              errors.
**   117   03-29-2007    RSK    Ver 1.15: Re-wrote to match search order from
**                              tuner DLL.
**   137   06-18-2007    DAD    Ver 1.16: Fix possible divide-by-0 error for
**                              multi-tuners that have
**                              (delta IF1) > (f_out-f_outbw/2).
**   147   07-27-2007    RSK    Ver 1.17: Corrected calculation (-) to (+)
**                              Added logic to force f_Center within 1/2 f_Step.
**   177 S 02-26-2008    RSK    Ver 1.18: Corrected calculation using LO1 > MAX/2
**                              Type casts added to preserve correct sign.
**   N/A I 06-17-2008    RSK    Ver 1.19: Refactoring avoidance of DECT
**                              frequencies into MT_ResetExclZones().
**   N/A I 06-20-2008    RSK    Ver 1.21: New VERSION number for ver checking.
**
*****************************************************************************/

/*  Version of this module                         */
#define MT2063_SPUR_VERSION 10201	/*  Version 01.21 */

/*  Implement ceiling, floor functions.  */
#define ceil(n, d) (((n) < 0) ? (-((-(n))/(d))) : (n)/(d) + ((n)%(d) != 0))
#define uceil(n, d) ((n)/(d) + ((n)%(d) != 0))
#define floor(n, d) (((n) < 0) ? (-((-(n))/(d))) - ((n)%(d) != 0) : (n)/(d))
#define ufloor(n, d) ((n)/(d))

struct MT2063_FIFZone_t {
823 824
	s32 min_;
	s32 max_;
825 826 827 828
};

#if MT2063_TUNER_CNT > 1
static struct MT2063_AvoidSpursData_t *TunerList[MT2063_TUNER_CNT];
829
static u32 TunerCount = 0;
830 831
#endif

832
static u32 MT2063_RegisterTuner(struct MT2063_AvoidSpursData_t *pAS_Info)
833 834 835
{
#if MT2063_TUNER_CNT == 1
	pAS_Info->nAS_Algorithm = 1;
836
	return 0;
837
#else
838
	u32 index;
839 840 841 842 843 844 845 846

	pAS_Info->nAS_Algorithm = 2;

	/*
	 **  Check to see if tuner is already registered
	 */
	for (index = 0; index < TunerCount; index++) {
		if (TunerList[index] == pAS_Info) {
847
			return 0;	/* Already here - no problem  */
848 849 850 851 852 853 854 855 856
		}
	}

	/*
	 ** Add tuner to list - if there is room.
	 */
	if (TunerCount < MT2063_TUNER_CNT) {
		TunerList[TunerCount] = pAS_Info;
		TunerCount++;
857
		return 0;
858
	} else
859
		return -ENODEV;
860 861 862
#endif
}

863
static void MT2063_UnRegisterTuner(struct MT2063_AvoidSpursData_t *pAS_Info)
864
{
865
#if MT2063_TUNER_CNT > 1
866
	u32 index;
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882

	for (index = 0; index < TunerCount; index++) {
		if (TunerList[index] == pAS_Info) {
			TunerList[index] = TunerList[--TunerCount];
		}
	}
#endif
}

/*
**  Reset all exclusion zones.
**  Add zones to protect the PLL FracN regions near zero
**
**   N/A I 06-17-2008    RSK    Ver 1.19: Refactoring avoidance of DECT
**                              frequencies into MT_ResetExclZones().
*/
883
static void MT2063_ResetExclZones(struct MT2063_AvoidSpursData_t *pAS_Info)
884
{
885
	u32 center;
886
#if MT2063_TUNER_CNT > 1
887
	u32 index;
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
	struct MT2063_AvoidSpursData_t *adj;
#endif

	pAS_Info->nZones = 0;	/*  this clears the used list  */
	pAS_Info->usedZones = NULL;	/*  reset ptr                  */
	pAS_Info->freeZones = NULL;	/*  reset ptr                  */

	center =
	    pAS_Info->f_ref *
	    ((pAS_Info->f_if1_Center - pAS_Info->f_if1_bw / 2 +
	      pAS_Info->f_in) / pAS_Info->f_ref) - pAS_Info->f_in;
	while (center <
	       pAS_Info->f_if1_Center + pAS_Info->f_if1_bw / 2 +
	       pAS_Info->f_LO1_FracN_Avoid) {
		/*  Exclude LO1 FracN  */
		MT2063_AddExclZone(pAS_Info,
				   center - pAS_Info->f_LO1_FracN_Avoid,
				   center - 1);
		MT2063_AddExclZone(pAS_Info, center + 1,
				   center + pAS_Info->f_LO1_FracN_Avoid);
		center += pAS_Info->f_ref;
	}

	center =
	    pAS_Info->f_ref *
	    ((pAS_Info->f_if1_Center - pAS_Info->f_if1_bw / 2 -
	      pAS_Info->f_out) / pAS_Info->f_ref) + pAS_Info->f_out;
	while (center <
	       pAS_Info->f_if1_Center + pAS_Info->f_if1_bw / 2 +
	       pAS_Info->f_LO2_FracN_Avoid) {
		/*  Exclude LO2 FracN  */
		MT2063_AddExclZone(pAS_Info,
				   center - pAS_Info->f_LO2_FracN_Avoid,
				   center - 1);
		MT2063_AddExclZone(pAS_Info, center + 1,
				   center + pAS_Info->f_LO2_FracN_Avoid);
		center += pAS_Info->f_ref;
	}

	if (MT2063_EXCLUDE_US_DECT_FREQUENCIES(pAS_Info->avoidDECT)) {
		/*  Exclude LO1 values that conflict with DECT channels */
		MT2063_AddExclZone(pAS_Info, 1920836000 - pAS_Info->f_in, 1922236000 - pAS_Info->f_in);	/* Ctr = 1921.536 */
		MT2063_AddExclZone(pAS_Info, 1922564000 - pAS_Info->f_in, 1923964000 - pAS_Info->f_in);	/* Ctr = 1923.264 */
		MT2063_AddExclZone(pAS_Info, 1924292000 - pAS_Info->f_in, 1925692000 - pAS_Info->f_in);	/* Ctr = 1924.992 */
		MT2063_AddExclZone(pAS_Info, 1926020000 - pAS_Info->f_in, 1927420000 - pAS_Info->f_in);	/* Ctr = 1926.720 */
		MT2063_AddExclZone(pAS_Info, 1927748000 - pAS_Info->f_in, 1929148000 - pAS_Info->f_in);	/* Ctr = 1928.448 */
	}

	if (MT2063_EXCLUDE_EURO_DECT_FREQUENCIES(pAS_Info->avoidDECT)) {
		MT2063_AddExclZone(pAS_Info, 1896644000 - pAS_Info->f_in, 1898044000 - pAS_Info->f_in);	/* Ctr = 1897.344 */
		MT2063_AddExclZone(pAS_Info, 1894916000 - pAS_Info->f_in, 1896316000 - pAS_Info->f_in);	/* Ctr = 1895.616 */
		MT2063_AddExclZone(pAS_Info, 1893188000 - pAS_Info->f_in, 1894588000 - pAS_Info->f_in);	/* Ctr = 1893.888 */
		MT2063_AddExclZone(pAS_Info, 1891460000 - pAS_Info->f_in, 1892860000 - pAS_Info->f_in);	/* Ctr = 1892.16  */
		MT2063_AddExclZone(pAS_Info, 1889732000 - pAS_Info->f_in, 1891132000 - pAS_Info->f_in);	/* Ctr = 1890.432 */
		MT2063_AddExclZone(pAS_Info, 1888004000 - pAS_Info->f_in, 1889404000 - pAS_Info->f_in);	/* Ctr = 1888.704 */
		MT2063_AddExclZone(pAS_Info, 1886276000 - pAS_Info->f_in, 1887676000 - pAS_Info->f_in);	/* Ctr = 1886.976 */
		MT2063_AddExclZone(pAS_Info, 1884548000 - pAS_Info->f_in, 1885948000 - pAS_Info->f_in);	/* Ctr = 1885.248 */
		MT2063_AddExclZone(pAS_Info, 1882820000 - pAS_Info->f_in, 1884220000 - pAS_Info->f_in);	/* Ctr = 1883.52  */
		MT2063_AddExclZone(pAS_Info, 1881092000 - pAS_Info->f_in, 1882492000 - pAS_Info->f_in);	/* Ctr = 1881.792 */
	}
#if MT2063_TUNER_CNT > 1
	/*
	 ** Iterate through all adjacent tuners and exclude frequencies related to them
	 */
	for (index = 0; index < TunerCount; ++index) {
		adj = TunerList[index];
		if (pAS_Info == adj)	/* skip over our own data, don't process it */
			continue;

		/*
		 **  Add 1st IF exclusion zone covering adjacent tuner's LO2
		 **  at "adjfLO2 + f_out" +/- m_MinLOSpacing
		 */
		if (adj->f_LO2 != 0)
			MT2063_AddExclZone(pAS_Info,
					   (adj->f_LO2 + pAS_Info->f_out) -
					   pAS_Info->f_min_LO_Separation,
					   (adj->f_LO2 + pAS_Info->f_out) +
					   pAS_Info->f_min_LO_Separation);

		/*
		 **  Add 1st IF exclusion zone covering adjacent tuner's LO1
		 **  at "adjfLO1 - f_in" +/- m_MinLOSpacing
		 */
		if (adj->f_LO1 != 0)
			MT2063_AddExclZone(pAS_Info,
					   (adj->f_LO1 - pAS_Info->f_in) -
					   pAS_Info->f_min_LO_Separation,
					   (adj->f_LO1 - pAS_Info->f_in) +
					   pAS_Info->f_min_LO_Separation);
	}
#endif
}

static struct MT2063_ExclZone_t *InsertNode(struct MT2063_AvoidSpursData_t
					    *pAS_Info,
					    struct MT2063_ExclZone_t *pPrevNode)
{
	struct MT2063_ExclZone_t *pNode;
	/*  Check for a node in the free list  */
	if (pAS_Info->freeZones != NULL) {
		/*  Use one from the free list  */
		pNode = pAS_Info->freeZones;
		pAS_Info->freeZones = pNode->next_;
	} else {
		/*  Grab a node from the array  */
		pNode = &pAS_Info->MT2063_ExclZones[pAS_Info->nZones];
	}

	if (pPrevNode != NULL) {
		pNode->next_ = pPrevNode->next_;
		pPrevNode->next_ = pNode;
	} else {		/*  insert at the beginning of the list  */

		pNode->next_ = pAS_Info->usedZones;
		pAS_Info->usedZones = pNode;
	}

	pAS_Info->nZones++;
	return pNode;
}

static struct MT2063_ExclZone_t *RemoveNode(struct MT2063_AvoidSpursData_t
					    *pAS_Info,
					    struct MT2063_ExclZone_t *pPrevNode,
					    struct MT2063_ExclZone_t
					    *pNodeToRemove)
{
	struct MT2063_ExclZone_t *pNext = pNodeToRemove->next_;

	/*  Make previous node point to the subsequent node  */
	if (pPrevNode != NULL)
		pPrevNode->next_ = pNext;

	/*  Add pNodeToRemove to the beginning of the freeZones  */
	pNodeToRemove->next_ = pAS_Info->freeZones;
	pAS_Info->freeZones = pNodeToRemove;

	/*  Decrement node count  */
	pAS_Info->nZones--;

	return pNext;
}

/*****************************************************************************
**
**  Name: MT_AddExclZone
**
**  Description:    Add (and merge) an exclusion zone into the list.
**                  If the range (f_min, f_max) is totally outside the
**                  1st IF BW, ignore the entry.
**                  If the range (f_min, f_max) is negative, ignore the entry.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   103   01-31-2005    DAD    Ver 1.14: In MT_AddExclZone(), if the range
**                              (f_min, f_max) < 0, ignore the entry.
**
*****************************************************************************/
1049
static void MT2063_AddExclZone(struct MT2063_AvoidSpursData_t *pAS_Info,
1050
			u32 f_min, u32 f_max)
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
{
	struct MT2063_ExclZone_t *pNode = pAS_Info->usedZones;
	struct MT2063_ExclZone_t *pPrev = NULL;
	struct MT2063_ExclZone_t *pNext = NULL;

	/*  Check to see if this overlaps the 1st IF filter  */
	if ((f_max > (pAS_Info->f_if1_Center - (pAS_Info->f_if1_bw / 2)))
	    && (f_min < (pAS_Info->f_if1_Center + (pAS_Info->f_if1_bw / 2)))
	    && (f_min < f_max)) {
		/*
		 **                1           2          3        4         5          6
		 **
		 **   New entry:  |---|    |--|        |--|       |-|      |---|         |--|
		 **                     or          or        or       or        or
		 **   Existing:  |--|          |--|      |--|    |---|      |-|      |--|
		 */

		/*  Check for our place in the list  */
		while ((pNode != NULL) && (pNode->max_ < f_min)) {
			pPrev = pNode;
			pNode = pNode->next_;
		}

		if ((pNode != NULL) && (pNode->min_ < f_max)) {
			/*  Combine me with pNode  */
			if (f_min < pNode->min_)
				pNode->min_ = f_min;
			if (f_max > pNode->max_)
				pNode->max_ = f_max;
		} else {
			pNode = InsertNode(pAS_Info, pPrev);
			pNode->min_ = f_min;
			pNode->max_ = f_max;
		}

		/*  Look for merging possibilities  */
		pNext = pNode->next_;
		while ((pNext != NULL) && (pNext->min_ < pNode->max_)) {
			if (pNext->max_ > pNode->max_)
				pNode->max_ = pNext->max_;
			pNext = RemoveNode(pAS_Info, pNode, pNext);	/*  Remove pNext, return ptr to pNext->next  */
		}
	}
}

/*****************************************************************************
**
**  Name: MT_ChooseFirstIF
**
**  Description:    Choose the best available 1st IF
**                  If f_Desired is not excluded, choose that first.
**                  Otherwise, return the value closest to f_Center that is
**                  not excluded
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   117   03-29-2007    RSK    Ver 1.15: Re-wrote to match search order from
**                              tuner DLL.
**   147   07-27-2007    RSK    Ver 1.17: Corrected calculation (-) to (+)
**                              Added logic to force f_Center within 1/2 f_Step.
**
*****************************************************************************/
1115
static u32 MT2063_ChooseFirstIF(struct MT2063_AvoidSpursData_t *pAS_Info)
1116 1117 1118 1119 1120 1121 1122
{
	/*
	 ** Update "f_Desired" to be the nearest "combinational-multiple" of "f_LO1_Step".
	 ** The resulting number, F_LO1 must be a multiple of f_LO1_Step.  And F_LO1 is the arithmetic sum
	 ** of f_in + f_Center.  Neither f_in, nor f_Center must be a multiple of f_LO1_Step.
	 ** However, the sum must be.
	 */
1123
	const u32 f_Desired =
1124 1125 1126 1127
	    pAS_Info->f_LO1_Step *
	    ((pAS_Info->f_if1_Request + pAS_Info->f_in +
	      pAS_Info->f_LO1_Step / 2) / pAS_Info->f_LO1_Step) -
	    pAS_Info->f_in;
1128
	const u32 f_Step =
1129 1130 1131
	    (pAS_Info->f_LO1_Step >
	     pAS_Info->f_LO2_Step) ? pAS_Info->f_LO1_Step : pAS_Info->
	    f_LO2_Step;
1132 1133 1134 1135 1136 1137 1138 1139
	u32 f_Center;

	s32 i;
	s32 j = 0;
	u32 bDesiredExcluded = 0;
	u32 bZeroExcluded = 0;
	s32 tmpMin, tmpMax;
	s32 bestDiff;
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
	struct MT2063_ExclZone_t *pNode = pAS_Info->usedZones;
	struct MT2063_FIFZone_t zones[MT2063_MAX_ZONES];

	if (pAS_Info->nZones == 0)
		return f_Desired;

	/*  f_Center needs to be an integer multiple of f_Step away from f_Desired */
	if (pAS_Info->f_if1_Center > f_Desired)
		f_Center =
		    f_Desired +
		    f_Step *
		    ((pAS_Info->f_if1_Center - f_Desired +
		      f_Step / 2) / f_Step);
	else
		f_Center =
		    f_Desired -
		    f_Step *
		    ((f_Desired - pAS_Info->f_if1_Center +
		      f_Step / 2) / f_Step);

	//assert;
1161
	//if (!abs((s32) f_Center - (s32) pAS_Info->f_if1_Center) <= (s32) (f_Step/2))
1162 1163 1164 1165 1166 1167
	//          return 0;

	/*  Take MT_ExclZones, center around f_Center and change the resolution to f_Step  */
	while (pNode != NULL) {
		/*  floor function  */
		tmpMin =
1168
		    floor((s32) (pNode->min_ - f_Center), (s32) f_Step);
1169 1170 1171

		/*  ceil function  */
		tmpMax =
1172
		    ceil((s32) (pNode->max_ - f_Center), (s32) f_Step);
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217

		if ((pNode->min_ < f_Desired) && (pNode->max_ > f_Desired))
			bDesiredExcluded = 1;

		if ((tmpMin < 0) && (tmpMax > 0))
			bZeroExcluded = 1;

		/*  See if this zone overlaps the previous  */
		if ((j > 0) && (tmpMin < zones[j - 1].max_))
			zones[j - 1].max_ = tmpMax;
		else {
			/*  Add new zone  */
			//assert(j<MT2063_MAX_ZONES);
			//if (j>=MT2063_MAX_ZONES)
			//break;

			zones[j].min_ = tmpMin;
			zones[j].max_ = tmpMax;
			j++;
		}
		pNode = pNode->next_;
	}

	/*
	 **  If the desired is okay, return with it
	 */
	if (bDesiredExcluded == 0)
		return f_Desired;

	/*
	 **  If the desired is excluded and the center is okay, return with it
	 */
	if (bZeroExcluded == 0)
		return f_Center;

	/*  Find the value closest to 0 (f_Center)  */
	bestDiff = zones[0].min_;
	for (i = 0; i < j; i++) {
		if (abs(zones[i].min_) < abs(bestDiff))
			bestDiff = zones[i].min_;
		if (abs(zones[i].max_) < abs(bestDiff))
			bestDiff = zones[i].max_;
	}

	if (bestDiff < 0)
1218
		return f_Center - ((u32) (-bestDiff) * f_Step);
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246

	return f_Center + (bestDiff * f_Step);
}

/****************************************************************************
**
**  Name: gcd
**
**  Description:    Uses Euclid's algorithm
**
**  Parameters:     u, v     - unsigned values whose GCD is desired.
**
**  Global:         None
**
**  Returns:        greatest common divisor of u and v, if either value
**                  is 0, the other value is returned as the result.
**
**  Dependencies:   None.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   06-01-2004    JWS    Original
**   N/A   08-03-2004    DAD    Changed to Euclid's since it can handle
**                              unsigned numbers.
**
****************************************************************************/
1247
static u32 MT2063_gcd(u32 u, u32 v)
1248
{
1249
	u32 r;
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282

	while (v != 0) {
		r = u % v;
		u = v;
		v = r;
	}

	return u;
}

/****************************************************************************
**
**  Name: umax
**
**  Description:    Implements a simple maximum function for unsigned numbers.
**                  Implemented as a function rather than a macro to avoid
**                  multiple evaluation of the calling parameters.
**
**  Parameters:     a, b     - Values to be compared
**
**  Global:         None
**
**  Returns:        larger of the input values.
**
**  Dependencies:   None.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   06-02-2004    JWS    Original
**
****************************************************************************/
1283
static u32 MT2063_umax(u32 a, u32 b)
1284 1285 1286 1287 1288
{
	return (a >= b) ? a : b;
}

#if MT2063_TUNER_CNT > 1
1289
static s32 RoundAwayFromZero(s32 n, s32 d)
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
{
	return (n < 0) ? floor(n, d) : ceil(n, d);
}

/****************************************************************************
**
**  Name: IsSpurInAdjTunerBand
**
**  Description:    Checks to see if a spur will be present within the IF's
**                  bandwidth or near the zero IF.
**                  (fIFOut +/- fIFBW/2, -fIFOut +/- fIFBW/2)
**                                  and
**                  (0 +/- fZIFBW/2)
**
**                    ma   mb               me   mf               mc   md
**                  <--+-+-+-----------------+-+-+-----------------+-+-+-->
**                     |   ^                   0                   ^   |
**                     ^   b=-fIFOut+fIFBW/2      -b=+fIFOut-fIFBW/2   ^
**                     a=-fIFOut-fIFBW/2              -a=+fIFOut+fIFBW/2
**
**                  Note that some equations are doubled to prevent round-off
**                  problems when calculating fIFBW/2
**
**                  The spur frequencies are computed as:
**
**                     fSpur = n * f1 - m * f2 - fOffset
**
**  Parameters:     f1      - The 1st local oscillator (LO) frequency
**                            of the tuner whose output we are examining
**                  f2      - The 1st local oscillator (LO) frequency
**                            of the adjacent tuner
**                  fOffset - The 2nd local oscillator of the tuner whose
**                            output we are examining
**                  fIFOut  - Output IF center frequency
**                  fIFBW   - Output IF Bandwidth
**                  nMaxH   - max # of LO harmonics to search
**                  fp      - If spur, positive distance to spur-free band edge (returned)
**                  fm      - If spur, negative distance to spur-free band edge (returned)
**
**  Returns:        1 if an LO spur would be present, otherwise 0.
**
**  Dependencies:   None.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   01-21-2005    JWS    Original, adapted from MT_DoubleConversion.
**   115   03-23-2007    DAD    Fix declaration of spur due to truncation
**                              errors.
**   137   06-18-2007    DAD    Ver 1.16: Fix possible divide-by-0 error for
**                              multi-tuners that have
**                              (delta IF1) > (f_out-f_outbw/2).
**   177 S 02-26-2008    RSK    Ver 1.18: Corrected calculation using LO1 > MAX/2
**                              Type casts added to preserve correct sign.
**
****************************************************************************/
1347 1348 1349 1350 1351 1352 1353 1354
static u32 IsSpurInAdjTunerBand(u32 bIsMyOutput,
				    u32 f1,
				    u32 f2,
				    u32 fOffset,
				    u32 fIFOut,
				    u32 fIFBW,
				    u32 fZIFBW,
				    u32 nMaxH, u32 * fp, u32 * fm)
1355
{
1356
	u32 bSpurFound = 0;
1357

1358 1359
	const u32 fHalf_IFBW = fIFBW / 2;
	const u32 fHalf_ZIFBW = fZIFBW / 2;
1360 1361 1362

	/* Calculate a scale factor for all frequencies, so that our
	   calculations all stay within 31 bits */
1363
	const u32 f_Scale =
1364 1365 1366 1367 1368 1369 1370 1371
	    ((f1 +
	      (fOffset + fIFOut +
	       fHalf_IFBW) / nMaxH) / (MAX_UDATA / 2 / nMaxH)) + 1;

	/*
	 **  After this scaling, _f1, _f2, and _f3 are guaranteed to fit into
	 **  signed data types (smaller than MAX_UDATA/2)
	 */
1372 1373 1374
	const s32 _f1 = (s32) (f1 / f_Scale);
	const s32 _f2 = (s32) (f2 / f_Scale);
	const s32 _f3 = (s32) (fOffset / f_Scale);
1375

1376 1377 1378
	const s32 c = (s32) (fIFOut - fHalf_IFBW) / (s32) f_Scale;
	const s32 d = (s32) ((fIFOut + fHalf_IFBW) / f_Scale);
	const s32 f = (s32) (fHalf_ZIFBW / f_Scale);
1379

1380
	s32 ma, mb, mc, md, me, mf;
1381

1382 1383 1384
	s32 fp_ = 0;
	s32 fm_ = 0;
	s32 n;
1385 1386 1387 1388 1389 1390 1391 1392 1393

	/*
	 **  If the other tuner does not have an LO frequency defined,
	 **  assume that we cannot interfere with it
	 */
	if (f2 == 0)
		return 0;

	/* Check out all multiples of f1 from -nMaxH to +nMaxH */
1394 1395
	for (n = -(s32) nMaxH; n <= (s32) nMaxH; ++n) {
		const s32 nf1 = n * _f1;
1396 1397 1398
		md = (_f3 + d - nf1) / _f2;

		/* If # f2 harmonics > nMaxH, then no spurs present */
1399
		if (md <= -(s32) nMaxH)
1400 1401 1402
			break;

		ma = (_f3 - d - nf1) / _f2;
1403
		if ((ma == md) || (ma >= (s32) (nMaxH)))
1404 1405 1406 1407
			continue;

		mc = (_f3 + c - nf1) / _f2;
		if (mc != md) {
1408 1409 1410
			const s32 m = (n < 0) ? md : mc;
			const s32 fspur = (nf1 + m * _f2 - _f3);
			const s32 den = (bIsMyOutput ? n - 1 : n);
1411 1412 1413 1414 1415
			if (den == 0) {
				fp_ = (d - fspur) * f_Scale;
				fm_ = (fspur - c) * f_Scale;
			} else {
				fp_ =
1416
				    (s32) RoundAwayFromZero((d - fspur) *
1417 1418
								f_Scale, den);
				fm_ =
1419
				    (s32) RoundAwayFromZero((fspur - c) *
1420 1421
								f_Scale, den);
			}
1422 1423
			if (((u32) abs(fm_) >= f_Scale)
			    && ((u32) abs(fp_) >= f_Scale)) {
1424 1425 1426 1427 1428 1429 1430 1431 1432
				bSpurFound = 1;
				break;
			}
		}

		/* Location of Zero-IF-spur to be checked */
		mf = (_f3 + f - nf1) / _f2;
		me = (_f3 - f - nf1) / _f2;
		if (me != mf) {
1433 1434 1435
			const s32 m = (n < 0) ? mf : me;
			const s32 fspur = (nf1 + m * _f2 - _f3);
			const s32 den = (bIsMyOutput ? n - 1 : n);
1436 1437 1438 1439 1440
			if (den == 0) {
				fp_ = (d - fspur) * f_Scale;
				fm_ = (fspur - c) * f_Scale;
			} else {
				fp_ =
1441
				    (s32) RoundAwayFromZero((f - fspur) *
1442 1443
								f_Scale, den);
				fm_ =
1444
				    (s32) RoundAwayFromZero((fspur + f) *
1445 1446
								f_Scale, den);
			}
1447 1448
			if (((u32) abs(fm_) >= f_Scale)
			    && ((u32) abs(fp_) >= f_Scale)) {
1449 1450 1451 1452 1453 1454 1455
				bSpurFound = 1;
				break;
			}
		}

		mb = (_f3 - c - nf1) / _f2;
		if (ma != mb) {
1456 1457 1458
			const s32 m = (n < 0) ? mb : ma;
			const s32 fspur = (nf1 + m * _f2 - _f3);
			const s32 den = (bIsMyOutput ? n - 1 : n);
1459 1460 1461 1462 1463
			if (den == 0) {
				fp_ = (d - fspur) * f_Scale;
				fm_ = (fspur - c) * f_Scale;
			} else {
				fp_ =
1464
				    (s32) RoundAwayFromZero((-c - fspur) *
1465 1466
								f_Scale, den);
				fm_ =
1467
				    (s32) RoundAwayFromZero((fspur + d) *
1468 1469
								f_Scale, den);
			}
1470 1471
			if (((u32) abs(fm_) >= f_Scale)
			    && ((u32) abs(fp_) >= f_Scale)) {
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
				bSpurFound = 1;
				break;
			}
		}
	}

	/*
	 **  Verify that fm & fp are both positive
	 **  Add one to ensure next 1st IF choice is not right on the edge
	 */
	if (fp_ < 0) {
		*fp = -fm_ + 1;
		*fm = -fp_ + 1;
	} else if (fp_ > 0) {
		*fp = fp_ + 1;
		*fm = fm_ + 1;
	} else {
		*fp = 1;
		*fm = abs(fm_) + 1;
	}

	return bSpurFound;
}
#endif

/****************************************************************************
**
**  Name: IsSpurInBand
**
**  Description:    Checks to see if a spur will be present within the IF's
**                  bandwidth. (fIFOut +/- fIFBW, -fIFOut +/- fIFBW)
**
**                    ma   mb                                     mc   md
**                  <--+-+-+-------------------+-------------------+-+-+-->
**                     |   ^                   0                   ^   |
**                     ^   b=-fIFOut+fIFBW/2      -b=+fIFOut-fIFBW/2   ^
**                     a=-fIFOut-fIFBW/2              -a=+fIFOut+fIFBW/2
**
**                  Note that some equations are doubled to prevent round-off
**                  problems when calculating fIFBW/2
**
**  Parameters:     pAS_Info - Avoid Spurs information block
**                  fm       - If spur, amount f_IF1 has to move negative
**                  fp       - If spur, amount f_IF1 has to move positive
**
**  Global:         None
**
**  Returns:        1 if an LO spur would be present, otherwise 0.
**
**  Dependencies:   None.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   11-28-2002    DAD    Implemented algorithm from applied patent
**
****************************************************************************/
1530 1531
static u32 IsSpurInBand(struct MT2063_AvoidSpursData_t *pAS_Info,
			    u32 * fm, u32 * fp)
1532 1533 1534 1535
{
	/*
	 **  Calculate LO frequency settings.
	 */
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
	u32 n, n0;
	const u32 f_LO1 = pAS_Info->f_LO1;
	const u32 f_LO2 = pAS_Info->f_LO2;
	const u32 d = pAS_Info->f_out + pAS_Info->f_out_bw / 2;
	const u32 c = d - pAS_Info->f_out_bw;
	const u32 f = pAS_Info->f_zif_bw / 2;
	const u32 f_Scale = (f_LO1 / (MAX_UDATA / 2 / pAS_Info->maxH1)) + 1;
	s32 f_nsLO1, f_nsLO2;
	s32 f_Spur;
	u32 ma, mb, mc, md, me, mf;
	u32 lo_gcd, gd_Scale, gc_Scale, gf_Scale, hgds, hgfs, hgcs;
1547
#if MT2063_TUNER_CNT > 1
1548
	u32 index;
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559

	struct MT2063_AvoidSpursData_t *adj;
#endif
	*fm = 0;

	/*
	 ** For each edge (d, c & f), calculate a scale, based on the gcd
	 ** of f_LO1, f_LO2 and the edge value.  Use the larger of this
	 ** gcd-based scale factor or f_Scale.
	 */
	lo_gcd = MT2063_gcd(f_LO1, f_LO2);
1560
	gd_Scale = MT2063_umax((u32) MT2063_gcd(lo_gcd, d), f_Scale);
1561
	hgds = gd_Scale / 2;
1562
	gc_Scale = MT2063_umax((u32) MT2063_gcd(lo_gcd, c), f_Scale);
1563
	hgcs = gc_Scale / 2;
1564
	gf_Scale = MT2063_umax((u32) MT2063_gcd(lo_gcd, f), f_Scale);
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
	hgfs = gf_Scale / 2;

	n0 = uceil(f_LO2 - d, f_LO1 - f_LO2);

	/*  Check out all multiples of LO1 from n0 to m_maxLOSpurHarmonic  */
	for (n = n0; n <= pAS_Info->maxH1; ++n) {
		md = (n * ((f_LO1 + hgds) / gd_Scale) -
		      ((d + hgds) / gd_Scale)) / ((f_LO2 + hgds) / gd_Scale);

		/*  If # fLO2 harmonics > m_maxLOSpurHarmonic, then no spurs present  */
		if (md >= pAS_Info->maxH1)
			break;

		ma = (n * ((f_LO1 + hgds) / gd_Scale) +
		      ((d + hgds) / gd_Scale)) / ((f_LO2 + hgds) / gd_Scale);

		/*  If no spurs between +/- (f_out + f_IFBW/2), then try next harmonic  */
		if (md == ma)
			continue;

		mc = (n * ((f_LO1 + hgcs) / gc_Scale) -
		      ((c + hgcs) / gc_Scale)) / ((f_LO2 + hgcs) / gc_Scale);
		if (mc != md) {
1588 1589
			f_nsLO1 = (s32) (n * (f_LO1 / gc_Scale));
			f_nsLO2 = (s32) (mc * (f_LO2 / gc_Scale));
1590 1591 1592 1593
			f_Spur =
			    (gc_Scale * (f_nsLO1 - f_nsLO2)) +
			    n * (f_LO1 % gc_Scale) - mc * (f_LO2 % gc_Scale);

1594 1595
			*fp = ((f_Spur - (s32) c) / (mc - n)) + 1;
			*fm = (((s32) d - f_Spur) / (mc - n)) + 1;
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
			return 1;
		}

		/*  Location of Zero-IF-spur to be checked  */
		me = (n * ((f_LO1 + hgfs) / gf_Scale) +
		      ((f + hgfs) / gf_Scale)) / ((f_LO2 + hgfs) / gf_Scale);
		mf = (n * ((f_LO1 + hgfs) / gf_Scale) -
		      ((f + hgfs) / gf_Scale)) / ((f_LO2 + hgfs) / gf_Scale);
		if (me != mf) {
			f_nsLO1 = n * (f_LO1 / gf_Scale);
			f_nsLO2 = me * (f_LO2 / gf_Scale);
			f_Spur =
			    (gf_Scale * (f_nsLO1 - f_nsLO2)) +
			    n * (f_LO1 % gf_Scale) - me * (f_LO2 % gf_Scale);

1611 1612
			*fp = ((f_Spur + (s32) f) / (me - n)) + 1;
			*fm = (((s32) f - f_Spur) / (me - n)) + 1;
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
			return 1;
		}

		mb = (n * ((f_LO1 + hgcs) / gc_Scale) +
		      ((c + hgcs) / gc_Scale)) / ((f_LO2 + hgcs) / gc_Scale);
		if (ma != mb) {
			f_nsLO1 = n * (f_LO1 / gc_Scale);
			f_nsLO2 = ma * (f_LO2 / gc_Scale);
			f_Spur =
			    (gc_Scale * (f_nsLO1 - f_nsLO2)) +
			    n * (f_LO1 % gc_Scale) - ma * (f_LO2 % gc_Scale);

1625 1626
			*fp = (((s32) d + f_Spur) / (ma - n)) + 1;
			*fm = (-(f_Spur + (s32) c) / (ma - n)) + 1;
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
			return 1;
		}
	}

#if MT2063_TUNER_CNT > 1
	/*  If no spur found, see if there are more tuners on the same board  */
	for (index = 0; index < TunerCount; ++index) {
		adj = TunerList[index];
		if (pAS_Info == adj)	/* skip over our own data, don't process it */
			continue;

		/*  Look for LO-related spurs from the adjacent tuner generated into my IF output  */
		if (IsSpurInAdjTunerBand(1,	/*  check my IF output                     */
					 pAS_Info->f_LO1,	/*  my fLO1                                */
					 adj->f_LO1,	/*  the other tuner's fLO1                 */
					 pAS_Info->f_LO2,	/*  my fLO2                                */
					 pAS_Info->f_out,	/*  my fOut                                */
					 pAS_Info->f_out_bw,	/*  my output IF bandwidth                 */
					 pAS_Info->f_zif_bw,	/*  my Zero-IF bandwidth                   */
					 pAS_Info->maxH2, fp,	/*  minimum amount to move LO's positive   */
					 fm))	/*  miminum amount to move LO's negative   */
			return 1;
		/*  Look for LO-related spurs from my tuner generated into the adjacent tuner's IF output  */
		if (IsSpurInAdjTunerBand(0,	/*  check his IF output                    */
					 pAS_Info->f_LO1,	/*  my fLO1                                */
					 adj->f_LO1,	/*  the other tuner's fLO1                 */
					 adj->f_LO2,	/*  the other tuner's fLO2                 */
					 adj->f_out,	/*  the other tuner's fOut                 */
					 adj->f_out_bw,	/*  the other tuner's output IF bandwidth  */
					 pAS_Info->f_zif_bw,	/*  the other tuner's Zero-IF bandwidth    */
					 adj->maxH2, fp,	/*  minimum amount to move LO's positive   */
					 fm))	/*  miminum amount to move LO's negative   */
			return 1;
	}
#endif
	/*  No spurs found  */
	return 0;
}

/*****************************************************************************
**
**  Name: MT_AvoidSpurs
**
**  Description:    Main entry point to avoid spurs.
**                  Checks for existing spurs in present LO1, LO2 freqs
**                  and if present, chooses spur-free LO1, LO2 combination
**                  that tunes the same input/output frequencies.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   096   04-06-2005    DAD    Ver 1.11: Fix divide by 0 error if maxH==0.
**
*****************************************************************************/
1682
static u32 MT2063_AvoidSpurs(void *h, struct MT2063_AvoidSpursData_t * pAS_Info)
1683
{
1684
	u32 status = 0;
1685
	u32 fm, fp;		/*  restricted range on LO's        */
1686 1687 1688 1689
	pAS_Info->bSpurAvoided = 0;
	pAS_Info->nSpursFound = 0;

	if (pAS_Info->maxH1 == 0)
1690
		return 0;
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704

	/*
	 **  Avoid LO Generated Spurs
	 **
	 **  Make sure that have no LO-related spurs within the IF output
	 **  bandwidth.
	 **
	 **  If there is an LO spur in this band, start at the current IF1 frequency
	 **  and work out until we find a spur-free frequency or run up against the
	 **  1st IF SAW band edge.  Use temporary copies of fLO1 and fLO2 so that they
	 **  will be unchanged if a spur-free setting is not found.
	 */
	pAS_Info->bSpurPresent = IsSpurInBand(pAS_Info, &fm, &fp);
	if (pAS_Info->bSpurPresent) {
1705 1706 1707 1708 1709
		u32 zfIF1 = pAS_Info->f_LO1 - pAS_Info->f_in;	/*  current attempt at a 1st IF  */
		u32 zfLO1 = pAS_Info->f_LO1;	/*  current attempt at an LO1 freq  */
		u32 zfLO2 = pAS_Info->f_LO2;	/*  current attempt at an LO2 freq  */
		u32 delta_IF1;
		u32 new_IF1;
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816

		/*
		 **  Spur was found, attempt to find a spur-free 1st IF
		 */
		do {
			pAS_Info->nSpursFound++;

			/*  Raise f_IF1_upper, if needed  */
			MT2063_AddExclZone(pAS_Info, zfIF1 - fm, zfIF1 + fp);

			/*  Choose next IF1 that is closest to f_IF1_CENTER              */
			new_IF1 = MT2063_ChooseFirstIF(pAS_Info);

			if (new_IF1 > zfIF1) {
				pAS_Info->f_LO1 += (new_IF1 - zfIF1);
				pAS_Info->f_LO2 += (new_IF1 - zfIF1);
			} else {
				pAS_Info->f_LO1 -= (zfIF1 - new_IF1);
				pAS_Info->f_LO2 -= (zfIF1 - new_IF1);
			}
			zfIF1 = new_IF1;

			if (zfIF1 > pAS_Info->f_if1_Center)
				delta_IF1 = zfIF1 - pAS_Info->f_if1_Center;
			else
				delta_IF1 = pAS_Info->f_if1_Center - zfIF1;
		}
		/*
		 **  Continue while the new 1st IF is still within the 1st IF bandwidth
		 **  and there is a spur in the band (again)
		 */
		while ((2 * delta_IF1 + pAS_Info->f_out_bw <=
			pAS_Info->f_if1_bw)
		       && (pAS_Info->bSpurPresent =
			   IsSpurInBand(pAS_Info, &fm, &fp)));

		/*
		 ** Use the LO-spur free values found.  If the search went all the way to
		 ** the 1st IF band edge and always found spurs, just leave the original
		 ** choice.  It's as "good" as any other.
		 */
		if (pAS_Info->bSpurPresent == 1) {
			status |= MT2063_SPUR_PRESENT_ERR;
			pAS_Info->f_LO1 = zfLO1;
			pAS_Info->f_LO2 = zfLO2;
		} else
			pAS_Info->bSpurAvoided = 1;
	}

	status |=
	    ((pAS_Info->
	      nSpursFound << MT2063_SPUR_SHIFT) & MT2063_SPUR_CNT_MASK);

	return (status);
}

//end of mt2063_spuravoid.c
//=================================================================
//#################################################################
//=================================================================

/*
**  The expected version of MT_AvoidSpursData_t
**  If the version is different, an updated file is needed from Microtune
*/
/* Expecting version 1.21 of the Spur Avoidance API */

typedef enum {
	MT2063_SET_ATTEN,
	MT2063_INCR_ATTEN,
	MT2063_DECR_ATTEN
} MT2063_ATTEN_CNTL_MODE;

//#define TUNER_MT2063_OPTIMIZATION
/*
** Constants used by the tuning algorithm
*/
#define MT2063_REF_FREQ          (16000000UL)	/* Reference oscillator Frequency (in Hz) */
#define MT2063_IF1_BW            (22000000UL)	/* The IF1 filter bandwidth (in Hz) */
#define MT2063_TUNE_STEP_SIZE       (50000UL)	/* Tune in steps of 50 kHz */
#define MT2063_SPUR_STEP_HZ        (250000UL)	/* Step size (in Hz) to move IF1 when avoiding spurs */
#define MT2063_ZIF_BW             (2000000UL)	/* Zero-IF spur-free bandwidth (in Hz) */
#define MT2063_MAX_HARMONICS_1         (15UL)	/* Highest intra-tuner LO Spur Harmonic to be avoided */
#define MT2063_MAX_HARMONICS_2          (5UL)	/* Highest inter-tuner LO Spur Harmonic to be avoided */
#define MT2063_MIN_LO_SEP         (1000000UL)	/* Minimum inter-tuner LO frequency separation */
#define MT2063_LO1_FRACN_AVOID          (0UL)	/* LO1 FracN numerator avoid region (in Hz) */
#define MT2063_LO2_FRACN_AVOID     (199999UL)	/* LO2 FracN numerator avoid region (in Hz) */
#define MT2063_MIN_FIN_FREQ      (44000000UL)	/* Minimum input frequency (in Hz) */
#define MT2063_MAX_FIN_FREQ    (1100000000UL)	/* Maximum input frequency (in Hz) */
#define MT2063_MIN_FOUT_FREQ     (36000000UL)	/* Minimum output frequency (in Hz) */
#define MT2063_MAX_FOUT_FREQ     (57000000UL)	/* Maximum output frequency (in Hz) */
#define MT2063_MIN_DNC_FREQ    (1293000000UL)	/* Minimum LO2 frequency (in Hz) */
#define MT2063_MAX_DNC_FREQ    (1614000000UL)	/* Maximum LO2 frequency (in Hz) */
#define MT2063_MIN_UPC_FREQ    (1396000000UL)	/* Minimum LO1 frequency (in Hz) */
#define MT2063_MAX_UPC_FREQ    (2750000000UL)	/* Maximum LO1 frequency (in Hz) */

/*
**  Define the supported Part/Rev codes for the MT2063
*/
#define MT2063_B0       (0x9B)
#define MT2063_B1       (0x9C)
#define MT2063_B2       (0x9D)
#define MT2063_B3       (0x9E)

/*
**  The number of Tuner Registers
*/
1817
static const u32 MT2063_Num_Registers = MT2063_REG_END_REGS;
1818 1819 1820

#define USE_GLOBAL_TUNER			0

1821
static u32 nMT2063MaxTuners = 1;
1822
static u32 nMT2063OpenTuners = 0;
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845

/*
**  Constants for setting receiver modes.
**  (6 modes defined at this time, enumerated by MT2063_RCVR_MODES)
**  (DNC1GC & DNC2GC are the values, which are used, when the specific
**   DNC Output is selected, the other is always off)
**
**   If PAL-L or L' is received, set:
**       MT2063_SetParam(hMT2063,MT2063_TAGC,1);
**
**                --------------+----------------------------------------------
**                 Mode 0 :     | MT2063_CABLE_QAM
**                 Mode 1 :     | MT2063_CABLE_ANALOG
**                 Mode 2 :     | MT2063_OFFAIR_COFDM
**                 Mode 3 :     | MT2063_OFFAIR_COFDM_SAWLESS
**                 Mode 4 :     | MT2063_OFFAIR_ANALOG
**                 Mode 5 :     | MT2063_OFFAIR_8VSB
**                --------------+----+----+----+----+-----+-----+--------------
**                 Mode         |  0 |  1 |  2 |  3 |  4  |  5  |
**                --------------+----+----+----+----+-----+-----+
**
**
*/
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
static const u8 RFAGCEN[] = { 0, 0, 0, 0, 0, 0 };
static const u8 LNARIN[] = { 0, 0, 3, 3, 3, 3 };
static const u8 FIFFQEN[] = { 1, 1, 1, 1, 1, 1 };
static const u8 FIFFQ[] = { 0, 0, 0, 0, 0, 0 };
static const u8 DNC1GC[] = { 0, 0, 0, 0, 0, 0 };
static const u8 DNC2GC[] = { 0, 0, 0, 0, 0, 0 };
static const u8 ACLNAMAX[] = { 31, 31, 31, 31, 31, 31 };
static const u8 LNATGT[] = { 44, 43, 43, 43, 43, 43 };
static const u8 RFOVDIS[] = { 0, 0, 0, 0, 0, 0 };
static const u8 ACRFMAX[] = { 31, 31, 31, 31, 31, 31 };
static const u8 PD1TGT[] = { 36, 36, 38, 38, 36, 38 };
static const u8 FIFOVDIS[] = { 0, 0, 0, 0, 0, 0 };
static const u8 ACFIFMAX[] = { 29, 29, 29, 29, 29, 29 };
static const u8 PD2TGT[] = { 40, 33, 38, 42, 30, 38 };
1860 1861 1862 1863 1864 1865

/*
**  Local Function Prototypes - not available for external access.
*/

/*  Forward declaration(s):  */
1866 1867 1868 1869 1870 1871
static u32 MT2063_CalcLO1Mult(u32 * Div, u32 * FracN, u32 f_LO,
				  u32 f_LO_Step, u32 f_Ref);
static u32 MT2063_CalcLO2Mult(u32 * Div, u32 * FracN, u32 f_LO,
				  u32 f_LO_Step, u32 f_Ref);
static u32 MT2063_fLO_FractionalTerm(u32 f_ref, u32 num,
					 u32 denom);
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901

/******************************************************************************
**
**  Name: MT2063_Open
**
**  Description:    Initialize the tuner's register values.
**
**  Parameters:     MT2063_Addr  - Serial bus address of the tuner.
**                  hMT2063      - Tuner handle passed back.
**                  hUserData    - User-defined data, if needed for the
**                                 MT_ReadSub() & MT_WriteSub functions.
**
**  Returns:        status:
**                      MT_OK             - No errors
**                      MT_TUNER_ID_ERR   - Tuner Part/Rev code mismatch
**                      MT_TUNER_INIT_ERR - Tuner initialization failed
**                      MT_COMM_ERR       - Serial bus communications error
**                      MT_ARG_NULL       - Null pointer argument passed
**                      MT_TUNER_CNT_ERR  - Too many tuners open
**
**  Dependencies:   MT_ReadSub  - Read byte(s) of data from the two-wire bus
**                  MT_WriteSub - Write byte(s) of data to the two-wire bus
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   138   06-19-2007    DAD    Ver 1.00: Initial, derived from mt2067_b.
**
******************************************************************************/
1902
static u32 MT2063_Open(struct dvb_frontend *fe)
1903
{
1904
	u32 status;	/*  Status to be returned.  */
1905 1906 1907 1908
	struct mt2063_state *state = fe->tuner_priv;

	/*  Default tuner handle to NULL.  If successful, it will be reassigned  */

1909 1910
	if (state->MT2063_init == false)
		state->rcvr_mode = MT2063_CABLE_QAM;
1911

1912
	status = MT2063_RegisterTuner(&state->AS_Data);
1913
	if (status >= 0) {
1914 1915
		state->rcvr_mode = MT2063_CABLE_QAM;
		status = MT2063_ReInit(state);
1916 1917
	}

1918
	if (status < 0)
1919
		/*  MT2063_Close handles the un-registration of the tuner  */
1920
		MT2063_Close(state);
1921
	else {
1922
		state->MT2063_init = true;
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
	}

	return (status);
}

/******************************************************************************
**
**  Name: MT2063_Close
**
**  Description:    Release the handle to the tuner.
**
**  Parameters:     hMT2063      - Handle to the MT2063 tuner
**
**  Returns:        status:
**                      MT_OK         - No errors
**                      MT_INV_HANDLE - Invalid tuner handle
**
**  Dependencies:   mt_errordef.h - definition of error codes
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   138   06-19-2007    DAD    Ver 1.00: Initial, derived from mt2067_b.
**
******************************************************************************/
1949
static u32 MT2063_Close(struct mt2063_state *state)
1950 1951
{
	/* Unregister tuner with SpurAvoidance routines (if needed) */
1952
	MT2063_UnRegisterTuner(&state->AS_Data);
1953
	/* Now remove the tuner from our own list of tuners */
1954

1955
	return 0;
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
}

/****************************************************************************
**
**  Name: MT2063_GetLocked
**
**  Description:    Checks to see if LO1 and LO2 are locked.
**
**  Parameters:     h            - Open handle to the tuner (from MT2063_Open).
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_UPC_UNLOCK    - Upconverter PLL unlocked
**                      MT_DNC_UNLOCK    - Downconverter PLL unlocked
**                      MT_COMM_ERR      - Serial bus communications error
**                      MT_INV_HANDLE    - Invalid tuner handle
**
**  Dependencies:   MT_ReadSub    - Read byte(s) of data from the serial bus
**                  MT_Sleep      - Delay execution for x milliseconds
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   138   06-19-2007    DAD    Ver 1.00: Initial, derived from mt2067_b.
**
****************************************************************************/
1983
static u32 MT2063_GetLocked(struct mt2063_state *state)
1984
{
1985 1986 1987 1988 1989
	const u32 nMaxWait = 100;	/*  wait a maximum of 100 msec   */
	const u32 nPollRate = 2;	/*  poll status bits every 2 ms */
	const u32 nMaxLoops = nMaxWait / nPollRate;
	const u8 LO1LK = 0x80;
	u8 LO2LK = 0x08;
1990
	u32 status = 0;	/* Status to be returned        */
1991
	u32 nDelays = 0;
1992 1993

	/*  LO2 Lock bit was in a different place for B0 version  */
1994
	if (state->tuner_id == MT2063_B0)
1995 1996 1997 1998
		LO2LK = 0x40;

	do {
		status |=
1999
		    MT2063_ReadSub(state,
2000
				   MT2063_REG_LO_STATUS,
2001
				   &state->reg[MT2063_REG_LO_STATUS], 1);
2002

2003
		if (status < 0)
2004 2005
			return (status);

2006
		if ((state->reg[MT2063_REG_LO_STATUS] & (LO1LK | LO2LK)) ==
2007 2008 2009
		    (LO1LK | LO2LK)) {
			return (status);
		}
2010
		msleep(nPollRate);	/*  Wait between retries  */
2011 2012 2013
	}
	while (++nDelays < nMaxLoops);

2014
	if ((state->reg[MT2063_REG_LO_STATUS] & LO1LK) == 0x00)
2015
		status |= MT2063_UPC_UNLOCK;
2016
	if ((state->reg[MT2063_REG_LO_STATUS] & LO2LK) == 0x00)
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
		status |= MT2063_DNC_UNLOCK;

	return (status);
}

/****************************************************************************
**
**  Name: MT2063_GetParam
**
**  Description:    Gets a tuning algorithm parameter.
**
**                  This function provides access to the internals of the
**                  tuning algorithm - mostly for testing purposes.
**
**  Parameters:     h           - Tuner handle (returned by MT2063_Open)
**                  param       - Tuning algorithm parameter
**                                (see enum MT2063_Param)
**                  pValue      - ptr to returned value
**
**                  param                     Description
**                  ----------------------    --------------------------------
**                  MT2063_IC_ADDR            Serial Bus address of this tuner
**                  MT2063_MAX_OPEN           Max # of MT2063's allowed open
**                  MT2063_NUM_OPEN           # of MT2063's open
**                  MT2063_SRO_FREQ           crystal frequency
**                  MT2063_STEPSIZE           minimum tuning step size
**                  MT2063_INPUT_FREQ         input center frequency
**                  MT2063_LO1_FREQ           LO1 Frequency
**                  MT2063_LO1_STEPSIZE       LO1 minimum step size
**                  MT2063_LO1_FRACN_AVOID    LO1 FracN keep-out region
**                  MT2063_IF1_ACTUAL         Current 1st IF in use
**                  MT2063_IF1_REQUEST        Requested 1st IF
**                  MT2063_IF1_CENTER         Center of 1st IF SAW filter
**                  MT2063_IF1_BW             Bandwidth of 1st IF SAW filter
**                  MT2063_ZIF_BW             zero-IF bandwidth
**                  MT2063_LO2_FREQ           LO2 Frequency
**                  MT2063_LO2_STEPSIZE       LO2 minimum step size
**                  MT2063_LO2_FRACN_AVOID    LO2 FracN keep-out region
**                  MT2063_OUTPUT_FREQ        output center frequency
**                  MT2063_OUTPUT_BW          output bandwidth
**                  MT2063_LO_SEPARATION      min inter-tuner LO separation
**                  MT2063_AS_ALG             ID of avoid-spurs algorithm in use
**                  MT2063_MAX_HARM1          max # of intra-tuner harmonics
**                  MT2063_MAX_HARM2          max # of inter-tuner harmonics
**                  MT2063_EXCL_ZONES         # of 1st IF exclusion zones
**                  MT2063_NUM_SPURS          # of spurs found/avoided
**                  MT2063_SPUR_AVOIDED       >0 spurs avoided
**                  MT2063_SPUR_PRESENT       >0 spurs in output (mathematically)
**                  MT2063_RCVR_MODE          Predefined modes.
**                  MT2063_ACLNA              LNA attenuator gain code
**                  MT2063_ACRF               RF attenuator gain code
**                  MT2063_ACFIF              FIF attenuator gain code
**                  MT2063_ACLNA_MAX          LNA attenuator limit
**                  MT2063_ACRF_MAX           RF attenuator limit
**                  MT2063_ACFIF_MAX          FIF attenuator limit
**                  MT2063_PD1                Actual value of PD1
**                  MT2063_PD2                Actual value of PD2
**                  MT2063_DNC_OUTPUT_ENABLE  DNC output selection
**                  MT2063_VGAGC              VGA gain code
**                  MT2063_VGAOI              VGA output current
**                  MT2063_TAGC               TAGC setting
**                  MT2063_AMPGC              AMP gain code
**                  MT2063_AVOID_DECT         Avoid DECT Frequencies
**                  MT2063_CTFILT_SW          Cleartune filter selection
**
**  Usage:          status |= MT2063_GetParam(hMT2063,
**                                            MT2063_IF1_ACTUAL,
**                                            &f_IF1_Actual);
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_INV_HANDLE    - Invalid tuner handle
**                      MT_ARG_NULL      - Null pointer argument passed
**                      MT_ARG_RANGE     - Invalid parameter requested
**
**  Dependencies:   USERS MUST CALL MT2063_Open() FIRST!
**
**  See Also:       MT2063_SetParam, MT2063_Open
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   138   06-19-2007    DAD    Ver 1.00: Initial, derived from mt2067_b.
**   154   09-13-2007    RSK    Ver 1.05: Get/SetParam changes for LOx_FREQ
**         10-31-2007    PINZ   Ver 1.08: Get/SetParam add VGAGC, VGAOI, AMPGC, TAGC
**   173 M 01-23-2008    RSK    Ver 1.12: Read LO1C and LO2C registers from HW
**                                        in GetParam.
**         04-18-2008    PINZ   Ver 1.15: Add SetParam LNARIN & PDxTGT
**                                        Split SetParam up to ACLNA / ACLNA_MAX
**                                        removed ACLNA_INRC/DECR (+RF & FIF)
**                                        removed GCUAUTO / BYPATNDN/UP
**   175 I 16-06-2008    PINZ   Ver 1.16: Add control to avoid US DECT freqs.
**   175 I 06-19-2008    RSK    Ver 1.17: Refactor DECT control to SpurAvoid.
**         06-24-2008    PINZ   Ver 1.18: Add Get/SetParam CTFILT_SW
**
****************************************************************************/
2114
static u32 MT2063_GetParam(struct mt2063_state *state, enum MT2063_Param param, u32 *pValue)
2115
{
2116
	u32 status = 0;	/* Status to be returned        */
2117 2118
	u32 Div;
	u32 Num;
2119 2120

	if (pValue == NULL)
2121
		return -EINVAL;
2122

2123 2124 2125
	switch (param) {
		/*  Serial Bus address of this tuner      */
	case MT2063_IC_ADDR:
2126
		*pValue = state->config->tuner_address;
2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140
		break;

		/*  Max # of MT2063's allowed to be open  */
	case MT2063_MAX_OPEN:
		*pValue = nMT2063MaxTuners;
		break;

		/*  # of MT2063's open                    */
	case MT2063_NUM_OPEN:
		*pValue = nMT2063OpenTuners;
		break;

		/*  crystal frequency                     */
	case MT2063_SRO_FREQ:
2141
		*pValue = state->AS_Data.f_ref;
2142 2143 2144 2145
		break;

		/*  minimum tuning step size              */
	case MT2063_STEPSIZE:
2146
		*pValue = state->AS_Data.f_LO2_Step;
2147 2148 2149 2150
		break;

		/*  input center frequency                */
	case MT2063_INPUT_FREQ:
2151
		*pValue = state->AS_Data.f_in;
2152 2153 2154 2155 2156 2157 2158
		break;

		/*  LO1 Frequency                         */
	case MT2063_LO1_FREQ:
		{
			/* read the actual tuner register values for LO1C_1 and LO1C_2 */
			status |=
2159
			    MT2063_ReadSub(state,
2160
					   MT2063_REG_LO1C_1,
2161
					   &state->
2162
					   reg[MT2063_REG_LO1C_1], 2);
2163 2164 2165 2166 2167
			Div = state->reg[MT2063_REG_LO1C_1];
			Num = state->reg[MT2063_REG_LO1C_2] & 0x3F;
			state->AS_Data.f_LO1 =
			    (state->AS_Data.f_ref * Div) +
			    MT2063_fLO_FractionalTerm(state->AS_Data.
2168 2169
						      f_ref, Num, 64);
		}
2170
		*pValue = state->AS_Data.f_LO1;
2171 2172 2173 2174
		break;

		/*  LO1 minimum step size                 */
	case MT2063_LO1_STEPSIZE:
2175
		*pValue = state->AS_Data.f_LO1_Step;
2176 2177 2178 2179
		break;

		/*  LO1 FracN keep-out region             */
	case MT2063_LO1_FRACN_AVOID_PARAM:
2180
		*pValue = state->AS_Data.f_LO1_FracN_Avoid;
2181 2182 2183 2184
		break;

		/*  Current 1st IF in use                 */
	case MT2063_IF1_ACTUAL:
2185
		*pValue = state->f_IF1_actual;
2186 2187 2188 2189
		break;

		/*  Requested 1st IF                      */
	case MT2063_IF1_REQUEST:
2190
		*pValue = state->AS_Data.f_if1_Request;
2191 2192 2193 2194
		break;

		/*  Center of 1st IF SAW filter           */
	case MT2063_IF1_CENTER:
2195
		*pValue = state->AS_Data.f_if1_Center;
2196 2197 2198 2199
		break;

		/*  Bandwidth of 1st IF SAW filter        */
	case MT2063_IF1_BW:
2200
		*pValue = state->AS_Data.f_if1_bw;
2201 2202 2203 2204
		break;

		/*  zero-IF bandwidth                     */
	case MT2063_ZIF_BW:
2205
		*pValue = state->AS_Data.f_zif_bw;
2206 2207 2208 2209 2210 2211 2212
		break;

		/*  LO2 Frequency                         */
	case MT2063_LO2_FREQ:
		{
			/* Read the actual tuner register values for LO2C_1, LO2C_2 and LO2C_3 */
			status |=
2213
			    MT2063_ReadSub(state,
2214
					   MT2063_REG_LO2C_1,
2215
					   &state->
2216 2217
					   reg[MT2063_REG_LO2C_1], 3);
			Div =
2218
			    (state->reg[MT2063_REG_LO2C_1] & 0xFE) >> 1;
2219
			Num =
2220
			    ((state->
2221
			      reg[MT2063_REG_LO2C_1] & 0x01) << 12) |
2222 2223
			    (state->
			     reg[MT2063_REG_LO2C_2] << 4) | (state->
2224 2225 2226
							     reg
							     [MT2063_REG_LO2C_3]
							     & 0x00F);
2227 2228 2229
			state->AS_Data.f_LO2 =
			    (state->AS_Data.f_ref * Div) +
			    MT2063_fLO_FractionalTerm(state->AS_Data.
2230 2231
						      f_ref, Num, 8191);
		}
2232
		*pValue = state->AS_Data.f_LO2;
2233 2234 2235 2236
		break;

		/*  LO2 minimum step size                 */
	case MT2063_LO2_STEPSIZE:
2237
		*pValue = state->AS_Data.f_LO2_Step;
2238 2239 2240 2241
		break;

		/*  LO2 FracN keep-out region             */
	case MT2063_LO2_FRACN_AVOID:
2242
		*pValue = state->AS_Data.f_LO2_FracN_Avoid;
2243 2244 2245 2246
		break;

		/*  output center frequency               */
	case MT2063_OUTPUT_FREQ:
2247
		*pValue = state->AS_Data.f_out;
2248 2249 2250 2251
		break;

		/*  output bandwidth                      */
	case MT2063_OUTPUT_BW:
2252
		*pValue = state->AS_Data.f_out_bw - 750000;
2253 2254 2255 2256
		break;

		/*  min inter-tuner LO separation         */
	case MT2063_LO_SEPARATION:
2257
		*pValue = state->AS_Data.f_min_LO_Separation;
2258 2259 2260 2261
		break;

		/*  ID of avoid-spurs algorithm in use    */
	case MT2063_AS_ALG:
2262
		*pValue = state->AS_Data.nAS_Algorithm;
2263 2264 2265 2266
		break;

		/*  max # of intra-tuner harmonics        */
	case MT2063_MAX_HARM1:
2267
		*pValue = state->AS_Data.maxH1;
2268 2269 2270 2271
		break;

		/*  max # of inter-tuner harmonics        */
	case MT2063_MAX_HARM2:
2272
		*pValue = state->AS_Data.maxH2;
2273 2274 2275 2276
		break;

		/*  # of 1st IF exclusion zones           */
	case MT2063_EXCL_ZONES:
2277
		*pValue = state->AS_Data.nZones;
2278 2279 2280 2281
		break;

		/*  # of spurs found/avoided              */
	case MT2063_NUM_SPURS:
2282
		*pValue = state->AS_Data.nSpursFound;
2283 2284 2285 2286
		break;

		/*  >0 spurs avoided                      */
	case MT2063_SPUR_AVOIDED:
2287
		*pValue = state->AS_Data.bSpurAvoided;
2288 2289 2290 2291
		break;

		/*  >0 spurs in output (mathematically)   */
	case MT2063_SPUR_PRESENT:
2292
		*pValue = state->AS_Data.bSpurPresent;
2293 2294 2295 2296
		break;

		/*  Predefined receiver setup combination */
	case MT2063_RCVR_MODE:
2297
		*pValue = state->rcvr_mode;
2298 2299 2300 2301 2302
		break;

	case MT2063_PD1:
	case MT2063_PD2: {
		u8 mask = (param == MT2063_PD1 ? 0x01 : 0x03);	/* PD1 vs PD2 */
2303
		u8 orig = (state->reg[MT2063_REG_BYP_CTRL]);
2304 2305 2306 2307 2308 2309 2310 2311
		u8 reg = (orig & 0xF1) | mask;	/* Only set 3 bits (not 5) */
		int i;

		*pValue = 0;

		/* Initiate ADC output to reg 0x0A */
		if (reg != orig)
			status |=
2312
			    MT2063_WriteSub(state,
2313 2314 2315 2316 2317 2318 2319 2320
					    MT2063_REG_BYP_CTRL,
					    &reg, 1);

		if (status < 0)
			return (status);

		for (i = 0; i < 8; i++) {
			status |=
2321
			    MT2063_ReadSub(state,
2322
					   MT2063_REG_ADC_OUT,
2323
					   &state->
2324 2325 2326 2327 2328 2329
					   reg
					   [MT2063_REG_ADC_OUT],
					   1);

			if (status >= 0)
				*pValue +=
2330
				    state->
2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343
				    reg[MT2063_REG_ADC_OUT];
			else {
				if (i)
					*pValue /= i;
				return (status);
			}
		}
		*pValue /= 8;	/*  divide by number of reads  */
		*pValue >>= 2;	/*  only want 6 MSB's out of 8  */

		/* Restore value of Register BYP_CTRL */
		if (reg != orig)
			status |=
2344
			    MT2063_WriteSub(state,
2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
					    MT2063_REG_BYP_CTRL,
						&orig, 1);
		}
		break;

		/*  Get LNA attenuator code                */
	case MT2063_ACLNA:
	{
		u8 val;
		status |=
2355
		    MT2063_GetReg(state, MT2063_REG_XO_STATUS,
2356 2357 2358 2359 2360 2361 2362 2363 2364 2365
				  &val);
		*pValue = val & 0x1f;
	}
	break;

	/*  Get RF attenuator code                */
	case MT2063_ACRF:
	{
		u8 val;
		status |=
2366
		    MT2063_GetReg(state, MT2063_REG_RF_STATUS,
2367 2368 2369 2370
				  &val);
		*pValue = val & 0x1f;
	}
	break;
2371

2372 2373 2374 2375 2376
	/*  Get FIF attenuator code               */
	case MT2063_ACFIF:
	{
		u8 val;
		status |=
2377
		    MT2063_GetReg(state, MT2063_REG_FIF_STATUS,
2378 2379 2380 2381 2382 2383 2384 2385 2386 2387
				  &val);
		*pValue = val & 0x1f;
	}
	break;

	/*  Get LNA attenuator limit              */
	case MT2063_ACLNA_MAX:
	{
		u8 val;
		status |=
2388
		    MT2063_GetReg(state, MT2063_REG_LNA_OV,
2389 2390 2391 2392 2393 2394 2395 2396 2397 2398
				  &val);
		*pValue = val & 0x1f;
	}
	break;

	/*  Get RF attenuator limit               */
	case MT2063_ACRF_MAX:
	{
		u8 val;
		status |=
2399
		    MT2063_GetReg(state, MT2063_REG_RF_OV,
2400 2401 2402 2403 2404 2405 2406 2407 2408 2409
				  &val);
		*pValue = val & 0x1f;
	}
	break;

	/*  Get FIF attenuator limit               */
	case MT2063_ACFIF_MAX:
	{
		u8 val;
		status |=
2410
		    MT2063_GetReg(state, MT2063_REG_FIF_OV,
2411 2412 2413 2414 2415 2416 2417 2418
				  &val);
		*pValue = val & 0x1f;
	}
	break;

	/*  Get current used DNC output */
	case MT2063_DNC_OUTPUT_ENABLE:
	{
2419 2420
		if ((state->reg[MT2063_REG_DNC_GAIN] & 0x03) == 0x03) {	/* if DNC1 is off */
			if ((state->reg[MT2063_REG_VGA_GAIN] & 0x03) == 0x03)	/* if DNC2 is off */
2421 2422 2423 2424 2425 2426 2427
				*pValue =
				    (u32) MT2063_DNC_NONE;
			else
				*pValue =
				    (u32) MT2063_DNC_2;
		} else {	/* DNC1 is on */

2428
			if ((state->reg[MT2063_REG_VGA_GAIN] & 0x03) == 0x03)	/* if DNC2 is off */
2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439
				*pValue =
				    (u32) MT2063_DNC_1;
			else
				*pValue =
				    (u32) MT2063_DNC_BOTH;
		}
	}
	break;

	/*  Get VGA Gain Code */
		case MT2063_VGAGC:
2440
		*pValue = ((state->reg[MT2063_REG_VGA_GAIN] & 0x0C) >> 2);
2441 2442 2443 2444
		break;

		/*  Get VGA bias current */
	case MT2063_VGAOI:
2445
		*pValue = (state->reg[MT2063_REG_RSVD_31] & 0x07);
2446 2447 2448 2449
		break;

		/*  Get TAGC setting */
	case MT2063_TAGC:
2450
		*pValue = (state->reg[MT2063_REG_RSVD_1E] & 0x03);
2451 2452 2453 2454
		break;

		/*  Get AMP Gain Code */
	case MT2063_AMPGC:
2455
		*pValue = (state->reg[MT2063_REG_TEMP_SEL] & 0x03);
2456 2457 2458 2459
		break;

		/*  Avoid DECT Frequencies  */
	case MT2063_AVOID_DECT:
2460
		*pValue = state->AS_Data.avoidDECT;
2461 2462 2463 2464
		break;

		/*  Cleartune filter selection: 0 - by IC (default), 1 - by software  */
	case MT2063_CTFILT_SW:
2465
		*pValue = state->ctfilt_sw;
2466 2467 2468 2469 2470 2471
		break;

	case MT2063_EOP:
	default:
		status |= -ERANGE;
	}
2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503
	return (status);
}

/****************************************************************************
**
**  Name: MT2063_GetReg
**
**  Description:    Gets an MT2063 register.
**
**  Parameters:     h           - Tuner handle (returned by MT2063_Open)
**                  reg         - MT2063 register/subaddress location
**                  *val        - MT2063 register/subaddress value
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_COMM_ERR      - Serial bus communications error
**                      MT_INV_HANDLE    - Invalid tuner handle
**                      MT_ARG_NULL      - Null pointer argument passed
**                      MT_ARG_RANGE     - Argument out of range
**
**  Dependencies:   USERS MUST CALL MT2063_Open() FIRST!
**
**                  Use this function if you need to read a register from
**                  the MT2063.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   138   06-19-2007    DAD    Ver 1.00: Initial, derived from mt2067_b.
**
****************************************************************************/
2504
static u32 MT2063_GetReg(struct mt2063_state *state, u8 reg, u8 * val)
2505
{
2506
	u32 status = 0;	/* Status to be returned        */
2507 2508

	if (val == NULL)
2509
		return -EINVAL;
2510 2511

	if (reg >= MT2063_REG_END_REGS)
2512
		return -ERANGE;
2513

2514
	status = MT2063_ReadSub(state, reg, &state->reg[reg], 1);
2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555

	return (status);
}

/******************************************************************************
**
**  Name: MT2063_SetReceiverMode
**
**  Description:    Set the MT2063 receiver mode
**
**   --------------+----------------------------------------------
**    Mode 0 :     | MT2063_CABLE_QAM
**    Mode 1 :     | MT2063_CABLE_ANALOG
**    Mode 2 :     | MT2063_OFFAIR_COFDM
**    Mode 3 :     | MT2063_OFFAIR_COFDM_SAWLESS
**    Mode 4 :     | MT2063_OFFAIR_ANALOG
**    Mode 5 :     | MT2063_OFFAIR_8VSB
**   --------------+----+----+----+----+-----+--------------------
**  (DNC1GC & DNC2GC are the values, which are used, when the specific
**   DNC Output is selected, the other is always off)
**
**                |<----------   Mode  -------------->|
**    Reg Field   |  0  |  1  |  2  |  3  |  4  |  5  |
**    ------------+-----+-----+-----+-----+-----+-----+
**    RFAGCen     | OFF | OFF | OFF | OFF | OFF | OFF
**    LNARin      |   0 |   0 |   3 |   3 |  3  |  3
**    FIFFQen     |   1 |   1 |   1 |   1 |  1  |  1
**    FIFFq       |   0 |   0 |   0 |   0 |  0  |  0
**    DNC1gc      |   0 |   0 |   0 |   0 |  0  |  0
**    DNC2gc      |   0 |   0 |   0 |   0 |  0  |  0
**    GCU Auto    |   1 |   1 |   1 |   1 |  1  |  1
**    LNA max Atn |  31 |  31 |  31 |  31 | 31  | 31
**    LNA Target  |  44 |  43 |  43 |  43 | 43  | 43
**    ign  RF Ovl |   0 |   0 |   0 |   0 |  0  |  0
**    RF  max Atn |  31 |  31 |  31 |  31 | 31  | 31
**    PD1 Target  |  36 |  36 |  38 |  38 | 36  | 38
**    ign FIF Ovl |   0 |   0 |   0 |   0 |  0  |  0
**    FIF max Atn |   5 |   5 |   5 |   5 |  5  |  5
**    PD2 Target  |  40 |  33 |  42 |  42 | 33  | 42
**
**
2556
**  Parameters:     state       - ptr to mt2063_state structure
2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601
**                  Mode        - desired reciever mode
**
**  Usage:          status = MT2063_SetReceiverMode(hMT2063, Mode);
**
**  Returns:        status:
**                      MT_OK             - No errors
**                      MT_COMM_ERR       - Serial bus communications error
**
**  Dependencies:   MT2063_SetReg - Write a byte of data to a HW register.
**                  Assumes that the tuner cache is valid.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   138   06-19-2007    DAD    Ver 1.00: Initial, derived from mt2067_b.
**   N/A   01-10-2007    PINZ   Added additional GCU Settings, FIFF Calib will be triggered
**   155   10-01-2007    DAD    Ver 1.06: Add receiver mode for SECAM positive
**                                        modulation
**                                        (MT2063_ANALOG_TV_POS_NO_RFAGC_MODE)
**   N/A   10-22-2007    PINZ   Ver 1.07: Changed some Registers at init to have
**                                        the same settings as with MT Launcher
**   N/A   10-30-2007    PINZ             Add SetParam VGAGC & VGAOI
**                                        Add SetParam DNC_OUTPUT_ENABLE
**                                        Removed VGAGC from receiver mode,
**                                        default now 1
**   N/A   10-31-2007    PINZ   Ver 1.08: Add SetParam TAGC, removed from rcvr-mode
**                                        Add SetParam AMPGC, removed from rcvr-mode
**                                        Corrected names of GCU values
**                                        reorganized receiver modes, removed,
**                                        (MT2063_ANALOG_TV_POS_NO_RFAGC_MODE)
**                                        Actualized Receiver-Mode values
**   N/A   11-12-2007    PINZ   Ver 1.09: Actualized Receiver-Mode values
**   N/A   11-27-2007    PINZ             Improved buffered writing
**         01-03-2008    PINZ   Ver 1.10: Added a trigger of BYPATNUP for
**                                        correct wakeup of the LNA after shutdown
**                                        Set AFCsd = 1 as default
**                                        Changed CAP1sel default
**         01-14-2008    PINZ   Ver 1.11: Updated gain settings
**         04-18-2008    PINZ   Ver 1.15: Add SetParam LNARIN & PDxTGT
**                                        Split SetParam up to ACLNA / ACLNA_MAX
**                                        removed ACLNA_INRC/DECR (+RF & FIF)
**                                        removed GCUAUTO / BYPATNDN/UP
**
******************************************************************************/
2602
static u32 MT2063_SetReceiverMode(struct mt2063_state *state,
2603 2604
				      enum MT2063_RCVR_MODES Mode)
{
2605
	u32 status = 0;	/* Status to be returned        */
2606 2607
	u8 val;
	u32 longval;
2608 2609

	if (Mode >= MT2063_NUM_RCVR_MODES)
2610
		status = -ERANGE;
2611 2612

	/* RFAGCen */
2613
	if (status >= 0) {
2614
		val =
2615
		    (state->
2616
		     reg[MT2063_REG_PD1_TGT] & (u8) ~ 0x40) | (RFAGCEN[Mode]
2617 2618
								   ? 0x40 :
								   0x00);
2619 2620
		if (state->reg[MT2063_REG_PD1_TGT] != val) {
			status |= MT2063_SetReg(state, MT2063_REG_PD1_TGT, val);
2621 2622 2623 2624
		}
	}

	/* LNARin */
2625
	if (status >= 0) {
2626
		status |= MT2063_SetParam(state, MT2063_LNA_RIN, LNARIN[Mode]);
2627 2628 2629
	}

	/* FIFFQEN and FIFFQ */
2630
	if (status >= 0) {
2631
		val =
2632
		    (state->
2633
		     reg[MT2063_REG_FIFF_CTRL2] & (u8) ~ 0xF0) |
2634
		    (FIFFQEN[Mode] << 7) | (FIFFQ[Mode] << 4);
2635
		if (state->reg[MT2063_REG_FIFF_CTRL2] != val) {
2636
			status |=
2637
			    MT2063_SetReg(state, MT2063_REG_FIFF_CTRL2, val);
2638 2639
			/* trigger FIFF calibration, needed after changing FIFFQ */
			val =
2640
			    (state->reg[MT2063_REG_FIFF_CTRL] | (u8) 0x01);
2641
			status |=
2642
			    MT2063_SetReg(state, MT2063_REG_FIFF_CTRL, val);
2643
			val =
2644
			    (state->
2645
			     reg[MT2063_REG_FIFF_CTRL] & (u8) ~ 0x01);
2646
			status |=
2647
			    MT2063_SetReg(state, MT2063_REG_FIFF_CTRL, val);
2648 2649 2650 2651
		}
	}

	/* DNC1GC & DNC2GC */
2652 2653
	status |= MT2063_GetParam(state, MT2063_DNC_OUTPUT_ENABLE, &longval);
	status |= MT2063_SetParam(state, MT2063_DNC_OUTPUT_ENABLE, longval);
2654 2655

	/* acLNAmax */
2656
	if (status >= 0) {
2657
		status |=
2658
		    MT2063_SetParam(state, MT2063_ACLNA_MAX, ACLNAMAX[Mode]);
2659 2660 2661
	}

	/* LNATGT */
2662
	if (status >= 0) {
2663
		status |= MT2063_SetParam(state, MT2063_LNA_TGT, LNATGT[Mode]);
2664 2665 2666
	}

	/* ACRF */
2667
	if (status >= 0) {
2668
		status |=
2669
		    MT2063_SetParam(state, MT2063_ACRF_MAX, ACRFMAX[Mode]);
2670 2671 2672
	}

	/* PD1TGT */
2673
	if (status >= 0) {
2674
		status |= MT2063_SetParam(state, MT2063_PD1_TGT, PD1TGT[Mode]);
2675 2676 2677
	}

	/* FIFATN */
2678
	if (status >= 0) {
2679
		status |=
2680
		    MT2063_SetParam(state, MT2063_ACFIF_MAX, ACFIFMAX[Mode]);
2681 2682 2683
	}

	/* PD2TGT */
2684
	if (status >= 0) {
2685
		status |= MT2063_SetParam(state, MT2063_PD2_TGT, PD2TGT[Mode]);
2686 2687 2688
	}

	/* Ignore ATN Overload */
2689
	if (status >= 0) {
2690
		val =
2691
		    (state->
2692
		     reg[MT2063_REG_LNA_TGT] & (u8) ~ 0x80) | (RFOVDIS[Mode]
2693 2694
								   ? 0x80 :
								   0x00);
2695 2696
		if (state->reg[MT2063_REG_LNA_TGT] != val) {
			status |= MT2063_SetReg(state, MT2063_REG_LNA_TGT, val);
2697 2698 2699 2700
		}
	}

	/* Ignore FIF Overload */
2701
	if (status >= 0) {
2702
		val =
2703
		    (state->
2704
		     reg[MT2063_REG_PD1_TGT] & (u8) ~ 0x80) |
2705
		    (FIFOVDIS[Mode] ? 0x80 : 0x00);
2706 2707
		if (state->reg[MT2063_REG_PD1_TGT] != val) {
			status |= MT2063_SetReg(state, MT2063_REG_PD1_TGT, val);
2708 2709 2710
		}
	}

2711
	if (status >= 0)
2712
		state->rcvr_mode = Mode;
2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750

	return (status);
}

/******************************************************************************
**
**  Name: MT2063_ReInit
**
**  Description:    Initialize the tuner's register values.
**
**  Parameters:     h           - Tuner handle (returned by MT2063_Open)
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_TUNER_ID_ERR   - Tuner Part/Rev code mismatch
**                      MT_INV_HANDLE    - Invalid tuner handle
**                      MT_COMM_ERR      - Serial bus communications error
**
**  Dependencies:   MT_ReadSub  - Read byte(s) of data from the two-wire bus
**                  MT_WriteSub - Write byte(s) of data to the two-wire bus
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   138   06-19-2007    DAD    Ver 1.00: Initial, derived from mt2067_b.
**   148   09-04-2007    RSK    Ver 1.02: Corrected logic of Reg 3B Reference
**   153   09-07-2007    RSK    Ver 1.03: Lock Time improvements
**   N/A   10-31-2007    PINZ   Ver 1.08: Changed values suitable to rcvr-mode 0
**   N/A   11-12-2007    PINZ   Ver 1.09: Changed values suitable to rcvr-mode 0
**   N/A   01-03-2007    PINZ   Ver 1.10: Added AFCsd = 1 into defaults
**   N/A   01-04-2007    PINZ   Ver 1.10: Changed CAP1sel default
**         01-14-2008    PINZ   Ver 1.11: Updated gain settings
**         03-18-2008    PINZ   Ver 1.13: Added Support for B3
**   175 I 06-19-2008    RSK    Ver 1.17: Refactor DECT control to SpurAvoid.
**         06-24-2008    PINZ   Ver 1.18: Add Get/SetParam CTFILT_SW
**
******************************************************************************/
2751
static u32 MT2063_ReInit(struct mt2063_state *state)
2752
{
2753
	u8 all_resets = 0xF0;	/* reset/load bits */
2754
	u32 status = 0;	/* Status to be returned */
2755
	u8 *def = NULL;
2756 2757 2758 2759
	u32 FCRUN;
	s32 maxReads;
	u32 fcu_osc;
	u32 i;
2760
	u8 MT2063B0_defaults[] = {	/* Reg,  Value */
2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782
		0x19, 0x05,
		0x1B, 0x1D,
		0x1C, 0x1F,
		0x1D, 0x0F,
		0x1E, 0x3F,
		0x1F, 0x0F,
		0x20, 0x3F,
		0x22, 0x21,
		0x23, 0x3F,
		0x24, 0x20,
		0x25, 0x3F,
		0x27, 0xEE,
		0x2C, 0x27,	/*  bit at 0x20 is cleared below  */
		0x30, 0x03,
		0x2C, 0x07,	/*  bit at 0x20 is cleared here   */
		0x2D, 0x87,
		0x2E, 0xAA,
		0x28, 0xE1,	/*  Set the FIFCrst bit here      */
		0x28, 0xE0,	/*  Clear the FIFCrst bit here    */
		0x00
	};
	/* writing 0x05 0xf0 sw-resets all registers, so we write only needed changes */
2783
	u8 MT2063B1_defaults[] = {	/* Reg,  Value */
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807
		0x05, 0xF0,
		0x11, 0x10,	/* New Enable AFCsd */
		0x19, 0x05,
		0x1A, 0x6C,
		0x1B, 0x24,
		0x1C, 0x28,
		0x1D, 0x8F,
		0x1E, 0x14,
		0x1F, 0x8F,
		0x20, 0x57,
		0x22, 0x21,	/* New - ver 1.03 */
		0x23, 0x3C,	/* New - ver 1.10 */
		0x24, 0x20,	/* New - ver 1.03 */
		0x2C, 0x24,	/*  bit at 0x20 is cleared below  */
		0x2D, 0x87,	/*  FIFFQ=0  */
		0x2F, 0xF3,
		0x30, 0x0C,	/* New - ver 1.11 */
		0x31, 0x1B,	/* New - ver 1.11 */
		0x2C, 0x04,	/*  bit at 0x20 is cleared here  */
		0x28, 0xE1,	/*  Set the FIFCrst bit here      */
		0x28, 0xE0,	/*  Clear the FIFCrst bit here    */
		0x00
	};
	/* writing 0x05 0xf0 sw-resets all registers, so we write only needed changes */
2808
	u8 MT2063B3_defaults[] = {	/* Reg,  Value */
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818
		0x05, 0xF0,
		0x19, 0x3D,
		0x2C, 0x24,	/*  bit at 0x20 is cleared below  */
		0x2C, 0x04,	/*  bit at 0x20 is cleared here  */
		0x28, 0xE1,	/*  Set the FIFCrst bit here      */
		0x28, 0xE0,	/*  Clear the FIFCrst bit here    */
		0x00
	};

	/*  Read the Part/Rev code from the tuner */
2819
	status = MT2063_ReadSub(state, MT2063_REG_PART_REV, state->reg, 1);
2820 2821 2822 2823
	if (status < 0)
		return status;

	/* Check the part/rev code */
2824 2825 2826
	if (((state->reg[MT2063_REG_PART_REV] != MT2063_B0)	/*  MT2063 B0  */
	    &&(state->reg[MT2063_REG_PART_REV] != MT2063_B1)	/*  MT2063 B1  */
	    &&(state->reg[MT2063_REG_PART_REV] != MT2063_B3)))	/*  MT2063 B3  */
2827 2828 2829
		return -ENODEV;	/*  Wrong tuner Part/Rev code */

	/*  Check the 2nd byte of the Part/Rev code from the tuner */
2830
	status = MT2063_ReadSub(state,
2831
			        MT2063_REG_RSVD_3B,
2832
			        &state->reg[MT2063_REG_RSVD_3B], 1);
2833 2834

	if (status >= 0
2835
	    &&((state->reg[MT2063_REG_RSVD_3B] & 0x80) != 0x00))	/* b7 != 0 ==> NOT MT2063 */
2836
		return -ENODEV;	/*  Wrong tuner Part/Rev code */
2837 2838

	/*  Reset the tuner  */
2839
	status = MT2063_WriteSub(state, MT2063_REG_LO2CQ_3, &all_resets, 1);
2840 2841
	if (status < 0)
		return status;
2842 2843

	/* change all of the default values that vary from the HW reset values */
2844 2845
	/*  def = (state->reg[PART_REV] == MT2063_B0) ? MT2063B0_defaults : MT2063B1_defaults; */
	switch (state->reg[MT2063_REG_PART_REV]) {
2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858
	case MT2063_B3:
		def = MT2063B3_defaults;
		break;

	case MT2063_B1:
		def = MT2063B1_defaults;
		break;

	case MT2063_B0:
		def = MT2063B0_defaults;
		break;

	default:
2859
		return -ENODEV;
2860 2861 2862
		break;
	}

2863
	while (status >= 0 && *def) {
2864 2865
		u8 reg = *def++;
		u8 val = *def++;
2866
		status = MT2063_WriteSub(state, reg, &val, 1);
2867
	}
2868 2869
	if (status < 0)
		return status;
2870 2871

	/*  Wait for FIFF location to complete.  */
2872 2873 2874 2875
	FCRUN = 1;
	maxReads = 10;
	while (status >= 0 && (FCRUN != 0) && (maxReads-- > 0)) {
		msleep(2);
2876
		status = MT2063_ReadSub(state,
2877
					 MT2063_REG_XO_STATUS,
2878
					 &state->
2879
					 reg[MT2063_REG_XO_STATUS], 1);
2880
		FCRUN = (state->reg[MT2063_REG_XO_STATUS] & 0x40) >> 6;
2881 2882
	}

2883 2884
	if (FCRUN != 0)
		return -ENODEV;
2885

2886
	status = MT2063_ReadSub(state,
2887
			   MT2063_REG_FIFFC,
2888
			   &state->reg[MT2063_REG_FIFFC], 1);
2889 2890
	if (status < 0)
		return status;
2891

2892
	/* Read back all the registers from the tuner */
2893
	status = MT2063_ReadSub(state,
2894
				MT2063_REG_PART_REV,
2895
				state->reg, MT2063_REG_END_REGS);
2896 2897 2898 2899
	if (status < 0)
		return status;

	/*  Initialize the tuner state.  */
2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954
	state->tuner_id = state->reg[MT2063_REG_PART_REV];
	state->AS_Data.f_ref = MT2063_REF_FREQ;
	state->AS_Data.f_if1_Center = (state->AS_Data.f_ref / 8) *
				      ((u32) state->reg[MT2063_REG_FIFFC] + 640);
	state->AS_Data.f_if1_bw = MT2063_IF1_BW;
	state->AS_Data.f_out = 43750000UL;
	state->AS_Data.f_out_bw = 6750000UL;
	state->AS_Data.f_zif_bw = MT2063_ZIF_BW;
	state->AS_Data.f_LO1_Step = state->AS_Data.f_ref / 64;
	state->AS_Data.f_LO2_Step = MT2063_TUNE_STEP_SIZE;
	state->AS_Data.maxH1 = MT2063_MAX_HARMONICS_1;
	state->AS_Data.maxH2 = MT2063_MAX_HARMONICS_2;
	state->AS_Data.f_min_LO_Separation = MT2063_MIN_LO_SEP;
	state->AS_Data.f_if1_Request = state->AS_Data.f_if1_Center;
	state->AS_Data.f_LO1 = 2181000000UL;
	state->AS_Data.f_LO2 = 1486249786UL;
	state->f_IF1_actual = state->AS_Data.f_if1_Center;
	state->AS_Data.f_in = state->AS_Data.f_LO1 - state->f_IF1_actual;
	state->AS_Data.f_LO1_FracN_Avoid = MT2063_LO1_FRACN_AVOID;
	state->AS_Data.f_LO2_FracN_Avoid = MT2063_LO2_FRACN_AVOID;
	state->num_regs = MT2063_REG_END_REGS;
	state->AS_Data.avoidDECT = MT2063_AVOID_BOTH;
	state->ctfilt_sw = 0;

	state->CTFiltMax[0] = 69230000;
	state->CTFiltMax[1] = 105770000;
	state->CTFiltMax[2] = 140350000;
	state->CTFiltMax[3] = 177110000;
	state->CTFiltMax[4] = 212860000;
	state->CTFiltMax[5] = 241130000;
	state->CTFiltMax[6] = 274370000;
	state->CTFiltMax[7] = 309820000;
	state->CTFiltMax[8] = 342450000;
	state->CTFiltMax[9] = 378870000;
	state->CTFiltMax[10] = 416210000;
	state->CTFiltMax[11] = 456500000;
	state->CTFiltMax[12] = 495790000;
	state->CTFiltMax[13] = 534530000;
	state->CTFiltMax[14] = 572610000;
	state->CTFiltMax[15] = 598970000;
	state->CTFiltMax[16] = 635910000;
	state->CTFiltMax[17] = 672130000;
	state->CTFiltMax[18] = 714840000;
	state->CTFiltMax[19] = 739660000;
	state->CTFiltMax[20] = 770410000;
	state->CTFiltMax[21] = 814660000;
	state->CTFiltMax[22] = 846950000;
	state->CTFiltMax[23] = 867820000;
	state->CTFiltMax[24] = 915980000;
	state->CTFiltMax[25] = 947450000;
	state->CTFiltMax[26] = 983110000;
	state->CTFiltMax[27] = 1021630000;
	state->CTFiltMax[28] = 1061870000;
	state->CTFiltMax[29] = 1098330000;
	state->CTFiltMax[30] = 1138990000;
2955 2956 2957 2958 2959 2960

	/*
	 **   Fetch the FCU osc value and use it and the fRef value to
	 **   scale all of the Band Max values
	 */

2961 2962
	state->reg[MT2063_REG_CTUNE_CTRL] = 0x0A;
	status = MT2063_WriteSub(state,
2963
				 MT2063_REG_CTUNE_CTRL,
2964
				 &state->reg[MT2063_REG_CTUNE_CTRL], 1);
2965 2966 2967
	if (status < 0)
		return status;
	/*  Read the ClearTune filter calibration value  */
2968
	status = MT2063_ReadSub(state,
2969
			        MT2063_REG_FIFFC,
2970
			        &state->reg[MT2063_REG_FIFFC], 1);
2971 2972 2973
	if (status < 0)
		return status;

2974
	fcu_osc = state->reg[MT2063_REG_FIFFC];
2975

2976 2977
	state->reg[MT2063_REG_CTUNE_CTRL] = 0x00;
	status = MT2063_WriteSub(state,
2978
				 MT2063_REG_CTUNE_CTRL,
2979
				 &state->reg[MT2063_REG_CTUNE_CTRL], 1);
2980 2981 2982 2983 2984
	if (status < 0)
		return status;

	/*  Adjust each of the values in the ClearTune filter cross-over table  */
	for (i = 0; i < 31; i++)
2985
		state->CTFiltMax[i] =(state->CTFiltMax[i] / 768) * (fcu_osc + 640);
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072

	return (status);
}

/****************************************************************************
**
**  Name: MT2063_SetParam
**
**  Description:    Sets a tuning algorithm parameter.
**
**                  This function provides access to the internals of the
**                  tuning algorithm.  You can override many of the tuning
**                  algorithm defaults using this function.
**
**  Parameters:     h           - Tuner handle (returned by MT2063_Open)
**                  param       - Tuning algorithm parameter
**                                (see enum MT2063_Param)
**                  nValue      - value to be set
**
**                  param                     Description
**                  ----------------------    --------------------------------
**                  MT2063_SRO_FREQ           crystal frequency
**                  MT2063_STEPSIZE           minimum tuning step size
**                  MT2063_LO1_FREQ           LO1 frequency
**                  MT2063_LO1_STEPSIZE       LO1 minimum step size
**                  MT2063_LO1_FRACN_AVOID    LO1 FracN keep-out region
**                  MT2063_IF1_REQUEST        Requested 1st IF
**                  MT2063_ZIF_BW             zero-IF bandwidth
**                  MT2063_LO2_FREQ           LO2 frequency
**                  MT2063_LO2_STEPSIZE       LO2 minimum step size
**                  MT2063_LO2_FRACN_AVOID    LO2 FracN keep-out region
**                  MT2063_OUTPUT_FREQ        output center frequency
**                  MT2063_OUTPUT_BW          output bandwidth
**                  MT2063_LO_SEPARATION      min inter-tuner LO separation
**                  MT2063_MAX_HARM1          max # of intra-tuner harmonics
**                  MT2063_MAX_HARM2          max # of inter-tuner harmonics
**                  MT2063_RCVR_MODE          Predefined modes
**                  MT2063_LNA_RIN            Set LNA Rin (*)
**                  MT2063_LNA_TGT            Set target power level at LNA (*)
**                  MT2063_PD1_TGT            Set target power level at PD1 (*)
**                  MT2063_PD2_TGT            Set target power level at PD2 (*)
**                  MT2063_ACLNA_MAX          LNA attenuator limit (*)
**                  MT2063_ACRF_MAX           RF attenuator limit (*)
**                  MT2063_ACFIF_MAX          FIF attenuator limit (*)
**                  MT2063_DNC_OUTPUT_ENABLE  DNC output selection
**                  MT2063_VGAGC              VGA gain code
**                  MT2063_VGAOI              VGA output current
**                  MT2063_TAGC               TAGC setting
**                  MT2063_AMPGC              AMP gain code
**                  MT2063_AVOID_DECT         Avoid DECT Frequencies
**                  MT2063_CTFILT_SW          Cleartune filter selection
**
**                  (*) This parameter is set by MT2063_RCVR_MODE, do not call
**                      additionally.
**
**  Usage:          status |= MT2063_SetParam(hMT2063,
**                                            MT2063_STEPSIZE,
**                                            50000);
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_INV_HANDLE    - Invalid tuner handle
**                      MT_ARG_NULL      - Null pointer argument passed
**                      MT_ARG_RANGE     - Invalid parameter requested
**                                         or set value out of range
**                                         or non-writable parameter
**
**  Dependencies:   USERS MUST CALL MT2063_Open() FIRST!
**
**  See Also:       MT2063_GetParam, MT2063_Open
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   138   06-19-2007    DAD    Ver 1.00: Initial, derived from mt2067_b.
**   154   09-13-2007    RSK    Ver 1.05: Get/SetParam changes for LOx_FREQ
**         10-31-2007    PINZ   Ver 1.08: Get/SetParam add VGAGC, VGAOI, AMPGC, TAGC
**         04-18-2008    PINZ   Ver 1.15: Add SetParam LNARIN & PDxTGT
**                                        Split SetParam up to ACLNA / ACLNA_MAX
**                                        removed ACLNA_INRC/DECR (+RF & FIF)
**                                        removed GCUAUTO / BYPATNDN/UP
**   175 I 06-06-2008    PINZ   Ver 1.16: Add control to avoid US DECT freqs.
**   175 I 06-19-2008    RSK    Ver 1.17: Refactor DECT control to SpurAvoid.
**         06-24-2008    PINZ   Ver 1.18: Add Get/SetParam CTFILT_SW
**
****************************************************************************/
3073
static u32 MT2063_SetParam(struct mt2063_state *state,
3074 3075
		           enum MT2063_Param param,
			   enum MT2063_DNC_Output_Enable nValue)
3076
{
3077
	u32 status = 0;	/* Status to be returned        */
3078
	u8 val = 0;
3079

3080 3081 3082
	switch (param) {
		/*  crystal frequency                     */
	case MT2063_SRO_FREQ:
3083 3084 3085 3086 3087 3088 3089
		state->AS_Data.f_ref = nValue;
		state->AS_Data.f_LO1_FracN_Avoid = 0;
		state->AS_Data.f_LO2_FracN_Avoid = nValue / 80 - 1;
		state->AS_Data.f_LO1_Step = nValue / 64;
		state->AS_Data.f_if1_Center =
		    (state->AS_Data.f_ref / 8) *
		    (state->reg[MT2063_REG_FIFFC] + 640);
3090
		break;
3091

3092 3093
		/*  minimum tuning step size              */
	case MT2063_STEPSIZE:
3094
		state->AS_Data.f_LO2_Step = nValue;
3095
		break;
3096

3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109
		/*  LO1 frequency                         */
	case MT2063_LO1_FREQ:
		{
			/* Note: LO1 and LO2 are BOTH written at toggle of LDLOos  */
			/* Capture the Divider and Numerator portions of other LO  */
			u8 tempLO2CQ[3];
			u8 tempLO2C[3];
			u8 tmpOneShot;
			u32 Div, FracN;
			u8 restore = 0;

			/* Buffer the queue for restoration later and get actual LO2 values. */
			status |=
3110
			    MT2063_ReadSub(state,
3111 3112 3113
					   MT2063_REG_LO2CQ_1,
					   &(tempLO2CQ[0]), 3);
			status |=
3114
			    MT2063_ReadSub(state,
3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126
					   MT2063_REG_LO2C_1,
					   &(tempLO2C[0]), 3);

			/* clear the one-shot bits */
			tempLO2CQ[2] = tempLO2CQ[2] & 0x0F;
			tempLO2C[2] = tempLO2C[2] & 0x0F;

			/* only write the queue values if they are different from the actual. */
			if ((tempLO2CQ[0] != tempLO2C[0]) ||
			    (tempLO2CQ[1] != tempLO2C[1]) ||
			    (tempLO2CQ[2] != tempLO2C[2])) {
				/* put actual LO2 value into queue (with 0 in one-shot bits) */
3127
				status |=
3128
				    MT2063_WriteSub(state,
3129 3130
						    MT2063_REG_LO2CQ_1,
						    &(tempLO2C[0]), 3);
3131

3132
				if (status == 0) {
3133
					/* cache the bytes just written. */
3134
					state->reg[MT2063_REG_LO2CQ_1] =
3135
					    tempLO2C[0];
3136
					state->reg[MT2063_REG_LO2CQ_2] =
3137
					    tempLO2C[1];
3138
					state->reg[MT2063_REG_LO2CQ_3] =
3139
					    tempLO2C[2];
3140
				}
3141
				restore = 1;
3142 3143
			}

3144 3145 3146
			/* Calculate the Divider and Numberator components of LO1 */
			status =
			    MT2063_CalcLO1Mult(&Div, &FracN, nValue,
3147
					       state->AS_Data.f_ref /
3148
					       64,
3149 3150
					       state->AS_Data.f_ref);
			state->reg[MT2063_REG_LO1CQ_1] =
3151
			    (u8) (Div & 0x00FF);
3152
			state->reg[MT2063_REG_LO1CQ_2] =
3153 3154
			    (u8) (FracN);
			status |=
3155
			    MT2063_WriteSub(state,
3156
					    MT2063_REG_LO1CQ_1,
3157
					    &state->
3158 3159 3160 3161 3162
					    reg[MT2063_REG_LO1CQ_1], 2);

			/* set the one-shot bit to load the pair of LO values */
			tmpOneShot = tempLO2CQ[2] | 0xE0;
			status |=
3163
			    MT2063_WriteSub(state,
3164 3165 3166 3167 3168 3169 3170
					    MT2063_REG_LO2CQ_3,
					    &tmpOneShot, 1);

			/* only restore the queue values if they were different from the actual. */
			if (restore) {
				/* put actual LO2 value into queue (0 in one-shot bits) */
				status |=
3171
				    MT2063_WriteSub(state,
3172 3173
						    MT2063_REG_LO2CQ_1,
						    &(tempLO2CQ[0]), 3);
3174

3175
				/* cache the bytes just written. */
3176
				state->reg[MT2063_REG_LO2CQ_1] =
3177
				    tempLO2CQ[0];
3178
				state->reg[MT2063_REG_LO2CQ_2] =
3179
				    tempLO2CQ[1];
3180
				state->reg[MT2063_REG_LO2CQ_3] =
3181 3182
				    tempLO2CQ[2];
			}
3183

3184
			MT2063_GetParam(state,
3185
					MT2063_LO1_FREQ,
3186
					&state->AS_Data.f_LO1);
3187 3188
		}
		break;
3189

3190 3191
		/*  LO1 minimum step size                 */
	case MT2063_LO1_STEPSIZE:
3192
		state->AS_Data.f_LO1_Step = nValue;
3193
		break;
3194

3195 3196
		/*  LO1 FracN keep-out region             */
	case MT2063_LO1_FRACN_AVOID_PARAM:
3197
		state->AS_Data.f_LO1_FracN_Avoid = nValue;
3198
		break;
3199

3200 3201
		/*  Requested 1st IF                      */
	case MT2063_IF1_REQUEST:
3202
		state->AS_Data.f_if1_Request = nValue;
3203
		break;
3204

3205 3206
		/*  zero-IF bandwidth                     */
	case MT2063_ZIF_BW:
3207
		state->AS_Data.f_zif_bw = nValue;
3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223
		break;

		/*  LO2 frequency                         */
	case MT2063_LO2_FREQ:
		{
			/* Note: LO1 and LO2 are BOTH written at toggle of LDLOos  */
			/* Capture the Divider and Numerator portions of other LO  */
			u8 tempLO1CQ[2];
			u8 tempLO1C[2];
			u32 Div2;
			u32 FracN2;
			u8 tmpOneShot;
			u8 restore = 0;

			/* Buffer the queue for restoration later and get actual LO2 values. */
			status |=
3224
			    MT2063_ReadSub(state,
3225 3226 3227
					   MT2063_REG_LO1CQ_1,
					   &(tempLO1CQ[0]), 2);
			status |=
3228
			    MT2063_ReadSub(state,
3229 3230 3231 3232 3233 3234 3235
					   MT2063_REG_LO1C_1,
					   &(tempLO1C[0]), 2);

			/* only write the queue values if they are different from the actual. */
			if ((tempLO1CQ[0] != tempLO1C[0])
			    || (tempLO1CQ[1] != tempLO1C[1])) {
				/* put actual LO1 value into queue */
3236
				status |=
3237
				    MT2063_WriteSub(state,
3238
						    MT2063_REG_LO1CQ_1,
3239 3240 3241
						    &(tempLO1C[0]), 2);

				/* cache the bytes just written. */
3242
				state->reg[MT2063_REG_LO1CQ_1] =
3243
				    tempLO1C[0];
3244
				state->reg[MT2063_REG_LO1CQ_2] =
3245 3246 3247
				    tempLO1C[1];
				restore = 1;
			}
3248

3249 3250 3251
			/* Calculate the Divider and Numberator components of LO2 */
			status =
			    MT2063_CalcLO2Mult(&Div2, &FracN2, nValue,
3252
					       state->AS_Data.f_ref /
3253
					       8191,
3254 3255
					       state->AS_Data.f_ref);
			state->reg[MT2063_REG_LO2CQ_1] =
3256 3257
			    (u8) ((Div2 << 1) |
				      ((FracN2 >> 12) & 0x01)) & 0xFF;
3258
			state->reg[MT2063_REG_LO2CQ_2] =
3259
			    (u8) ((FracN2 >> 4) & 0xFF);
3260
			state->reg[MT2063_REG_LO2CQ_3] =
3261 3262
			    (u8) ((FracN2 & 0x0F));
			status |=
3263
			    MT2063_WriteSub(state,
3264
					    MT2063_REG_LO1CQ_1,
3265
					    &state->
3266 3267 3268 3269
					    reg[MT2063_REG_LO1CQ_1], 3);

			/* set the one-shot bit to load the LO values */
			tmpOneShot =
3270
			    state->reg[MT2063_REG_LO2CQ_3] | 0xE0;
3271
			status |=
3272
			    MT2063_WriteSub(state,
3273 3274 3275 3276 3277 3278
					    MT2063_REG_LO2CQ_3,
					    &tmpOneShot, 1);

			/* only restore LO1 queue value if they were different from the actual. */
			if (restore) {
				/* put previous LO1 queue value back into queue */
3279
				status |=
3280
				    MT2063_WriteSub(state,
3281 3282
						    MT2063_REG_LO1CQ_1,
						    &(tempLO1CQ[0]), 2);
3283

3284
				/* cache the bytes just written. */
3285
				state->reg[MT2063_REG_LO1CQ_1] =
3286
				    tempLO1CQ[0];
3287
				state->reg[MT2063_REG_LO1CQ_2] =
3288 3289
				    tempLO1CQ[1];
			}
3290

3291
			MT2063_GetParam(state,
3292
					MT2063_LO2_FREQ,
3293
					&state->AS_Data.f_LO2);
3294 3295
		}
		break;
3296

3297 3298
		/*  LO2 minimum step size                 */
	case MT2063_LO2_STEPSIZE:
3299
		state->AS_Data.f_LO2_Step = nValue;
3300
		break;
3301

3302 3303
		/*  LO2 FracN keep-out region             */
	case MT2063_LO2_FRACN_AVOID:
3304
		state->AS_Data.f_LO2_FracN_Avoid = nValue;
3305
		break;
3306

3307 3308
		/*  output center frequency               */
	case MT2063_OUTPUT_FREQ:
3309
		state->AS_Data.f_out = nValue;
3310
		break;
3311

3312 3313
		/*  output bandwidth                      */
	case MT2063_OUTPUT_BW:
3314
		state->AS_Data.f_out_bw = nValue + 750000;
3315
		break;
3316

3317 3318
		/*  min inter-tuner LO separation         */
	case MT2063_LO_SEPARATION:
3319
		state->AS_Data.f_min_LO_Separation = nValue;
3320
		break;
3321

3322 3323
		/*  max # of intra-tuner harmonics        */
	case MT2063_MAX_HARM1:
3324
		state->AS_Data.maxH1 = nValue;
3325
		break;
3326

3327 3328
		/*  max # of inter-tuner harmonics        */
	case MT2063_MAX_HARM2:
3329
		state->AS_Data.maxH2 = nValue;
3330
		break;
3331

3332 3333
	case MT2063_RCVR_MODE:
		status |=
3334
		    MT2063_SetReceiverMode(state,
3335 3336 3337
					   (enum MT2063_RCVR_MODES)
					   nValue);
		break;
3338

3339 3340 3341
		/* Set LNA Rin -- nValue is desired value */
	case MT2063_LNA_RIN:
		val =
3342
		    (state->
3343 3344
		     reg[MT2063_REG_CTRL_2C] & (u8) ~ 0x03) |
		    (nValue & 0x03);
3345
		if (state->reg[MT2063_REG_CTRL_2C] != val) {
3346
			status |=
3347
			    MT2063_SetReg(state, MT2063_REG_CTRL_2C,
3348 3349 3350
					  val);
		}
		break;
3351

3352 3353 3354
		/* Set target power level at LNA -- nValue is desired value */
	case MT2063_LNA_TGT:
		val =
3355
		    (state->
3356 3357
		     reg[MT2063_REG_LNA_TGT] & (u8) ~ 0x3F) |
		    (nValue & 0x3F);
3358
		if (state->reg[MT2063_REG_LNA_TGT] != val) {
3359
			status |=
3360
			    MT2063_SetReg(state, MT2063_REG_LNA_TGT,
3361 3362 3363
					  val);
		}
		break;
3364

3365 3366 3367
		/* Set target power level at PD1 -- nValue is desired value */
	case MT2063_PD1_TGT:
		val =
3368
		    (state->
3369 3370
		     reg[MT2063_REG_PD1_TGT] & (u8) ~ 0x3F) |
		    (nValue & 0x3F);
3371
		if (state->reg[MT2063_REG_PD1_TGT] != val) {
3372
			status |=
3373
			    MT2063_SetReg(state, MT2063_REG_PD1_TGT,
3374 3375 3376
					  val);
		}
		break;
3377

3378 3379 3380
		/* Set target power level at PD2 -- nValue is desired value */
	case MT2063_PD2_TGT:
		val =
3381
		    (state->
3382 3383
		     reg[MT2063_REG_PD2_TGT] & (u8) ~ 0x3F) |
		    (nValue & 0x3F);
3384
		if (state->reg[MT2063_REG_PD2_TGT] != val) {
3385
			status |=
3386
			    MT2063_SetReg(state, MT2063_REG_PD2_TGT,
3387 3388 3389
					  val);
		}
		break;
3390

3391 3392 3393
		/* Set LNA atten limit -- nValue is desired value */
	case MT2063_ACLNA_MAX:
		val =
3394
		    (state->
3395 3396 3397
		     reg[MT2063_REG_LNA_OV] & (u8) ~ 0x1F) | (nValue
								  &
								  0x1F);
3398
		if (state->reg[MT2063_REG_LNA_OV] != val) {
3399
			status |=
3400
			    MT2063_SetReg(state, MT2063_REG_LNA_OV,
3401 3402 3403
					  val);
		}
		break;
3404

3405 3406 3407
		/* Set RF atten limit -- nValue is desired value */
	case MT2063_ACRF_MAX:
		val =
3408
		    (state->
3409 3410 3411
		     reg[MT2063_REG_RF_OV] & (u8) ~ 0x1F) | (nValue
								 &
								 0x1F);
3412
		if (state->reg[MT2063_REG_RF_OV] != val) {
3413
			status |=
3414
			    MT2063_SetReg(state, MT2063_REG_RF_OV, val);
3415 3416
		}
		break;
3417

3418 3419
		/* Set FIF atten limit -- nValue is desired value, max. 5 if no B3 */
	case MT2063_ACFIF_MAX:
3420
		if (state->reg[MT2063_REG_PART_REV] != MT2063_B3
3421 3422 3423
		    && nValue > 5)
			nValue = 5;
		val =
3424
		    (state->
3425 3426 3427
		     reg[MT2063_REG_FIF_OV] & (u8) ~ 0x1F) | (nValue
								  &
								  0x1F);
3428
		if (state->reg[MT2063_REG_FIF_OV] != val) {
3429
			status |=
3430
			    MT2063_SetReg(state, MT2063_REG_FIF_OV,
3431 3432 3433
					  val);
		}
		break;
3434

3435 3436
	case MT2063_DNC_OUTPUT_ENABLE:
		/* selects, which DNC output is used */
3437
		switch (nValue) {
3438 3439
		case MT2063_DNC_NONE:
			{
3440 3441
				val = (state->reg[MT2063_REG_DNC_GAIN] & 0xFC) | 0x03;	/* Set DNC1GC=3 */
				if (state->reg[MT2063_REG_DNC_GAIN] !=
3442 3443
				    val)
					status |=
3444
					    MT2063_SetReg(state,
3445 3446 3447
							  MT2063_REG_DNC_GAIN,
							  val);

3448 3449
				val = (state->reg[MT2063_REG_VGA_GAIN] & 0xFC) | 0x03;	/* Set DNC2GC=3 */
				if (state->reg[MT2063_REG_VGA_GAIN] !=
3450 3451
				    val)
					status |=
3452
					    MT2063_SetReg(state,
3453 3454 3455
							  MT2063_REG_VGA_GAIN,
							  val);

3456 3457
				val = (state->reg[MT2063_REG_RSVD_20] & ~0x40);	/* Set PD2MUX=0 */
				if (state->reg[MT2063_REG_RSVD_20] !=
3458 3459
				    val)
					status |=
3460
					    MT2063_SetReg(state,
3461 3462
							  MT2063_REG_RSVD_20,
							  val);
3463 3464 3465

				break;
			}
3466 3467
		case MT2063_DNC_1:
			{
3468 3469
				val = (state->reg[MT2063_REG_DNC_GAIN] & 0xFC) | (DNC1GC[state->rcvr_mode] & 0x03);	/* Set DNC1GC=x */
				if (state->reg[MT2063_REG_DNC_GAIN] !=
3470 3471
				    val)
					status |=
3472
					    MT2063_SetReg(state,
3473 3474
							  MT2063_REG_DNC_GAIN,
							  val);
3475

3476 3477
				val = (state->reg[MT2063_REG_VGA_GAIN] & 0xFC) | 0x03;	/* Set DNC2GC=3 */
				if (state->reg[MT2063_REG_VGA_GAIN] !=
3478 3479
				    val)
					status |=
3480
					    MT2063_SetReg(state,
3481 3482
							  MT2063_REG_VGA_GAIN,
							  val);
3483

3484 3485
				val = (state->reg[MT2063_REG_RSVD_20] & ~0x40);	/* Set PD2MUX=0 */
				if (state->reg[MT2063_REG_RSVD_20] !=
3486 3487
				    val)
					status |=
3488
					    MT2063_SetReg(state,
3489 3490
							  MT2063_REG_RSVD_20,
							  val);
3491

3492
				break;
3493
			}
3494 3495
		case MT2063_DNC_2:
			{
3496 3497
				val = (state->reg[MT2063_REG_DNC_GAIN] & 0xFC) | 0x03;	/* Set DNC1GC=3 */
				if (state->reg[MT2063_REG_DNC_GAIN] !=
3498 3499
				    val)
					status |=
3500
					    MT2063_SetReg(state,
3501 3502
							  MT2063_REG_DNC_GAIN,
							  val);
3503

3504 3505
				val = (state->reg[MT2063_REG_VGA_GAIN] & 0xFC) | (DNC2GC[state->rcvr_mode] & 0x03);	/* Set DNC2GC=x */
				if (state->reg[MT2063_REG_VGA_GAIN] !=
3506 3507
				    val)
					status |=
3508
					    MT2063_SetReg(state,
3509 3510 3511
							  MT2063_REG_VGA_GAIN,
							  val);

3512 3513
				val = (state->reg[MT2063_REG_RSVD_20] | 0x40);	/* Set PD2MUX=1 */
				if (state->reg[MT2063_REG_RSVD_20] !=
3514 3515
				    val)
					status |=
3516
					    MT2063_SetReg(state,
3517 3518
							  MT2063_REG_RSVD_20,
							  val);
3519

3520 3521 3522
				break;
			}
		case MT2063_DNC_BOTH:
3523
			{
3524 3525
				val = (state->reg[MT2063_REG_DNC_GAIN] & 0xFC) | (DNC1GC[state->rcvr_mode] & 0x03);	/* Set DNC1GC=x */
				if (state->reg[MT2063_REG_DNC_GAIN] !=
3526 3527
				    val)
					status |=
3528
					    MT2063_SetReg(state,
3529 3530 3531
							  MT2063_REG_DNC_GAIN,
							  val);

3532 3533
				val = (state->reg[MT2063_REG_VGA_GAIN] & 0xFC) | (DNC2GC[state->rcvr_mode] & 0x03);	/* Set DNC2GC=x */
				if (state->reg[MT2063_REG_VGA_GAIN] !=
3534 3535
				    val)
					status |=
3536
					    MT2063_SetReg(state,
3537 3538 3539
							  MT2063_REG_VGA_GAIN,
							  val);

3540 3541
				val = (state->reg[MT2063_REG_RSVD_20] | 0x40);	/* Set PD2MUX=1 */
				if (state->reg[MT2063_REG_RSVD_20] !=
3542 3543
				    val)
					status |=
3544
					    MT2063_SetReg(state,
3545 3546 3547 3548
							  MT2063_REG_RSVD_20,
							  val);

				break;
3549
			}
3550
		default:
3551
			break;
3552 3553
		}
		break;
3554

3555 3556 3557
	case MT2063_VGAGC:
		/* Set VGA gain code */
		val =
3558
		    (state->
3559 3560
		     reg[MT2063_REG_VGA_GAIN] & (u8) ~ 0x0C) |
		    ((nValue & 0x03) << 2);
3561
		if (state->reg[MT2063_REG_VGA_GAIN] != val) {
3562
			status |=
3563
			    MT2063_SetReg(state, MT2063_REG_VGA_GAIN,
3564 3565 3566
					  val);
		}
		break;
3567

3568 3569 3570
	case MT2063_VGAOI:
		/* Set VGA bias current */
		val =
3571
		    (state->
3572 3573
		     reg[MT2063_REG_RSVD_31] & (u8) ~ 0x07) |
		    (nValue & 0x07);
3574
		if (state->reg[MT2063_REG_RSVD_31] != val) {
3575
			status |=
3576
			    MT2063_SetReg(state, MT2063_REG_RSVD_31,
3577
					  val);
3578
		}
3579 3580 3581 3582 3583
		break;

	case MT2063_TAGC:
		/* Set TAGC */
		val =
3584
		    (state->
3585 3586
		     reg[MT2063_REG_RSVD_1E] & (u8) ~ 0x03) |
		    (nValue & 0x03);
3587
		if (state->reg[MT2063_REG_RSVD_1E] != val) {
3588
			status |=
3589
			    MT2063_SetReg(state, MT2063_REG_RSVD_1E,
3590 3591 3592 3593 3594 3595 3596
					  val);
		}
		break;

	case MT2063_AMPGC:
		/* Set Amp gain code */
		val =
3597
		    (state->
3598 3599
		     reg[MT2063_REG_TEMP_SEL] & (u8) ~ 0x03) |
		    (nValue & 0x03);
3600
		if (state->reg[MT2063_REG_TEMP_SEL] != val) {
3601
			status |=
3602
			    MT2063_SetReg(state, MT2063_REG_TEMP_SEL,
3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614
					  val);
		}
		break;

		/*  Avoid DECT Frequencies                */
	case MT2063_AVOID_DECT:
		{
			enum MT2063_DECT_Avoid_Type newAvoidSetting =
			    (enum MT2063_DECT_Avoid_Type)nValue;
			if ((newAvoidSetting >=
			     MT2063_NO_DECT_AVOIDANCE)
			    && (newAvoidSetting <= MT2063_AVOID_BOTH)) {
3615
				state->AS_Data.avoidDECT =
3616 3617 3618 3619 3620 3621 3622
				    newAvoidSetting;
			}
		}
		break;

		/*  Cleartune filter selection: 0 - by IC (default), 1 - by software  */
	case MT2063_CTFILT_SW:
3623
		state->ctfilt_sw = (nValue & 0x01);
3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644
		break;

		/*  These parameters are read-only  */
	case MT2063_IC_ADDR:
	case MT2063_MAX_OPEN:
	case MT2063_NUM_OPEN:
	case MT2063_INPUT_FREQ:
	case MT2063_IF1_ACTUAL:
	case MT2063_IF1_CENTER:
	case MT2063_IF1_BW:
	case MT2063_AS_ALG:
	case MT2063_EXCL_ZONES:
	case MT2063_SPUR_AVOIDED:
	case MT2063_NUM_SPURS:
	case MT2063_SPUR_PRESENT:
	case MT2063_ACLNA:
	case MT2063_ACRF:
	case MT2063_ACFIF:
	case MT2063_EOP:
	default:
		status |= -ERANGE;
3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675
	}
	return (status);
}

/****************************************************************************
**
**  Name: MT2063_ClearPowerMaskBits
**
**  Description:    Clears the power-down mask bits for various sections of
**                  the MT2063
**
**  Parameters:     h           - Tuner handle (returned by MT2063_Open)
**                  Bits        - Mask bits to be cleared.
**
**                  See definition of MT2063_Mask_Bits type for description
**                  of each of the power bits.
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_INV_HANDLE    - Invalid tuner handle
**                      MT_COMM_ERR      - Serial bus communications error
**
**  Dependencies:   USERS MUST CALL MT2063_Open() FIRST!
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   138   06-19-2007    DAD    Ver 1.00: Initial, derived from mt2067_b.
**
****************************************************************************/
3676
static u32 MT2063_ClearPowerMaskBits(struct mt2063_state *state, enum MT2063_Mask_Bits Bits)
3677
{
3678
	u32 status = 0;	/* Status to be returned        */
3679

3680 3681
	Bits = (enum MT2063_Mask_Bits)(Bits & MT2063_ALL_SD);	/* Only valid bits for this tuner */
	if ((Bits & 0xFF00) != 0) {
3682
		state->reg[MT2063_REG_PWR_2] &= ~(u8) (Bits >> 8);
3683
		status |=
3684
		    MT2063_WriteSub(state,
3685
				    MT2063_REG_PWR_2,
3686
				    &state->reg[MT2063_REG_PWR_2], 1);
3687 3688
	}
	if ((Bits & 0xFF) != 0) {
3689
		state->reg[MT2063_REG_PWR_1] &= ~(u8) (Bits & 0xFF);
3690
		status |=
3691
		    MT2063_WriteSub(state,
3692
				    MT2063_REG_PWR_1,
3693
				    &state->reg[MT2063_REG_PWR_1], 1);
3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726
	}

	return (status);
}

/****************************************************************************
**
**  Name: MT2063_SoftwareShutdown
**
**  Description:    Enables or disables software shutdown function.  When
**                  Shutdown==1, any section whose power mask is set will be
**                  shutdown.
**
**  Parameters:     h           - Tuner handle (returned by MT2063_Open)
**                  Shutdown    - 1 = shutdown the masked sections, otherwise
**                                power all sections on
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_INV_HANDLE    - Invalid tuner handle
**                      MT_COMM_ERR      - Serial bus communications error
**
**  Dependencies:   USERS MUST CALL MT2063_Open() FIRST!
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   138   06-19-2007    DAD    Ver 1.00: Initial, derived from mt2067_b.
**         01-03-2008    PINZ   Ver 1.xx: Added a trigger of BYPATNUP for
**                              correct wakeup of the LNA
**
****************************************************************************/
3727
static u32 MT2063_SoftwareShutdown(struct mt2063_state *state, u8 Shutdown)
3728
{
3729
	u32 status = 0;	/* Status to be returned        */
3730

3731
	if (Shutdown == 1)
3732
		state->reg[MT2063_REG_PWR_1] |= 0x04;	/* Turn the bit on */
3733
	else
3734
		state->reg[MT2063_REG_PWR_1] &= ~0x04;	/* Turn off the bit */
3735

3736
	status |=
3737
	    MT2063_WriteSub(state,
3738
			    MT2063_REG_PWR_1,
3739
			    &state->reg[MT2063_REG_PWR_1], 1);
3740 3741

	if (Shutdown != 1) {
3742 3743
		state->reg[MT2063_REG_BYP_CTRL] =
		    (state->reg[MT2063_REG_BYP_CTRL] & 0x9F) | 0x40;
3744
		status |=
3745
		    MT2063_WriteSub(state,
3746
				    MT2063_REG_BYP_CTRL,
3747
				    &state->reg[MT2063_REG_BYP_CTRL],
3748
				    1);
3749 3750
		state->reg[MT2063_REG_BYP_CTRL] =
		    (state->reg[MT2063_REG_BYP_CTRL] & 0x9F);
3751
		status |=
3752
		    MT2063_WriteSub(state,
3753
				    MT2063_REG_BYP_CTRL,
3754
				    &state->reg[MT2063_REG_BYP_CTRL],
3755
				    1);
3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788
	}

	return (status);
}

/****************************************************************************
**
**  Name: MT2063_SetReg
**
**  Description:    Sets an MT2063 register.
**
**  Parameters:     h           - Tuner handle (returned by MT2063_Open)
**                  reg         - MT2063 register/subaddress location
**                  val         - MT2063 register/subaddress value
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_COMM_ERR      - Serial bus communications error
**                      MT_INV_HANDLE    - Invalid tuner handle
**                      MT_ARG_RANGE     - Argument out of range
**
**  Dependencies:   USERS MUST CALL MT2063_Open() FIRST!
**
**                  Use this function if you need to override a default
**                  register value
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   138   06-19-2007    DAD    Ver 1.00: Initial, derived from mt2067_b.
**
****************************************************************************/
3789
static u32 MT2063_SetReg(struct mt2063_state *state, u8 reg, u8 val)
3790
{
3791
	u32 status = 0;	/* Status to be returned        */
3792 3793

	if (reg >= MT2063_REG_END_REGS)
3794
		status |= -ERANGE;
3795

3796
	status = MT2063_WriteSub(state, reg, &val,
3797 3798
			         1);
	if (status >= 0)
3799
		state->reg[reg] = val;
3800 3801 3802 3803

	return (status);
}

3804
static u32 MT2063_Round_fLO(u32 f_LO, u32 f_LO_Step, u32 f_ref)
3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839
{
	return f_ref * (f_LO / f_ref)
	    + f_LO_Step * (((f_LO % f_ref) + (f_LO_Step / 2)) / f_LO_Step);
}

/****************************************************************************
**
**  Name: fLO_FractionalTerm
**
**  Description:    Calculates the portion contributed by FracN / denom.
**
**                  This function preserves maximum precision without
**                  risk of overflow.  It accurately calculates
**                  f_ref * num / denom to within 1 HZ with fixed math.
**
**  Parameters:     num       - Fractional portion of the multiplier
**                  denom     - denominator portion of the ratio
**                              This routine successfully handles denom values
**                              up to and including 2^18.
**                  f_Ref     - SRO frequency.  This calculation handles
**                              f_ref as two separate 14-bit fields.
**                              Therefore, a maximum value of 2^28-1
**                              may safely be used for f_ref.  This is
**                              the genesis of the magic number "14" and the
**                              magic mask value of 0x03FFF.
**
**  Returns:        f_ref * num / denom
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   138   06-19-2007    DAD    Ver 1.00: Initial, derived from mt2067_b.
**
****************************************************************************/
3840 3841
static u32 MT2063_fLO_FractionalTerm(u32 f_ref,
					 u32 num, u32 denom)
3842
{
3843 3844 3845 3846
	u32 t1 = (f_ref >> 14) * num;
	u32 term1 = t1 / denom;
	u32 loss = t1 % denom;
	u32 term2 =
3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877
	    (((f_ref & 0x00003FFF) * num + (loss << 14)) + (denom / 2)) / denom;
	return ((term1 << 14) + term2);
}

/****************************************************************************
**
**  Name: CalcLO1Mult
**
**  Description:    Calculates Integer divider value and the numerator
**                  value for a FracN PLL.
**
**                  This function assumes that the f_LO and f_Ref are
**                  evenly divisible by f_LO_Step.
**
**  Parameters:     Div       - OUTPUT: Whole number portion of the multiplier
**                  FracN     - OUTPUT: Fractional portion of the multiplier
**                  f_LO      - desired LO frequency.
**                  f_LO_Step - Minimum step size for the LO (in Hz).
**                  f_Ref     - SRO frequency.
**                  f_Avoid   - Range of PLL frequencies to avoid near
**                              integer multiples of f_Ref (in Hz).
**
**  Returns:        Recalculated LO frequency.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   138   06-19-2007    DAD    Ver 1.00: Initial, derived from mt2067_b.
**
****************************************************************************/
3878 3879 3880 3881
static u32 MT2063_CalcLO1Mult(u32 * Div,
				  u32 * FracN,
				  u32 f_LO,
				  u32 f_LO_Step, u32 f_Ref)
3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920
{
	/*  Calculate the whole number portion of the divider */
	*Div = f_LO / f_Ref;

	/*  Calculate the numerator value (round to nearest f_LO_Step) */
	*FracN =
	    (64 * (((f_LO % f_Ref) + (f_LO_Step / 2)) / f_LO_Step) +
	     (f_Ref / f_LO_Step / 2)) / (f_Ref / f_LO_Step);

	return (f_Ref * (*Div)) + MT2063_fLO_FractionalTerm(f_Ref, *FracN, 64);
}

/****************************************************************************
**
**  Name: CalcLO2Mult
**
**  Description:    Calculates Integer divider value and the numerator
**                  value for a FracN PLL.
**
**                  This function assumes that the f_LO and f_Ref are
**                  evenly divisible by f_LO_Step.
**
**  Parameters:     Div       - OUTPUT: Whole number portion of the multiplier
**                  FracN     - OUTPUT: Fractional portion of the multiplier
**                  f_LO      - desired LO frequency.
**                  f_LO_Step - Minimum step size for the LO (in Hz).
**                  f_Ref     - SRO frequency.
**                  f_Avoid   - Range of PLL frequencies to avoid near
**                              integer multiples of f_Ref (in Hz).
**
**  Returns:        Recalculated LO frequency.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   138   06-19-2007    DAD    Ver 1.00: Initial, derived from mt2067_b.
**
****************************************************************************/
3921 3922 3923 3924
static u32 MT2063_CalcLO2Mult(u32 * Div,
				  u32 * FracN,
				  u32 f_LO,
				  u32 f_LO_Step, u32 f_Ref)
3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944
{
	/*  Calculate the whole number portion of the divider */
	*Div = f_LO / f_Ref;

	/*  Calculate the numerator value (round to nearest f_LO_Step) */
	*FracN =
	    (8191 * (((f_LO % f_Ref) + (f_LO_Step / 2)) / f_LO_Step) +
	     (f_Ref / f_LO_Step / 2)) / (f_Ref / f_LO_Step);

	return (f_Ref * (*Div)) + MT2063_fLO_FractionalTerm(f_Ref, *FracN,
							    8191);
}

/****************************************************************************
**
**  Name: FindClearTuneFilter
**
**  Description:    Calculate the corrrect ClearTune filter to be used for
**                  a given input frequency.
**
3945
**  Parameters:     state       - ptr to tuner data structure
3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959
**                  f_in        - RF input center frequency (in Hz).
**
**  Returns:        ClearTune filter number (0-31)
**
**  Dependencies:   MUST CALL MT2064_Open BEFORE FindClearTuneFilter!
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**         04-10-2008   PINZ    Ver 1.14: Use software-controlled ClearTune
**                                        cross-over frequency values.
**
****************************************************************************/
3960
static u32 FindClearTuneFilter(struct mt2063_state *state, u32 f_in)
3961
{
3962 3963
	u32 RFBand;
	u32 idx;		/*  index loop                      */
3964 3965 3966 3967 3968 3969

	/*
	 **  Find RF Band setting
	 */
	RFBand = 31;		/*  def when f_in > all    */
	for (idx = 0; idx < 31; ++idx) {
3970
		if (state->CTFiltMax[idx] >= f_in) {
3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018
			RFBand = idx;
			break;
		}
	}
	return (RFBand);
}

/****************************************************************************
**
**  Name: MT2063_Tune
**
**  Description:    Change the tuner's tuned frequency to RFin.
**
**  Parameters:     h           - Open handle to the tuner (from MT2063_Open).
**                  f_in        - RF input center frequency (in Hz).
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_INV_HANDLE    - Invalid tuner handle
**                      MT_UPC_UNLOCK    - Upconverter PLL unlocked
**                      MT_DNC_UNLOCK    - Downconverter PLL unlocked
**                      MT_COMM_ERR      - Serial bus communications error
**                      MT_SPUR_CNT_MASK - Count of avoided LO spurs
**                      MT_SPUR_PRESENT  - LO spur possible in output
**                      MT_FIN_RANGE     - Input freq out of range
**                      MT_FOUT_RANGE    - Output freq out of range
**                      MT_UPC_RANGE     - Upconverter freq out of range
**                      MT_DNC_RANGE     - Downconverter freq out of range
**
**  Dependencies:   MUST CALL MT2063_Open BEFORE MT2063_Tune!
**
**                  MT_ReadSub       - Read data from the two-wire serial bus
**                  MT_WriteSub      - Write data to the two-wire serial bus
**                  MT_Sleep         - Delay execution for x milliseconds
**                  MT2063_GetLocked - Checks to see if LO1 and LO2 are locked
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   138   06-19-2007    DAD    Ver 1.00: Initial, derived from mt2067_b.
**         04-10-2008   PINZ    Ver 1.05: Use software-controlled ClearTune
**                                        cross-over frequency values.
**   175 I 16-06-2008   PINZ    Ver 1.16: Add control to avoid US DECT freqs.
**   175 I 06-19-2008    RSK    Ver 1.17: Refactor DECT control to SpurAvoid.
**         06-24-2008    PINZ   Ver 1.18: Add Get/SetParam CTFILT_SW
**
****************************************************************************/
4019
static u32 MT2063_Tune(struct mt2063_state *state, u32 f_in)
4020 4021
{				/* RF input center frequency   */

4022
	u32 status = 0;	/*  status of operation             */
4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035
	u32 LO1;		/*  1st LO register value           */
	u32 Num1;		/*  Numerator for LO1 reg. value    */
	u32 f_IF1;		/*  1st IF requested                */
	u32 LO2;		/*  2nd LO register value           */
	u32 Num2;		/*  Numerator for LO2 reg. value    */
	u32 ofLO1, ofLO2;	/*  last time's LO frequencies      */
	u32 ofin, ofout;	/*  last time's I/O frequencies     */
	u8 fiffc = 0x80;	/*  FIFF center freq from tuner     */
	u32 fiffof;		/*  Offset from FIFF center freq    */
	const u8 LO1LK = 0x80;	/*  Mask for LO1 Lock bit           */
	u8 LO2LK = 0x08;	/*  Mask for LO2 Lock bit           */
	u8 val;
	u32 RFBand;
4036 4037 4038

	/*  Check the input and output frequency ranges                   */
	if ((f_in < MT2063_MIN_FIN_FREQ) || (f_in > MT2063_MAX_FIN_FREQ))
4039
		return -EINVAL;
4040

4041 4042
	if ((state->AS_Data.f_out < MT2063_MIN_FOUT_FREQ)
	    || (state->AS_Data.f_out > MT2063_MAX_FOUT_FREQ))
4043
		return -EINVAL;
4044 4045 4046 4047

	/*
	 **  Save original LO1 and LO2 register values
	 */
4048 4049 4050 4051
	ofLO1 = state->AS_Data.f_LO1;
	ofLO2 = state->AS_Data.f_LO2;
	ofin = state->AS_Data.f_in;
	ofout = state->AS_Data.f_out;
4052 4053 4054 4055

	/*
	 **  Find and set RF Band setting
	 */
4056 4057 4058
	if (state->ctfilt_sw == 1) {
		val = (state->reg[MT2063_REG_CTUNE_CTRL] | 0x08);
		if (state->reg[MT2063_REG_CTUNE_CTRL] != val) {
4059
			status |=
4060
			    MT2063_SetReg(state, MT2063_REG_CTUNE_CTRL, val);
4061
		}
4062 4063 4064 4065
		val = state->reg[MT2063_REG_CTUNE_OV];
		RFBand = FindClearTuneFilter(state, f_in);
		state->reg[MT2063_REG_CTUNE_OV] =
		    (u8) ((state->reg[MT2063_REG_CTUNE_OV] & ~0x1F)
4066
			      | RFBand);
4067
		if (state->reg[MT2063_REG_CTUNE_OV] != val) {
4068
			status |=
4069
			    MT2063_SetReg(state, MT2063_REG_CTUNE_OV, val);
4070 4071 4072 4073 4074 4075
		}
	}

	/*
	 **  Read the FIFF Center Frequency from the tuner
	 */
4076
	if (status >= 0) {
4077
		status |=
4078
		    MT2063_ReadSub(state,
4079
				   MT2063_REG_FIFFC,
4080 4081
				   &state->reg[MT2063_REG_FIFFC], 1);
		fiffc = state->reg[MT2063_REG_FIFFC];
4082 4083 4084 4085
	}
	/*
	 **  Assign in the requested values
	 */
4086
	state->AS_Data.f_in = f_in;
4087
	/*  Request a 1st IF such that LO1 is on a step size */
4088 4089 4090 4091
	state->AS_Data.f_if1_Request =
	    MT2063_Round_fLO(state->AS_Data.f_if1_Request + f_in,
			     state->AS_Data.f_LO1_Step,
			     state->AS_Data.f_ref) - f_in;
4092 4093 4094 4095 4096

	/*
	 **  Calculate frequency settings.  f_IF1_FREQ + f_in is the
	 **  desired LO1 frequency
	 */
4097
	MT2063_ResetExclZones(&state->AS_Data);
4098

4099
	f_IF1 = MT2063_ChooseFirstIF(&state->AS_Data);
4100

4101 4102 4103
	state->AS_Data.f_LO1 =
	    MT2063_Round_fLO(f_IF1 + f_in, state->AS_Data.f_LO1_Step,
			     state->AS_Data.f_ref);
4104

4105 4106 4107
	state->AS_Data.f_LO2 =
	    MT2063_Round_fLO(state->AS_Data.f_LO1 - state->AS_Data.f_out - f_in,
			     state->AS_Data.f_LO2_Step, state->AS_Data.f_ref);
4108 4109 4110 4111 4112

	/*
	 ** Check for any LO spurs in the output bandwidth and adjust
	 ** the LO settings to avoid them if needed
	 */
4113
	status |= MT2063_AvoidSpurs(state, &state->AS_Data);
4114 4115 4116 4117 4118
	/*
	 ** MT_AvoidSpurs spurs may have changed the LO1 & LO2 values.
	 ** Recalculate the LO frequencies and the values to be placed
	 ** in the tuning registers.
	 */
4119 4120 4121 4122 4123 4124 4125 4126 4127
	state->AS_Data.f_LO1 =
	    MT2063_CalcLO1Mult(&LO1, &Num1, state->AS_Data.f_LO1,
			       state->AS_Data.f_LO1_Step, state->AS_Data.f_ref);
	state->AS_Data.f_LO2 =
	    MT2063_Round_fLO(state->AS_Data.f_LO1 - state->AS_Data.f_out - f_in,
			     state->AS_Data.f_LO2_Step, state->AS_Data.f_ref);
	state->AS_Data.f_LO2 =
	    MT2063_CalcLO2Mult(&LO2, &Num2, state->AS_Data.f_LO2,
			       state->AS_Data.f_LO2_Step, state->AS_Data.f_ref);
4128 4129 4130 4131

	/*
	 **  Check the upconverter and downconverter frequency ranges
	 */
4132 4133
	if ((state->AS_Data.f_LO1 < MT2063_MIN_UPC_FREQ)
	    || (state->AS_Data.f_LO1 > MT2063_MAX_UPC_FREQ))
4134
		status |= MT2063_UPC_RANGE;
4135 4136
	if ((state->AS_Data.f_LO2 < MT2063_MIN_DNC_FREQ)
	    || (state->AS_Data.f_LO2 > MT2063_MAX_DNC_FREQ))
4137 4138
		status |= MT2063_DNC_RANGE;
	/*  LO2 Lock bit was in a different place for B0 version  */
4139
	if (state->tuner_id == MT2063_B0)
4140 4141 4142 4143 4144 4145
		LO2LK = 0x40;

	/*
	 **  If we have the same LO frequencies and we're already locked,
	 **  then skip re-programming the LO registers.
	 */
4146 4147 4148
	if ((ofLO1 != state->AS_Data.f_LO1)
	    || (ofLO2 != state->AS_Data.f_LO2)
	    || ((state->reg[MT2063_REG_LO_STATUS] & (LO1LK | LO2LK)) !=
4149 4150 4151 4152 4153 4154 4155 4156 4157
		(LO1LK | LO2LK))) {
		/*
		 **  Calculate the FIFFOF register value
		 **
		 **            IF1_Actual
		 **  FIFFOF = ------------ - 8 * FIFFC - 4992
		 **             f_ref/64
		 */
		fiffof =
4158 4159
		    (state->AS_Data.f_LO1 -
		     f_in) / (state->AS_Data.f_ref / 64) - 8 * (u32) fiffc -
4160 4161 4162 4163 4164 4165 4166 4167
		    4992;
		if (fiffof > 0xFF)
			fiffof = 0xFF;

		/*
		 **  Place all of the calculated values into the local tuner
		 **  register fields.
		 */
4168
		if (status >= 0) {
4169 4170 4171
			state->reg[MT2063_REG_LO1CQ_1] = (u8) (LO1 & 0xFF);	/* DIV1q */
			state->reg[MT2063_REG_LO1CQ_2] = (u8) (Num1 & 0x3F);	/* NUM1q */
			state->reg[MT2063_REG_LO2CQ_1] = (u8) (((LO2 & 0x7F) << 1)	/* DIV2q */
4172
								   |(Num2 >> 12));	/* NUM2q (hi) */
4173 4174
			state->reg[MT2063_REG_LO2CQ_2] = (u8) ((Num2 & 0x0FF0) >> 4);	/* NUM2q (mid) */
			state->reg[MT2063_REG_LO2CQ_3] = (u8) (0xE0 | (Num2 & 0x000F));	/* NUM2q (lo) */
4175 4176 4177 4178 4179 4180

			/*
			 ** Now write out the computed register values
			 ** IMPORTANT: There is a required order for writing
			 **            (0x05 must follow all the others).
			 */
4181 4182
			status |= MT2063_WriteSub(state, MT2063_REG_LO1CQ_1, &state->reg[MT2063_REG_LO1CQ_1], 5);	/* 0x01 - 0x05 */
			if (state->tuner_id == MT2063_B0) {
4183
				/* Re-write the one-shot bits to trigger the tune operation */
4184
				status |= MT2063_WriteSub(state, MT2063_REG_LO2CQ_3, &state->reg[MT2063_REG_LO2CQ_3], 1);	/* 0x05 */
4185 4186
			}
			/* Write out the FIFF offset only if it's changing */
4187
			if (state->reg[MT2063_REG_FIFF_OFFSET] !=
4188
			    (u8) fiffof) {
4189
				state->reg[MT2063_REG_FIFF_OFFSET] =
4190
				    (u8) fiffof;
4191
				status |=
4192
				    MT2063_WriteSub(state,
4193
						    MT2063_REG_FIFF_OFFSET,
4194
						    &state->
4195 4196 4197 4198 4199 4200 4201 4202 4203
						    reg[MT2063_REG_FIFF_OFFSET],
						    1);
			}
		}

		/*
		 **  Check for LO's locking
		 */

4204
		if (status >= 0) {
4205
			status |= MT2063_GetLocked(state);
4206 4207
		}
		/*
4208
		 **  If we locked OK, assign calculated data to mt2063_state structure
4209
		 */
4210
		if (status >= 0) {
4211
			state->f_IF1_actual = state->AS_Data.f_LO1 - f_in;
4212 4213 4214 4215 4216 4217
		}
	}

	return (status);
}

4218
static u32 MT_Tune_atv(void *h, u32 f_in, u32 bw_in,
4219 4220 4221
		    enum MTTune_atv_standard tv_type)
{

4222
	u32 status = 0;
4223

4224 4225 4226 4227 4228 4229
	s32 pict_car = 0;
	s32 pict2chanb_vsb = 0;
	s32 pict2chanb_snd = 0;
	s32 pict2snd1 = 0;
	s32 pict2snd2 = 0;
	s32 ch_bw = 0;
4230

4231 4232 4233
	s32 if_mid = 0;
	s32 rcvr_mode = 0;
	u32 mode_get = 0;
4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344

	switch (tv_type) {
	case MTTUNEA_PAL_B:{
			pict_car = 38900000;
			ch_bw = 8000000;
			pict2chanb_vsb = -1250000;
			pict2snd1 = 5500000;
			pict2snd2 = 5742000;
			rcvr_mode = 1;
			break;
		}
	case MTTUNEA_PAL_G:{
			pict_car = 38900000;
			ch_bw = 7000000;
			pict2chanb_vsb = -1250000;
			pict2snd1 = 5500000;
			pict2snd2 = 0;
			rcvr_mode = 1;
			break;
		}
	case MTTUNEA_PAL_I:{
			pict_car = 38900000;
			ch_bw = 8000000;
			pict2chanb_vsb = -1250000;
			pict2snd1 = 6000000;
			pict2snd2 = 0;
			rcvr_mode = 1;
			break;
		}
	case MTTUNEA_PAL_L:{
			pict_car = 38900000;
			ch_bw = 8000000;
			pict2chanb_vsb = -1250000;
			pict2snd1 = 6500000;
			pict2snd2 = 0;
			rcvr_mode = 1;
			break;
		}
	case MTTUNEA_PAL_MN:{
			pict_car = 38900000;
			ch_bw = 6000000;
			pict2chanb_vsb = -1250000;
			pict2snd1 = 4500000;
			pict2snd2 = 0;
			rcvr_mode = 1;
			break;
		}
	case MTTUNEA_PAL_DK:{
			pict_car = 38900000;
			ch_bw = 8000000;
			pict2chanb_vsb = -1250000;
			pict2snd1 = 6500000;
			pict2snd2 = 0;
			rcvr_mode = 1;
			break;
		}
	case MTTUNEA_DIGITAL:{
			pict_car = 36125000;
			ch_bw = 8000000;
			pict2chanb_vsb = -(ch_bw / 2);
			pict2snd1 = 0;
			pict2snd2 = 0;
			rcvr_mode = 2;
			break;
		}
	case MTTUNEA_FMRADIO:{
			pict_car = 38900000;
			ch_bw = 8000000;
			pict2chanb_vsb = -(ch_bw / 2);
			pict2snd1 = 0;
			pict2snd2 = 0;
			rcvr_mode = 4;
			//f_in -= 2900000;
			break;
		}
	case MTTUNEA_DVBC:{
			pict_car = 36125000;
			ch_bw = 8000000;
			pict2chanb_vsb = -(ch_bw / 2);
			pict2snd1 = 0;
			pict2snd2 = 0;
			rcvr_mode = MT2063_CABLE_QAM;
			break;
		}
	case MTTUNEA_DVBT:{
			pict_car = 36125000;
			ch_bw = bw_in;	//8000000
			pict2chanb_vsb = -(ch_bw / 2);
			pict2snd1 = 0;
			pict2snd2 = 0;
			rcvr_mode = MT2063_OFFAIR_COFDM;
			break;
		}
	case MTTUNEA_UNKNOWN:
		break;
	default:
		break;
	}

	pict2chanb_snd = pict2chanb_vsb - ch_bw;
	if_mid = pict_car - (pict2chanb_vsb + (ch_bw / 2));

	status |= MT2063_SetParam(h, MT2063_STEPSIZE, 125000);
	status |= MT2063_SetParam(h, MT2063_OUTPUT_FREQ, if_mid);
	status |= MT2063_SetParam(h, MT2063_OUTPUT_BW, ch_bw);
	status |= MT2063_GetParam(h, MT2063_RCVR_MODE, &mode_get);

	status |= MT2063_SetParam(h, MT2063_RCVR_MODE, rcvr_mode);
	status |= MT2063_Tune(h, (f_in + (pict2chanb_vsb + (ch_bw / 2))));
	status |= MT2063_GetParam(h, MT2063_RCVR_MODE, &mode_get);

4345
	return (u32) status;
4346 4347 4348 4349
}

static int mt2063_init(struct dvb_frontend *fe)
{
4350
	u32 status = -EINVAL;
4351 4352
	struct mt2063_state *state = fe->tuner_priv;

4353 4354 4355
	status = MT2063_Open(fe);
	status |= MT2063_SoftwareShutdown(state, 1);
	status |= MT2063_ClearPowerMaskBits(state, MT2063_ALL_SD);
4356

4357
	if (0 != status) {
4358 4359 4360 4361 4362 4363 4364 4365 4366
		printk("%s %d error status = 0x%x!!\n", __func__, __LINE__,
		       status);
		return -1;
	}

	return 0;
}

static int mt2063_get_status(struct dvb_frontend *fe, u32 * status)
4367
{
4368 4369 4370 4371 4372
	int rc = 0;

	//get tuner lock status

	return rc;
4373
}
4374 4375

static int mt2063_get_state(struct dvb_frontend *fe,
4376
			    enum tuner_param param, struct tuner_state *tunstate)
4377
{
4378
	struct mt2063_state *state = fe->tuner_priv;
4379

4380 4381
	switch (param) {
	case DVBFE_TUNER_FREQUENCY:
4382
		//get frequency
4383 4384 4385 4386 4387 4388
		break;
	case DVBFE_TUNER_TUNERSTEP:
		break;
	case DVBFE_TUNER_IFFREQ:
		break;
	case DVBFE_TUNER_BANDWIDTH:
4389
		//get bandwidth
4390
		break;
4391
	case DVBFE_TUNER_REFCLOCK:
4392
		tunstate->refclock = (u32) MT2063_GetLocked(state);
4393 4394 4395 4396 4397
		break;
	default:
		break;
	}

4398
	return (int)tunstate->refclock;
4399 4400 4401
}

static int mt2063_set_state(struct dvb_frontend *fe,
4402
			    enum tuner_param param, struct tuner_state *tunstate)
4403
{
4404
	struct mt2063_state *state = fe->tuner_priv;
4405
	u32 status = 0;
4406

4407 4408
	switch (param) {
	case DVBFE_TUNER_FREQUENCY:
4409 4410 4411
		//set frequency

		status =
4412 4413 4414
		    MT_Tune_atv(state,
				tunstate->frequency, tunstate->bandwidth,
				state->tv_type);
4415

4416
		state->frequency = tunstate->frequency;
4417 4418 4419 4420 4421 4422
		break;
	case DVBFE_TUNER_TUNERSTEP:
		break;
	case DVBFE_TUNER_IFFREQ:
		break;
	case DVBFE_TUNER_BANDWIDTH:
4423
		//set bandwidth
4424
		state->bandwidth = tunstate->bandwidth;
4425 4426 4427 4428 4429
		break;
	case DVBFE_TUNER_REFCLOCK:

		break;
	case DVBFE_TUNER_OPEN:
4430
		status = MT2063_Open(fe);
4431 4432
		break;
	case DVBFE_TUNER_SOFTWARE_SHUTDOWN:
4433
		status = MT2063_SoftwareShutdown(state, 1);
4434 4435 4436
		break;
	case DVBFE_TUNER_CLEAR_POWER_MASKBITS:
		status =
4437
		    MT2063_ClearPowerMaskBits(state,
4438
					      MT2063_ALL_SD);
4439 4440 4441 4442 4443
		break;
	default:
		break;
	}

4444 4445 4446 4447
	return (int)status;
}

static int mt2063_release(struct dvb_frontend *fe)
4448
{
4449
	struct mt2063_state *state = fe->tuner_priv;
4450 4451 4452 4453 4454

	fe->tuner_priv = NULL;
	kfree(state);

	return 0;
4455 4456 4457
}

static struct dvb_tuner_ops mt2063_ops = {
4458
	.info = {
4459 4460 4461 4462 4463 4464 4465
		 .name = "MT2063 Silicon Tuner",
		 .frequency_min = 45000000,
		 .frequency_max = 850000000,
		 .frequency_step = 0,
		 },

	.init = mt2063_init,
4466
	.sleep = MT2063_Sleep,
4467 4468 4469 4470
	.get_status = mt2063_get_status,
	.get_state = mt2063_get_state,
	.set_state = mt2063_set_state,
	.release = mt2063_release
4471 4472
};

4473 4474 4475
struct dvb_frontend *mt2063_attach(struct dvb_frontend *fe,
				   struct mt2063_config *config,
				   struct i2c_adapter *i2c)
4476
{
4477
	struct mt2063_state *state = NULL;
4478

4479
	state = kzalloc(sizeof(struct mt2063_state), GFP_KERNEL);
4480 4481 4482
	if (state == NULL)
		goto error;

4483 4484 4485 4486
	state->config = config;
	state->i2c = i2c;
	state->frontend = fe;
	state->reference = config->refclock / 1000;	/* kHz */
4487
	state->MT2063_init = false;
4488 4489
	fe->tuner_priv = state;
	fe->ops.tuner_ops = mt2063_ops;
4490

4491
	printk("%s: Attaching MT2063 \n", __func__);
4492 4493 4494 4495 4496 4497 4498
	return fe;

error:
	kfree(state);
	return NULL;
}

4499
EXPORT_SYMBOL(mt2063_attach);
4500 4501
MODULE_PARM_DESC(verbose, "Set Verbosity level");

4502 4503 4504
MODULE_AUTHOR("Henry");
MODULE_DESCRIPTION("MT2063 Silicon tuner");
MODULE_LICENSE("GPL");