cx231xx-avcore.c 92.6 KB
Newer Older
1
/*
2 3
   cx231xx_avcore.c - driver for Conexant Cx23100/101/102
		      USB video capture devices
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

   Copyright (C) 2008 <srinivasa.deevi at conexant dot com>

   This program contains the specific code to control the avdecoder chip and
   other related usb control functions for cx231xx based chipset.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/init.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/bitmap.h>
#include <linux/usb.h>
#include <linux/i2c.h>
#include <linux/mm.h>
#include <linux/mutex.h>
34
#include <media/tuner.h>
35 36 37 38 39 40

#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-chip-ident.h>

#include "cx231xx.h"
41
#include "cx231xx-dif.h"
42

43
#define TUNER_MODE_FM_RADIO 0
44
/******************************************************************************
45 46 47 48 49 50 51 52 53 54 55
			-: BLOCK ARRANGEMENT :-
	I2S block ----------------------|
	[I2S audio]			|
					|
	Analog Front End --> Direct IF -|-> Cx25840 --> Audio
	[video & audio]			|   [Audio]
					|
					|-> Cx25840 --> Video
					    [Video]

*******************************************************************************/
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
/******************************************************************************
 *                    VERVE REGISTER                                          *
	* 								      *
 ******************************************************************************/
static int verve_write_byte(struct cx231xx *dev, u8 saddr, u8 data)
{
	return cx231xx_write_i2c_data(dev, VERVE_I2C_ADDRESS,
					saddr, 1, data, 1);
}

static int verve_read_byte(struct cx231xx *dev, u8 saddr, u8 *data)
{
	int status;
	u32 temp = 0;

	status = cx231xx_read_i2c_data(dev, VERVE_I2C_ADDRESS,
					saddr, 1, &temp, 1);
	*data = (u8) temp;
	return status;
}
void initGPIO(struct cx231xx *dev)
{
	u32 _gpio_direction = 0;
	u32 value = 0;
	u8 val = 0;

	_gpio_direction = _gpio_direction & 0xFC0003FF;
	_gpio_direction = _gpio_direction | 0x03FDFC00;
	cx231xx_send_gpio_cmd(dev, _gpio_direction, (u8 *)&value, 4, 0, 0);

	verve_read_byte(dev, 0x07, &val);
	cx231xx_info(" verve_read_byte address0x07=0x%x\n", val);
	verve_write_byte(dev, 0x07, 0xF4);
	verve_read_byte(dev, 0x07, &val);
	cx231xx_info(" verve_read_byte address0x07=0x%x\n", val);

	cx231xx_capture_start(dev, 1, 2);

	cx231xx_mode_register(dev, EP_MODE_SET, 0x0500FE00);
	cx231xx_mode_register(dev, GBULK_BIT_EN, 0xFFFDFFFF);

}
void uninitGPIO(struct cx231xx *dev)
{
	u8 value[4] = { 0, 0, 0, 0 };

	cx231xx_capture_start(dev, 0, 2);
	verve_write_byte(dev, 0x07, 0x14);
	cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
			0x68, value, 4);
}
107 108 109 110

/******************************************************************************
 *                    A F E - B L O C K    C O N T R O L   functions          *
 * 				[ANALOG FRONT END]			      *
111
 ******************************************************************************/
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
static int afe_write_byte(struct cx231xx *dev, u16 saddr, u8 data)
{
	return cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
					saddr, 2, data, 1);
}

static int afe_read_byte(struct cx231xx *dev, u16 saddr, u8 *data)
{
	int status;
	u32 temp = 0;

	status = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
					saddr, 2, &temp, 1);
	*data = (u8) temp;
	return status;
}

int cx231xx_afe_init_super_block(struct cx231xx *dev, u32 ref_count)
130
{
131 132
	int status = 0;
	u8 temp = 0;
133
	u8 afe_power_status = 0;
134 135 136 137
	int i = 0;

	/* super block initialize */
	temp = (u8) (ref_count & 0xff);
138
	status = afe_write_byte(dev, SUP_BLK_TUNE2, temp);
139 140
	if (status < 0)
		return status;
141

142
	status = afe_read_byte(dev, SUP_BLK_TUNE2, &afe_power_status);
143 144
	if (status < 0)
		return status;
145 146 147

	temp = (u8) ((ref_count & 0x300) >> 8);
	temp |= 0x40;
148
	status = afe_write_byte(dev, SUP_BLK_TUNE1, temp);
149 150 151
	if (status < 0)
		return status;

152
	status = afe_write_byte(dev, SUP_BLK_PLL2, 0x0f);
153 154
	if (status < 0)
		return status;
155 156

	/* enable pll     */
157 158
	while (afe_power_status != 0x18) {
		status = afe_write_byte(dev, SUP_BLK_PWRDN, 0x18);
159 160 161 162 163 164
		if (status < 0) {
			cx231xx_info(
			": Init Super Block failed in send cmd\n");
			break;
		}

165 166
		status = afe_read_byte(dev, SUP_BLK_PWRDN, &afe_power_status);
		afe_power_status &= 0xff;
167
		if (status < 0) {
168
			cx231xx_info(
169
			": Init Super Block failed in receive cmd\n");
170 171 172 173
			break;
		}
		i++;
		if (i == 10) {
174 175
			cx231xx_info(
			": Init Super Block force break in loop !!!!\n");
176 177 178 179 180 181 182 183 184
			status = -1;
			break;
		}
	}

	if (status < 0)
		return status;

	/* start tuning filter */
185
	status = afe_write_byte(dev, SUP_BLK_TUNE3, 0x40);
186 187 188
	if (status < 0)
		return status;

189 190 191
	msleep(5);

	/* exit tuning */
192
	status = afe_write_byte(dev, SUP_BLK_TUNE3, 0x00);
193 194

	return status;
195 196
}

197
int cx231xx_afe_init_channels(struct cx231xx *dev)
198
{
199 200 201
	int status = 0;

	/* power up all 3 channels, clear pd_buffer */
202 203 204
	status = afe_write_byte(dev, ADC_PWRDN_CLAMP_CH1, 0x00);
	status = afe_write_byte(dev, ADC_PWRDN_CLAMP_CH2, 0x00);
	status = afe_write_byte(dev, ADC_PWRDN_CLAMP_CH3, 0x00);
205 206

	/* Enable quantizer calibration */
207
	status = afe_write_byte(dev, ADC_COM_QUANT, 0x02);
208 209

	/* channel initialize, force modulator (fb) reset */
210 211 212
	status = afe_write_byte(dev, ADC_FB_FRCRST_CH1, 0x17);
	status = afe_write_byte(dev, ADC_FB_FRCRST_CH2, 0x17);
	status = afe_write_byte(dev, ADC_FB_FRCRST_CH3, 0x17);
213 214

	/* start quantilizer calibration  */
215 216 217
	status = afe_write_byte(dev, ADC_CAL_ATEST_CH1, 0x10);
	status = afe_write_byte(dev, ADC_CAL_ATEST_CH2, 0x10);
	status = afe_write_byte(dev, ADC_CAL_ATEST_CH3, 0x10);
218 219 220
	msleep(5);

	/* exit modulator (fb) reset */
221 222 223
	status = afe_write_byte(dev, ADC_FB_FRCRST_CH1, 0x07);
	status = afe_write_byte(dev, ADC_FB_FRCRST_CH2, 0x07);
	status = afe_write_byte(dev, ADC_FB_FRCRST_CH3, 0x07);
224 225

	/* enable the pre_clamp in each channel for single-ended input */
226 227 228
	status = afe_write_byte(dev, ADC_NTF_PRECLMP_EN_CH1, 0xf0);
	status = afe_write_byte(dev, ADC_NTF_PRECLMP_EN_CH2, 0xf0);
	status = afe_write_byte(dev, ADC_NTF_PRECLMP_EN_CH3, 0xf0);
229 230

	/* use diode instead of resistor, so set term_en to 0, res_en to 0  */
231
	status = cx231xx_reg_mask_write(dev, AFE_DEVICE_ADDRESS, 8,
232
				   ADC_QGAIN_RES_TRM_CH1, 3, 7, 0x00);
233
	status = cx231xx_reg_mask_write(dev, AFE_DEVICE_ADDRESS, 8,
234
				   ADC_QGAIN_RES_TRM_CH2, 3, 7, 0x00);
235
	status = cx231xx_reg_mask_write(dev, AFE_DEVICE_ADDRESS, 8,
236 237 238
				   ADC_QGAIN_RES_TRM_CH3, 3, 7, 0x00);

	/* dynamic element matching off */
239 240 241
	status = afe_write_byte(dev, ADC_DCSERVO_DEM_CH1, 0x03);
	status = afe_write_byte(dev, ADC_DCSERVO_DEM_CH2, 0x03);
	status = afe_write_byte(dev, ADC_DCSERVO_DEM_CH3, 0x03);
242 243

	return status;
244 245
}

246
int cx231xx_afe_setup_AFE_for_baseband(struct cx231xx *dev)
247
{
248
	u8 c_value = 0;
249
	int status = 0;
250

251
	status = afe_read_byte(dev, ADC_PWRDN_CLAMP_CH2, &c_value);
252
	c_value &= (~(0x50));
253
	status = afe_write_byte(dev, ADC_PWRDN_CLAMP_CH2, c_value);
254

255
	return status;
256 257 258
}

/*
259 260 261 262
	The Analog Front End in Cx231xx has 3 channels. These
	channels are used to share between different inputs
	like tuner, s-video and composite inputs.

263 264 265 266
	channel 1 ----- pin 1  to pin4(in reg is 1-4)
	channel 2 ----- pin 5  to pin8(in reg is 5-8)
	channel 3 ----- pin 9 to pin 12(in reg is 9-11)
*/
267
int cx231xx_afe_set_input_mux(struct cx231xx *dev, u32 input_mux)
268
{
269 270 271 272
	u8 ch1_setting = (u8) input_mux;
	u8 ch2_setting = (u8) (input_mux >> 8);
	u8 ch3_setting = (u8) (input_mux >> 16);
	int status = 0;
273
	u8 value = 0;
274 275

	if (ch1_setting != 0) {
276
		status = afe_read_byte(dev, ADC_INPUT_CH1, &value);
277 278 279
		value &= (!INPUT_SEL_MASK);
		value |= (ch1_setting - 1) << 4;
		value &= 0xff;
280
		status = afe_write_byte(dev, ADC_INPUT_CH1, value);
281 282 283
	}

	if (ch2_setting != 0) {
284
		status = afe_read_byte(dev, ADC_INPUT_CH2, &value);
285 286 287
		value &= (!INPUT_SEL_MASK);
		value |= (ch2_setting - 1) << 4;
		value &= 0xff;
288
		status = afe_write_byte(dev, ADC_INPUT_CH2, value);
289 290
	}

291 292
	/* For ch3_setting, the value to put in the register is
	   7 less than the input number */
293
	if (ch3_setting != 0) {
294
		status = afe_read_byte(dev, ADC_INPUT_CH3, &value);
295 296 297
		value &= (!INPUT_SEL_MASK);
		value |= (ch3_setting - 1) << 4;
		value &= 0xff;
298
		status = afe_write_byte(dev, ADC_INPUT_CH3, value);
299 300 301
	}

	return status;
302 303
}

304
int cx231xx_afe_set_mode(struct cx231xx *dev, enum AFE_MODE mode)
305
{
306 307
	int status = 0;

308 309 310 311 312
	/*
	* FIXME: We need to implement the AFE code for LOW IF and for HI IF.
	* Currently, only baseband works.
	*/

313 314
	switch (mode) {
	case AFE_MODE_LOW_IF:
315
		cx231xx_Setup_AFE_for_LowIF(dev);
316 317
		break;
	case AFE_MODE_BASEBAND:
318
		status = cx231xx_afe_setup_AFE_for_baseband(dev);
319 320 321 322 323 324 325 326 327 328 329 330
		break;
	case AFE_MODE_EU_HI_IF:
		/* SetupAFEforEuHiIF(); */
		break;
	case AFE_MODE_US_HI_IF:
		/* SetupAFEforUsHiIF(); */
		break;
	case AFE_MODE_JAPAN_HI_IF:
		/* SetupAFEforJapanHiIF(); */
		break;
	}

331
	if ((mode != dev->afe_mode) &&
332
		(dev->video_input == CX231XX_VMUX_TELEVISION))
333
		status = cx231xx_afe_adjust_ref_count(dev,
334 335
						     CX231XX_VMUX_TELEVISION);

336
	dev->afe_mode = mode;
337 338

	return status;
339 340
}

341
int cx231xx_afe_update_power_control(struct cx231xx *dev,
342
					enum AV_MODE avmode)
343
{
344
	u8 afe_power_status = 0;
345 346 347
	int status = 0;

	switch (dev->model) {
348
	case CX231XX_BOARD_CNXT_CARRAERA:
349
	case CX231XX_BOARD_CNXT_RDE_250:
350
	case CX231XX_BOARD_CNXT_SHELBY:
351
	case CX231XX_BOARD_CNXT_RDU_250:
352 353 354
	case CX231XX_BOARD_CNXT_RDE_253S:
	case CX231XX_BOARD_CNXT_RDU_253S:
	case CX231XX_BOARD_CNXT_VIDEO_GRABBER:
355
	case CX231XX_BOARD_HAUPPAUGE_EXETER:
356
		if (avmode == POLARIS_AVMODE_ANALOGT_TV) {
357
			while (afe_power_status != (FLD_PWRDN_TUNING_BIAS |
358
						FLD_PWRDN_ENABLE_PLL)) {
359
				status = afe_write_byte(dev, SUP_BLK_PWRDN,
360
							FLD_PWRDN_TUNING_BIAS |
361 362 363
							FLD_PWRDN_ENABLE_PLL);
				status |= afe_read_byte(dev, SUP_BLK_PWRDN,
							&afe_power_status);
364 365 366 367
				if (status < 0)
					break;
			}

368 369 370 371 372 373
			status = afe_write_byte(dev, ADC_PWRDN_CLAMP_CH1,
							0x00);
			status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH2,
							0x00);
			status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH3,
							0x00);
374
		} else if (avmode == POLARIS_AVMODE_DIGITAL) {
375 376 377 378 379 380 381 382 383 384
			status = afe_write_byte(dev, ADC_PWRDN_CLAMP_CH1,
							0x70);
			status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH2,
							0x70);
			status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH3,
							0x70);

			status |= afe_read_byte(dev, SUP_BLK_PWRDN,
						  &afe_power_status);
			afe_power_status |= FLD_PWRDN_PD_BANDGAP |
385 386
						FLD_PWRDN_PD_BIAS |
						FLD_PWRDN_PD_TUNECK;
387 388
			status |= afe_write_byte(dev, SUP_BLK_PWRDN,
						   afe_power_status);
389
		} else if (avmode == POLARIS_AVMODE_ENXTERNAL_AV) {
390
			while (afe_power_status != (FLD_PWRDN_TUNING_BIAS |
391
						FLD_PWRDN_ENABLE_PLL)) {
392
				status = afe_write_byte(dev, SUP_BLK_PWRDN,
393
							FLD_PWRDN_TUNING_BIAS |
394 395 396
							FLD_PWRDN_ENABLE_PLL);
				status |= afe_read_byte(dev, SUP_BLK_PWRDN,
							&afe_power_status);
397 398 399 400
				if (status < 0)
					break;
			}

401 402 403 404 405 406
			status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH1,
						0x00);
			status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH2,
						0x00);
			status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH3,
						0x00);
407 408 409 410 411 412 413
		} else {
			cx231xx_info("Invalid AV mode input\n");
			status = -1;
		}
		break;
	default:
		if (avmode == POLARIS_AVMODE_ANALOGT_TV) {
414
			while (afe_power_status != (FLD_PWRDN_TUNING_BIAS |
415
						FLD_PWRDN_ENABLE_PLL)) {
416
				status = afe_write_byte(dev, SUP_BLK_PWRDN,
417
							FLD_PWRDN_TUNING_BIAS |
418 419 420
							FLD_PWRDN_ENABLE_PLL);
				status |= afe_read_byte(dev, SUP_BLK_PWRDN,
							&afe_power_status);
421 422 423 424
				if (status < 0)
					break;
			}

425 426 427 428 429 430
			status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH1,
							0x40);
			status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH2,
							0x40);
			status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH3,
							0x00);
431
		} else if (avmode == POLARIS_AVMODE_DIGITAL) {
432 433 434 435 436 437 438 439 440 441
			status = afe_write_byte(dev, ADC_PWRDN_CLAMP_CH1,
							0x70);
			status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH2,
							0x70);
			status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH3,
							0x70);

			status |= afe_read_byte(dev, SUP_BLK_PWRDN,
						       &afe_power_status);
			afe_power_status |= FLD_PWRDN_PD_BANDGAP |
442 443
						FLD_PWRDN_PD_BIAS |
						FLD_PWRDN_PD_TUNECK;
444 445
			status |= afe_write_byte(dev, SUP_BLK_PWRDN,
							afe_power_status);
446
		} else if (avmode == POLARIS_AVMODE_ENXTERNAL_AV) {
447
			while (afe_power_status != (FLD_PWRDN_TUNING_BIAS |
448
						FLD_PWRDN_ENABLE_PLL)) {
449
				status = afe_write_byte(dev, SUP_BLK_PWRDN,
450
							FLD_PWRDN_TUNING_BIAS |
451 452 453
							FLD_PWRDN_ENABLE_PLL);
				status |= afe_read_byte(dev, SUP_BLK_PWRDN,
							&afe_power_status);
454 455 456 457
				if (status < 0)
					break;
			}

458 459 460 461 462 463
			status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH1,
							0x00);
			status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH2,
							0x00);
			status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH3,
							0x40);
464 465 466 467 468 469 470
		} else {
			cx231xx_info("Invalid AV mode input\n");
			status = -1;
		}
	}			/* switch  */

	return status;
471 472
}

473
int cx231xx_afe_adjust_ref_count(struct cx231xx *dev, u32 video_input)
474
{
475 476
	u8 input_mode = 0;
	u8 ntf_mode = 0;
477 478 479 480 481
	int status = 0;

	dev->video_input = video_input;

	if (video_input == CX231XX_VMUX_TELEVISION) {
482 483 484
		status = afe_read_byte(dev, ADC_INPUT_CH3, &input_mode);
		status = afe_read_byte(dev, ADC_NTF_PRECLMP_EN_CH3,
					&ntf_mode);
485
	} else {
486 487 488
		status = afe_read_byte(dev, ADC_INPUT_CH1, &input_mode);
		status = afe_read_byte(dev, ADC_NTF_PRECLMP_EN_CH1,
					&ntf_mode);
489
	}
490

491 492 493 494
	input_mode = (ntf_mode & 0x3) | ((input_mode & 0x6) << 1);

	switch (input_mode) {
	case SINGLE_ENDED:
495
		dev->afe_ref_count = 0x23C;
496 497
		break;
	case LOW_IF:
498
		dev->afe_ref_count = 0x24C;
499 500
		break;
	case EU_IF:
501
		dev->afe_ref_count = 0x258;
502 503
		break;
	case US_IF:
504
		dev->afe_ref_count = 0x260;
505 506 507 508 509
		break;
	default:
		break;
	}

510
	status = cx231xx_afe_init_super_block(dev, dev->afe_ref_count);
511

512 513
	return status;
}
514

515 516
/******************************************************************************
 *     V I D E O / A U D I O    D E C O D E R    C O N T R O L   functions    *
517
 ******************************************************************************/
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
static int vid_blk_write_byte(struct cx231xx *dev, u16 saddr, u8 data)
{
	return cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
					saddr, 2, data, 1);
}

static int vid_blk_read_byte(struct cx231xx *dev, u16 saddr, u8 *data)
{
	int status;
	u32 temp = 0;

	status = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
					saddr, 2, &temp, 1);
	*data = (u8) temp;
	return status;
}

static int vid_blk_write_word(struct cx231xx *dev, u16 saddr, u32 data)
{
	return cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
					saddr, 2, data, 4);
}

static int vid_blk_read_word(struct cx231xx *dev, u16 saddr, u32 *data)
{
	return cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
					saddr, 2, data, 4);
}
546 547 548 549 550 551 552 553 554 555 556
int cx231xx_check_fw(struct cx231xx *dev)
{
	u8 temp = 0;
	int status = 0;
	status = vid_blk_read_byte(dev, DL_CTL_ADDRESS_LOW, &temp);
	if (status < 0)
		return status;
	else
		return temp;

}
557

558 559
int cx231xx_set_video_input_mux(struct cx231xx *dev, u8 input)
{
560 561 562 563 564 565 566
	int status = 0;

	switch (INPUT(input)->type) {
	case CX231XX_VMUX_COMPOSITE1:
	case CX231XX_VMUX_SVIDEO:
		if ((dev->current_pcb_config.type == USB_BUS_POWER) &&
		    (dev->power_mode != POLARIS_AVMODE_ENXTERNAL_AV)) {
567 568 569
			/* External AV */
			status = cx231xx_set_power_mode(dev,
					POLARIS_AVMODE_ENXTERNAL_AV);
570
			if (status < 0) {
571 572 573
				cx231xx_errdev("%s: set_power_mode : Failed to"
						" set Power - errCode [%d]!\n",
						__func__, status);
574 575 576
				return status;
			}
		}
577 578 579
		status = cx231xx_set_decoder_video_input(dev,
							 INPUT(input)->type,
							 INPUT(input)->vmux);
580 581 582 583 584
		break;
	case CX231XX_VMUX_TELEVISION:
	case CX231XX_VMUX_CABLE:
		if ((dev->current_pcb_config.type == USB_BUS_POWER) &&
		    (dev->power_mode != POLARIS_AVMODE_ANALOGT_TV)) {
585 586 587
			/* Tuner */
			status = cx231xx_set_power_mode(dev,
						POLARIS_AVMODE_ANALOGT_TV);
588
			if (status < 0) {
589 590 591
				cx231xx_errdev("%s: set_power_mode:Failed"
					" to set Power - errCode [%d]!\n",
					__func__, status);
592 593 594
				return status;
			}
		}
595 596 597 598 599 600
		if (dev->tuner_type == TUNER_NXP_TDA18271)
			status = cx231xx_set_decoder_video_input(dev,
							CX231XX_VMUX_TELEVISION,
							INPUT(input)->vmux);
		else
			status = cx231xx_set_decoder_video_input(dev,
601 602
							CX231XX_VMUX_COMPOSITE1,
							INPUT(input)->vmux);
603

604 605
		break;
	default:
606
		cx231xx_errdev("%s: set_power_mode : Unknown Input %d !\n",
607 608 609 610 611 612 613 614
		     __func__, INPUT(input)->type);
		break;
	}

	/* save the selection */
	dev->video_input = input;

	return status;
615 616
}

617 618
int cx231xx_set_decoder_video_input(struct cx231xx *dev,
				u8 pin_type, u8 input)
619
{
620 621 622 623
	int status = 0;
	u32 value = 0;

	if (pin_type != dev->video_input) {
624
		status = cx231xx_afe_adjust_ref_count(dev, pin_type);
625
		if (status < 0) {
626
			cx231xx_errdev("%s: adjust_ref_count :Failed to set"
627
				"AFE input mux - errCode [%d]!\n",
628
				__func__, status);
629 630 631
			return status;
		}
	}
632

633 634
	/* call afe block to set video inputs */
	status = cx231xx_afe_set_input_mux(dev, input);
635
	if (status < 0) {
636
		cx231xx_errdev("%s: set_input_mux :Failed to set"
637
				" AFE input mux - errCode [%d]!\n",
638
				__func__, status);
639 640 641 642 643
		return status;
	}

	switch (pin_type) {
	case CX231XX_VMUX_COMPOSITE1:
644
		status = vid_blk_read_word(dev, AFE_CTRL, &value);
645 646 647
		value |= (0 << 13) | (1 << 4);
		value &= ~(1 << 5);

648 649 650 651
		/* set [24:23] [22:15] to 0  */
		value &= (~(0x1ff8000));
		/* set FUNC_MODE[24:23] = 2 IF_MOD[22:15] = 0  */
		value |= 0x1000000;
652
		status = vid_blk_write_word(dev, AFE_CTRL, value);
653

654
		status = vid_blk_read_word(dev, OUT_CTRL1, &value);
655
		value |= (1 << 7);
656
		status = vid_blk_write_word(dev, OUT_CTRL1, value);
657 658 659

		/* Set vip 1.1 output mode */
		status = cx231xx_read_modify_write_i2c_dword(dev,
660
							VID_BLK_I2C_ADDRESS,
661 662 663 664 665 666 667
							OUT_CTRL1,
							FLD_OUT_MODE,
							OUT_MODE_VIP11);

		/* Tell DIF object to go to baseband mode  */
		status = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND);
		if (status < 0) {
668 669
			cx231xx_errdev("%s: cx231xx_dif set to By pass"
						   " mode- errCode [%d]!\n",
670 671 672 673 674
				__func__, status);
			return status;
		}

		/* Read the DFE_CTRL1 register */
675
		status = vid_blk_read_word(dev, DFE_CTRL1, &value);
676 677 678 679 680 681 682 683

		/* enable the VBI_GATE_EN */
		value |= FLD_VBI_GATE_EN;

		/* Enable the auto-VGA enable */
		value |= FLD_VGA_AUTO_EN;

		/* Write it back */
684
		status = vid_blk_write_word(dev, DFE_CTRL1, value);
685 686 687

		/* Disable auto config of registers */
		status = cx231xx_read_modify_write_i2c_dword(dev,
688
					VID_BLK_I2C_ADDRESS,
689 690 691 692 693
					MODE_CTRL, FLD_ACFG_DIS,
					cx231xx_set_field(FLD_ACFG_DIS, 1));

		/* Set CVBS input mode */
		status = cx231xx_read_modify_write_i2c_dword(dev,
694
			VID_BLK_I2C_ADDRESS,
695 696 697 698 699 700
			MODE_CTRL, FLD_INPUT_MODE,
			cx231xx_set_field(FLD_INPUT_MODE, INPUT_MODE_CVBS_0));
		break;
	case CX231XX_VMUX_SVIDEO:
		/* Disable the use of  DIF */

701
		status = vid_blk_read_word(dev, AFE_CTRL, &value);
702

703 704 705 706 707
		/* set [24:23] [22:15] to 0 */
		value &= (~(0x1ff8000));
		/* set FUNC_MODE[24:23] = 2
		IF_MOD[22:15] = 0 DCR_BYP_CH2[4:4] = 1; */
		value |= 0x1000010;
708
		status = vid_blk_write_word(dev, AFE_CTRL, value);
709 710 711 712

		/* Tell DIF object to go to baseband mode */
		status = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND);
		if (status < 0) {
713 714
			cx231xx_errdev("%s: cx231xx_dif set to By pass"
						   " mode- errCode [%d]!\n",
715 716 717 718 719
				__func__, status);
			return status;
		}

		/* Read the DFE_CTRL1 register */
720
		status = vid_blk_read_word(dev, DFE_CTRL1, &value);
721 722 723 724 725 726 727 728

		/* enable the VBI_GATE_EN */
		value |= FLD_VBI_GATE_EN;

		/* Enable the auto-VGA enable */
		value |= FLD_VGA_AUTO_EN;

		/* Write it back */
729
		status = vid_blk_write_word(dev, DFE_CTRL1, value);
730 731 732

		/* Disable auto config of registers  */
		status =  cx231xx_read_modify_write_i2c_dword(dev,
733
					VID_BLK_I2C_ADDRESS,
734 735 736 737 738
					MODE_CTRL, FLD_ACFG_DIS,
					cx231xx_set_field(FLD_ACFG_DIS, 1));

		/* Set YC input mode */
		status = cx231xx_read_modify_write_i2c_dword(dev,
739
			VID_BLK_I2C_ADDRESS,
740 741 742 743 744
			MODE_CTRL,
			FLD_INPUT_MODE,
			cx231xx_set_field(FLD_INPUT_MODE, INPUT_MODE_YC_1));

		/* Chroma to ADC2 */
745
		status = vid_blk_read_word(dev, AFE_CTRL, &value);
746 747 748 749 750 751 752
		value |= FLD_CHROMA_IN_SEL;	/* set the chroma in select */

		/* Clear VGA_SEL_CH2 and VGA_SEL_CH3 (bits 7 and 8)
		   This sets them to use video
		   rather than audio.  Only one of the two will be in use. */
		value &= ~(FLD_VGA_SEL_CH2 | FLD_VGA_SEL_CH3);

753
		status = vid_blk_write_word(dev, AFE_CTRL, value);
754

755
		status = cx231xx_afe_set_mode(dev, AFE_MODE_BASEBAND);
756 757 758 759 760
		break;
	case CX231XX_VMUX_TELEVISION:
	case CX231XX_VMUX_CABLE:
	default:
		switch (dev->model) {
761
		case CX231XX_BOARD_CNXT_CARRAERA:
762
		case CX231XX_BOARD_CNXT_RDE_250:
763
		case CX231XX_BOARD_CNXT_SHELBY:
764 765 766
		case CX231XX_BOARD_CNXT_RDU_250:
			/* Disable the use of  DIF   */

767
			status = vid_blk_read_word(dev, AFE_CTRL, &value);
768 769 770
			value |= (0 << 13) | (1 << 4);
			value &= ~(1 << 5);

771 772 773 774
			/* set [24:23] [22:15] to 0 */
			value &= (~(0x1FF8000));
			/* set FUNC_MODE[24:23] = 2 IF_MOD[22:15] = 0 */
			value |= 0x1000000;
775 776 777
			status = vid_blk_write_word(dev, AFE_CTRL, value);

			status = vid_blk_read_word(dev, OUT_CTRL1, &value);
778
			value |= (1 << 7);
779
			status = vid_blk_write_word(dev, OUT_CTRL1, value);
780 781

			/* Set vip 1.1 output mode */
782
			status = cx231xx_read_modify_write_i2c_dword(dev,
783
							VID_BLK_I2C_ADDRESS,
784 785
							OUT_CTRL1, FLD_OUT_MODE,
							OUT_MODE_VIP11);
786

787 788 789
			/* Tell DIF object to go to baseband mode */
			status = cx231xx_dif_set_standard(dev,
							  DIF_USE_BASEBAND);
790
			if (status < 0) {
791 792 793
				cx231xx_errdev("%s: cx231xx_dif set to By pass"
						" mode- errCode [%d]!\n",
						__func__, status);
794 795 796 797
				return status;
			}

			/* Read the DFE_CTRL1 register */
798
			status = vid_blk_read_word(dev, DFE_CTRL1, &value);
799 800 801 802 803 804 805 806

			/* enable the VBI_GATE_EN */
			value |= FLD_VBI_GATE_EN;

			/* Enable the auto-VGA enable */
			value |= FLD_VGA_AUTO_EN;

			/* Write it back */
807
			status = vid_blk_write_word(dev, DFE_CTRL1, value);
808 809

			/* Disable auto config of registers */
810
			status = cx231xx_read_modify_write_i2c_dword(dev,
811
					VID_BLK_I2C_ADDRESS,
812 813
					MODE_CTRL, FLD_ACFG_DIS,
					cx231xx_set_field(FLD_ACFG_DIS, 1));
814 815

			/* Set CVBS input mode */
816
			status = cx231xx_read_modify_write_i2c_dword(dev,
817
				VID_BLK_I2C_ADDRESS,
818
				MODE_CTRL, FLD_INPUT_MODE,
819 820
				cx231xx_set_field(FLD_INPUT_MODE,
						INPUT_MODE_CVBS_0));
821 822 823
			break;
		default:
			/* Enable the DIF for the tuner */
824

825 826
			/* Reinitialize the DIF */
			status = cx231xx_dif_set_standard(dev, dev->norm);
827
			if (status < 0) {
828 829 830
				cx231xx_errdev("%s: cx231xx_dif set to By pass"
						" mode- errCode [%d]!\n",
						__func__, status);
831 832 833
				return status;
			}

834
			/* Make sure bypass is cleared */
835
			status = vid_blk_read_word(dev, DIF_MISC_CTRL, &value);
836 837 838 839 840

			/* Clear the bypass bit */
			value &= ~FLD_DIF_DIF_BYPASS;

			/* Enable the use of the DIF block */
841
			status = vid_blk_write_word(dev, DIF_MISC_CTRL, value);
842

843
			/* Read the DFE_CTRL1 register */
844
			status = vid_blk_read_word(dev, DFE_CTRL1, &value);
845

846 847
			/* Disable the VBI_GATE_EN */
			value &= ~FLD_VBI_GATE_EN;
848

849 850 851
			/* Enable the auto-VGA enable, AGC, and
			   set the skip count to 2 */
			value |= FLD_VGA_AUTO_EN | FLD_AGC_AUTO_EN | 0x00200000;
852 853

			/* Write it back */
854
			status = vid_blk_write_word(dev, DFE_CTRL1, value);
855

856
			/* Wait until AGC locks up */
857
			msleep(1);
858

859 860
			/* Disable the auto-VGA enable AGC */
			value &= ~(FLD_VGA_AUTO_EN);
861

862
			/* Write it back */
863
			status = vid_blk_write_word(dev, DFE_CTRL1, value);
864

865
			/* Enable Polaris B0 AGC output */
866
			status = vid_blk_read_word(dev, PIN_CTRL, &value);
867 868 869
			value |= (FLD_OEF_AGC_RF) |
				 (FLD_OEF_AGC_IFVGA) |
				 (FLD_OEF_AGC_IF);
870
			status = vid_blk_write_word(dev, PIN_CTRL, value);
871 872 873

			/* Set vip 1.1 output mode */
			status = cx231xx_read_modify_write_i2c_dword(dev,
874
						VID_BLK_I2C_ADDRESS,
875 876 877 878 879
						OUT_CTRL1, FLD_OUT_MODE,
						OUT_MODE_VIP11);

			/* Disable auto config of registers */
			status = cx231xx_read_modify_write_i2c_dword(dev,
880
					VID_BLK_I2C_ADDRESS,
881 882 883 884 885
					MODE_CTRL, FLD_ACFG_DIS,
					cx231xx_set_field(FLD_ACFG_DIS, 1));

			/* Set CVBS input mode */
			status = cx231xx_read_modify_write_i2c_dword(dev,
886
				VID_BLK_I2C_ADDRESS,
887
				MODE_CTRL, FLD_INPUT_MODE,
888 889
				cx231xx_set_field(FLD_INPUT_MODE,
						INPUT_MODE_CVBS_0));
890

891 892
			/* Set some bits in AFE_CTRL so that channel 2 or 3
			 * is ready to receive audio */
893 894 895
			/* Clear clamp for channels 2 and 3      (bit 16-17) */
			/* Clear droop comp                      (bit 19-20) */
			/* Set VGA_SEL (for audio control)       (bit 7-8) */
896
			status = vid_blk_read_word(dev, AFE_CTRL, &value);
897

898 899 900 901
			/*Set Func mode:01-DIF 10-baseband 11-YUV*/
			value &= (~(FLD_FUNC_MODE));
			value |= 0x800000;

902 903
			value |= FLD_VGA_SEL_CH3 | FLD_VGA_SEL_CH2;

904
			status = vid_blk_write_word(dev, AFE_CTRL, value);
905 906 907 908 909 910 911 912

			if (dev->tuner_type == TUNER_NXP_TDA18271) {
				status = vid_blk_read_word(dev, PIN_CTRL,
				 &value);
				status = vid_blk_write_word(dev, PIN_CTRL,
				 (value & 0xFFFFFFEF));
			}

913
			break;
914 915 916 917 918 919

		}
		break;
	}

	/* Set raw VBI mode */
920
	status = cx231xx_read_modify_write_i2c_dword(dev,
921
				VID_BLK_I2C_ADDRESS,
922 923
				OUT_CTRL1, FLD_VBIHACTRAW_EN,
				cx231xx_set_field(FLD_VBIHACTRAW_EN, 1));
924

925
	status = vid_blk_read_word(dev, OUT_CTRL1, &value);
926 927
	if (value & 0x02) {
		value |= (1 << 19);
928
		status = vid_blk_write_word(dev, OUT_CTRL1, value);
929 930 931
	}

	return status;
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
void cx231xx_enable656(struct cx231xx *dev)
{
	u8 temp = 0;
	int status;
    /*enable TS1 data[0:7] as output to export 656*/

	status = vid_blk_write_byte(dev, TS1_PIN_CTL0, 0xFF);

    /*enable TS1 clock as output to export 656*/

	status = vid_blk_read_byte(dev, TS1_PIN_CTL1, &temp);
	temp = temp|0x04;

	status = vid_blk_write_byte(dev, TS1_PIN_CTL1, temp);

}
EXPORT_SYMBOL_GPL(cx231xx_enable656);

void cx231xx_disable656(struct cx231xx *dev)
{
	u8 temp = 0;
	int status;


	status = vid_blk_write_byte(dev, TS1_PIN_CTL0, 0x00);

	status = vid_blk_read_byte(dev, TS1_PIN_CTL1, &temp);
	temp = temp&0xFB;

	status = vid_blk_write_byte(dev, TS1_PIN_CTL1, temp);
}
EXPORT_SYMBOL_GPL(cx231xx_disable656);

967
/*
968 969 970
 * Handle any video-mode specific overrides that are different
 * on a per video standards basis after touching the MODE_CTRL
 * register which resets many values for autodetect
971 972 973
 */
int cx231xx_do_mode_ctrl_overrides(struct cx231xx *dev)
{
974 975 976 977 978 979
	int status = 0;

	cx231xx_info("do_mode_ctrl_overrides : 0x%x\n",
		     (unsigned int)dev->norm);

	/* Change the DFE_CTRL3 bp_percent to fix flagging */
980
	status = vid_blk_write_word(dev, DFE_CTRL3, 0xCD3F0280);
981

982
	if (dev->norm & (V4L2_STD_NTSC | V4L2_STD_PAL_M)) {
983 984
		cx231xx_info("do_mode_ctrl_overrides NTSC\n");

985 986 987
		/* Move the close caption lines out of active video,
		   adjust the active video start point */
		status = cx231xx_read_modify_write_i2c_dword(dev,
988
							VID_BLK_I2C_ADDRESS,
989 990
							VERT_TIM_CTRL,
							FLD_VBLANK_CNT, 0x18);
991
		status = cx231xx_read_modify_write_i2c_dword(dev,
992
							VID_BLK_I2C_ADDRESS,
993 994 995
							VERT_TIM_CTRL,
							FLD_VACTIVE_CNT,
							0x1E6000);
996
		status = cx231xx_read_modify_write_i2c_dword(dev,
997
							VID_BLK_I2C_ADDRESS,
998 999
							VERT_TIM_CTRL,
							FLD_V656BLANK_CNT,
1000
							0x1C000000);
1001

1002
		status = cx231xx_read_modify_write_i2c_dword(dev,
1003
							VID_BLK_I2C_ADDRESS,
1004 1005 1006 1007
							HORIZ_TIM_CTRL,
							FLD_HBLANK_CNT,
							cx231xx_set_field
							(FLD_HBLANK_CNT, 0x79));
1008

1009 1010 1011
	} else if (dev->norm & V4L2_STD_SECAM) {
		cx231xx_info("do_mode_ctrl_overrides SECAM\n");
		status =  cx231xx_read_modify_write_i2c_dword(dev,
1012
							VID_BLK_I2C_ADDRESS,
1013 1014
							VERT_TIM_CTRL,
							FLD_VBLANK_CNT, 0x24);
1015 1016 1017 1018 1019 1020 1021
		status = cx231xx_read_modify_write_i2c_dword(dev,
							VID_BLK_I2C_ADDRESS,
							VERT_TIM_CTRL,
							FLD_V656BLANK_CNT,
							cx231xx_set_field
							(FLD_V656BLANK_CNT,
							0x28));
1022
		/* Adjust the active video horizontal start point */
1023
		status = cx231xx_read_modify_write_i2c_dword(dev,
1024
							VID_BLK_I2C_ADDRESS,
1025 1026 1027 1028
							HORIZ_TIM_CTRL,
							FLD_HBLANK_CNT,
							cx231xx_set_field
							(FLD_HBLANK_CNT, 0x85));
1029 1030 1031
	} else {
		cx231xx_info("do_mode_ctrl_overrides PAL\n");
		status = cx231xx_read_modify_write_i2c_dword(dev,
1032
							VID_BLK_I2C_ADDRESS,
1033 1034
							VERT_TIM_CTRL,
							FLD_VBLANK_CNT, 0x24);
1035 1036 1037 1038 1039 1040 1041
		status = cx231xx_read_modify_write_i2c_dword(dev,
							VID_BLK_I2C_ADDRESS,
							VERT_TIM_CTRL,
							FLD_V656BLANK_CNT,
							cx231xx_set_field
							(FLD_V656BLANK_CNT,
							0x28));
1042
		/* Adjust the active video horizontal start point */
1043
		status = cx231xx_read_modify_write_i2c_dword(dev,
1044
							VID_BLK_I2C_ADDRESS,
1045 1046 1047 1048
							HORIZ_TIM_CTRL,
							FLD_HBLANK_CNT,
							cx231xx_set_field
							(FLD_HBLANK_CNT, 0x85));
1049

1050 1051 1052
	}

	return status;
1053 1054
}

1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
int cx231xx_unmute_audio(struct cx231xx *dev)
{
	return vid_blk_write_byte(dev, PATH1_VOL_CTL, 0x24);
}
EXPORT_SYMBOL_GPL(cx231xx_unmute_audio);

int stopAudioFirmware(struct cx231xx *dev)
{
	return vid_blk_write_byte(dev, DL_CTL_CONTROL, 0x03);
}

int restartAudioFirmware(struct cx231xx *dev)
{
	return vid_blk_write_byte(dev, DL_CTL_CONTROL, 0x13);
}

1071 1072
int cx231xx_set_audio_input(struct cx231xx *dev, u8 input)
{
1073 1074 1075 1076 1077 1078 1079 1080
	int status = 0;
	enum AUDIO_INPUT ainput = AUDIO_INPUT_LINE;

	switch (INPUT(input)->amux) {
	case CX231XX_AMUX_VIDEO:
		ainput = AUDIO_INPUT_TUNER_TV;
		break;
	case CX231XX_AMUX_LINE_IN:
1081
		status = cx231xx_i2s_blk_set_audio_input(dev, input);
1082 1083 1084 1085 1086 1087 1088 1089 1090
		ainput = AUDIO_INPUT_LINE;
		break;
	default:
		break;
	}

	status = cx231xx_set_audio_decoder_input(dev, ainput);

	return status;
1091 1092
}

1093 1094
int cx231xx_set_audio_decoder_input(struct cx231xx *dev,
				    enum AUDIO_INPUT audio_input)
1095
{
1096 1097
	u32 dwval;
	int status;
1098
	u8 gen_ctrl;
1099 1100 1101
	u32 value = 0;

	/* Put it in soft reset   */
1102
	status = vid_blk_read_byte(dev, GENERAL_CTL, &gen_ctrl);
1103
	gen_ctrl |= 1;
1104
	status = vid_blk_write_byte(dev, GENERAL_CTL, gen_ctrl);
1105 1106 1107 1108

	switch (audio_input) {
	case AUDIO_INPUT_LINE:
		/* setup AUD_IO control from Merlin paralle output */
1109 1110
		value = cx231xx_set_field(FLD_AUD_CHAN1_SRC,
					  AUD_CHAN_SRC_PARALLEL);
1111
		status = vid_blk_write_word(dev, AUD_IO_CTRL, value);
1112 1113 1114 1115

		/* setup input to Merlin, SRC2 connect to AC97
		   bypass upsample-by-2, slave mode, sony mode, left justify
		   adr 091c, dat 01000000 */
1116
		status = vid_blk_read_word(dev, AC97_CTL, &dwval);
1117

1118 1119
		status = vid_blk_write_word(dev, AC97_CTL,
					   (dwval | FLD_AC97_UP2X_BYPASS));
1120 1121

		/* select the parallel1 and SRC3 */
1122
		status = vid_blk_write_word(dev, BAND_OUT_SEL,
1123 1124
				cx231xx_set_field(FLD_SRC3_IN_SEL, 0x0) |
				cx231xx_set_field(FLD_SRC3_CLK_SEL, 0x0) |
1125
				cx231xx_set_field(FLD_PARALLEL1_SRC_SEL, 0x0));
1126 1127 1128

		/* unmute all, AC97 in, independence mode
		   adr 08d0, data 0x00063073 */
1129
		status = vid_blk_write_word(dev, DL_CTL, 0x3000001);
1130
		status = vid_blk_write_word(dev, PATH1_CTL1, 0x00063073);
1131 1132

		/* set AVC maximum threshold, adr 08d4, dat ffff0024 */
1133 1134 1135
		status = vid_blk_read_word(dev, PATH1_VOL_CTL, &dwval);
		status = vid_blk_write_word(dev, PATH1_VOL_CTL,
					   (dwval | FLD_PATH1_AVC_THRESHOLD));
1136 1137

		/* set SC maximum threshold, adr 08ec, dat ffffb3a3 */
1138 1139 1140
		status = vid_blk_read_word(dev, PATH1_SC_CTL, &dwval);
		status = vid_blk_write_word(dev, PATH1_SC_CTL,
					   (dwval | FLD_PATH1_SC_THRESHOLD));
1141 1142 1143 1144
		break;

	case AUDIO_INPUT_TUNER_TV:
	default:
1145
		status = stopAudioFirmware(dev);
1146
		/* Setup SRC sources and clocks */
1147
		status = vid_blk_write_word(dev, BAND_OUT_SEL,
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
			cx231xx_set_field(FLD_SRC6_IN_SEL, 0x00)         |
			cx231xx_set_field(FLD_SRC6_CLK_SEL, 0x01)        |
			cx231xx_set_field(FLD_SRC5_IN_SEL, 0x00)         |
			cx231xx_set_field(FLD_SRC5_CLK_SEL, 0x02)        |
			cx231xx_set_field(FLD_SRC4_IN_SEL, 0x02)         |
			cx231xx_set_field(FLD_SRC4_CLK_SEL, 0x03)        |
			cx231xx_set_field(FLD_SRC3_IN_SEL, 0x00)         |
			cx231xx_set_field(FLD_SRC3_CLK_SEL, 0x00)        |
			cx231xx_set_field(FLD_BASEBAND_BYPASS_CTL, 0x00) |
			cx231xx_set_field(FLD_AC97_SRC_SEL, 0x03)        |
			cx231xx_set_field(FLD_I2S_SRC_SEL, 0x00)         |
			cx231xx_set_field(FLD_PARALLEL2_SRC_SEL, 0x02)   |
1160
			cx231xx_set_field(FLD_PARALLEL1_SRC_SEL, 0x01));
1161 1162

		/* Setup the AUD_IO control */
1163
		status = vid_blk_write_word(dev, AUD_IO_CTRL,
1164 1165 1166 1167
			cx231xx_set_field(FLD_I2S_PORT_DIR, 0x00)  |
			cx231xx_set_field(FLD_I2S_OUT_SRC, 0x00)   |
			cx231xx_set_field(FLD_AUD_CHAN3_SRC, 0x00) |
			cx231xx_set_field(FLD_AUD_CHAN2_SRC, 0x00) |
1168
			cx231xx_set_field(FLD_AUD_CHAN1_SRC, 0x03));
1169

1170
		status = vid_blk_write_word(dev, PATH1_CTL1, 0x1F063870);
1171 1172

		/* setAudioStandard(_audio_standard); */
1173
		status = vid_blk_write_word(dev, PATH1_CTL1, 0x00063870);
1174 1175 1176

		status = restartAudioFirmware(dev);

1177
		switch (dev->model) {
1178
		case CX231XX_BOARD_CNXT_CARRAERA:
1179
		case CX231XX_BOARD_CNXT_RDE_250:
1180
		case CX231XX_BOARD_CNXT_SHELBY:
1181
		case CX231XX_BOARD_CNXT_RDU_250:
1182
		case CX231XX_BOARD_CNXT_VIDEO_GRABBER:
1183
			status = cx231xx_read_modify_write_i2c_dword(dev,
1184
					VID_BLK_I2C_ADDRESS,
1185 1186 1187
					CHIP_CTRL,
					FLD_SIF_EN,
					cx231xx_set_field(FLD_SIF_EN, 1));
1188
			break;
1189 1190
		case CX231XX_BOARD_CNXT_RDE_253S:
		case CX231XX_BOARD_CNXT_RDU_253S:
1191
		case CX231XX_BOARD_HAUPPAUGE_EXETER:
1192 1193 1194 1195 1196 1197
			status = cx231xx_read_modify_write_i2c_dword(dev,
					VID_BLK_I2C_ADDRESS,
					CHIP_CTRL,
					FLD_SIF_EN,
					cx231xx_set_field(FLD_SIF_EN, 0));
			break;
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
		default:
			break;
		}
		break;

	case AUDIO_INPUT_TUNER_FM:
		/*  use SIF for FM radio
		   setupFM();
		   setAudioStandard(_audio_standard);
		 */
		break;

	case AUDIO_INPUT_MUTE:
1211
		status = vid_blk_write_word(dev, PATH1_CTL1, 0x1F011012);
1212 1213
		break;
	}
1214

1215
	/* Take it out of soft reset */
1216
	status = vid_blk_read_byte(dev, GENERAL_CTL, &gen_ctrl);
1217
	gen_ctrl &= ~1;
1218
	status = vid_blk_write_byte(dev, GENERAL_CTL, gen_ctrl);
1219

1220 1221
	return status;
}
1222 1223 1224 1225

/* Set resolution of the video */
int cx231xx_resolution_set(struct cx231xx *dev)
{
1226
	/* set horzontal scale */
1227 1228 1229
	int status = vid_blk_write_word(dev, HSCALE_CTRL, dev->hscale);
	if (status)
		return status;
1230

1231
	/* set vertical scale */
1232 1233 1234
	status = vid_blk_write_word(dev, VSCALE_CTRL, dev->vscale);

	return status;
1235 1236
}

1237 1238 1239
/******************************************************************************
 *                    C H I P Specific  C O N T R O L   functions             *
 ******************************************************************************/
1240 1241
int cx231xx_init_ctrl_pin_status(struct cx231xx *dev)
{
1242 1243
	u32 value;
	int status = 0;
1244

1245
	status = vid_blk_read_word(dev, PIN_CTRL, &value);
1246
	value |= (~dev->board.ctl_pin_status_mask);
1247
	status = vid_blk_write_word(dev, PIN_CTRL, value);
1248

1249
	return status;
1250 1251
}

1252 1253
int cx231xx_set_agc_analog_digital_mux_select(struct cx231xx *dev,
					      u8 analog_or_digital)
1254
{
1255
	int status = 0;
1256

1257
	/* first set the direction to output */
1258 1259 1260
	status = cx231xx_set_gpio_direction(dev,
					    dev->board.
					    agc_analog_digital_select_gpio, 1);
1261

1262
	/* 0 - demod ; 1 - Analog mode */
1263
	status = cx231xx_set_gpio_value(dev,
1264 1265
				   dev->board.agc_analog_digital_select_gpio,
				   analog_or_digital);
1266

1267
	return status;
1268 1269 1270 1271
}

int cx231xx_enable_i2c_for_tuner(struct cx231xx *dev, u8 I2CIndex)
{
1272 1273
	u8 value[4] = { 0, 0, 0, 0 };
	int status = 0;
1274

1275
	cx231xx_info("Changing the i2c port for tuner to %d\n", I2CIndex);
1276

1277 1278
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
				       PWR_CTL_EN, value, 4);
1279 1280
	if (status < 0)
		return status;
1281

1282 1283 1284
	if (I2CIndex == I2C_1) {
		if (value[0] & I2C_DEMOD_EN) {
			value[0] &= ~I2C_DEMOD_EN;
1285
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1286 1287 1288 1289 1290
						   PWR_CTL_EN, value, 4);
		}
	} else {
		if (!(value[0] & I2C_DEMOD_EN)) {
			value[0] |= I2C_DEMOD_EN;
1291
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1292 1293 1294
						   PWR_CTL_EN, value, 4);
		}
	}
1295

1296
	return status;
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 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 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 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
}
EXPORT_SYMBOL_GPL(cx231xx_enable_i2c_for_tuner);
void update_HH_register_after_set_DIF(struct cx231xx *dev)
{
/*
	u8 status = 0;
	u32 value = 0;

	vid_blk_write_word(dev, PIN_CTRL, 0xA0FFF82F);
	vid_blk_write_word(dev, DIF_MISC_CTRL, 0x0A203F11);
	vid_blk_write_word(dev, DIF_SRC_PHASE_INC, 0x1BEFBF06);

	status = vid_blk_read_word(dev, AFE_CTRL_C2HH_SRC_CTRL, &value);
	vid_blk_write_word(dev, AFE_CTRL_C2HH_SRC_CTRL, 0x4485D390);
	status = vid_blk_read_word(dev, AFE_CTRL_C2HH_SRC_CTRL,  &value);
*/
}

void cx231xx_dump_HH_reg(struct cx231xx *dev)
{
	u8 status = 0;
	u32 value = 0;
	u16  i = 0;

	value = 0x45005390;
	status = vid_blk_write_word(dev, 0x104, value);

	for (i = 0x100; i < 0x140; i++) {
		status = vid_blk_read_word(dev, i, &value);
		cx231xx_info("reg0x%x=0x%x\n", i, value);
		i = i+3;
	}

	for (i = 0x300; i < 0x400; i++) {
		status = vid_blk_read_word(dev, i, &value);
		cx231xx_info("reg0x%x=0x%x\n", i, value);
		i = i+3;
	}

	for (i = 0x400; i < 0x440; i++) {
		status = vid_blk_read_word(dev, i,  &value);
		cx231xx_info("reg0x%x=0x%x\n", i, value);
		i = i+3;
	}

   status = vid_blk_read_word(dev, AFE_CTRL_C2HH_SRC_CTRL, &value);
   cx231xx_info("AFE_CTRL_C2HH_SRC_CTRL=0x%x\n", value);
   vid_blk_write_word(dev, AFE_CTRL_C2HH_SRC_CTRL, 0x4485D390);
   status = vid_blk_read_word(dev, AFE_CTRL_C2HH_SRC_CTRL, &value);
   cx231xx_info("AFE_CTRL_C2HH_SRC_CTRL=0x%x\n", value);

}
void cx231xx_dump_SC_reg(struct cx231xx *dev)
{
	u8 value[4] = { 0, 0, 0, 0 };
	int status = 0;
	cx231xx_info("cx231xx_dump_SC_reg %s!\n", __TIME__);

	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, BOARD_CFG_STAT,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", BOARD_CFG_STAT, value[0],
				 value[1], value[2], value[3]);
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, TS_MODE_REG,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", TS_MODE_REG, value[0],
				 value[1], value[2], value[3]);
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, TS1_CFG_REG,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", TS1_CFG_REG, value[0],
				 value[1], value[2], value[3]);
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, TS1_LENGTH_REG,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", TS1_LENGTH_REG, value[0],
				 value[1], value[2], value[3]);

	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, TS2_CFG_REG,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", TS2_CFG_REG, value[0],
				 value[1], value[2], value[3]);
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, TS2_LENGTH_REG,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", TS2_LENGTH_REG, value[0],
				 value[1], value[2], value[3]);
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, EP_MODE_SET,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", EP_MODE_SET, value[0],
				 value[1], value[2], value[3]);
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_PWR_PTN1,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_PWR_PTN1, value[0],
				 value[1], value[2], value[3]);

	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_PWR_PTN2,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_PWR_PTN2, value[0],
				 value[1], value[2], value[3]);
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_PWR_PTN3,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_PWR_PTN3, value[0],
				 value[1], value[2], value[3]);
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_PWR_MASK0,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_PWR_MASK0, value[0],
				 value[1], value[2], value[3]);
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_PWR_MASK1,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_PWR_MASK1, value[0],
				 value[1], value[2], value[3]);

	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_PWR_MASK2,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_PWR_MASK2, value[0],
				 value[1], value[2], value[3]);
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_GAIN,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_GAIN, value[0],
				 value[1], value[2], value[3]);
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_CAR_REG,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_CAR_REG, value[0],
				 value[1], value[2], value[3]);
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_OT_CFG1,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_OT_CFG1, value[0],
				 value[1], value[2], value[3]);

	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_OT_CFG2,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_OT_CFG2, value[0],
				 value[1], value[2], value[3]);
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN,
				 value, 4);
	cx231xx_info("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN, value[0],
				 value[1], value[2], value[3]);


}

void cx231xx_Setup_AFE_for_LowIF(struct cx231xx *dev)

{
	u8 status = 0;
	u8 value = 0;



	status = afe_read_byte(dev, ADC_STATUS2_CH3, &value);
	value = (value & 0xFE)|0x01;
	status = afe_write_byte(dev, ADC_STATUS2_CH3, value);

	status = afe_read_byte(dev, ADC_STATUS2_CH3, &value);
	value = (value & 0xFE)|0x00;
	status = afe_write_byte(dev, ADC_STATUS2_CH3, value);


/*
     config colibri to lo-if mode

     FIXME: ntf_mode = 2'b00 by default. But set 0x1 would reduce
	 the diff IF input by half,

	    for low-if agc defect
*/

	status = afe_read_byte(dev, ADC_NTF_PRECLMP_EN_CH3, &value);
	value = (value & 0xFC)|0x00;
	status = afe_write_byte(dev, ADC_NTF_PRECLMP_EN_CH3, value);

	status = afe_read_byte(dev, ADC_INPUT_CH3, &value);
	value = (value & 0xF9)|0x02;
	status = afe_write_byte(dev, ADC_INPUT_CH3, value);

	status = afe_read_byte(dev, ADC_FB_FRCRST_CH3, &value);
	value = (value & 0xFB)|0x04;
	status = afe_write_byte(dev, ADC_FB_FRCRST_CH3, value);

	status = afe_read_byte(dev, ADC_DCSERVO_DEM_CH3, &value);
	value = (value & 0xFC)|0x03;
	status = afe_write_byte(dev, ADC_DCSERVO_DEM_CH3, value);

	status = afe_read_byte(dev, ADC_CTRL_DAC1_CH3, &value);
	value = (value & 0xFB)|0x04;
	status = afe_write_byte(dev, ADC_CTRL_DAC1_CH3, value);

	status = afe_read_byte(dev, ADC_CTRL_DAC23_CH3, &value);
	value = (value & 0xF8)|0x06;
	status = afe_write_byte(dev, ADC_CTRL_DAC23_CH3, value);

	status = afe_read_byte(dev, ADC_CTRL_DAC23_CH3, &value);
	value = (value & 0x8F)|0x40;
	status = afe_write_byte(dev, ADC_CTRL_DAC23_CH3, value);

	status = afe_read_byte(dev, ADC_PWRDN_CLAMP_CH3, &value);
	value = (value & 0xDF)|0x20;
	status = afe_write_byte(dev, ADC_PWRDN_CLAMP_CH3, value);
}

void cx231xx_set_Colibri_For_LowIF(struct cx231xx *dev, u32 if_freq,
		 u8 spectral_invert, u32 mode)
{

    u32 colibri_carrier_offset = 0;
    u8 status = 0;
    u32 func_mode = 0;
    u32 standard = 0;
	u8 value[4] = { 0, 0, 0, 0 };

	switch (dev->model) {
	case CX231XX_BOARD_CNXT_CARRAERA:
	case CX231XX_BOARD_CNXT_RDE_250:
	case CX231XX_BOARD_CNXT_SHELBY:
	case CX231XX_BOARD_CNXT_RDU_250:
	case CX231XX_BOARD_CNXT_VIDEO_GRABBER:
		func_mode = 0x03;
		break;
	case CX231XX_BOARD_CNXT_RDE_253S:
	case CX231XX_BOARD_CNXT_RDU_253S:
		func_mode = 0x01;
		break;

	default:
		func_mode = 0x01;
	}

	cx231xx_info("Enter cx231xx_set_Colibri_For_LowIF()\n");
		value[0] = (u8) 0x6F;
		value[1] = (u8) 0x6F;
		value[2] = (u8) 0x6F;
		value[3] = (u8) 0x6F;
		status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
						PWR_CTL_EN, value, 4);
    if (1) {

	/*Set colibri for low IF*/
	status = cx231xx_afe_set_mode(dev, AFE_MODE_LOW_IF);


	/* Set C2HH for low IF operation.*/
	standard = dev->norm;
	status = cx231xx_dif_configure_C2HH_for_low_IF(dev, dev->active_mode,
						  func_mode, standard);


	/* Get colibri offsets.*/
	colibri_carrier_offset = cx231xx_Get_Colibri_CarrierOffset(mode,
					 standard);

	cx231xx_info("colibri_carrier_offset=%d, standard=0x%x\n",
				 colibri_carrier_offset, standard);

	/* Set the band Pass filter for DIF*/
	cx231xx_set_DIF_bandpass(dev, (if_freq+colibri_carrier_offset)
		 , spectral_invert, mode);
    }
}

u32 cx231xx_Get_Colibri_CarrierOffset(u32 mode, u32 standerd)
{
    u32 colibri_carrier_offset = 0;


    if (mode == TUNER_MODE_FM_RADIO) {
		colibri_carrier_offset = 1100000;
	} else if (standerd & (V4L2_STD_NTSC | V4L2_STD_NTSC_M_JP)) {
		colibri_carrier_offset = 4832000;  /*4.83MHz	*/
	} else if (standerd & (V4L2_STD_PAL_B | V4L2_STD_PAL_G)) {
		colibri_carrier_offset = 2700000;  /*2.70MHz       */
	} else if (standerd & (V4L2_STD_PAL_D | V4L2_STD_PAL_I
			| V4L2_STD_SECAM)) {
		colibri_carrier_offset = 2100000;  /*2.10MHz	*/
	}


    return colibri_carrier_offset;
}

void cx231xx_set_DIF_bandpass(struct cx231xx *dev, u32 if_freq,
		 u8 spectral_invert, u32 mode)
{

    unsigned long pll_freq_word;
    int status = 0;
    u32 dif_misc_ctrl_value = 0;
    u64 pll_freq_u64 = 0;
    u32 i = 0;


	cx231xx_info("if_freq=%d;spectral_invert=0x%x;mode=0x%x\n",
			 if_freq, spectral_invert, mode);


    if (mode == TUNER_MODE_FM_RADIO) {
	pll_freq_word = 0x905A1CAC;
	status = vid_blk_write_word(dev, DIF_PLL_FREQ_WORD,  pll_freq_word);

    } else /*KSPROPERTY_TUNER_MODE_TV*/{
       /* Calculate the PLL frequency word based on the adjusted if_freq*/
	pll_freq_word = if_freq;
	pll_freq_u64 = (u64)pll_freq_word << 28L;
	do_div(pll_freq_u64, 50000000);
	pll_freq_word = (u32)pll_freq_u64;
	/*pll_freq_word = 0x3463497;*/
	status = vid_blk_write_word(dev, DIF_PLL_FREQ_WORD,  pll_freq_word);

    if (spectral_invert) {
	if_freq -= 400000;
	/* Enable Spectral Invert*/
	status = vid_blk_read_word(dev, DIF_MISC_CTRL,
				 &dif_misc_ctrl_value);
	dif_misc_ctrl_value = dif_misc_ctrl_value | 0x00200000;
	status = vid_blk_write_word(dev, DIF_MISC_CTRL,
				 dif_misc_ctrl_value);
    } else {
	if_freq += 400000;
	/* Disable Spectral Invert*/
	status = vid_blk_read_word(dev, DIF_MISC_CTRL,
				 &dif_misc_ctrl_value);
	dif_misc_ctrl_value = dif_misc_ctrl_value & 0xFFDFFFFF;
	status = vid_blk_write_word(dev, DIF_MISC_CTRL,
				 dif_misc_ctrl_value);
    }

	if_freq = (if_freq/100000)*100000;

    if (if_freq < 3000000)
	if_freq = 3000000;

    if (if_freq > 16000000)
	if_freq = 16000000;
    }

    cx231xx_info("Enter IF=%d\n",
		 sizeof(Dif_set_array)/sizeof(struct dif_settings));
    for (i = 0; i < sizeof(Dif_set_array)/sizeof(struct dif_settings); i++) {
	if (Dif_set_array[i].if_freq == if_freq) {
		status = vid_blk_write_word(dev,
		 Dif_set_array[i].register_address, Dif_set_array[i].value);
	}
    }

1638 1639
}

1640 1641 1642
/******************************************************************************
 *                 D I F - B L O C K    C O N T R O L   functions             *
 ******************************************************************************/
1643
int cx231xx_dif_configure_C2HH_for_low_IF(struct cx231xx *dev, u32 mode,
1644
					  u32 function_mode, u32 standard)
1645
{
1646 1647
	int status = 0;

1648

1649 1650
	if (mode == V4L2_TUNER_RADIO) {
		/* C2HH */
1651 1652
		/* lo if big signal */
		status = cx231xx_reg_mask_write(dev,
1653
				VID_BLK_I2C_ADDRESS, 32,
1654 1655 1656
				AFE_CTRL_C2HH_SRC_CTRL, 30, 31, 0x1);
		/* FUNC_MODE = DIF */
		status = cx231xx_reg_mask_write(dev,
1657
				VID_BLK_I2C_ADDRESS, 32,
1658 1659 1660
				AFE_CTRL_C2HH_SRC_CTRL, 23, 24, function_mode);
		/* IF_MODE */
		status = cx231xx_reg_mask_write(dev,
1661
				VID_BLK_I2C_ADDRESS, 32,
1662 1663 1664
				AFE_CTRL_C2HH_SRC_CTRL, 15, 22, 0xFF);
		/* no inv */
		status = cx231xx_reg_mask_write(dev,
1665
				VID_BLK_I2C_ADDRESS, 32,
1666
				AFE_CTRL_C2HH_SRC_CTRL, 9, 9, 0x1);
1667 1668
	} else if (standard != DIF_USE_BASEBAND) {
		if (standard & V4L2_STD_MN) {
1669
			/* lo if big signal */
1670
			status = cx231xx_reg_mask_write(dev,
1671
					VID_BLK_I2C_ADDRESS, 32,
1672 1673
					AFE_CTRL_C2HH_SRC_CTRL, 30, 31, 0x1);
			/* FUNC_MODE = DIF */
1674
			status = cx231xx_reg_mask_write(dev,
1675
					VID_BLK_I2C_ADDRESS, 32,
1676
					AFE_CTRL_C2HH_SRC_CTRL, 23, 24,
1677 1678
					function_mode);
			/* IF_MODE */
1679
			status = cx231xx_reg_mask_write(dev,
1680
					VID_BLK_I2C_ADDRESS, 32,
1681 1682
					AFE_CTRL_C2HH_SRC_CTRL, 15, 22, 0xb);
			/* no inv */
1683
			status = cx231xx_reg_mask_write(dev,
1684
					VID_BLK_I2C_ADDRESS, 32,
1685 1686
					AFE_CTRL_C2HH_SRC_CTRL, 9, 9, 0x1);
			/* 0x124, AUD_CHAN1_SRC = 0x3 */
1687
			status = cx231xx_reg_mask_write(dev,
1688
					VID_BLK_I2C_ADDRESS, 32,
1689
					AUD_IO_CTRL, 0, 31, 0x00000003);
1690
		} else if ((standard == V4L2_STD_PAL_I) |
1691
			(standard & V4L2_STD_PAL_D) |
1692
			(standard & V4L2_STD_SECAM)) {
1693
			/* C2HH setup */
1694
			/* lo if big signal */
1695
			status = cx231xx_reg_mask_write(dev,
1696
					VID_BLK_I2C_ADDRESS, 32,
1697 1698
					AFE_CTRL_C2HH_SRC_CTRL, 30, 31, 0x1);
			/* FUNC_MODE = DIF */
1699
			status = cx231xx_reg_mask_write(dev,
1700
					VID_BLK_I2C_ADDRESS, 32,
1701
					AFE_CTRL_C2HH_SRC_CTRL, 23, 24,
1702 1703
					function_mode);
			/* IF_MODE */
1704
			status = cx231xx_reg_mask_write(dev,
1705
					VID_BLK_I2C_ADDRESS, 32,
1706
					AFE_CTRL_C2HH_SRC_CTRL, 15, 22, 0xF);
1707
			/* no inv */
1708
			status = cx231xx_reg_mask_write(dev,
1709
					VID_BLK_I2C_ADDRESS, 32,
1710
					AFE_CTRL_C2HH_SRC_CTRL, 9, 9, 0x1);
1711 1712
		} else {
			/* default PAL BG */
1713
			/* C2HH setup */
1714
			/* lo if big signal */
1715
			status = cx231xx_reg_mask_write(dev,
1716
					VID_BLK_I2C_ADDRESS, 32,
1717 1718
					AFE_CTRL_C2HH_SRC_CTRL, 30, 31, 0x1);
			/* FUNC_MODE = DIF */
1719
			status = cx231xx_reg_mask_write(dev,
1720
					VID_BLK_I2C_ADDRESS, 32,
1721
					AFE_CTRL_C2HH_SRC_CTRL, 23, 24,
1722 1723
					function_mode);
			/* IF_MODE */
1724
			status = cx231xx_reg_mask_write(dev,
1725
					VID_BLK_I2C_ADDRESS, 32,
1726
					AFE_CTRL_C2HH_SRC_CTRL, 15, 22, 0xE);
1727
			/* no inv */
1728
			status = cx231xx_reg_mask_write(dev,
1729
					VID_BLK_I2C_ADDRESS, 32,
1730
					AFE_CTRL_C2HH_SRC_CTRL, 9, 9, 0x1);
1731 1732 1733 1734
		}
	}

	return status;
1735 1736 1737 1738
}

int cx231xx_dif_set_standard(struct cx231xx *dev, u32 standard)
{
1739 1740 1741 1742 1743 1744
	int status = 0;
	u32 dif_misc_ctrl_value = 0;
	u32 func_mode = 0;

	cx231xx_info("%s: setStandard to %x\n", __func__, standard);

1745
	status = vid_blk_read_word(dev, DIF_MISC_CTRL, &dif_misc_ctrl_value);
1746 1747 1748 1749
	if (standard != DIF_USE_BASEBAND)
		dev->norm = standard;

	switch (dev->model) {
1750
	case CX231XX_BOARD_CNXT_CARRAERA:
1751
	case CX231XX_BOARD_CNXT_RDE_250:
1752
	case CX231XX_BOARD_CNXT_SHELBY:
1753
	case CX231XX_BOARD_CNXT_RDU_250:
1754
	case CX231XX_BOARD_CNXT_VIDEO_GRABBER:
1755
	case CX231XX_BOARD_HAUPPAUGE_EXETER:
1756 1757
		func_mode = 0x03;
		break;
1758 1759 1760 1761
	case CX231XX_BOARD_CNXT_RDE_253S:
	case CX231XX_BOARD_CNXT_RDU_253S:
		func_mode = 0x01;
		break;
1762 1763 1764 1765
	default:
		func_mode = 0x01;
	}

1766
	status = cx231xx_dif_configure_C2HH_for_low_IF(dev, dev->active_mode,
1767 1768 1769
						  func_mode, standard);

	if (standard == DIF_USE_BASEBAND) {	/* base band */
1770 1771
		/* There is a different SRC_PHASE_INC value
		   for baseband vs. DIF */
1772 1773 1774
		status = vid_blk_write_word(dev, DIF_SRC_PHASE_INC, 0xDF7DF83);
		status = vid_blk_read_word(dev, DIF_MISC_CTRL,
						&dif_misc_ctrl_value);
1775
		dif_misc_ctrl_value |= FLD_DIF_DIF_BYPASS;
1776 1777
		status = vid_blk_write_word(dev, DIF_MISC_CTRL,
						dif_misc_ctrl_value);
1778
	} else if (standard & V4L2_STD_PAL_D) {
1779
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1780
					   DIF_PLL_CTRL, 0, 31, 0x6503bc0c);
1781
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1782
					   DIF_PLL_CTRL1, 0, 31, 0xbd038c85);
1783
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1784
					   DIF_PLL_CTRL2, 0, 31, 0x1db4640a);
1785
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1786
					   DIF_PLL_CTRL3, 0, 31, 0x00008800);
1787
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1788
					   DIF_AGC_IF_REF, 0, 31, 0x444C1380);
1789
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1790
					   DIF_AGC_CTRL_IF, 0, 31, 0xDA302600);
1791
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1792
					   DIF_AGC_CTRL_INT, 0, 31, 0xDA261700);
1793
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1794
					   DIF_AGC_CTRL_RF, 0, 31, 0xDA262600);
1795
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1796 1797
					   DIF_AGC_IF_INT_CURRENT, 0, 31,
					   0x26001700);
1798
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1799 1800
					   DIF_AGC_RF_CURRENT, 0, 31,
					   0x00002660);
1801
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1802 1803
					   DIF_VIDEO_AGC_CTRL, 0, 31,
					   0x72500800);
1804
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1805 1806
					   DIF_VID_AUD_OVERRIDE, 0, 31,
					   0x27000100);
1807
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1808
					   DIF_AV_SEP_CTRL, 0, 31, 0x3F3934EA);
1809
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1810 1811
					   DIF_COMP_FLT_CTRL, 0, 31,
					   0x00000000);
1812
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1813 1814
					   DIF_SRC_PHASE_INC, 0, 31,
					   0x1befbf06);
1815
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1816 1817
					   DIF_SRC_GAIN_CONTROL, 0, 31,
					   0x000035e8);
1818
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1819 1820 1821 1822 1823
					   DIF_RPT_VARIANCE, 0, 31, 0x00000000);
		/* Save the Spec Inversion value */
		dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
		dif_misc_ctrl_value |= 0x3a023F11;
	} else if (standard & V4L2_STD_PAL_I) {
1824
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1825
					   DIF_PLL_CTRL, 0, 31, 0x6503bc0c);
1826
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1827
					   DIF_PLL_CTRL1, 0, 31, 0xbd038c85);
1828
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1829
					   DIF_PLL_CTRL2, 0, 31, 0x1db4640a);
1830
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1831
					   DIF_PLL_CTRL3, 0, 31, 0x00008800);
1832
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1833
					   DIF_AGC_IF_REF, 0, 31, 0x444C1380);
1834
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1835
					   DIF_AGC_CTRL_IF, 0, 31, 0xDA302600);
1836
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1837
					   DIF_AGC_CTRL_INT, 0, 31, 0xDA261700);
1838
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1839
					   DIF_AGC_CTRL_RF, 0, 31, 0xDA262600);
1840
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1841 1842
					   DIF_AGC_IF_INT_CURRENT, 0, 31,
					   0x26001700);
1843
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1844 1845
					   DIF_AGC_RF_CURRENT, 0, 31,
					   0x00002660);
1846
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1847 1848
					   DIF_VIDEO_AGC_CTRL, 0, 31,
					   0x72500800);
1849
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1850 1851
					   DIF_VID_AUD_OVERRIDE, 0, 31,
					   0x27000100);
1852
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1853
					   DIF_AV_SEP_CTRL, 0, 31, 0x5F39A934);
1854
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1855 1856
					   DIF_COMP_FLT_CTRL, 0, 31,
					   0x00000000);
1857
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1858 1859
					   DIF_SRC_PHASE_INC, 0, 31,
					   0x1befbf06);
1860
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1861 1862
					   DIF_SRC_GAIN_CONTROL, 0, 31,
					   0x000035e8);
1863
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1864 1865 1866 1867 1868 1869
					   DIF_RPT_VARIANCE, 0, 31, 0x00000000);
		/* Save the Spec Inversion value */
		dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
		dif_misc_ctrl_value |= 0x3a033F11;
	} else if (standard & V4L2_STD_PAL_M) {
		/* improved Low Frequency Phase Noise */
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
		status = vid_blk_write_word(dev, DIF_PLL_CTRL, 0xFF01FF0C);
		status = vid_blk_write_word(dev, DIF_PLL_CTRL1, 0xbd038c85);
		status = vid_blk_write_word(dev, DIF_PLL_CTRL2, 0x1db4640a);
		status = vid_blk_write_word(dev, DIF_PLL_CTRL3, 0x00008800);
		status = vid_blk_write_word(dev, DIF_AGC_IF_REF, 0x444C1380);
		status = vid_blk_write_word(dev, DIF_AGC_IF_INT_CURRENT,
						0x26001700);
		status = vid_blk_write_word(dev, DIF_AGC_RF_CURRENT,
						0x00002660);
		status = vid_blk_write_word(dev, DIF_VIDEO_AGC_CTRL,
						0x72500800);
		status = vid_blk_write_word(dev, DIF_VID_AUD_OVERRIDE,
						0x27000100);
		status = vid_blk_write_word(dev, DIF_AV_SEP_CTRL, 0x012c405d);
		status = vid_blk_write_word(dev, DIF_COMP_FLT_CTRL,
						0x009f50c1);
		status = vid_blk_write_word(dev, DIF_SRC_PHASE_INC,
						0x1befbf06);
		status = vid_blk_write_word(dev, DIF_SRC_GAIN_CONTROL,
						0x000035e8);
		status = vid_blk_write_word(dev, DIF_SOFT_RST_CTRL_REVB,
						0x00000000);
1892 1893 1894 1895 1896
		/* Save the Spec Inversion value */
		dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
		dif_misc_ctrl_value |= 0x3A0A3F10;
	} else if (standard & (V4L2_STD_PAL_N | V4L2_STD_PAL_Nc)) {
		/* improved Low Frequency Phase Noise */
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
		status = vid_blk_write_word(dev, DIF_PLL_CTRL, 0xFF01FF0C);
		status = vid_blk_write_word(dev, DIF_PLL_CTRL1, 0xbd038c85);
		status = vid_blk_write_word(dev, DIF_PLL_CTRL2, 0x1db4640a);
		status = vid_blk_write_word(dev, DIF_PLL_CTRL3, 0x00008800);
		status = vid_blk_write_word(dev, DIF_AGC_IF_REF, 0x444C1380);
		status = vid_blk_write_word(dev, DIF_AGC_IF_INT_CURRENT,
						0x26001700);
		status = vid_blk_write_word(dev, DIF_AGC_RF_CURRENT,
						0x00002660);
		status = vid_blk_write_word(dev, DIF_VIDEO_AGC_CTRL,
						0x72500800);
		status = vid_blk_write_word(dev, DIF_VID_AUD_OVERRIDE,
						0x27000100);
		status = vid_blk_write_word(dev, DIF_AV_SEP_CTRL,
						0x012c405d);
		status = vid_blk_write_word(dev, DIF_COMP_FLT_CTRL,
						0x009f50c1);
		status = vid_blk_write_word(dev, DIF_SRC_PHASE_INC,
						0x1befbf06);
		status = vid_blk_write_word(dev, DIF_SRC_GAIN_CONTROL,
						0x000035e8);
		status = vid_blk_write_word(dev, DIF_SOFT_RST_CTRL_REVB,
						0x00000000);
1920 1921 1922 1923
		/* Save the Spec Inversion value */
		dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
		dif_misc_ctrl_value = 0x3A093F10;
	} else if (standard &
1924 1925
		  (V4L2_STD_SECAM_B | V4L2_STD_SECAM_D | V4L2_STD_SECAM_G |
		   V4L2_STD_SECAM_K | V4L2_STD_SECAM_K1)) {
1926

1927
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1928
					   DIF_PLL_CTRL, 0, 31, 0x6503bc0c);
1929
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1930
					   DIF_PLL_CTRL1, 0, 31, 0xbd038c85);
1931
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1932
					   DIF_PLL_CTRL2, 0, 31, 0x1db4640a);
1933
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1934
					   DIF_PLL_CTRL3, 0, 31, 0x00008800);
1935
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1936
					   DIF_AGC_IF_REF, 0, 31, 0x888C0380);
1937
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1938
					   DIF_AGC_CTRL_IF, 0, 31, 0xe0262600);
1939
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1940
					   DIF_AGC_CTRL_INT, 0, 31, 0xc2171700);
1941
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1942
					   DIF_AGC_CTRL_RF, 0, 31, 0xc2262600);
1943
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1944 1945
					   DIF_AGC_IF_INT_CURRENT, 0, 31,
					   0x26001700);
1946
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1947 1948
					   DIF_AGC_RF_CURRENT, 0, 31,
					   0x00002660);
1949
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1950 1951
					   DIF_VID_AUD_OVERRIDE, 0, 31,
					   0x27000100);
1952
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1953
					   DIF_AV_SEP_CTRL, 0, 31, 0x3F3530ec);
1954
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1955 1956
					   DIF_COMP_FLT_CTRL, 0, 31,
					   0x00000000);
1957
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1958 1959
					   DIF_SRC_PHASE_INC, 0, 31,
					   0x1befbf06);
1960
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1961 1962
					   DIF_SRC_GAIN_CONTROL, 0, 31,
					   0x000035e8);
1963
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1964
					   DIF_RPT_VARIANCE, 0, 31, 0x00000000);
1965
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1966 1967 1968 1969 1970 1971 1972 1973
					   DIF_VIDEO_AGC_CTRL, 0, 31,
					   0xf4000000);

		/* Save the Spec Inversion value */
		dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
		dif_misc_ctrl_value |= 0x3a023F11;
	} else if (standard & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC)) {
		/* Is it SECAM_L1? */
1974
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1975
					   DIF_PLL_CTRL, 0, 31, 0x6503bc0c);
1976
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1977
					   DIF_PLL_CTRL1, 0, 31, 0xbd038c85);
1978
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1979
					   DIF_PLL_CTRL2, 0, 31, 0x1db4640a);
1980
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1981
					   DIF_PLL_CTRL3, 0, 31, 0x00008800);
1982
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1983
					   DIF_AGC_IF_REF, 0, 31, 0x888C0380);
1984
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1985
					   DIF_AGC_CTRL_IF, 0, 31, 0xe0262600);
1986
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1987
					   DIF_AGC_CTRL_INT, 0, 31, 0xc2171700);
1988
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1989
					   DIF_AGC_CTRL_RF, 0, 31, 0xc2262600);
1990
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1991 1992
					   DIF_AGC_IF_INT_CURRENT, 0, 31,
					   0x26001700);
1993
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1994 1995
					   DIF_AGC_RF_CURRENT, 0, 31,
					   0x00002660);
1996
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1997 1998
					   DIF_VID_AUD_OVERRIDE, 0, 31,
					   0x27000100);
1999
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2000
					   DIF_AV_SEP_CTRL, 0, 31, 0x3F3530ec);
2001
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2002 2003
					   DIF_COMP_FLT_CTRL, 0, 31,
					   0x00000000);
2004
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2005 2006
					   DIF_SRC_PHASE_INC, 0, 31,
					   0x1befbf06);
2007
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2008 2009
					   DIF_SRC_GAIN_CONTROL, 0, 31,
					   0x000035e8);
2010
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2011
					   DIF_RPT_VARIANCE, 0, 31, 0x00000000);
2012
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2013 2014 2015 2016 2017 2018 2019
					   DIF_VIDEO_AGC_CTRL, 0, 31,
					   0xf2560000);

		/* Save the Spec Inversion value */
		dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
		dif_misc_ctrl_value |= 0x3a023F11;

2020
	} else if (standard & V4L2_STD_NTSC_M) {
2021 2022
		/* V4L2_STD_NTSC_M (75 IRE Setup) Or
		   V4L2_STD_NTSC_M_JP (Japan,  0 IRE Setup) */
2023

2024 2025 2026 2027
		/* For NTSC the centre frequency of video coming out of
		   sidewinder is around 7.1MHz or 3.6MHz depending on the
		   spectral inversion. so for a non spectrally inverted channel
		   the pll freq word is 0x03420c49
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
		status = vid_blk_write_word(dev, DIF_PLL_CTRL, 0x6503BC0C);
		status = vid_blk_write_word(dev, DIF_PLL_CTRL1, 0xBD038C85);
		status = vid_blk_write_word(dev, DIF_PLL_CTRL2, 0x1DB4640A);
		status = vid_blk_write_word(dev, DIF_PLL_CTRL3, 0x00008800);
		status = vid_blk_write_word(dev, DIF_AGC_IF_REF, 0x444C0380);
		status = vid_blk_write_word(dev, DIF_AGC_IF_INT_CURRENT,
						0x26001700);
		status = vid_blk_write_word(dev, DIF_AGC_RF_CURRENT,
						0x00002660);
		status = vid_blk_write_word(dev, DIF_VIDEO_AGC_CTRL,
						0x04000800);
		status = vid_blk_write_word(dev, DIF_VID_AUD_OVERRIDE,
						0x27000100);
		status = vid_blk_write_word(dev, DIF_AV_SEP_CTRL, 0x01296e1f);

		status = vid_blk_write_word(dev, DIF_COMP_FLT_CTRL,
						0x009f50c1);
		status = vid_blk_write_word(dev, DIF_SRC_PHASE_INC,
						0x1befbf06);
		status = vid_blk_write_word(dev, DIF_SRC_GAIN_CONTROL,
						0x000035e8);

		status = vid_blk_write_word(dev, DIF_AGC_CTRL_IF, 0xC2262600);
		status = vid_blk_write_word(dev, DIF_AGC_CTRL_INT,
						0xC2262600);
		status = vid_blk_write_word(dev, DIF_AGC_CTRL_RF, 0xC2262600);
2056 2057 2058 2059

		/* Save the Spec Inversion value */
		dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
		dif_misc_ctrl_value |= 0x3a003F10;
2060 2061
	} else {
		/* default PAL BG */
2062
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2063
					   DIF_PLL_CTRL, 0, 31, 0x6503bc0c);
2064
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2065
					   DIF_PLL_CTRL1, 0, 31, 0xbd038c85);
2066
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2067
					   DIF_PLL_CTRL2, 0, 31, 0x1db4640a);
2068
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2069
					   DIF_PLL_CTRL3, 0, 31, 0x00008800);
2070
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2071
					   DIF_AGC_IF_REF, 0, 31, 0x444C1380);
2072
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2073
					   DIF_AGC_CTRL_IF, 0, 31, 0xDA302600);
2074
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2075
					   DIF_AGC_CTRL_INT, 0, 31, 0xDA261700);
2076
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2077
					   DIF_AGC_CTRL_RF, 0, 31, 0xDA262600);
2078
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2079 2080
					   DIF_AGC_IF_INT_CURRENT, 0, 31,
					   0x26001700);
2081
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2082 2083
					   DIF_AGC_RF_CURRENT, 0, 31,
					   0x00002660);
2084
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2085 2086
					   DIF_VIDEO_AGC_CTRL, 0, 31,
					   0x72500800);
2087
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2088 2089
					   DIF_VID_AUD_OVERRIDE, 0, 31,
					   0x27000100);
2090
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2091
					   DIF_AV_SEP_CTRL, 0, 31, 0x3F3530EC);
2092
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2093 2094
					   DIF_COMP_FLT_CTRL, 0, 31,
					   0x00A653A8);
2095
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2096 2097
					   DIF_SRC_PHASE_INC, 0, 31,
					   0x1befbf06);
2098
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2099 2100
					   DIF_SRC_GAIN_CONTROL, 0, 31,
					   0x000035e8);
2101
		status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2102 2103 2104 2105
					   DIF_RPT_VARIANCE, 0, 31, 0x00000000);
		/* Save the Spec Inversion value */
		dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
		dif_misc_ctrl_value |= 0x3a013F11;
2106 2107 2108 2109 2110 2111
	}

	/* The AGC values should be the same for all standards,
	   AUD_SRC_SEL[19] should always be disabled    */
	dif_misc_ctrl_value &= ~FLD_DIF_AUD_SRC_SEL;

2112 2113
	/* It is still possible to get Set Standard calls even when we
	   are in FM mode.
2114 2115 2116 2117 2118
	   This is done to override the value for FM. */
	if (dev->active_mode == V4L2_TUNER_RADIO)
		dif_misc_ctrl_value = 0x7a080000;

	/* Write the calculated value for misc ontrol register      */
2119
	status = vid_blk_write_word(dev, DIF_MISC_CTRL, dif_misc_ctrl_value);
2120 2121

	return status;
2122 2123 2124 2125 2126 2127 2128 2129
}

int cx231xx_tuner_pre_channel_change(struct cx231xx *dev)
{
	int status = 0;
	u32 dwval;

	/* Set the RF and IF k_agc values to 3 */
2130
	status = vid_blk_read_word(dev, DIF_AGC_IF_REF, &dwval);
2131 2132 2133
	dwval &= ~(FLD_DIF_K_AGC_RF | FLD_DIF_K_AGC_IF);
	dwval |= 0x33000000;

2134
	status = vid_blk_write_word(dev, DIF_AGC_IF_REF, dwval);
2135

2136
	return status;
2137 2138 2139 2140
}

int cx231xx_tuner_post_channel_change(struct cx231xx *dev)
{
2141
	int status = 0;
2142
	u32 dwval;
2143 2144
   cx231xx_info("cx231xx_tuner_post_channel_change  dev->tuner_type =0%d\n",
			 dev->tuner_type);
2145 2146
	/* Set the RF and IF k_agc values to 4 for PAL/NTSC and 8 for
	 * SECAM L/B/D standards */
2147
	status = vid_blk_read_word(dev, DIF_AGC_IF_REF, &dwval);
2148
	dwval &= ~(FLD_DIF_K_AGC_RF | FLD_DIF_K_AGC_IF);
2149

2150
	if (dev->norm & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_B |
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
			 V4L2_STD_SECAM_D)) {
			if (dev->tuner_type == TUNER_NXP_TDA18271) {
				dwval &= ~FLD_DIF_IF_REF;
				dwval |= 0x88000300;
			} else
				dwval |= 0x88000000;
		} else {
			if (dev->tuner_type == TUNER_NXP_TDA18271) {
				dwval &= ~FLD_DIF_IF_REF;
				dwval |= 0xCC000300;
			} else
				dwval |= 0x44000000;
		}
2164

2165
	status = vid_blk_write_word(dev, DIF_AGC_IF_REF, dwval);
2166

2167
	return status;
2168 2169
}

2170
/******************************************************************************
2171
 *        	    I 2 S - B L O C K    C O N T R O L   functions            *
2172
 ******************************************************************************/
2173
int cx231xx_i2s_blk_initialize(struct cx231xx *dev)
2174
{
2175 2176 2177
	int status = 0;
	u32 value;

2178
	status = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
2179
				       CH_PWR_CTRL1, 1, &value, 1);
2180 2181
	/* enables clock to delta-sigma and decimation filter */
	value |= 0x80;
2182
	status = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
2183 2184
					CH_PWR_CTRL1, 1, value, 1);
	/* power up all channel */
2185
	status = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
2186 2187 2188
					CH_PWR_CTRL2, 1, 0x00, 1);

	return status;
2189 2190
}

2191
int cx231xx_i2s_blk_update_power_control(struct cx231xx *dev,
2192
					enum AV_MODE avmode)
2193
{
2194 2195 2196 2197
	int status = 0;
	u32 value = 0;

	if (avmode != POLARIS_AVMODE_ENXTERNAL_AV) {
2198
		status = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
2199 2200
					  CH_PWR_CTRL2, 1, &value, 1);
		value |= 0xfe;
2201
		status = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
2202 2203
						CH_PWR_CTRL2, 1, value, 1);
	} else {
2204
		status = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
2205 2206 2207 2208
						CH_PWR_CTRL2, 1, 0x00, 1);
	}

	return status;
2209 2210
}

2211 2212
/* set i2s_blk for audio input types */
int cx231xx_i2s_blk_set_audio_input(struct cx231xx *dev, u8 audio_input)
2213
{
2214
	int status = 0;
2215

2216 2217
	switch (audio_input) {
	case CX231XX_AMUX_LINE_IN:
2218
		status = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
2219
						CH_PWR_CTRL2, 1, 0x00, 1);
2220
		status = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
2221 2222 2223 2224 2225 2226
						CH_PWR_CTRL1, 1, 0x80, 1);
		break;
	case CX231XX_AMUX_VIDEO:
	default:
		break;
	}
2227

2228
	dev->ctl_ainput = audio_input;
2229

2230
	return status;
2231 2232
}

2233 2234 2235
/******************************************************************************
 *                  P O W E R      C O N T R O L   functions                  *
 ******************************************************************************/
2236
int cx231xx_set_power_mode(struct cx231xx *dev, enum AV_MODE mode)
2237
{
2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249
	u8 value[4] = { 0, 0, 0, 0 };
	u32 tmp = 0;
	int status = 0;

	if (dev->power_mode != mode)
		dev->power_mode = mode;
	else {
		cx231xx_info(" setPowerMode::mode = %d, No Change req.\n",
			     mode);
		return 0;
	}

2250 2251
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN, value,
				       4);
2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
	if (status < 0)
		return status;

	tmp = *((u32 *) value);

	switch (mode) {
	case POLARIS_AVMODE_ENXTERNAL_AV:

		tmp &= (~PWR_MODE_MASK);

		tmp |= PWR_AV_EN;
		value[0] = (u8) tmp;
		value[1] = (u8) (tmp >> 8);
		value[2] = (u8) (tmp >> 16);
		value[3] = (u8) (tmp >> 24);
2267 2268
		status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
						PWR_CTL_EN, value, 4);
2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
		msleep(PWR_SLEEP_INTERVAL);

		tmp |= PWR_ISO_EN;
		value[0] = (u8) tmp;
		value[1] = (u8) (tmp >> 8);
		value[2] = (u8) (tmp >> 16);
		value[3] = (u8) (tmp >> 24);
		status =
		    cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, PWR_CTL_EN,
					   value, 4);
		msleep(PWR_SLEEP_INTERVAL);

		tmp |= POLARIS_AVMODE_ENXTERNAL_AV;
		value[0] = (u8) tmp;
		value[1] = (u8) (tmp >> 8);
		value[2] = (u8) (tmp >> 16);
		value[3] = (u8) (tmp >> 24);
2286 2287
		status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
						PWR_CTL_EN, value, 4);
2288

2289 2290
		/* reset state of xceive tuner */
		dev->xc_fw_load_done = 0;
2291 2292 2293 2294
		break;

	case POLARIS_AVMODE_ANALOGT_TV:

2295
		tmp |= PWR_DEMOD_EN;
2296 2297 2298 2299 2300
		tmp |= (I2C_DEMOD_EN);
		value[0] = (u8) tmp;
		value[1] = (u8) (tmp >> 8);
		value[2] = (u8) (tmp >> 16);
		value[3] = (u8) (tmp >> 24);
2301 2302
		status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
						PWR_CTL_EN, value, 4);
2303 2304 2305 2306 2307 2308 2309 2310
		msleep(PWR_SLEEP_INTERVAL);

		if (!(tmp & PWR_TUNER_EN)) {
			tmp |= (PWR_TUNER_EN);
			value[0] = (u8) tmp;
			value[1] = (u8) (tmp >> 8);
			value[2] = (u8) (tmp >> 16);
			value[3] = (u8) (tmp >> 24);
2311 2312
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
							PWR_CTL_EN, value, 4);
2313 2314 2315 2316 2317 2318 2319 2320 2321
			msleep(PWR_SLEEP_INTERVAL);
		}

		if (!(tmp & PWR_AV_EN)) {
			tmp |= PWR_AV_EN;
			value[0] = (u8) tmp;
			value[1] = (u8) (tmp >> 8);
			value[2] = (u8) (tmp >> 16);
			value[3] = (u8) (tmp >> 24);
2322 2323
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
							PWR_CTL_EN, value, 4);
2324 2325 2326 2327 2328 2329 2330 2331
			msleep(PWR_SLEEP_INTERVAL);
		}
		if (!(tmp & PWR_ISO_EN)) {
			tmp |= PWR_ISO_EN;
			value[0] = (u8) tmp;
			value[1] = (u8) (tmp >> 8);
			value[2] = (u8) (tmp >> 16);
			value[3] = (u8) (tmp >> 24);
2332 2333
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
							PWR_CTL_EN, value, 4);
2334 2335 2336 2337 2338 2339 2340 2341 2342
			msleep(PWR_SLEEP_INTERVAL);
		}

		if (!(tmp & POLARIS_AVMODE_ANALOGT_TV)) {
			tmp |= POLARIS_AVMODE_ANALOGT_TV;
			value[0] = (u8) tmp;
			value[1] = (u8) (tmp >> 8);
			value[2] = (u8) (tmp >> 16);
			value[3] = (u8) (tmp >> 24);
2343 2344
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
							PWR_CTL_EN, value, 4);
2345 2346 2347
			msleep(PWR_SLEEP_INTERVAL);
		}

2348 2349 2350
		if ((dev->model == CX231XX_BOARD_CNXT_CARRAERA) ||
		    (dev->model == CX231XX_BOARD_CNXT_RDE_250) ||
		    (dev->model == CX231XX_BOARD_CNXT_SHELBY) ||
2351 2352 2353 2354
		    (dev->model == CX231XX_BOARD_CNXT_RDU_250)) {
			/* tuner path to channel 1 from port 3 */
			cx231xx_enable_i2c_for_tuner(dev, I2C_3);

2355 2356 2357 2358 2359 2360 2361 2362 2363 2364
			/* reset the Tuner */
			cx231xx_gpio_set(dev, dev->board.tuner_gpio);

			if (dev->cx231xx_reset_analog_tuner)
				dev->cx231xx_reset_analog_tuner(dev);
		} else if ((dev->model == CX231XX_BOARD_CNXT_RDE_253S) ||
		    (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER) ||
		    (dev->model == CX231XX_BOARD_CNXT_RDU_253S)) {
			/* tuner path to channel 1 from port 3 */
			cx231xx_enable_i2c_for_tuner(dev, I2C_3);
2365 2366 2367 2368 2369 2370
			if (dev->cx231xx_reset_analog_tuner)
				dev->cx231xx_reset_analog_tuner(dev);
		} else if (dev->model == CX231XX_BOARD_HAUPPAUGE_EXETER) {
			/* tuner path to channel 1 from port 1 ?? */
			cx231xx_enable_i2c_for_tuner(dev, I2C_1);

2371 2372 2373
			if (dev->cx231xx_reset_analog_tuner)
				dev->cx231xx_reset_analog_tuner(dev);
		}
2374

2375 2376 2377 2378 2379 2380 2381 2382 2383
		break;

	case POLARIS_AVMODE_DIGITAL:
		if (!(tmp & PWR_TUNER_EN)) {
			tmp |= (PWR_TUNER_EN);
			value[0] = (u8) tmp;
			value[1] = (u8) (tmp >> 8);
			value[2] = (u8) (tmp >> 16);
			value[3] = (u8) (tmp >> 24);
2384 2385
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
							PWR_CTL_EN, value, 4);
2386 2387 2388 2389 2390 2391 2392 2393
			msleep(PWR_SLEEP_INTERVAL);
		}
		if (!(tmp & PWR_AV_EN)) {
			tmp |= PWR_AV_EN;
			value[0] = (u8) tmp;
			value[1] = (u8) (tmp >> 8);
			value[2] = (u8) (tmp >> 16);
			value[3] = (u8) (tmp >> 24);
2394 2395
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
							PWR_CTL_EN, value, 4);
2396 2397 2398 2399 2400 2401 2402 2403
			msleep(PWR_SLEEP_INTERVAL);
		}
		if (!(tmp & PWR_ISO_EN)) {
			tmp |= PWR_ISO_EN;
			value[0] = (u8) tmp;
			value[1] = (u8) (tmp >> 8);
			value[2] = (u8) (tmp >> 16);
			value[3] = (u8) (tmp >> 24);
2404 2405
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
							PWR_CTL_EN, value, 4);
2406 2407 2408 2409 2410 2411 2412 2413
			msleep(PWR_SLEEP_INTERVAL);
		}

		tmp |= POLARIS_AVMODE_DIGITAL | I2C_DEMOD_EN;
		value[0] = (u8) tmp;
		value[1] = (u8) (tmp >> 8);
		value[2] = (u8) (tmp >> 16);
		value[3] = (u8) (tmp >> 24);
2414 2415
		status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
						PWR_CTL_EN, value, 4);
2416 2417 2418 2419 2420 2421 2422 2423
		msleep(PWR_SLEEP_INTERVAL);

		if (!(tmp & PWR_DEMOD_EN)) {
			tmp |= PWR_DEMOD_EN;
			value[0] = (u8) tmp;
			value[1] = (u8) (tmp >> 8);
			value[2] = (u8) (tmp >> 16);
			value[3] = (u8) (tmp >> 24);
2424 2425
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
							PWR_CTL_EN, value, 4);
2426 2427 2428
			msleep(PWR_SLEEP_INTERVAL);
		}

2429 2430 2431
		if ((dev->model == CX231XX_BOARD_CNXT_CARRAERA) ||
		    (dev->model == CX231XX_BOARD_CNXT_RDE_250) ||
		    (dev->model == CX231XX_BOARD_CNXT_SHELBY) ||
2432 2433 2434 2435
		    (dev->model == CX231XX_BOARD_CNXT_RDU_250)) {
			/* tuner path to channel 1 from port 3 */
			cx231xx_enable_i2c_for_tuner(dev, I2C_3);

2436 2437 2438 2439 2440 2441 2442 2443 2444 2445
			/* reset the Tuner */
			cx231xx_gpio_set(dev, dev->board.tuner_gpio);

			if (dev->cx231xx_reset_analog_tuner)
				dev->cx231xx_reset_analog_tuner(dev);
		} else if ((dev->model == CX231XX_BOARD_CNXT_RDE_253S) ||
		    (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER) ||
		    (dev->model == CX231XX_BOARD_CNXT_RDU_253S)) {
			/* tuner path to channel 1 from port 3 */
			cx231xx_enable_i2c_for_tuner(dev, I2C_3);
2446 2447 2448 2449 2450 2451
			if (dev->cx231xx_reset_analog_tuner)
				dev->cx231xx_reset_analog_tuner(dev);
		} else if (dev->model == CX231XX_BOARD_HAUPPAUGE_EXETER) {
			/* tuner path to channel 1 from port 1 ?? */
			cx231xx_enable_i2c_for_tuner(dev, I2C_1);

2452 2453 2454
			if (dev->cx231xx_reset_analog_tuner)
				dev->cx231xx_reset_analog_tuner(dev);
		}
2455

2456 2457 2458 2459 2460 2461 2462 2463
		break;

	default:
		break;
	}

	msleep(PWR_SLEEP_INTERVAL);

2464 2465
	/* For power saving, only enable Pwr_resetout_n
	   when digital TV is selected. */
2466 2467 2468 2469 2470 2471
	if (mode == POLARIS_AVMODE_DIGITAL) {
		tmp |= PWR_RESETOUT_EN;
		value[0] = (u8) tmp;
		value[1] = (u8) (tmp >> 8);
		value[2] = (u8) (tmp >> 16);
		value[3] = (u8) (tmp >> 24);
2472 2473
		status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
						PWR_CTL_EN, value, 4);
2474 2475 2476
		msleep(PWR_SLEEP_INTERVAL);
	}

2477 2478
	/* update power control for afe */
	status = cx231xx_afe_update_power_control(dev, mode);
2479

2480 2481
	/* update power control for i2s_blk */
	status = cx231xx_i2s_blk_update_power_control(dev, mode);
2482

2483 2484
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN, value,
				       4);
2485 2486

	return status;
2487 2488 2489 2490
}

int cx231xx_power_suspend(struct cx231xx *dev)
{
2491 2492 2493
	u8 value[4] = { 0, 0, 0, 0 };
	u32 tmp = 0;
	int status = 0;
2494

2495 2496
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN,
				       value, 4);
2497 2498
	if (status > 0)
		return status;
2499

2500 2501
	tmp = *((u32 *) value);
	tmp &= (~PWR_MODE_MASK);
2502

2503 2504 2505 2506
	value[0] = (u8) tmp;
	value[1] = (u8) (tmp >> 8);
	value[2] = (u8) (tmp >> 16);
	value[3] = (u8) (tmp >> 24);
2507 2508
	status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, PWR_CTL_EN,
					value, 4);
2509

2510
	return status;
2511 2512
}

2513 2514 2515
/******************************************************************************
 *                  S T R E A M    C O N T R O L   functions                  *
 ******************************************************************************/
2516 2517
int cx231xx_start_stream(struct cx231xx *dev, u32 ep_mask)
{
2518 2519 2520
	u8 value[4] = { 0x0, 0x0, 0x0, 0x0 };
	u32 tmp = 0;
	int status = 0;
2521

2522
	cx231xx_info("cx231xx_start_stream():: ep_mask = %x\n", ep_mask);
2523 2524
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, EP_MODE_SET,
				       value, 4);
2525 2526
	if (status < 0)
		return status;
2527

2528 2529 2530 2531 2532 2533
	tmp = *((u32 *) value);
	tmp |= ep_mask;
	value[0] = (u8) tmp;
	value[1] = (u8) (tmp >> 8);
	value[2] = (u8) (tmp >> 16);
	value[3] = (u8) (tmp >> 24);
2534

2535 2536
	status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, EP_MODE_SET,
					value, 4);
2537

2538
	return status;
2539 2540 2541 2542
}

int cx231xx_stop_stream(struct cx231xx *dev, u32 ep_mask)
{
2543 2544 2545
	u8 value[4] = { 0x0, 0x0, 0x0, 0x0 };
	u32 tmp = 0;
	int status = 0;
2546

2547 2548 2549 2550 2551
	cx231xx_info("cx231xx_stop_stream():: ep_mask = %x\n", ep_mask);
	status =
	    cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, EP_MODE_SET, value, 4);
	if (status < 0)
		return status;
2552

2553 2554 2555 2556 2557 2558
	tmp = *((u32 *) value);
	tmp &= (~ep_mask);
	value[0] = (u8) tmp;
	value[1] = (u8) (tmp >> 8);
	value[2] = (u8) (tmp >> 16);
	value[3] = (u8) (tmp >> 24);
2559

2560 2561
	status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, EP_MODE_SET,
					value, 4);
2562

2563
	return status;
2564 2565 2566 2567
}

int cx231xx_initialize_stream_xfer(struct cx231xx *dev, u32 media_type)
{
2568
	int status = 0;
2569 2570
	u32 value = 0;
	u8 val[4] = { 0, 0, 0, 0 };
2571

2572 2573
	if (dev->udev->speed == USB_SPEED_HIGH) {
		switch (media_type) {
2574
		case 81: /* audio */
2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596
			cx231xx_info("%s: Audio enter HANC\n", __func__);
			status =
			    cx231xx_mode_register(dev, TS_MODE_REG, 0x9300);
			break;

		case 2:	/* vbi */
			cx231xx_info("%s: set vanc registers\n", __func__);
			status = cx231xx_mode_register(dev, TS_MODE_REG, 0x300);
			break;

		case 3:	/* sliced cc */
			cx231xx_info("%s: set hanc registers\n", __func__);
			status =
			    cx231xx_mode_register(dev, TS_MODE_REG, 0x1300);
			break;

		case 0:	/* video */
			cx231xx_info("%s: set video registers\n", __func__);
			status = cx231xx_mode_register(dev, TS_MODE_REG, 0x100);
			break;

		case 4:	/* ts1 */
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621
			cx231xx_info("%s: set ts1 registers", __func__);

		if (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER) {
			cx231xx_info(" MPEG\n");
			value &= 0xFFFFFFFC;
			value |= 0x3;

			status = cx231xx_mode_register(dev, TS_MODE_REG, value);

			val[0] = 0x04;
			val[1] = 0xA3;
			val[2] = 0x3B;
			val[3] = 0x00;
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
				 TS1_CFG_REG, val, 4);

			val[0] = 0x00;
			val[1] = 0x08;
			val[2] = 0x00;
			val[3] = 0x08;
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
				 TS1_LENGTH_REG, val, 4);

		} else {
			cx231xx_info(" BDA\n");
2622
			status = cx231xx_mode_register(dev, TS_MODE_REG, 0x101);
2623 2624
			status = cx231xx_mode_register(dev, TS1_CFG_REG, 0x010);
		}
2625
			break;
2626

2627 2628 2629 2630 2631 2632 2633 2634 2635 2636
		case 6:	/* ts1 parallel mode */
			cx231xx_info("%s: set ts1 parrallel mode registers\n",
				     __func__);
			status = cx231xx_mode_register(dev, TS_MODE_REG, 0x100);
			status = cx231xx_mode_register(dev, TS1_CFG_REG, 0x400);
			break;
		}
	} else {
		status = cx231xx_mode_register(dev, TS_MODE_REG, 0x101);
	}
2637

2638 2639
	return status;
}
2640 2641 2642

int cx231xx_capture_start(struct cx231xx *dev, int start, u8 media_type)
{
2643
	int rc = -1;
2644
	u32 ep_mask = -1;
2645
	struct pcb_config *pcb_config;
2646 2647

	/* get EP for media type */
2648
	pcb_config = (struct pcb_config *)&dev->current_pcb_config;
2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700

	if (pcb_config->config_num == 1) {
		switch (media_type) {
		case 0:	/* Video */
			ep_mask = ENABLE_EP4;	/* ep4  [00:1000] */
			break;
		case 1:	/* Audio */
			ep_mask = ENABLE_EP3;	/* ep3  [00:0100] */
			break;
		case 2:	/* Vbi */
			ep_mask = ENABLE_EP5;	/* ep5 [01:0000] */
			break;
		case 3:	/* Sliced_cc */
			ep_mask = ENABLE_EP6;	/* ep6 [10:0000] */
			break;
		case 4:	/* ts1 */
		case 6:	/* ts1 parallel mode */
			ep_mask = ENABLE_EP1;	/* ep1 [00:0001] */
			break;
		case 5:	/* ts2 */
			ep_mask = ENABLE_EP2;	/* ep2 [00:0010] */
			break;
		}

	} else if (pcb_config->config_num > 1) {
		switch (media_type) {
		case 0:	/* Video */
			ep_mask = ENABLE_EP4;	/* ep4  [00:1000] */
			break;
		case 1:	/* Audio */
			ep_mask = ENABLE_EP3;	/* ep3  [00:0100] */
			break;
		case 2:	/* Vbi */
			ep_mask = ENABLE_EP5;	/* ep5 [01:0000] */
			break;
		case 3:	/* Sliced_cc */
			ep_mask = ENABLE_EP6;	/* ep6 [10:0000] */
			break;
		case 4:	/* ts1 */
		case 6:	/* ts1 parallel mode */
			ep_mask = ENABLE_EP1;	/* ep1 [00:0001] */
			break;
		case 5:	/* ts2 */
			ep_mask = ENABLE_EP2;	/* ep2 [00:0010] */
			break;
		}

	}

	if (start) {
		rc = cx231xx_initialize_stream_xfer(dev, media_type);

2701
		if (rc < 0)
2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712
			return rc;

		/* enable video capture */
		if (ep_mask > 0)
			rc = cx231xx_start_stream(dev, ep_mask);
	} else {
		/* disable video capture */
		if (ep_mask > 0)
			rc = cx231xx_stop_stream(dev, ep_mask);
	}

2713 2714 2715 2716
	if (dev->mode == CX231XX_ANALOG_MODE)
		;/* do any in Analog mode */
	else
		;/* do any in digital mode */
2717 2718 2719

	return rc;
}
2720
EXPORT_SYMBOL_GPL(cx231xx_capture_start);
2721

2722 2723 2724
/*****************************************************************************
*                   G P I O   B I T control functions                        *
******************************************************************************/
2725
int cx231xx_set_gpio_bit(struct cx231xx *dev, u32 gpio_bit, u8 *gpio_val)
2726
{
2727
	int status = 0;
2728

2729
	status = cx231xx_send_gpio_cmd(dev, gpio_bit, gpio_val, 4, 0, 0);
2730

2731
	return status;
2732 2733
}

2734
int cx231xx_get_gpio_bit(struct cx231xx *dev, u32 gpio_bit, u8 *gpio_val)
2735
{
2736
	int status = 0;
2737

2738
	status = cx231xx_send_gpio_cmd(dev, gpio_bit, gpio_val, 4, 0, 1);
2739

2740
	return status;
2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754
}

/*
* cx231xx_set_gpio_direction
*      Sets the direction of the GPIO pin to input or output
*
* Parameters :
*      pin_number : The GPIO Pin number to program the direction for
*                   from 0 to 31
*      pin_value : The Direction of the GPIO Pin under reference.
*                      0 = Input direction
*                      1 = Output direction
*/
int cx231xx_set_gpio_direction(struct cx231xx *dev,
2755
			       int pin_number, int pin_value)
2756 2757
{
	int status = 0;
2758
	u32 value = 0;
2759

2760
	/* Check for valid pin_number - if 32 , bail out */
2761
	if (pin_number >= 32)
2762
		return -EINVAL;
2763

2764 2765
	/* input */
	if (pin_value == 0)
2766
		value = dev->gpio_dir & (~(1 << pin_number));	/* clear */
2767
	else
2768
		value = dev->gpio_dir | (1 << pin_number);
2769

2770
	status = cx231xx_set_gpio_bit(dev, value, (u8 *) &dev->gpio_val);
2771

2772
	/* cache the value for future */
2773 2774
	dev->gpio_dir = value;

2775
	return status;
2776 2777 2778
}

/*
2779
* cx231xx_set_gpio_value
2780 2781 2782 2783 2784 2785 2786 2787 2788
*      Sets the value of the GPIO pin to Logic high or low. The Pin under
*      reference should ALREADY BE SET IN OUTPUT MODE !!!!!!!!!
*
* Parameters :
*      pin_number : The GPIO Pin number to program the direction for
*      pin_value : The value of the GPIO Pin under reference.
*                      0 = set it to 0
*                      1 = set it to 1
*/
2789
int cx231xx_set_gpio_value(struct cx231xx *dev, int pin_number, int pin_value)
2790
{
2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802
	int status = 0;
	u32 value = 0;

	/* Check for valid pin_number - if 0xFF , bail out */
	if (pin_number >= 32)
		return -EINVAL;

	/* first do a sanity check - if the Pin is not output, make it output */
	if ((dev->gpio_dir & (1 << pin_number)) == 0x00) {
		/* It was in input mode */
		value = dev->gpio_dir | (1 << pin_number);
		dev->gpio_dir = value;
2803 2804
		status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
					      (u8 *) &dev->gpio_val);
2805
		value = 0;
2806
	}
2807

2808
	if (pin_value == 0)
2809
		value = dev->gpio_val & (~(1 << pin_number));
2810
	else
2811
		value = dev->gpio_val | (1 << pin_number);
2812

2813 2814
	/* store the value */
	dev->gpio_val = value;
2815

2816
	/* toggle bit0 of GP_IO */
2817
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
2818

2819
	return status;
2820 2821
}

2822 2823 2824
/*****************************************************************************
*                      G P I O I2C related functions                         *
******************************************************************************/
2825 2826 2827 2828 2829
int cx231xx_gpio_i2c_start(struct cx231xx *dev)
{
	int status = 0;

	/* set SCL to output 1 ; set SDA to output 1 */
2830 2831 2832 2833 2834
	dev->gpio_dir |= 1 << dev->board.tuner_scl_gpio;
	dev->gpio_dir |= 1 << dev->board.tuner_sda_gpio;
	dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
	dev->gpio_val |= 1 << dev->board.tuner_sda_gpio;

2835 2836
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
	if (status < 0)
2837 2838 2839
		return -EINVAL;

	/* set SCL to output 1; set SDA to output 0 */
2840 2841
	dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
	dev->gpio_val &= ~(1 << dev->board.tuner_sda_gpio);
2842

2843 2844
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
	if (status < 0)
2845 2846
		return -EINVAL;

2847 2848 2849
	/* set SCL to output 0; set SDA to output 0      */
	dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
	dev->gpio_val &= ~(1 << dev->board.tuner_sda_gpio);
2850

2851 2852
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
	if (status < 0)
2853 2854 2855 2856 2857 2858 2859
		return -EINVAL;

	return status;
}

int cx231xx_gpio_i2c_end(struct cx231xx *dev)
{
2860
	int status = 0;
2861

2862 2863 2864
	/* set SCL to output 0; set SDA to output 0      */
	dev->gpio_dir |= 1 << dev->board.tuner_scl_gpio;
	dev->gpio_dir |= 1 << dev->board.tuner_sda_gpio;
2865

2866 2867
	dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
	dev->gpio_val &= ~(1 << dev->board.tuner_sda_gpio);
2868

2869 2870
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
	if (status < 0)
2871 2872
		return -EINVAL;

2873 2874 2875
	/* set SCL to output 1; set SDA to output 0      */
	dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
	dev->gpio_val &= ~(1 << dev->board.tuner_sda_gpio);
2876

2877 2878
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
	if (status < 0)
2879 2880 2881 2882
		return -EINVAL;

	/* set SCL to input ,release SCL cable control
	   set SDA to input ,release SDA cable control */
2883 2884
	dev->gpio_dir &= ~(1 << dev->board.tuner_scl_gpio);
	dev->gpio_dir &= ~(1 << dev->board.tuner_sda_gpio);
2885

2886
	status =
2887 2888
	    cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
	if (status < 0)
2889
		return -EINVAL;
2890

2891 2892 2893 2894 2895
	return status;
}

int cx231xx_gpio_i2c_write_byte(struct cx231xx *dev, u8 data)
{
2896 2897
	int status = 0;
	u8 i;
2898 2899

	/* set SCL to output ; set SDA to output */
2900 2901 2902 2903 2904 2905 2906 2907
	dev->gpio_dir |= 1 << dev->board.tuner_scl_gpio;
	dev->gpio_dir |= 1 << dev->board.tuner_sda_gpio;

	for (i = 0; i < 8; i++) {
		if (((data << i) & 0x80) == 0) {
			/* set SCL to output 0; set SDA to output 0     */
			dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
			dev->gpio_val &= ~(1 << dev->board.tuner_sda_gpio);
2908 2909
			status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
						      (u8 *)&dev->gpio_val);
2910 2911 2912

			/* set SCL to output 1; set SDA to output 0     */
			dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
2913 2914
			status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
						      (u8 *)&dev->gpio_val);
2915 2916 2917

			/* set SCL to output 0; set SDA to output 0     */
			dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2918 2919
			status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
						      (u8 *)&dev->gpio_val);
2920
		} else {
2921 2922 2923
			/* set SCL to output 0; set SDA to output 1     */
			dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
			dev->gpio_val |= 1 << dev->board.tuner_sda_gpio;
2924 2925
			status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
						      (u8 *)&dev->gpio_val);
2926 2927 2928

			/* set SCL to output 1; set SDA to output 1     */
			dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
2929 2930
			status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
						      (u8 *)&dev->gpio_val);
2931 2932 2933

			/* set SCL to output 0; set SDA to output 1     */
			dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2934 2935
			status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
						      (u8 *)&dev->gpio_val);
2936
		}
2937 2938 2939 2940
	}
	return status;
}

2941
int cx231xx_gpio_i2c_read_byte(struct cx231xx *dev, u8 *buf)
2942 2943
{
	u8 value = 0;
2944 2945 2946
	int status = 0;
	u32 gpio_logic_value = 0;
	u8 i;
2947 2948

	/* read byte */
2949
	for (i = 0; i < 8; i++) {	/* send write I2c addr */
2950 2951

		/* set SCL to output 0; set SDA to input */
2952
		dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2953 2954
		status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
					      (u8 *)&dev->gpio_val);
2955 2956

		/* set SCL to output 1; set SDA to input */
2957
		dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
2958 2959
		status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
					      (u8 *)&dev->gpio_val);
2960 2961 2962

		/* get SDA data bit */
		gpio_logic_value = dev->gpio_val;
2963 2964 2965
		status = cx231xx_get_gpio_bit(dev, dev->gpio_dir,
					      (u8 *)&dev->gpio_val);
		if ((dev->gpio_val & (1 << dev->board.tuner_sda_gpio)) != 0)
2966
			value |= (1 << (8 - i - 1));
2967 2968 2969 2970 2971

		dev->gpio_val = gpio_logic_value;
	}

	/* set SCL to output 0,finish the read latest SCL signal.
2972 2973
	   !!!set SDA to input, never to modify SDA direction at
	   the same times */
2974
	dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2975
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
2976

2977 2978
	/* store the value */
	*buf = value & 0xff;
2979 2980 2981 2982 2983 2984

	return status;
}

int cx231xx_gpio_i2c_read_ack(struct cx231xx *dev)
{
2985
	int status = 0;
2986
	u32 gpio_logic_value = 0;
2987 2988
	int nCnt = 10;
	int nInit = nCnt;
2989

2990 2991
	/* clock stretch; set SCL to input; set SDA to input;
	   get SCL value till SCL = 1 */
2992 2993
	dev->gpio_dir &= ~(1 << dev->board.tuner_sda_gpio);
	dev->gpio_dir &= ~(1 << dev->board.tuner_scl_gpio);
2994 2995

	gpio_logic_value = dev->gpio_val;
2996
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
2997

2998
	do {
2999
		msleep(2);
3000 3001
		status = cx231xx_get_gpio_bit(dev, dev->gpio_dir,
					      (u8 *)&dev->gpio_val);
3002
		nCnt--;
3003 3004 3005
	} while (((dev->gpio_val &
			  (1 << dev->board.tuner_scl_gpio)) == 0) &&
			 (nCnt > 0));
3006

3007
	if (nCnt == 0)
3008
		cx231xx_info("No ACK after %d msec -GPIO I2C failed!",
3009
			     nInit * 10);
3010

3011 3012 3013 3014 3015
	/*
	 * readAck
	 * through clock stretch, slave has given a SCL signal,
	 * so the SDA data can be directly read.
	 */
3016
	status = cx231xx_get_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
3017

3018
	if ((dev->gpio_val & 1 << dev->board.tuner_sda_gpio) == 0) {
3019
		dev->gpio_val = gpio_logic_value;
3020
		dev->gpio_val &= ~(1 << dev->board.tuner_sda_gpio);
3021 3022 3023
		status = 0;
	} else {
		dev->gpio_val = gpio_logic_value;
3024
		dev->gpio_val |= (1 << dev->board.tuner_sda_gpio);
3025 3026
	}

3027 3028
	/* read SDA end, set the SCL to output 0, after this operation,
	   SDA direction can be changed. */
3029
	dev->gpio_val = gpio_logic_value;
3030 3031
	dev->gpio_dir |= (1 << dev->board.tuner_scl_gpio);
	dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
3032
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
3033 3034 3035 3036 3037 3038

	return status;
}

int cx231xx_gpio_i2c_write_ack(struct cx231xx *dev)
{
3039
	int status = 0;
3040 3041

	/* set SDA to ouput */
3042
	dev->gpio_dir |= 1 << dev->board.tuner_sda_gpio;
3043
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
3044 3045

	/* set SCL = 0 (output); set SDA = 0 (output) */
3046 3047
	dev->gpio_val &= ~(1 << dev->board.tuner_sda_gpio);
	dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
3048
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
3049 3050

	/* set SCL = 1 (output); set SDA = 0 (output) */
3051
	dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
3052
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
3053 3054

	/* set SCL = 0 (output); set SDA = 0 (output) */
3055
	dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
3056
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
3057 3058

	/* set SDA to input,and then the slave will read data from SDA. */
3059
	dev->gpio_dir &= ~(1 << dev->board.tuner_sda_gpio);
3060
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
3061 3062 3063 3064 3065 3066

	return status;
}

int cx231xx_gpio_i2c_write_nak(struct cx231xx *dev)
{
3067
	int status = 0;
3068 3069

	/* set scl to output ; set sda to input */
3070 3071
	dev->gpio_dir |= 1 << dev->board.tuner_scl_gpio;
	dev->gpio_dir &= ~(1 << dev->board.tuner_sda_gpio);
3072
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
3073 3074

	/* set scl to output 0; set sda to input */
3075
	dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
3076
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
3077 3078

	/* set scl to output 1; set sda to input */
3079
	dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
3080
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
3081 3082 3083 3084

	return status;
}

3085 3086 3087
/*****************************************************************************
*                      G P I O I2C related functions                         *
******************************************************************************/
3088 3089 3090
/* cx231xx_gpio_i2c_read
 * Function to read data from gpio based I2C interface
 */
3091
int cx231xx_gpio_i2c_read(struct cx231xx *dev, u8 dev_addr, u8 *buf, u8 len)
3092
{
3093 3094
	int status = 0;
	int i = 0;
3095

3096
	/* get the lock */
3097 3098 3099 3100 3101 3102
	mutex_lock(&dev->gpio_i2c_lock);

	/* start */
	status = cx231xx_gpio_i2c_start(dev);

	/* write dev_addr */
3103
	status = cx231xx_gpio_i2c_write_byte(dev, (dev_addr << 1) + 1);
3104 3105 3106 3107

	/* readAck */
	status = cx231xx_gpio_i2c_read_ack(dev);

3108 3109 3110 3111 3112
	/* read data */
	for (i = 0; i < len; i++) {
		/* read data */
		buf[i] = 0;
		status = cx231xx_gpio_i2c_read_byte(dev, &buf[i]);
3113

3114 3115 3116 3117 3118
		if ((i + 1) != len) {
			/* only do write ack if we more length */
			status = cx231xx_gpio_i2c_write_ack(dev);
		}
	}
3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134

	/* write NAK - inform reads are complete */
	status = cx231xx_gpio_i2c_write_nak(dev);

	/* write end */
	status = cx231xx_gpio_i2c_end(dev);

	/* release the lock */
	mutex_unlock(&dev->gpio_i2c_lock);

	return status;
}

/* cx231xx_gpio_i2c_write
 * Function to write data to gpio based I2C interface
 */
3135
int cx231xx_gpio_i2c_write(struct cx231xx *dev, u8 dev_addr, u8 *buf, u8 len)
3136
{
3137 3138
	int status = 0;
	int i = 0;
3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149

	/* get the lock */
	mutex_lock(&dev->gpio_i2c_lock);

	/* start */
	status = cx231xx_gpio_i2c_start(dev);

	/* write dev_addr */
	status = cx231xx_gpio_i2c_write_byte(dev, dev_addr << 1);

	/* read Ack */
3150
	status = cx231xx_gpio_i2c_read_ack(dev);
3151

3152
	for (i = 0; i < len; i++) {
3153
		/* Write data */
3154
		status = cx231xx_gpio_i2c_write_byte(dev, buf[i]);
3155

3156 3157 3158
		/* read Ack */
		status = cx231xx_gpio_i2c_read_ack(dev);
	}
3159

3160
	/* write End */
3161 3162 3163 3164 3165 3166 3167
	status = cx231xx_gpio_i2c_end(dev);

	/* release the lock */
	mutex_unlock(&dev->gpio_i2c_lock);

	return 0;
}