cx231xx-avcore.c 82.8 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 34 35 36 37 38 39 40 41

   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/version.h>
#include <linux/mm.h>
#include <linux/mutex.h>

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

#include "cx231xx.h"

42 43
/******************************************************************************
 *            C O L I B R I - B L O C K    C O N T R O L   functions          *
44
 ******************************************************************************/
45 46
int cx231xx_colibri_init_super_block(struct cx231xx *dev, u32 ref_count)
{
47 48 49 50 51 52 53
	int status = 0;
	u8 temp = 0;
	u32 colibri_power_status = 0;
	int i = 0;

	/* super block initialize */
	temp = (u8) (ref_count & 0xff);
54 55
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
					SUP_BLK_TUNE2, 2, temp, 1);
56 57
	if (status < 0)
		return status;
58

59 60 61
	status = cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
				       SUP_BLK_TUNE2, 2,
				       &colibri_power_status, 1);
62 63
	if (status < 0)
		return status;
64 65 66

	temp = (u8) ((ref_count & 0x300) >> 8);
	temp |= 0x40;
67 68
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
					SUP_BLK_TUNE1, 2, temp, 1);
69 70 71
	if (status < 0)
		return status;

72 73
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
					SUP_BLK_PLL2, 2, 0x0f, 1);
74 75
	if (status < 0)
		return status;
76 77 78

	/* enable pll     */
	while (colibri_power_status != 0x18) {
79 80
		status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
						SUP_BLK_PWRDN, 2, 0x18, 1);
81 82 83 84 85 86
		if (status < 0) {
			cx231xx_info(
			": Init Super Block failed in send cmd\n");
			break;
		}

87 88 89
		status = cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
					       SUP_BLK_PWRDN, 2,
					       &colibri_power_status, 1);
90 91
		colibri_power_status &= 0xff;
		if (status < 0) {
92
			cx231xx_info(
93
			": Init Super Block failed in receive cmd\n");
94 95 96 97
			break;
		}
		i++;
		if (i == 10) {
98 99
			cx231xx_info(
			": Init Super Block force break in loop !!!!\n");
100 101 102 103 104 105 106 107 108
			status = -1;
			break;
		}
	}

	if (status < 0)
		return status;

	/* start tuning filter */
109 110
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
					SUP_BLK_TUNE3, 2, 0x40, 1);
111 112 113
	if (status < 0)
		return status;

114 115 116 117 118 119 120 121
	msleep(5);

	/* exit tuning */
	status =
	    cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS, SUP_BLK_TUNE3,
				   2, 0x00, 1);

	return status;
122 123 124 125
}

int cx231xx_colibri_init_channels(struct cx231xx *dev)
{
126 127 128
	int status = 0;

	/* power up all 3 channels, clear pd_buffer */
129
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
130
				   ADC_PWRDN_CLAMP_CH1, 2, 0x00, 1);
131
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
132
				   ADC_PWRDN_CLAMP_CH2, 2, 0x00, 1);
133
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
134 135 136
				   ADC_PWRDN_CLAMP_CH3, 2, 0x00, 1);

	/* Enable quantizer calibration */
137 138
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
					ADC_COM_QUANT, 2, 0x02, 1);
139 140

	/* channel initialize, force modulator (fb) reset */
141
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
142
				   ADC_FB_FRCRST_CH1, 2, 0x17, 1);
143
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
144
				   ADC_FB_FRCRST_CH2, 2, 0x17, 1);
145
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
146 147 148
				   ADC_FB_FRCRST_CH3, 2, 0x17, 1);

	/* start quantilizer calibration  */
149
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
150
				   ADC_CAL_ATEST_CH1, 2, 0x10, 1);
151
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
152
				   ADC_CAL_ATEST_CH2, 2, 0x10, 1);
153
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
154 155 156 157
				   ADC_CAL_ATEST_CH3, 2, 0x10, 1);
	msleep(5);

	/* exit modulator (fb) reset */
158
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
159
				   ADC_FB_FRCRST_CH1, 2, 0x07, 1);
160
	status =  cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
161
				   ADC_FB_FRCRST_CH2, 2, 0x07, 1);
162
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
163 164 165
				   ADC_FB_FRCRST_CH3, 2, 0x07, 1);

	/* enable the pre_clamp in each channel for single-ended input */
166
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
167
				   ADC_NTF_PRECLMP_EN_CH1, 2, 0xf0, 1);
168
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
169
				   ADC_NTF_PRECLMP_EN_CH2, 2, 0xf0, 1);
170
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
171 172 173
				   ADC_NTF_PRECLMP_EN_CH3, 2, 0xf0, 1);

	/* use diode instead of resistor, so set term_en to 0, res_en to 0  */
174
	status = cx231xx_reg_mask_write(dev, Colibri_DEVICE_ADDRESS, 8,
175
				   ADC_QGAIN_RES_TRM_CH1, 3, 7, 0x00);
176
	status = cx231xx_reg_mask_write(dev, Colibri_DEVICE_ADDRESS, 8,
177
				   ADC_QGAIN_RES_TRM_CH2, 3, 7, 0x00);
178
	status = cx231xx_reg_mask_write(dev, Colibri_DEVICE_ADDRESS, 8,
179 180 181
				   ADC_QGAIN_RES_TRM_CH3, 3, 7, 0x00);

	/* dynamic element matching off */
182
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
183
				   ADC_DCSERVO_DEM_CH1, 2, 0x03, 1);
184
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
185
				   ADC_DCSERVO_DEM_CH2, 2, 0x03, 1);
186
	status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
187 188 189
				   ADC_DCSERVO_DEM_CH3, 2, 0x03, 1);

	return status;
190 191 192 193
}

int cx231xx_colibri_setup_AFE_for_baseband(struct cx231xx *dev)
{
194 195
	u32 c_value = 0;
	int status = 0;
196

197 198 199 200 201 202 203
	status =
	    cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
				  ADC_PWRDN_CLAMP_CH2, 2, &c_value, 1);
	c_value &= (~(0x50));
	status =
	    cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
				   ADC_PWRDN_CLAMP_CH2, 2, c_value, 1);
204

205
	return status;
206 207 208
}

/*
209 210 211 212
	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.

213 214 215 216 217 218
	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)
*/
int cx231xx_colibri_set_input_mux(struct cx231xx *dev, u32 input_mux)
{
219 220 221 222 223 224 225 226 227 228 229 230 231
	u8 ch1_setting = (u8) input_mux;
	u8 ch2_setting = (u8) (input_mux >> 8);
	u8 ch3_setting = (u8) (input_mux >> 16);
	int status = 0;
	u32 value = 0;

	if (ch1_setting != 0) {
		status =
		    cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
					  ADC_INPUT_CH1, 2, &value, 1);
		value &= (!INPUT_SEL_MASK);
		value |= (ch1_setting - 1) << 4;
		value &= 0xff;
232
		status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
233 234 235 236 237 238 239 240 241 242
					   ADC_INPUT_CH1, 2, value, 1);
	}

	if (ch2_setting != 0) {
		status =
		    cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
					  ADC_INPUT_CH2, 2, &value, 1);
		value &= (!INPUT_SEL_MASK);
		value |= (ch2_setting - 1) << 4;
		value &= 0xff;
243
		status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
244 245 246
					   ADC_INPUT_CH2, 2, value, 1);
	}

247 248
	/* For ch3_setting, the value to put in the register is
	   7 less than the input number */
249
	if (ch3_setting != 0) {
250
		status = cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
251 252 253 254
					  ADC_INPUT_CH3, 2, &value, 1);
		value &= (!INPUT_SEL_MASK);
		value |= (ch3_setting - 1) << 4;
		value &= 0xff;
255
		status = cx231xx_write_i2c_data(dev, Colibri_DEVICE_ADDRESS,
256 257 258 259
					   ADC_INPUT_CH3, 2, value, 1);
	}

	return status;
260 261 262 263
}

int cx231xx_colibri_set_mode(struct cx231xx *dev, enum AFE_MODE mode)
{
264 265
	int status = 0;

266 267 268 269 270
	/*
	* FIXME: We need to implement the AFE code for LOW IF and for HI IF.
	* Currently, only baseband works.
	*/

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
	switch (mode) {
	case AFE_MODE_LOW_IF:
		/* SetupAFEforLowIF();  */
		break;
	case AFE_MODE_BASEBAND:
		status = cx231xx_colibri_setup_AFE_for_baseband(dev);
		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;
	}

289 290
	if ((mode != dev->colibri_mode) &&
		(dev->video_input == CX231XX_VMUX_TELEVISION))
291
		status = cx231xx_colibri_adjust_ref_count(dev,
292 293 294 295 296
						     CX231XX_VMUX_TELEVISION);

	dev->colibri_mode = mode;

	return status;
297 298
}

299 300
int cx231xx_colibri_update_power_control(struct cx231xx *dev,
					enum AV_MODE avmode)
301
{
302 303 304 305 306 307 308
	u32 colibri_power_status = 0;
	int status = 0;

	switch (dev->model) {
	case CX231XX_BOARD_CNXT_RDE_250:
	case CX231XX_BOARD_CNXT_RDU_250:
		if (avmode == POLARIS_AVMODE_ANALOGT_TV) {
309 310
			while (colibri_power_status != (FLD_PWRDN_TUNING_BIAS |
						FLD_PWRDN_ENABLE_PLL)) {
311 312 313
				status = cx231xx_write_i2c_data(dev,
							Colibri_DEVICE_ADDRESS,
							SUP_BLK_PWRDN, 2,
314 315 316 317
							FLD_PWRDN_TUNING_BIAS |
							FLD_PWRDN_ENABLE_PLL,
							1);
				status |= cx231xx_read_i2c_data(dev,
318 319 320 321
							Colibri_DEVICE_ADDRESS,
							SUP_BLK_PWRDN, 2,
							&colibri_power_status,
							1);
322 323 324 325
				if (status < 0)
					break;
			}

326 327
			status = cx231xx_write_i2c_data(dev,
						   Colibri_DEVICE_ADDRESS,
328 329
						   ADC_PWRDN_CLAMP_CH1, 2, 0x00,
						   1);
330
			status |= cx231xx_write_i2c_data(dev,
331
						   Colibri_DEVICE_ADDRESS,
332 333
						   ADC_PWRDN_CLAMP_CH2, 2, 0x00,
						   1);
334
			status |= cx231xx_write_i2c_data(dev,
335
						   Colibri_DEVICE_ADDRESS,
336 337 338
						   ADC_PWRDN_CLAMP_CH3, 2, 0x00,
						   1);
		} else if (avmode == POLARIS_AVMODE_DIGITAL) {
339 340
			status = cx231xx_write_i2c_data(dev,
						   Colibri_DEVICE_ADDRESS,
341 342
						   ADC_PWRDN_CLAMP_CH1, 2, 0x70,
						   1);
343
			status |= cx231xx_write_i2c_data(dev,
344
						   Colibri_DEVICE_ADDRESS,
345 346
						   ADC_PWRDN_CLAMP_CH2, 2, 0x70,
						   1);
347
			status |= cx231xx_write_i2c_data(dev,
348
						   Colibri_DEVICE_ADDRESS,
349 350 351
						   ADC_PWRDN_CLAMP_CH3, 2, 0x70,
						   1);

352
			status |= cx231xx_read_i2c_data(dev,
353
						  Colibri_DEVICE_ADDRESS,
354 355
						  SUP_BLK_PWRDN, 2,
						  &colibri_power_status, 1);
356 357 358 359
			colibri_power_status |= FLD_PWRDN_PD_BANDGAP |
						FLD_PWRDN_PD_BIAS |
						FLD_PWRDN_PD_TUNECK;
			status |= cx231xx_write_i2c_data(dev,
360
						   Colibri_DEVICE_ADDRESS,
361 362 363
						   SUP_BLK_PWRDN, 2,
						   colibri_power_status, 1);
		} else if (avmode == POLARIS_AVMODE_ENXTERNAL_AV) {
364 365
			while (colibri_power_status != (FLD_PWRDN_TUNING_BIAS |
						FLD_PWRDN_ENABLE_PLL)) {
366 367 368
				status = cx231xx_write_i2c_data(dev,
							Colibri_DEVICE_ADDRESS,
							SUP_BLK_PWRDN, 2,
369 370 371 372
							FLD_PWRDN_TUNING_BIAS |
							FLD_PWRDN_ENABLE_PLL,
							1);
				status |= cx231xx_read_i2c_data(dev,
373 374 375 376
							Colibri_DEVICE_ADDRESS,
							SUP_BLK_PWRDN, 2,
							&colibri_power_status,
							1);
377 378 379 380
				if (status < 0)
					break;
			}

381
			status |= cx231xx_write_i2c_data(dev,
382
						   Colibri_DEVICE_ADDRESS,
383 384
						   ADC_PWRDN_CLAMP_CH1, 2, 0x00,
						   1);
385
			status |= cx231xx_write_i2c_data(dev,
386
						   Colibri_DEVICE_ADDRESS,
387 388
						   ADC_PWRDN_CLAMP_CH2, 2, 0x00,
						   1);
389
			status |= cx231xx_write_i2c_data(dev,
390
						   Colibri_DEVICE_ADDRESS,
391 392 393 394 395 396 397 398 399
						   ADC_PWRDN_CLAMP_CH3, 2, 0x00,
						   1);
		} else {
			cx231xx_info("Invalid AV mode input\n");
			status = -1;
		}
		break;
	default:
		if (avmode == POLARIS_AVMODE_ANALOGT_TV) {
400 401
			while (colibri_power_status != (FLD_PWRDN_TUNING_BIAS |
						FLD_PWRDN_ENABLE_PLL)) {
402 403 404
				status = cx231xx_write_i2c_data(dev,
							Colibri_DEVICE_ADDRESS,
							SUP_BLK_PWRDN, 2,
405 406 407 408
							FLD_PWRDN_TUNING_BIAS |
							FLD_PWRDN_ENABLE_PLL,
							1);
				status |= cx231xx_read_i2c_data(dev,
409 410 411 412
							Colibri_DEVICE_ADDRESS,
							SUP_BLK_PWRDN, 2,
							&colibri_power_status,
							1);
413 414 415 416
				if (status < 0)
					break;
			}

417
			status |= cx231xx_write_i2c_data(dev,
418 419 420
							Colibri_DEVICE_ADDRESS,
							ADC_PWRDN_CLAMP_CH1, 2,
							0x40, 1);
421
			status |= cx231xx_write_i2c_data(dev,
422 423 424
							Colibri_DEVICE_ADDRESS,
							ADC_PWRDN_CLAMP_CH2, 2,
							0x40, 1);
425
			status |= cx231xx_write_i2c_data(dev,
426 427 428
							Colibri_DEVICE_ADDRESS,
							ADC_PWRDN_CLAMP_CH3, 2,
							0x00, 1);
429
		} else if (avmode == POLARIS_AVMODE_DIGITAL) {
430 431 432 433
			status = cx231xx_write_i2c_data(dev,
							Colibri_DEVICE_ADDRESS,
							ADC_PWRDN_CLAMP_CH1, 2,
							0x70, 1);
434
			status |= cx231xx_write_i2c_data(dev,
435 436 437
							Colibri_DEVICE_ADDRESS,
							ADC_PWRDN_CLAMP_CH2, 2,
							0x70, 1);
438
			status |= cx231xx_write_i2c_data(dev,
439 440 441 442
							Colibri_DEVICE_ADDRESS,
							ADC_PWRDN_CLAMP_CH3, 2,
							0x70, 1);

443
			status |= cx231xx_read_i2c_data(dev,
444 445 446 447
						       Colibri_DEVICE_ADDRESS,
						       SUP_BLK_PWRDN, 2,
						       &colibri_power_status,
						       1);
448 449 450 451
			colibri_power_status |= FLD_PWRDN_PD_BANDGAP |
						FLD_PWRDN_PD_BIAS |
						FLD_PWRDN_PD_TUNECK;
			status |= cx231xx_write_i2c_data(dev,
452 453 454 455
							Colibri_DEVICE_ADDRESS,
							SUP_BLK_PWRDN, 2,
							colibri_power_status,
							1);
456
		} else if (avmode == POLARIS_AVMODE_ENXTERNAL_AV) {
457 458
			while (colibri_power_status != (FLD_PWRDN_TUNING_BIAS |
						FLD_PWRDN_ENABLE_PLL)) {
459 460 461
				status = cx231xx_write_i2c_data(dev,
							Colibri_DEVICE_ADDRESS,
							SUP_BLK_PWRDN, 2,
462 463 464 465
							FLD_PWRDN_TUNING_BIAS |
							FLD_PWRDN_ENABLE_PLL,
							1);
				status |= cx231xx_read_i2c_data(dev,
466 467 468 469
							Colibri_DEVICE_ADDRESS,
							SUP_BLK_PWRDN, 2,
							&colibri_power_status,
							1);
470 471 472 473
				if (status < 0)
					break;
			}

474
			status |= cx231xx_write_i2c_data(dev,
475 476 477
							Colibri_DEVICE_ADDRESS,
							ADC_PWRDN_CLAMP_CH1, 2,
							0x00, 1);
478
			status |= cx231xx_write_i2c_data(dev,
479 480 481
							Colibri_DEVICE_ADDRESS,
							ADC_PWRDN_CLAMP_CH2, 2,
							0x00, 1);
482
			status |= cx231xx_write_i2c_data(dev,
483 484 485
							Colibri_DEVICE_ADDRESS,
							ADC_PWRDN_CLAMP_CH3, 2,
							0x40, 1);
486 487 488 489 490 491 492
		} else {
			cx231xx_info("Invalid AV mode input\n");
			status = -1;
		}
	}			/* switch  */

	return status;
493 494 495 496
}

int cx231xx_colibri_adjust_ref_count(struct cx231xx *dev, u32 video_input)
{
497 498 499 500 501 502 503
	u32 input_mode = 0;
	u32 ntf_mode = 0;
	int status = 0;

	dev->video_input = video_input;

	if (video_input == CX231XX_VMUX_TELEVISION) {
504
		status = cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
505
					  ADC_INPUT_CH3, 2, &input_mode, 1);
506
		status = cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
507 508 509
					  ADC_NTF_PRECLMP_EN_CH3, 2, &ntf_mode,
					  1);
	} else {
510
		status = cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
511
					  ADC_INPUT_CH1, 2, &input_mode, 1);
512
		status = cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
513 514 515
					  ADC_NTF_PRECLMP_EN_CH1, 2, &ntf_mode,
					  1);
	}
516

517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
	input_mode = (ntf_mode & 0x3) | ((input_mode & 0x6) << 1);

	switch (input_mode) {
	case SINGLE_ENDED:
		dev->colibri_ref_count = 0x23C;
		break;
	case LOW_IF:
		dev->colibri_ref_count = 0x24C;
		break;
	case EU_IF:
		dev->colibri_ref_count = 0x258;
		break;
	case US_IF:
		dev->colibri_ref_count = 0x260;
		break;
	default:
		break;
	}

	status = cx231xx_colibri_init_super_block(dev, dev->colibri_ref_count);
537

538 539
	return status;
}
540

541 542
/******************************************************************************
 *     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    *
543
 ******************************************************************************/
544 545
int cx231xx_set_video_input_mux(struct cx231xx *dev, u8 input)
{
546 547 548 549 550 551 552
	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)) {
553 554 555
			/* External AV */
			status = cx231xx_set_power_mode(dev,
					POLARIS_AVMODE_ENXTERNAL_AV);
556
			if (status < 0) {
557 558 559
				cx231xx_errdev("%s: set_power_mode : Failed to"
						" set Power - errCode [%d]!\n",
						__func__, status);
560 561 562
				return status;
			}
		}
563 564 565
		status = cx231xx_set_decoder_video_input(dev,
							 INPUT(input)->type,
							 INPUT(input)->vmux);
566 567 568 569 570
		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)) {
571 572 573
			/* Tuner */
			status = cx231xx_set_power_mode(dev,
						POLARIS_AVMODE_ANALOGT_TV);
574
			if (status < 0) {
575 576 577
				cx231xx_errdev("%s: set_power_mode:Failed"
					" to set Power - errCode [%d]!\n",
					__func__, status);
578 579 580
				return status;
			}
		}
581 582 583
		status = cx231xx_set_decoder_video_input(dev,
							CX231XX_VMUX_COMPOSITE1,
							INPUT(input)->vmux);
584 585
		break;
	default:
586
		cx231xx_errdev("%s: set_power_mode : Unknown Input %d !\n",
587 588 589 590 591 592 593 594
		     __func__, INPUT(input)->type);
		break;
	}

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

	return status;
595 596
}

597 598
int cx231xx_set_decoder_video_input(struct cx231xx *dev,
				u8 pin_type, u8 input)
599
{
600 601 602 603 604 605
	int status = 0;
	u32 value = 0;

	if (pin_type != dev->video_input) {
		status = cx231xx_colibri_adjust_ref_count(dev, pin_type);
		if (status < 0) {
606 607 608
			cx231xx_errdev("%s: adjust_ref_count :Failed to set"
				"Colibri input mux - errCode [%d]!\n",
				__func__, status);
609 610 611
			return status;
		}
	}
612

613 614 615
	/* call colibri block to set video inputs */
	status = cx231xx_colibri_set_input_mux(dev, input);
	if (status < 0) {
616 617 618
		cx231xx_errdev("%s: set_input_mux :Failed to set"
				" Colibri input mux - errCode [%d]!\n",
				__func__, status);
619 620 621 622 623
		return status;
	}

	switch (pin_type) {
	case CX231XX_VMUX_COMPOSITE1:
624 625 626 627 628 629
		status = cx231xx_read_i2c_data(dev,
						HAMMERHEAD_I2C_ADDRESS,
						AFE_CTRL, 2, &value, 4);
		value |= (0 << 13) | (1 << 4);
		value &= ~(1 << 5);

630 631 632 633
		/* set [24:23] [22:15] to 0  */
		value &= (~(0x1ff8000));
		/* set FUNC_MODE[24:23] = 2 IF_MOD[22:15] = 0  */
		value |= 0x1000000;
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
						AFE_CTRL, 2, value, 4);

		status = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
						OUT_CTRL1, 2, &value, 4);
		value |= (1 << 7);
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
						OUT_CTRL1, 2, value, 4);

		/* Set vip 1.1 output mode */
		status = cx231xx_read_modify_write_i2c_dword(dev,
							HAMMERHEAD_I2C_ADDRESS,
							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) {
653 654
			cx231xx_errdev("%s: cx231xx_dif set to By pass"
						   " mode- errCode [%d]!\n",
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
				__func__, status);
			return status;
		}

		/* Read the DFE_CTRL1 register */
		status = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
						DFE_CTRL1, 2, &value, 4);

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

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

		/* Write it back */
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
						DFE_CTRL1, 2, value, 4);

		/* Disable auto config of registers */
		status = cx231xx_read_modify_write_i2c_dword(dev,
					HAMMERHEAD_I2C_ADDRESS,
					MODE_CTRL, FLD_ACFG_DIS,
					cx231xx_set_field(FLD_ACFG_DIS, 1));

		/* Set CVBS input mode */
		status = cx231xx_read_modify_write_i2c_dword(dev,
			HAMMERHEAD_I2C_ADDRESS,
			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 */

		status = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
					       AFE_CTRL, 2, &value, 4);

691 692 693 694 695
		/* 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;
696 697 698 699 700 701 702
		status = cx231xx_write_i2c_data(dev,
						HAMMERHEAD_I2C_ADDRESS,
						AFE_CTRL, 2, value, 4);

		/* Tell DIF object to go to baseband mode */
		status = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND);
		if (status < 0) {
703 704
			cx231xx_errdev("%s: cx231xx_dif set to By pass"
						   " mode- errCode [%d]!\n",
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
				__func__, status);
			return status;
		}

		/* Read the DFE_CTRL1 register */
		status = cx231xx_read_i2c_data(dev,
					       HAMMERHEAD_I2C_ADDRESS,
					       DFE_CTRL1, 2, &value, 4);

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

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

		/* Write it back */
		status = cx231xx_write_i2c_data(dev,
						HAMMERHEAD_I2C_ADDRESS,
						DFE_CTRL1, 2, value, 4);

		/* Disable auto config of registers  */
		status =  cx231xx_read_modify_write_i2c_dword(dev,
					HAMMERHEAD_I2C_ADDRESS,
					MODE_CTRL, FLD_ACFG_DIS,
					cx231xx_set_field(FLD_ACFG_DIS, 1));

		/* Set YC input mode */
		status = cx231xx_read_modify_write_i2c_dword(dev,
			HAMMERHEAD_I2C_ADDRESS,
			MODE_CTRL,
			FLD_INPUT_MODE,
			cx231xx_set_field(FLD_INPUT_MODE, INPUT_MODE_YC_1));

		/* Chroma to ADC2 */
		status = cx231xx_read_i2c_data(dev,
						HAMMERHEAD_I2C_ADDRESS,
						AFE_CTRL, 2, &value, 4);
		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);

		status = cx231xx_write_i2c_data(dev,
						HAMMERHEAD_I2C_ADDRESS,
						AFE_CTRL, 2, value, 4);

		status = cx231xx_colibri_set_mode(dev, AFE_MODE_BASEBAND);
		break;
	case CX231XX_VMUX_TELEVISION:
	case CX231XX_VMUX_CABLE:
	default:
		switch (dev->model) {
		case CX231XX_BOARD_CNXT_RDE_250:
		case CX231XX_BOARD_CNXT_RDU_250:
			/* Disable the use of  DIF   */

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

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

779 780 781 782
			status = cx231xx_read_i2c_data(dev,
						       HAMMERHEAD_I2C_ADDRESS,
						       OUT_CTRL1, 2,
						       &value, 4);
783
			value |= (1 << 7);
784 785 786 787
			status = cx231xx_write_i2c_data(dev,
							HAMMERHEAD_I2C_ADDRESS,
							OUT_CTRL1, 2,
							value, 4);
788 789

			/* Set vip 1.1 output mode */
790 791 792 793
			status = cx231xx_read_modify_write_i2c_dword(dev,
							HAMMERHEAD_I2C_ADDRESS,
							OUT_CTRL1, FLD_OUT_MODE,
							OUT_MODE_VIP11);
794

795 796 797
			/* Tell DIF object to go to baseband mode */
			status = cx231xx_dif_set_standard(dev,
							  DIF_USE_BASEBAND);
798
			if (status < 0) {
799 800 801
				cx231xx_errdev("%s: cx231xx_dif set to By pass"
						" mode- errCode [%d]!\n",
						__func__, status);
802 803 804 805
				return status;
			}

			/* Read the DFE_CTRL1 register */
806 807 808 809
			status = cx231xx_read_i2c_data(dev,
						       HAMMERHEAD_I2C_ADDRESS,
						       DFE_CTRL1, 2,
						       &value, 4);
810 811 812 813 814 815 816 817

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

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

			/* Write it back */
818 819 820 821
			status = cx231xx_write_i2c_data(dev,
							HAMMERHEAD_I2C_ADDRESS,
							DFE_CTRL1, 2,
							value, 4);
822 823

			/* Disable auto config of registers */
824 825 826 827
			status = cx231xx_read_modify_write_i2c_dword(dev,
					HAMMERHEAD_I2C_ADDRESS,
					MODE_CTRL, FLD_ACFG_DIS,
					cx231xx_set_field(FLD_ACFG_DIS, 1));
828 829

			/* Set CVBS input mode */
830 831 832
			status = cx231xx_read_modify_write_i2c_dword(dev,
				HAMMERHEAD_I2C_ADDRESS,
				MODE_CTRL, FLD_INPUT_MODE,
833 834
				cx231xx_set_field(FLD_INPUT_MODE,
						INPUT_MODE_CVBS_0));
835 836 837
			break;
		default:
			/* Enable the DIF for the tuner */
838

839 840
			/* Reinitialize the DIF */
			status = cx231xx_dif_set_standard(dev, dev->norm);
841
			if (status < 0) {
842 843 844
				cx231xx_errdev("%s: cx231xx_dif set to By pass"
						" mode- errCode [%d]!\n",
						__func__, status);
845 846 847
				return status;
			}

848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
			/* Make sure bypass is cleared */
			status = cx231xx_read_i2c_data(dev,
						      HAMMERHEAD_I2C_ADDRESS,
						      DIF_MISC_CTRL,
						      2, &value, 4);

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

			/* Enable the use of the DIF block */
			status = cx231xx_write_i2c_data(dev,
							HAMMERHEAD_I2C_ADDRESS,
							DIF_MISC_CTRL,
							2, value, 4);

863
			/* Read the DFE_CTRL1 register */
864 865 866 867
			status = cx231xx_read_i2c_data(dev,
							HAMMERHEAD_I2C_ADDRESS,
							DFE_CTRL1, 2,
							&value, 4);
868

869 870
			/* Disable the VBI_GATE_EN */
			value &= ~FLD_VBI_GATE_EN;
871

872 873 874
			/* Enable the auto-VGA enable, AGC, and
			   set the skip count to 2 */
			value |= FLD_VGA_AUTO_EN | FLD_AGC_AUTO_EN | 0x00200000;
875 876

			/* Write it back */
877 878 879 880
			status = cx231xx_write_i2c_data(dev,
							HAMMERHEAD_I2C_ADDRESS,
							DFE_CTRL1, 2,
							value, 4);
881

882
			/* Wait until AGC locks up */
883
			msleep(1);
884

885 886
			/* Disable the auto-VGA enable AGC */
			value &= ~(FLD_VGA_AUTO_EN);
887

888 889 890 891 892
			/* Write it back */
			status = cx231xx_write_i2c_data(dev,
							HAMMERHEAD_I2C_ADDRESS,
							DFE_CTRL1, 2,
							value, 4);
893

894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
			/* Enable Polaris B0 AGC output */
			status = cx231xx_read_i2c_data(dev,
							HAMMERHEAD_I2C_ADDRESS,
							PIN_CTRL, 2,
							&value, 4);
			value |= (FLD_OEF_AGC_RF) |
				 (FLD_OEF_AGC_IFVGA) |
				 (FLD_OEF_AGC_IF);
			status = cx231xx_write_i2c_data(dev,
							HAMMERHEAD_I2C_ADDRESS,
							PIN_CTRL, 2,
							value, 4);

			/* Set vip 1.1 output mode */
			status = cx231xx_read_modify_write_i2c_dword(dev,
						HAMMERHEAD_I2C_ADDRESS,
						OUT_CTRL1, FLD_OUT_MODE,
						OUT_MODE_VIP11);

			/* Disable auto config of registers */
			status = cx231xx_read_modify_write_i2c_dword(dev,
					HAMMERHEAD_I2C_ADDRESS,
					MODE_CTRL, FLD_ACFG_DIS,
					cx231xx_set_field(FLD_ACFG_DIS, 1));

			/* Set CVBS input mode */
			status = cx231xx_read_modify_write_i2c_dword(dev,
				HAMMERHEAD_I2C_ADDRESS,
				MODE_CTRL, FLD_INPUT_MODE,
923 924
				cx231xx_set_field(FLD_INPUT_MODE,
						INPUT_MODE_CVBS_0));
925

926 927
			/* Set some bits in AFE_CTRL so that channel 2 or 3
			 * is ready to receive audio */
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
			/* 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) */
			status = cx231xx_read_i2c_data(dev,
							HAMMERHEAD_I2C_ADDRESS,
							AFE_CTRL, 2,
							&value, 4);

			value |= FLD_VGA_SEL_CH3 | FLD_VGA_SEL_CH2;

			status = cx231xx_write_i2c_data(dev,
							HAMMERHEAD_I2C_ADDRESS,
							AFE_CTRL, 2,
							value, 4);
			break;
943 944 945 946 947 948

		}
		break;
	}

	/* Set raw VBI mode */
949 950 951 952
	status = cx231xx_read_modify_write_i2c_dword(dev,
				HAMMERHEAD_I2C_ADDRESS,
				OUT_CTRL1, FLD_VBIHACTRAW_EN,
				cx231xx_set_field(FLD_VBIHACTRAW_EN, 1));
953

954 955 956
	status = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
				       OUT_CTRL1, 2,
				       &value, 4);
957 958
	if (value & 0x02) {
		value |= (1 << 19);
959
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
960 961 962 963
					   OUT_CTRL1, 2, value, 4);
	}

	return status;
964 965 966
}

/*
967 968 969
 * 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
970 971 972
 */
int cx231xx_do_mode_ctrl_overrides(struct cx231xx *dev)
{
973 974 975 976 977 978
	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 */
979 980 981
	status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
					DFE_CTRL3, 2,
					0xCD3F0280, 4);
982

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

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

1003
		status = cx231xx_read_modify_write_i2c_dword(dev,
1004 1005 1006 1007 1008
							HAMMERHEAD_I2C_ADDRESS,
							HORIZ_TIM_CTRL,
							FLD_HBLANK_CNT,
							cx231xx_set_field
							(FLD_HBLANK_CNT, 0x79));
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 1013 1014 1015
							HAMMERHEAD_I2C_ADDRESS,
							VERT_TIM_CTRL,
							FLD_VBLANK_CNT, 0x24);
		/* Adjust the active video horizontal start point */
1016
		status = cx231xx_read_modify_write_i2c_dword(dev,
1017 1018 1019 1020 1021
							HAMMERHEAD_I2C_ADDRESS,
							HORIZ_TIM_CTRL,
							FLD_HBLANK_CNT,
							cx231xx_set_field
							(FLD_HBLANK_CNT, 0x85));
1022 1023 1024
	} else {
		cx231xx_info("do_mode_ctrl_overrides PAL\n");
		status = cx231xx_read_modify_write_i2c_dword(dev,
1025 1026 1027 1028
							HAMMERHEAD_I2C_ADDRESS,
							VERT_TIM_CTRL,
							FLD_VBLANK_CNT, 0x24);
		/* Adjust the active video horizontal start point */
1029
		status = cx231xx_read_modify_write_i2c_dword(dev,
1030 1031 1032 1033 1034 1035 1036 1037
							HAMMERHEAD_I2C_ADDRESS,
							HORIZ_TIM_CTRL,
							FLD_HBLANK_CNT,
							cx231xx_set_field
							(FLD_HBLANK_CNT, 0x85));
	}

	return status;
1038 1039 1040 1041
}

int cx231xx_set_audio_input(struct cx231xx *dev, u8 input)
{
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
	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:
		status = cx231xx_flatiron_set_audio_input(dev, input);
		ainput = AUDIO_INPUT_LINE;
		break;
	default:
		break;
	}

	status = cx231xx_set_audio_decoder_input(dev, ainput);

	return status;
1060 1061
}

1062 1063
int cx231xx_set_audio_decoder_input(struct cx231xx *dev,
				    enum AUDIO_INPUT audio_input)
1064
{
1065 1066 1067 1068 1069 1070
	u32 dwval;
	int status;
	u32 gen_ctrl;
	u32 value = 0;

	/* Put it in soft reset   */
1071 1072
	status = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
				       GENERAL_CTL, 2, &gen_ctrl, 1);
1073
	gen_ctrl |= 1;
1074 1075
	status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
					GENERAL_CTL, 2, gen_ctrl, 1);
1076 1077 1078 1079

	switch (audio_input) {
	case AUDIO_INPUT_LINE:
		/* setup AUD_IO control from Merlin paralle output */
1080 1081 1082 1083 1084
		value = cx231xx_set_field(FLD_AUD_CHAN1_SRC,
					  AUD_CHAN_SRC_PARALLEL);
		status = cx231xx_write_i2c_data(dev,
						HAMMERHEAD_I2C_ADDRESS,
						AUD_IO_CTRL, 2, value, 4);
1085 1086 1087 1088

		/* setup input to Merlin, SRC2 connect to AC97
		   bypass upsample-by-2, slave mode, sony mode, left justify
		   adr 091c, dat 01000000 */
1089 1090
		status = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
					       AC97_CTL,
1091 1092
					  2, &dwval, 4);

1093
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1094 1095 1096 1097
					   AC97_CTL, 2,
					   (dwval | FLD_AC97_UP2X_BYPASS), 4);

		/* select the parallel1 and SRC3 */
1098 1099 1100 1101 1102 1103
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
				BAND_OUT_SEL, 2,
				cx231xx_set_field(FLD_SRC3_IN_SEL, 0x0) |
				cx231xx_set_field(FLD_SRC3_CLK_SEL, 0x0) |
				cx231xx_set_field(FLD_PARALLEL1_SRC_SEL, 0x0),
				4);
1104 1105 1106

		/* unmute all, AC97 in, independence mode
		   adr 08d0, data 0x00063073 */
1107
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1108 1109 1110
					   PATH1_CTL1, 2, 0x00063073, 4);

		/* set AVC maximum threshold, adr 08d4, dat ffff0024 */
1111
		status = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1112
					  PATH1_VOL_CTL, 2, &dwval, 4);
1113
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1114 1115 1116 1117 1118
					   PATH1_VOL_CTL, 2,
					   (dwval | FLD_PATH1_AVC_THRESHOLD),
					   4);

		/* set SC maximum threshold, adr 08ec, dat ffffb3a3 */
1119
		status = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1120
					  PATH1_SC_CTL, 2, &dwval, 4);
1121
		status =  cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1122 1123 1124 1125 1126 1127 1128 1129
					   PATH1_SC_CTL, 2,
					   (dwval | FLD_PATH1_SC_THRESHOLD), 4);
		break;

	case AUDIO_INPUT_TUNER_TV:
	default:

		/* Setup SRC sources and clocks */
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
			BAND_OUT_SEL, 2,
			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)   |
			cx231xx_set_field(FLD_PARALLEL1_SRC_SEL, 0x01), 4);
1145 1146

		/* Setup the AUD_IO control */
1147 1148 1149 1150 1151 1152 1153
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
			AUD_IO_CTRL, 2,
			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) |
			cx231xx_set_field(FLD_AUD_CHAN1_SRC, 0x03), 4);
1154

1155
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1156 1157 1158 1159
					   PATH1_CTL1, 2, 0x1F063870, 4);

		/* setAudioStandard(_audio_standard); */

1160
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1161 1162 1163 1164
					   PATH1_CTL1, 2, 0x00063870, 4);
		switch (dev->model) {
		case CX231XX_BOARD_CNXT_RDE_250:
		case CX231XX_BOARD_CNXT_RDU_250:
1165 1166 1167 1168 1169
			status = cx231xx_read_modify_write_i2c_dword(dev,
					HAMMERHEAD_I2C_ADDRESS,
					CHIP_CTRL,
					FLD_SIF_EN,
					cx231xx_set_field(FLD_SIF_EN, 1));
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
			break;
		default:
			break;
		}
		break;

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

	case AUDIO_INPUT_MUTE:
1184
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1185 1186 1187
					   PATH1_CTL1, 2, 0x1F011012, 4);
		break;
	}
1188

1189
	/* Take it out of soft reset */
1190 1191
	status = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
				       GENERAL_CTL, 2,  &gen_ctrl, 1);
1192
	gen_ctrl &= ~1;
1193 1194
	status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
					GENERAL_CTL, 2, gen_ctrl, 1);
1195

1196 1197
	return status;
}
1198 1199 1200 1201 1202

/* Set resolution of the video */
int cx231xx_resolution_set(struct cx231xx *dev)
{
	int width, height;
1203 1204
	u32 hscale, vscale;
	int status = 0;
1205 1206 1207 1208

	width = dev->width;
	height = dev->height;

1209
	get_scale(dev, width, height, &hscale, &vscale);
1210

1211
	/* set horzontal scale */
1212 1213
	status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
					HSCALE_CTRL, 2, hscale, 4);
1214

1215
	/* set vertical scale */
1216 1217
	status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
					VSCALE_CTRL, 2, vscale, 4);
1218

1219
	return status;
1220 1221
}

1222 1223 1224
/******************************************************************************
 *                    C H I P Specific  C O N T R O L   functions             *
 ******************************************************************************/
1225 1226
int cx231xx_init_ctrl_pin_status(struct cx231xx *dev)
{
1227 1228
	u32 value;
	int status = 0;
1229

1230 1231
	status = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS, PIN_CTRL,
				       2, &value, 4);
1232
	value |= (~dev->board.ctl_pin_status_mask);
1233 1234
	status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS, PIN_CTRL,
					2, value, 4);
1235

1236
	return status;
1237 1238
}

1239 1240
int cx231xx_set_agc_analog_digital_mux_select(struct cx231xx *dev,
					      u8 analog_or_digital)
1241
{
1242
	int status = 0;
1243

1244
	/* first set the direction to output */
1245 1246 1247
	status = cx231xx_set_gpio_direction(dev,
					    dev->board.
					    agc_analog_digital_select_gpio, 1);
1248

1249
	/* 0 - demod ; 1 - Analog mode */
1250
	status = cx231xx_set_gpio_value(dev,
1251 1252
				   dev->board.agc_analog_digital_select_gpio,
				   analog_or_digital);
1253

1254
	return status;
1255 1256 1257 1258
}

int cx231xx_enable_i2c_for_tuner(struct cx231xx *dev, u8 I2CIndex)
{
1259 1260
	u8 value[4] = { 0, 0, 0, 0 };
	int status = 0;
1261

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

1264 1265
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
				       PWR_CTL_EN, value, 4);
1266 1267
	if (status < 0)
		return status;
1268

1269 1270 1271
	if (I2CIndex == I2C_1) {
		if (value[0] & I2C_DEMOD_EN) {
			value[0] &= ~I2C_DEMOD_EN;
1272
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1273 1274 1275 1276 1277
						   PWR_CTL_EN, value, 4);
		}
	} else {
		if (!(value[0] & I2C_DEMOD_EN)) {
			value[0] |= I2C_DEMOD_EN;
1278
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1279 1280 1281
						   PWR_CTL_EN, value, 4);
		}
	}
1282

1283
	return status;
1284 1285 1286

}

1287 1288 1289
/******************************************************************************
 *                 D I F - B L O C K    C O N T R O L   functions             *
 ******************************************************************************/
1290
int cx231xx_dif_configure_C2HH_for_low_IF(struct cx231xx *dev, u32 mode,
1291
					  u32 function_mode, u32 standard)
1292
{
1293 1294 1295 1296
	int status = 0;

	if (mode == V4L2_TUNER_RADIO) {
		/* C2HH */
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
		/* lo if big signal */
		status = cx231xx_reg_mask_write(dev,
				HAMMERHEAD_I2C_ADDRESS, 32,
				AFE_CTRL_C2HH_SRC_CTRL, 30, 31, 0x1);
		/* FUNC_MODE = DIF */
		status = cx231xx_reg_mask_write(dev,
				HAMMERHEAD_I2C_ADDRESS, 32,
				AFE_CTRL_C2HH_SRC_CTRL, 23, 24, function_mode);
		/* IF_MODE */
		status = cx231xx_reg_mask_write(dev,
				HAMMERHEAD_I2C_ADDRESS, 32,
				AFE_CTRL_C2HH_SRC_CTRL, 15, 22, 0xFF);
		/* no inv */
		status = cx231xx_reg_mask_write(dev,
				HAMMERHEAD_I2C_ADDRESS, 32,
				AFE_CTRL_C2HH_SRC_CTRL, 9, 9, 0x1);
1313 1314
	} else if (standard != DIF_USE_BASEBAND) {
		if (standard & V4L2_STD_MN) {
1315
			/* lo if big signal */
1316 1317
			status = cx231xx_reg_mask_write(dev,
					HAMMERHEAD_I2C_ADDRESS, 32,
1318 1319
					AFE_CTRL_C2HH_SRC_CTRL, 30, 31, 0x1);
			/* FUNC_MODE = DIF */
1320 1321 1322
			status = cx231xx_reg_mask_write(dev,
					HAMMERHEAD_I2C_ADDRESS, 32,
					AFE_CTRL_C2HH_SRC_CTRL, 23, 24,
1323 1324
					function_mode);
			/* IF_MODE */
1325 1326
			status = cx231xx_reg_mask_write(dev,
					HAMMERHEAD_I2C_ADDRESS, 32,
1327 1328
					AFE_CTRL_C2HH_SRC_CTRL, 15, 22, 0xb);
			/* no inv */
1329 1330
			status = cx231xx_reg_mask_write(dev,
					HAMMERHEAD_I2C_ADDRESS, 32,
1331 1332
					AFE_CTRL_C2HH_SRC_CTRL, 9, 9, 0x1);
			/* 0x124, AUD_CHAN1_SRC = 0x3 */
1333 1334
			status = cx231xx_reg_mask_write(dev,
					HAMMERHEAD_I2C_ADDRESS, 32,
1335
					AUD_IO_CTRL, 0, 31, 0x00000003);
1336 1337
		} else if ((standard == V4L2_STD_PAL_I) |
			(standard & V4L2_STD_SECAM)) {
1338
			/* C2HH setup */
1339
			/* lo if big signal */
1340 1341
			status = cx231xx_reg_mask_write(dev,
					HAMMERHEAD_I2C_ADDRESS, 32,
1342 1343
					AFE_CTRL_C2HH_SRC_CTRL, 30, 31, 0x1);
			/* FUNC_MODE = DIF */
1344 1345 1346
			status = cx231xx_reg_mask_write(dev,
					HAMMERHEAD_I2C_ADDRESS, 32,
					AFE_CTRL_C2HH_SRC_CTRL, 23, 24,
1347 1348
					function_mode);
			/* IF_MODE */
1349 1350
			status = cx231xx_reg_mask_write(dev,
					HAMMERHEAD_I2C_ADDRESS, 32,
1351
					AFE_CTRL_C2HH_SRC_CTRL, 15, 22, 0xF);
1352
			/* no inv */
1353 1354
			status = cx231xx_reg_mask_write(dev,
					HAMMERHEAD_I2C_ADDRESS, 32,
1355
					AFE_CTRL_C2HH_SRC_CTRL, 9, 9, 0x1);
1356 1357
		} else {
			/* default PAL BG */
1358
			/* C2HH setup */
1359
			/* lo if big signal */
1360 1361
			status = cx231xx_reg_mask_write(dev,
					HAMMERHEAD_I2C_ADDRESS, 32,
1362 1363
					AFE_CTRL_C2HH_SRC_CTRL, 30, 31, 0x1);
			/* FUNC_MODE = DIF */
1364 1365 1366
			status = cx231xx_reg_mask_write(dev,
					HAMMERHEAD_I2C_ADDRESS, 32,
					AFE_CTRL_C2HH_SRC_CTRL, 23, 24,
1367 1368
					function_mode);
			/* IF_MODE */
1369 1370
			status = cx231xx_reg_mask_write(dev,
					HAMMERHEAD_I2C_ADDRESS, 32,
1371
					AFE_CTRL_C2HH_SRC_CTRL, 15, 22, 0xE);
1372
			/* no inv */
1373 1374
			status = cx231xx_reg_mask_write(dev,
					HAMMERHEAD_I2C_ADDRESS, 32,
1375
					AFE_CTRL_C2HH_SRC_CTRL, 9, 9, 0x1);
1376 1377 1378 1379
		}
	}

	return status;
1380 1381 1382 1383
}

int cx231xx_dif_set_standard(struct cx231xx *dev, u32 standard)
{
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
	int status = 0;
	u32 dif_misc_ctrl_value = 0;
	u32 func_mode = 0;

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

	status = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
				       DIF_MISC_CTRL, 2, &dif_misc_ctrl_value,
				       4);
	if (standard != DIF_USE_BASEBAND)
		dev->norm = standard;

	switch (dev->model) {
	case CX231XX_BOARD_CNXT_RDE_250:
	case CX231XX_BOARD_CNXT_RDU_250:
		func_mode = 0x03;
		break;
	default:
		func_mode = 0x01;
	}

1405
	status = cx231xx_dif_configure_C2HH_for_low_IF(dev, dev->active_mode,
1406 1407 1408
						  func_mode, standard);

	if (standard == DIF_USE_BASEBAND) {	/* base band */
1409 1410
		/* There is a different SRC_PHASE_INC value
		   for baseband vs. DIF */
1411 1412 1413
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
						DIF_SRC_PHASE_INC, 2, 0xDF7DF83,
						4);
1414 1415 1416
		status = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
					       DIF_MISC_CTRL, 2,
					       &dif_misc_ctrl_value, 4);
1417 1418 1419 1420 1421
		dif_misc_ctrl_value |= FLD_DIF_DIF_BYPASS;
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
						DIF_MISC_CTRL, 2,
						dif_misc_ctrl_value, 4);
	} else if (standard & V4L2_STD_PAL_D) {
1422
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1423
					   DIF_PLL_CTRL, 0, 31, 0x6503bc0c);
1424
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1425
					   DIF_PLL_CTRL1, 0, 31, 0xbd038c85);
1426
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1427
					   DIF_PLL_CTRL2, 0, 31, 0x1db4640a);
1428
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1429
					   DIF_PLL_CTRL3, 0, 31, 0x00008800);
1430
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1431
					   DIF_AGC_IF_REF, 0, 31, 0x444C1380);
1432
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1433
					   DIF_AGC_CTRL_IF, 0, 31, 0xDA302600);
1434
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1435
					   DIF_AGC_CTRL_INT, 0, 31, 0xDA261700);
1436
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1437
					   DIF_AGC_CTRL_RF, 0, 31, 0xDA262600);
1438
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1439 1440
					   DIF_AGC_IF_INT_CURRENT, 0, 31,
					   0x26001700);
1441
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1442 1443
					   DIF_AGC_RF_CURRENT, 0, 31,
					   0x00002660);
1444
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1445 1446
					   DIF_VIDEO_AGC_CTRL, 0, 31,
					   0x72500800);
1447
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1448 1449
					   DIF_VID_AUD_OVERRIDE, 0, 31,
					   0x27000100);
1450
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1451
					   DIF_AV_SEP_CTRL, 0, 31, 0x3F3934EA);
1452
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1453 1454
					   DIF_COMP_FLT_CTRL, 0, 31,
					   0x00000000);
1455
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1456 1457
					   DIF_SRC_PHASE_INC, 0, 31,
					   0x1befbf06);
1458
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1459 1460
					   DIF_SRC_GAIN_CONTROL, 0, 31,
					   0x000035e8);
1461
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1462 1463 1464 1465 1466
					   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) {
1467
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1468
					   DIF_PLL_CTRL, 0, 31, 0x6503bc0c);
1469
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1470
					   DIF_PLL_CTRL1, 0, 31, 0xbd038c85);
1471
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1472
					   DIF_PLL_CTRL2, 0, 31, 0x1db4640a);
1473
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1474
					   DIF_PLL_CTRL3, 0, 31, 0x00008800);
1475
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1476
					   DIF_AGC_IF_REF, 0, 31, 0x444C1380);
1477
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1478
					   DIF_AGC_CTRL_IF, 0, 31, 0xDA302600);
1479
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1480
					   DIF_AGC_CTRL_INT, 0, 31, 0xDA261700);
1481
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1482
					   DIF_AGC_CTRL_RF, 0, 31, 0xDA262600);
1483
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1484 1485
					   DIF_AGC_IF_INT_CURRENT, 0, 31,
					   0x26001700);
1486
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1487 1488
					   DIF_AGC_RF_CURRENT, 0, 31,
					   0x00002660);
1489
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1490 1491
					   DIF_VIDEO_AGC_CTRL, 0, 31,
					   0x72500800);
1492
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1493 1494
					   DIF_VID_AUD_OVERRIDE, 0, 31,
					   0x27000100);
1495
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1496
					   DIF_AV_SEP_CTRL, 0, 31, 0x5F39A934);
1497
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1498 1499
					   DIF_COMP_FLT_CTRL, 0, 31,
					   0x00000000);
1500
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1501 1502
					   DIF_SRC_PHASE_INC, 0, 31,
					   0x1befbf06);
1503
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1504 1505
					   DIF_SRC_GAIN_CONTROL, 0, 31,
					   0x000035e8);
1506
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
					   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 */
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
						DIF_PLL_CTRL, 2, 0xFF01FF0C, 4);
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
						DIF_PLL_CTRL1, 2, 0xbd038c85,
						4);
1518
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1519
					   DIF_PLL_CTRL2, 2, 0x1db4640a, 4);
1520
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1521
					   DIF_PLL_CTRL3, 2, 0x00008800, 4);
1522
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1523
					   DIF_AGC_IF_REF, 2, 0x444C1380, 4);
1524
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1525 1526
					   DIF_AGC_IF_INT_CURRENT, 2,
					   0x26001700, 4);
1527
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1528 1529
					   DIF_AGC_RF_CURRENT, 2, 0x00002660,
					   4);
1530
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1531 1532
					   DIF_VIDEO_AGC_CTRL, 2, 0x72500800,
					   4);
1533
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1534 1535
					   DIF_VID_AUD_OVERRIDE, 2, 0x27000100,
					   4);
1536
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1537
					   DIF_AV_SEP_CTRL, 2, 0x012c405d, 4);
1538
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1539
					   DIF_COMP_FLT_CTRL, 2, 0x009f50c1, 4);
1540
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1541
					   DIF_SRC_PHASE_INC, 2, 0x1befbf06, 4);
1542
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1543 1544
					   DIF_SRC_GAIN_CONTROL, 2, 0x000035e8,
					   4);
1545
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1546 1547 1548 1549 1550 1551 1552
					   DIF_SOFT_RST_CTRL_REVB, 2,
					   0x00000000, 4);
		/* 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 */
1553
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1554
					   DIF_PLL_CTRL, 2, 0xFF01FF0C, 4);
1555
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1556
					   DIF_PLL_CTRL1, 2, 0xbd038c85, 4);
1557
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1558
					   DIF_PLL_CTRL2, 2, 0x1db4640a, 4);
1559
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1560
					   DIF_PLL_CTRL3, 2, 0x00008800, 4);
1561
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1562
					   DIF_AGC_IF_REF, 2, 0x444C1380, 4);
1563
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1564 1565
					   DIF_AGC_IF_INT_CURRENT, 2,
					   0x26001700, 4);
1566
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1567 1568
					   DIF_AGC_RF_CURRENT, 2, 0x00002660,
					   4);
1569
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1570 1571
					   DIF_VIDEO_AGC_CTRL, 2, 0x72500800,
					   4);
1572
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1573 1574
					   DIF_VID_AUD_OVERRIDE, 2, 0x27000100,
					   4);
1575
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1576
					   DIF_AV_SEP_CTRL, 2, 0x012c405d, 4);
1577
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1578
					   DIF_COMP_FLT_CTRL, 2, 0x009f50c1, 4);
1579
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1580
					   DIF_SRC_PHASE_INC, 2, 0x1befbf06, 4);
1581
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1582 1583
					   DIF_SRC_GAIN_CONTROL, 2, 0x000035e8,
					   4);
1584
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1585 1586 1587 1588 1589 1590
					   DIF_SOFT_RST_CTRL_REVB, 2,
					   0x00000000, 4);
		/* Save the Spec Inversion value */
		dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
		dif_misc_ctrl_value = 0x3A093F10;
	} else if (standard &
1591 1592
		  (V4L2_STD_SECAM_B | V4L2_STD_SECAM_D | V4L2_STD_SECAM_G |
		   V4L2_STD_SECAM_K | V4L2_STD_SECAM_K1)) {
1593

1594
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1595
					   DIF_PLL_CTRL, 0, 31, 0x6503bc0c);
1596
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1597
					   DIF_PLL_CTRL1, 0, 31, 0xbd038c85);
1598
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1599
					   DIF_PLL_CTRL2, 0, 31, 0x1db4640a);
1600
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1601
					   DIF_PLL_CTRL3, 0, 31, 0x00008800);
1602
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1603
					   DIF_AGC_IF_REF, 0, 31, 0x888C0380);
1604
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1605
					   DIF_AGC_CTRL_IF, 0, 31, 0xe0262600);
1606
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1607
					   DIF_AGC_CTRL_INT, 0, 31, 0xc2171700);
1608
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1609
					   DIF_AGC_CTRL_RF, 0, 31, 0xc2262600);
1610
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1611 1612
					   DIF_AGC_IF_INT_CURRENT, 0, 31,
					   0x26001700);
1613
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1614 1615
					   DIF_AGC_RF_CURRENT, 0, 31,
					   0x00002660);
1616
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1617 1618
					   DIF_VID_AUD_OVERRIDE, 0, 31,
					   0x27000100);
1619
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1620
					   DIF_AV_SEP_CTRL, 0, 31, 0x3F3530ec);
1621
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1622 1623
					   DIF_COMP_FLT_CTRL, 0, 31,
					   0x00000000);
1624
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1625 1626
					   DIF_SRC_PHASE_INC, 0, 31,
					   0x1befbf06);
1627
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1628 1629
					   DIF_SRC_GAIN_CONTROL, 0, 31,
					   0x000035e8);
1630
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1631
					   DIF_RPT_VARIANCE, 0, 31, 0x00000000);
1632
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1633 1634 1635 1636 1637 1638 1639 1640
					   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? */
1641
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1642
					   DIF_PLL_CTRL, 0, 31, 0x6503bc0c);
1643
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1644
					   DIF_PLL_CTRL1, 0, 31, 0xbd038c85);
1645
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1646
					   DIF_PLL_CTRL2, 0, 31, 0x1db4640a);
1647
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1648
					   DIF_PLL_CTRL3, 0, 31, 0x00008800);
1649
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1650
					   DIF_AGC_IF_REF, 0, 31, 0x888C0380);
1651
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1652
					   DIF_AGC_CTRL_IF, 0, 31, 0xe0262600);
1653
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1654
					   DIF_AGC_CTRL_INT, 0, 31, 0xc2171700);
1655
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1656
					   DIF_AGC_CTRL_RF, 0, 31, 0xc2262600);
1657
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1658 1659
					   DIF_AGC_IF_INT_CURRENT, 0, 31,
					   0x26001700);
1660
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1661 1662
					   DIF_AGC_RF_CURRENT, 0, 31,
					   0x00002660);
1663
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1664 1665
					   DIF_VID_AUD_OVERRIDE, 0, 31,
					   0x27000100);
1666
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1667
					   DIF_AV_SEP_CTRL, 0, 31, 0x3F3530ec);
1668
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1669 1670
					   DIF_COMP_FLT_CTRL, 0, 31,
					   0x00000000);
1671
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1672 1673
					   DIF_SRC_PHASE_INC, 0, 31,
					   0x1befbf06);
1674
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1675 1676
					   DIF_SRC_GAIN_CONTROL, 0, 31,
					   0x000035e8);
1677
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1678
					   DIF_RPT_VARIANCE, 0, 31, 0x00000000);
1679
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
1680 1681 1682 1683 1684 1685 1686
					   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;

1687
	} else if (standard & V4L2_STD_NTSC_M) {
1688 1689
		/* V4L2_STD_NTSC_M (75 IRE Setup) Or
		   V4L2_STD_NTSC_M_JP (Japan,  0 IRE Setup) */
1690

1691 1692 1693 1694
		/* 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
1695 1696
		 */

1697
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1698
					   DIF_PLL_CTRL, 2, 0x6503BC0C, 4);
1699
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1700
					   DIF_PLL_CTRL1, 2, 0xBD038C85, 4);
1701
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1702
					   DIF_PLL_CTRL2, 2, 0x1DB4640A, 4);
1703
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1704
					   DIF_PLL_CTRL3, 2, 0x00008800, 4);
1705
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1706
					   DIF_AGC_IF_REF, 2, 0x444C0380, 4);
1707
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1708 1709
					   DIF_AGC_IF_INT_CURRENT, 2,
					   0x26001700, 4);
1710
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1711 1712
					   DIF_AGC_RF_CURRENT, 2, 0x00002660,
					   4);
1713
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1714 1715
					   DIF_VIDEO_AGC_CTRL, 2, 0x04000800,
					   4);
1716
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1717 1718
					   DIF_VID_AUD_OVERRIDE, 2, 0x27000100,
					   4);
1719
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1720 1721
					   DIF_AV_SEP_CTRL, 2, 0x01296e1f, 4);

1722
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1723
					   DIF_COMP_FLT_CTRL, 2, 0x009f50c1, 4);
1724
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1725
					   DIF_SRC_PHASE_INC, 2, 0x1befbf06, 4);
1726
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1727 1728 1729
					   DIF_SRC_GAIN_CONTROL, 2, 0x000035e8,
					   4);

1730
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1731
					   DIF_AGC_CTRL_IF, 2, 0xC2262600, 4);
1732
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1733
					   DIF_AGC_CTRL_INT, 2, 0xC2262600, 4);
1734
		status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
1735 1736 1737 1738 1739
					   DIF_AGC_CTRL_RF, 2, 0xC2262600, 4);

		/* Save the Spec Inversion value */
		dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
		dif_misc_ctrl_value |= 0x3a003F10;
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
	} else {
		/* default PAL BG */
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
					   DIF_PLL_CTRL, 0, 31, 0x6503bc0c);
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
					   DIF_PLL_CTRL1, 0, 31, 0xbd038c85);
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
					   DIF_PLL_CTRL2, 0, 31, 0x1db4640a);
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
					   DIF_PLL_CTRL3, 0, 31, 0x00008800);
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
					   DIF_AGC_IF_REF, 0, 31, 0x444C1380);
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
					   DIF_AGC_CTRL_IF, 0, 31, 0xDA302600);
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
					   DIF_AGC_CTRL_INT, 0, 31, 0xDA261700);
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
					   DIF_AGC_CTRL_RF, 0, 31, 0xDA262600);
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
					   DIF_AGC_IF_INT_CURRENT, 0, 31,
					   0x26001700);
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
					   DIF_AGC_RF_CURRENT, 0, 31,
					   0x00002660);
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
					   DIF_VIDEO_AGC_CTRL, 0, 31,
					   0x72500800);
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
					   DIF_VID_AUD_OVERRIDE, 0, 31,
					   0x27000100);
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
					   DIF_AV_SEP_CTRL, 0, 31, 0x3F3530EC);
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
					   DIF_COMP_FLT_CTRL, 0, 31,
					   0x00A653A8);
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
					   DIF_SRC_PHASE_INC, 0, 31,
					   0x1befbf06);
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
					   DIF_SRC_GAIN_CONTROL, 0, 31,
					   0x000035e8);
		status = cx231xx_reg_mask_write(dev, HAMMERHEAD_I2C_ADDRESS, 32,
					   DIF_RPT_VARIANCE, 0, 31, 0x00000000);
		/* Save the Spec Inversion value */
		dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
		dif_misc_ctrl_value |= 0x3a013F11;
1786 1787 1788 1789 1790 1791
	}

	/* 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;

1792 1793
	/* It is still possible to get Set Standard calls even when we
	   are in FM mode.
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
	   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      */
	status =
	    cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS, DIF_MISC_CTRL,
				   2, dif_misc_ctrl_value, 4);

	return status;
1804 1805 1806 1807 1808 1809 1810 1811
}

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

	/* Set the RF and IF k_agc values to 3 */
1812 1813
	status = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
				       DIF_AGC_IF_REF, 2, &dwval, 4);
1814 1815 1816
	dwval &= ~(FLD_DIF_K_AGC_RF | FLD_DIF_K_AGC_IF);
	dwval |= 0x33000000;

1817 1818
	status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
					DIF_AGC_IF_REF, 2, dwval, 4);
1819

1820
	return status;
1821 1822 1823 1824
}

int cx231xx_tuner_post_channel_change(struct cx231xx *dev)
{
1825
	int status = 0;
1826 1827
	u32 dwval;

1828 1829
	/* Set the RF and IF k_agc values to 4 for PAL/NTSC and 8 for
	 * SECAM L/B/D standards */
1830 1831 1832
	status = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
				       DIF_AGC_IF_REF, 2, &dwval, 4);
	dwval &= ~(FLD_DIF_K_AGC_RF | FLD_DIF_K_AGC_IF);
1833

1834 1835
	if (dev->norm & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_B |
			 V4L2_STD_SECAM_D))
1836
		dwval |= 0x88000000;
1837
	else
1838
		dwval |= 0x44000000;
1839

1840 1841
	status = cx231xx_write_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
					DIF_AGC_IF_REF, 2, dwval, 4);
1842

1843
	return status;
1844 1845
}

1846 1847 1848
/******************************************************************************
 *        F L A T I R O N - B L O C K    C O N T R O L   functions            *
 ******************************************************************************/
1849 1850
int cx231xx_flatiron_initialize(struct cx231xx *dev)
{
1851 1852 1853
	int status = 0;
	u32 value;

1854 1855
	status = cx231xx_read_i2c_data(dev, Flatrion_DEVICE_ADDRESS,
				       CH_PWR_CTRL1, 1, &value, 1);
1856 1857 1858 1859 1860 1861 1862 1863 1864
	/* enables clock to delta-sigma and decimation filter */
	value |= 0x80;
	status = cx231xx_write_i2c_data(dev, Flatrion_DEVICE_ADDRESS,
					CH_PWR_CTRL1, 1, value, 1);
	/* power up all channel */
	status = cx231xx_write_i2c_data(dev, Flatrion_DEVICE_ADDRESS,
					CH_PWR_CTRL2, 1, 0x00, 1);

	return status;
1865 1866
}

1867 1868
int cx231xx_flatiron_update_power_control(struct cx231xx *dev,
					enum AV_MODE avmode)
1869
{
1870 1871 1872 1873
	int status = 0;
	u32 value = 0;

	if (avmode != POLARIS_AVMODE_ENXTERNAL_AV) {
1874
		status = cx231xx_read_i2c_data(dev, Flatrion_DEVICE_ADDRESS,
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
					  CH_PWR_CTRL2, 1, &value, 1);
		value |= 0xfe;
		status = cx231xx_write_i2c_data(dev, Flatrion_DEVICE_ADDRESS,
						CH_PWR_CTRL2, 1, value, 1);
	} else {
		status = cx231xx_write_i2c_data(dev, Flatrion_DEVICE_ADDRESS,
						CH_PWR_CTRL2, 1, 0x00, 1);
	}

	return status;
1885 1886 1887 1888 1889
}

/* set flatiron for audio input types */
int cx231xx_flatiron_set_audio_input(struct cx231xx *dev, u8 audio_input)
{
1890
	int status = 0;
1891

1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
	switch (audio_input) {
	case CX231XX_AMUX_LINE_IN:
		status = cx231xx_write_i2c_data(dev, Flatrion_DEVICE_ADDRESS,
						CH_PWR_CTRL2, 1, 0x00, 1);
		status = cx231xx_write_i2c_data(dev, Flatrion_DEVICE_ADDRESS,
						CH_PWR_CTRL1, 1, 0x80, 1);
		break;
	case CX231XX_AMUX_VIDEO:
	default:
		break;
	}
1903

1904
	dev->ctl_ainput = audio_input;
1905

1906
	return status;
1907 1908
}

1909 1910 1911
/******************************************************************************
 *                  P O W E R      C O N T R O L   functions                  *
 ******************************************************************************/
1912
int cx231xx_set_power_mode(struct cx231xx *dev, enum AV_MODE mode)
1913
{
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
	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;
	}

	cx231xx_info(" setPowerMode::mode = %d\n", mode);

1928 1929
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN, value,
				       4);
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
	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);
1945 1946
		status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
						PWR_CTL_EN, value, 4);
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
		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);
1964 1965
		status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
						PWR_CTL_EN, value, 4);
1966

1967 1968
		/* reset state of xceive tuner */
		dev->xc_fw_load_done = 0;
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
		break;

	case POLARIS_AVMODE_ANALOGT_TV:

		tmp &= (~PWR_DEMOD_EN);
		tmp |= (I2C_DEMOD_EN);
		value[0] = (u8) tmp;
		value[1] = (u8) (tmp >> 8);
		value[2] = (u8) (tmp >> 16);
		value[3] = (u8) (tmp >> 24);
1979 1980
		status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
						PWR_CTL_EN, value, 4);
1981 1982 1983 1984 1985 1986 1987 1988
		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);
1989 1990
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
							PWR_CTL_EN, value, 4);
1991 1992 1993 1994 1995 1996 1997 1998 1999
			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);
2000 2001
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
							PWR_CTL_EN, value, 4);
2002 2003 2004 2005 2006 2007 2008 2009
			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);
2010 2011
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
							PWR_CTL_EN, value, 4);
2012 2013 2014 2015 2016 2017 2018 2019 2020
			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);
2021 2022
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
							PWR_CTL_EN, value, 4);
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
			msleep(PWR_SLEEP_INTERVAL);
		}

		if ((dev->model == CX231XX_BOARD_CNXT_RDE_250) ||
		    (dev->model == CX231XX_BOARD_CNXT_RDU_250)) {
			/* tuner path to channel 1 from port 3 */
			cx231xx_enable_i2c_for_tuner(dev, I2C_3);

			if (dev->cx231xx_reset_analog_tuner)
				dev->cx231xx_reset_analog_tuner(dev);
		}
		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);
2043 2044
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
							PWR_CTL_EN, value, 4);
2045 2046 2047 2048 2049 2050 2051 2052
			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);
2053 2054
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
							PWR_CTL_EN, value, 4);
2055 2056 2057 2058 2059 2060 2061 2062
			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);
2063 2064
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
							PWR_CTL_EN, value, 4);
2065 2066 2067 2068 2069 2070 2071 2072
			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);
2073 2074
		status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
						PWR_CTL_EN, value, 4);
2075 2076 2077 2078 2079 2080 2081 2082
		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);
2083 2084
			status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
							PWR_CTL_EN, value, 4);
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
			msleep(PWR_SLEEP_INTERVAL);
		}

		if ((dev->model == CX231XX_BOARD_CNXT_RDE_250) ||
		    (dev->model == CX231XX_BOARD_CNXT_RDU_250)) {
			/* tuner path to channel 1 from port 3 */
			cx231xx_enable_i2c_for_tuner(dev, I2C_3);

			if (dev->cx231xx_reset_analog_tuner)
				dev->cx231xx_reset_analog_tuner(dev);
		}
		break;

	default:
		break;
	}

	msleep(PWR_SLEEP_INTERVAL);

2104 2105
	/* For power saving, only enable Pwr_resetout_n
	   when digital TV is selected. */
2106 2107 2108 2109 2110 2111
	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);
2112 2113
		status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
						PWR_CTL_EN, value, 4);
2114 2115 2116 2117 2118 2119 2120 2121 2122
		msleep(PWR_SLEEP_INTERVAL);
	}

	/* update power control for colibri */
	status = cx231xx_colibri_update_power_control(dev, mode);

	/* update power control for flatiron */
	status = cx231xx_flatiron_update_power_control(dev, mode);

2123 2124
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN, value,
				       4);
2125 2126
	cx231xx_info(" The data of PWR_CTL_EN register 0x74"
				 "=0x%0x,0x%0x,0x%0x,0x%0x\n",
2127
		     value[0], value[1], value[2], value[3]);
2128 2129

	return status;
2130 2131 2132 2133
}

int cx231xx_power_suspend(struct cx231xx *dev)
{
2134 2135 2136
	u8 value[4] = { 0, 0, 0, 0 };
	u32 tmp = 0;
	int status = 0;
2137

2138 2139
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN,
				       value, 4);
2140 2141
	if (status > 0)
		return status;
2142

2143 2144
	tmp = *((u32 *) value);
	tmp &= (~PWR_MODE_MASK);
2145

2146 2147 2148 2149
	value[0] = (u8) tmp;
	value[1] = (u8) (tmp >> 8);
	value[2] = (u8) (tmp >> 16);
	value[3] = (u8) (tmp >> 24);
2150 2151
	status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, PWR_CTL_EN,
					value, 4);
2152

2153
	return status;
2154 2155
}

2156 2157 2158
/******************************************************************************
 *                  S T R E A M    C O N T R O L   functions                  *
 ******************************************************************************/
2159 2160
int cx231xx_start_stream(struct cx231xx *dev, u32 ep_mask)
{
2161 2162 2163
	u8 value[4] = { 0x0, 0x0, 0x0, 0x0 };
	u32 tmp = 0;
	int status = 0;
2164

2165
	cx231xx_info("cx231xx_start_stream():: ep_mask = %x\n", ep_mask);
2166 2167
	status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, EP_MODE_SET,
				       value, 4);
2168 2169
	if (status < 0)
		return status;
2170

2171 2172 2173 2174 2175 2176
	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);
2177

2178 2179
	status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, EP_MODE_SET,
					value, 4);
2180

2181
	return status;
2182 2183 2184 2185
}

int cx231xx_stop_stream(struct cx231xx *dev, u32 ep_mask)
{
2186 2187 2188
	u8 value[4] = { 0x0, 0x0, 0x0, 0x0 };
	u32 tmp = 0;
	int status = 0;
2189

2190 2191 2192 2193 2194
	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;
2195

2196 2197 2198 2199 2200 2201
	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);
2202

2203 2204
	status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, EP_MODE_SET,
					value, 4);
2205

2206
	return status;
2207 2208 2209 2210
}

int cx231xx_initialize_stream_xfer(struct cx231xx *dev, u32 media_type)
{
2211
	int status = 0;
2212

2213 2214
	if (dev->udev->speed == USB_SPEED_HIGH) {
		switch (media_type) {
2215
		case 81: /* audio */
2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251
			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 */
			cx231xx_info("%s: set ts1 registers\n", __func__);
			status = cx231xx_mode_register(dev, TS_MODE_REG, 0x101);
			status = cx231xx_mode_register(dev, TS1_CFG_REG, 0x400);
			break;
		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);
	}
2252

2253 2254
	return status;
}
2255 2256 2257 2258

int cx231xx_capture_start(struct cx231xx *dev, int start, u8 media_type)
{
	int rc;
2259
	u32 ep_mask = -1;
2260
	struct pcb_config *pcb_config;
2261 2262

	/* get EP for media type */
2263
	pcb_config = (struct pcb_config *)&dev->current_pcb_config;
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315

	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);

2316
		if (rc < 0)
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327
			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);
	}

2328 2329 2330 2331
	if (dev->mode == CX231XX_ANALOG_MODE)
		;/* do any in Analog mode */
	else
		;/* do any in digital mode */
2332 2333 2334

	return rc;
}
2335
EXPORT_SYMBOL_GPL(cx231xx_capture_start);
2336

2337 2338 2339
/*****************************************************************************
*                   G P I O   B I T control functions                        *
******************************************************************************/
2340
int cx231xx_set_gpio_bit(struct cx231xx *dev, u32 gpio_bit, u8 * gpio_val)
2341
{
2342
	int status = 0;
2343

2344
	status = cx231xx_send_gpio_cmd(dev, gpio_bit, gpio_val, 4, 0, 0);
2345

2346
	return status;
2347 2348
}

2349
int cx231xx_get_gpio_bit(struct cx231xx *dev, u32 gpio_bit, u8 * gpio_val)
2350
{
2351
	int status = 0;
2352

2353
	status = cx231xx_send_gpio_cmd(dev, gpio_bit, gpio_val, 4, 0, 1);
2354

2355
	return status;
2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369
}

/*
* 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,
2370
			       int pin_number, int pin_value)
2371 2372
{
	int status = 0;
2373
	u32 value = 0;
2374

2375
	/* Check for valid pin_number - if 32 , bail out */
2376
	if (pin_number >= 32)
2377
		return -EINVAL;
2378

2379 2380
	/* input */
	if (pin_value == 0)
2381
		value = dev->gpio_dir & (~(1 << pin_number));	/* clear */
2382
	else
2383
		value = dev->gpio_dir | (1 << pin_number);
2384

2385
	status = cx231xx_set_gpio_bit(dev, value, (u8 *) &dev->gpio_val);
2386

2387
	/* cache the value for future */
2388 2389
	dev->gpio_dir = value;

2390
	return status;
2391 2392 2393
}

/*
2394
* cx231xx_set_gpio_value
2395 2396 2397 2398 2399 2400 2401 2402 2403
*      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
*/
2404
int cx231xx_set_gpio_value(struct cx231xx *dev, int pin_number, int pin_value)
2405
{
2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417
	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;
2418 2419
		status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
					      (u8 *) &dev->gpio_val);
2420
		value = 0;
2421
	}
2422

2423
	if (pin_value == 0)
2424
		value = dev->gpio_val & (~(1 << pin_number));
2425
	else
2426
		value = dev->gpio_val | (1 << pin_number);
2427

2428 2429
	/* store the value */
	dev->gpio_val = value;
2430

2431
	/* toggle bit0 of GP_IO */
2432
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
2433

2434
	return status;
2435 2436
}

2437 2438 2439
/*****************************************************************************
*                      G P I O I2C related functions                         *
******************************************************************************/
2440 2441 2442 2443 2444
int cx231xx_gpio_i2c_start(struct cx231xx *dev)
{
	int status = 0;

	/* set SCL to output 1 ; set SDA to output 1 */
2445 2446 2447 2448 2449
	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;

2450 2451
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
	if (status < 0)
2452 2453 2454
		return -EINVAL;

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

2458 2459
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
	if (status < 0)
2460 2461
		return -EINVAL;

2462 2463 2464
	/* 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);
2465

2466 2467
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
	if (status < 0)
2468 2469 2470 2471 2472 2473 2474
		return -EINVAL;

	return status;
}

int cx231xx_gpio_i2c_end(struct cx231xx *dev)
{
2475
	int status = 0;
2476

2477 2478 2479
	/* 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;
2480

2481 2482
	dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
	dev->gpio_val &= ~(1 << dev->board.tuner_sda_gpio);
2483

2484 2485
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
	if (status < 0)
2486 2487
		return -EINVAL;

2488 2489 2490
	/* 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);
2491

2492 2493
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
	if (status < 0)
2494 2495 2496 2497
		return -EINVAL;

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

2501
	status =
2502 2503
	    cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
	if (status < 0)
2504
		return -EINVAL;
2505

2506 2507 2508 2509 2510
	return status;
}

int cx231xx_gpio_i2c_write_byte(struct cx231xx *dev, u8 data)
{
2511 2512
	int status = 0;
	u8 i;
2513 2514

	/* set SCL to output ; set SDA to output */
2515 2516 2517 2518 2519 2520 2521 2522
	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);
2523 2524
			status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
						      (u8 *)&dev->gpio_val);
2525 2526 2527

			/* set SCL to output 1; set SDA to output 0     */
			dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
2528 2529
			status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
						      (u8 *)&dev->gpio_val);
2530 2531 2532

			/* set SCL to output 0; set SDA to output 0     */
			dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2533 2534
			status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
						      (u8 *)&dev->gpio_val);
2535
		} else {
2536 2537 2538
			/* 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;
2539 2540
			status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
						      (u8 *)&dev->gpio_val);
2541 2542 2543

			/* set SCL to output 1; set SDA to output 1     */
			dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
2544 2545
			status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
						      (u8 *)&dev->gpio_val);
2546 2547 2548

			/* set SCL to output 0; set SDA to output 1     */
			dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2549 2550
			status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
						      (u8 *)&dev->gpio_val);
2551
		}
2552 2553 2554 2555
	}
	return status;
}

2556
int cx231xx_gpio_i2c_read_byte(struct cx231xx *dev, u8 * buf)
2557 2558
{
	u8 value = 0;
2559 2560 2561
	int status = 0;
	u32 gpio_logic_value = 0;
	u8 i;
2562 2563

	/* read byte */
2564
	for (i = 0; i < 8; i++) {	/* send write I2c addr */
2565 2566

		/* set SCL to output 0; set SDA to input */
2567
		dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2568 2569
		status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
					      (u8 *)&dev->gpio_val);
2570 2571

		/* set SCL to output 1; set SDA to input */
2572
		dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
2573 2574
		status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
					      (u8 *)&dev->gpio_val);
2575 2576 2577

		/* get SDA data bit */
		gpio_logic_value = dev->gpio_val;
2578 2579 2580
		status = cx231xx_get_gpio_bit(dev, dev->gpio_dir,
					      (u8 *)&dev->gpio_val);
		if ((dev->gpio_val & (1 << dev->board.tuner_sda_gpio)) != 0)
2581
			value |= (1 << (8 - i - 1));
2582 2583 2584 2585 2586

		dev->gpio_val = gpio_logic_value;
	}

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

2592 2593
	/* store the value */
	*buf = value & 0xff;
2594 2595 2596 2597 2598 2599

	return status;
}

int cx231xx_gpio_i2c_read_ack(struct cx231xx *dev)
{
2600
	int status = 0;
2601
	u32 gpio_logic_value = 0;
2602 2603
	int nCnt = 10;
	int nInit = nCnt;
2604

2605 2606
	/* clock stretch; set SCL to input; set SDA to input;
	   get SCL value till SCL = 1 */
2607 2608
	dev->gpio_dir &= ~(1 << dev->board.tuner_sda_gpio);
	dev->gpio_dir &= ~(1 << dev->board.tuner_scl_gpio);
2609 2610

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

2613
	do {
2614
		msleep(2);
2615 2616
		status = cx231xx_get_gpio_bit(dev, dev->gpio_dir,
					      (u8 *)&dev->gpio_val);
2617
		nCnt--;
2618 2619 2620
	} while (((dev->gpio_val &
			  (1 << dev->board.tuner_scl_gpio)) == 0) &&
			 (nCnt > 0));
2621

2622
	if (nCnt == 0)
2623
		cx231xx_info("No ACK after %d msec -GPIO I2C failed!",
2624
			     nInit * 10);
2625 2626

	/* readAck
2627 2628 2629
	   throuth clock stretch ,slave has given a SCL signal,
	   so the SDA data can be directly read.  */
	status = cx231xx_get_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
2630

2631
	if ((dev->gpio_val & 1 << dev->board.tuner_sda_gpio) == 0) {
2632
		dev->gpio_val = gpio_logic_value;
2633
		dev->gpio_val &= ~(1 << dev->board.tuner_sda_gpio);
2634 2635 2636
		status = 0;
	} else {
		dev->gpio_val = gpio_logic_value;
2637
		dev->gpio_val |= (1 << dev->board.tuner_sda_gpio);
2638 2639
	}

2640 2641
	/* read SDA end, set the SCL to output 0, after this operation,
	   SDA direction can be changed. */
2642
	dev->gpio_val = gpio_logic_value;
2643 2644
	dev->gpio_dir |= (1 << dev->board.tuner_scl_gpio);
	dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2645
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
2646 2647 2648 2649 2650 2651

	return status;
}

int cx231xx_gpio_i2c_write_ack(struct cx231xx *dev)
{
2652
	int status = 0;
2653 2654

	/* set SDA to ouput */
2655
	dev->gpio_dir |= 1 << dev->board.tuner_sda_gpio;
2656
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
2657 2658

	/* set SCL = 0 (output); set SDA = 0 (output) */
2659 2660
	dev->gpio_val &= ~(1 << dev->board.tuner_sda_gpio);
	dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2661
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
2662 2663

	/* set SCL = 1 (output); set SDA = 0 (output) */
2664
	dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
2665
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
2666 2667

	/* set SCL = 0 (output); set SDA = 0 (output) */
2668
	dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2669
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
2670 2671

	/* set SDA to input,and then the slave will read data from SDA. */
2672
	dev->gpio_dir &= ~(1 << dev->board.tuner_sda_gpio);
2673
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
2674 2675 2676 2677 2678 2679

	return status;
}

int cx231xx_gpio_i2c_write_nak(struct cx231xx *dev)
{
2680
	int status = 0;
2681 2682

	/* set scl to output ; set sda to input */
2683 2684
	dev->gpio_dir |= 1 << dev->board.tuner_scl_gpio;
	dev->gpio_dir &= ~(1 << dev->board.tuner_sda_gpio);
2685
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
2686 2687

	/* set scl to output 0; set sda to input */
2688
	dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2689
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
2690 2691

	/* set scl to output 1; set sda to input */
2692
	dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
2693
	status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, (u8 *)&dev->gpio_val);
2694 2695 2696 2697

	return status;
}

2698 2699 2700
/*****************************************************************************
*                      G P I O I2C related functions                         *
******************************************************************************/
2701 2702 2703
/* cx231xx_gpio_i2c_read
 * Function to read data from gpio based I2C interface
 */
2704
int cx231xx_gpio_i2c_read(struct cx231xx *dev, u8 dev_addr, u8 * buf, u8 len)
2705
{
2706 2707
	int status = 0;
	int i = 0;
2708

2709
	/* get the lock */
2710 2711 2712 2713 2714 2715
	mutex_lock(&dev->gpio_i2c_lock);

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

	/* write dev_addr */
2716
	status = cx231xx_gpio_i2c_write_byte(dev, (dev_addr << 1) + 1);
2717 2718 2719 2720

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

2721 2722 2723 2724 2725
	/* read data */
	for (i = 0; i < len; i++) {
		/* read data */
		buf[i] = 0;
		status = cx231xx_gpio_i2c_read_byte(dev, &buf[i]);
2726

2727 2728 2729 2730 2731
		if ((i + 1) != len) {
			/* only do write ack if we more length */
			status = cx231xx_gpio_i2c_write_ack(dev);
		}
	}
2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747

	/* 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
 */
2748
int cx231xx_gpio_i2c_write(struct cx231xx *dev, u8 dev_addr, u8 * buf, u8 len)
2749
{
2750 2751
	int status = 0;
	int i = 0;
2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762

	/* 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 */
2763
	status = cx231xx_gpio_i2c_read_ack(dev);
2764

2765
	for (i = 0; i < len; i++) {
2766
		/* Write data */
2767
		status = cx231xx_gpio_i2c_write_byte(dev, buf[i]);
2768

2769 2770 2771
		/* read Ack */
		status = cx231xx_gpio_i2c_read_ack(dev);
	}
2772

2773
	/* write End */
2774 2775 2776 2777 2778 2779 2780
	status = cx231xx_gpio_i2c_end(dev);

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

	return 0;
}