treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / usb / cx231xx / cx231xx-avcore.c
blob0974965e848f7e86fe30184ac3da788033842069
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 cx231xx_avcore.c - driver for Conexant Cx23100/101/102
4 USB video capture devices
6 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
8 This program contains the specific code to control the avdecoder chip and
9 other related usb control functions for cx231xx based chipset.
13 #include "cx231xx.h"
14 #include <linux/init.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/bitmap.h>
19 #include <linux/i2c.h>
20 #include <linux/mm.h>
21 #include <linux/mutex.h>
22 #include <media/tuner.h>
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-ioctl.h>
27 #include "cx231xx-dif.h"
29 #define TUNER_MODE_FM_RADIO 0
30 /******************************************************************************
31 -: BLOCK ARRANGEMENT :-
32 I2S block ----------------------|
33 [I2S audio] |
35 Analog Front End --> Direct IF -|-> Cx25840 --> Audio
36 [video & audio] | [Audio]
38 |-> Cx25840 --> Video
39 [Video]
41 *******************************************************************************/
42 /******************************************************************************
43 * VERVE REGISTER *
44 * *
45 ******************************************************************************/
46 static int verve_write_byte(struct cx231xx *dev, u8 saddr, u8 data)
48 return cx231xx_write_i2c_data(dev, VERVE_I2C_ADDRESS,
49 saddr, 1, data, 1);
52 static int verve_read_byte(struct cx231xx *dev, u8 saddr, u8 *data)
54 int status;
55 u32 temp = 0;
57 status = cx231xx_read_i2c_data(dev, VERVE_I2C_ADDRESS,
58 saddr, 1, &temp, 1);
59 *data = (u8) temp;
60 return status;
62 void initGPIO(struct cx231xx *dev)
64 u32 _gpio_direction = 0;
65 u32 value = 0;
66 u8 val = 0;
68 _gpio_direction = _gpio_direction & 0xFC0003FF;
69 _gpio_direction = _gpio_direction | 0x03FDFC00;
70 cx231xx_send_gpio_cmd(dev, _gpio_direction, (u8 *)&value, 4, 0, 0);
72 verve_read_byte(dev, 0x07, &val);
73 dev_dbg(dev->dev, "verve_read_byte address0x07=0x%x\n", val);
74 verve_write_byte(dev, 0x07, 0xF4);
75 verve_read_byte(dev, 0x07, &val);
76 dev_dbg(dev->dev, "verve_read_byte address0x07=0x%x\n", val);
78 cx231xx_capture_start(dev, 1, Vbi);
80 cx231xx_mode_register(dev, EP_MODE_SET, 0x0500FE00);
81 cx231xx_mode_register(dev, GBULK_BIT_EN, 0xFFFDFFFF);
84 void uninitGPIO(struct cx231xx *dev)
86 u8 value[4] = { 0, 0, 0, 0 };
88 cx231xx_capture_start(dev, 0, Vbi);
89 verve_write_byte(dev, 0x07, 0x14);
90 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
91 0x68, value, 4);
94 /******************************************************************************
95 * A F E - B L O C K C O N T R O L functions *
96 * [ANALOG FRONT END] *
97 ******************************************************************************/
98 static int afe_write_byte(struct cx231xx *dev, u16 saddr, u8 data)
100 return cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
101 saddr, 2, data, 1);
104 static int afe_read_byte(struct cx231xx *dev, u16 saddr, u8 *data)
106 int status;
107 u32 temp = 0;
109 status = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
110 saddr, 2, &temp, 1);
111 *data = (u8) temp;
112 return status;
115 int cx231xx_afe_init_super_block(struct cx231xx *dev, u32 ref_count)
117 int status = 0;
118 u8 temp = 0;
119 u8 afe_power_status = 0;
120 int i = 0;
122 /* super block initialize */
123 temp = (u8) (ref_count & 0xff);
124 status = afe_write_byte(dev, SUP_BLK_TUNE2, temp);
125 if (status < 0)
126 return status;
128 status = afe_read_byte(dev, SUP_BLK_TUNE2, &afe_power_status);
129 if (status < 0)
130 return status;
132 temp = (u8) ((ref_count & 0x300) >> 8);
133 temp |= 0x40;
134 status = afe_write_byte(dev, SUP_BLK_TUNE1, temp);
135 if (status < 0)
136 return status;
138 status = afe_write_byte(dev, SUP_BLK_PLL2, 0x0f);
139 if (status < 0)
140 return status;
142 /* enable pll */
143 while (afe_power_status != 0x18) {
144 status = afe_write_byte(dev, SUP_BLK_PWRDN, 0x18);
145 if (status < 0) {
146 dev_dbg(dev->dev,
147 "%s: Init Super Block failed in send cmd\n",
148 __func__);
149 break;
152 status = afe_read_byte(dev, SUP_BLK_PWRDN, &afe_power_status);
153 afe_power_status &= 0xff;
154 if (status < 0) {
155 dev_dbg(dev->dev,
156 "%s: Init Super Block failed in receive cmd\n",
157 __func__);
158 break;
160 i++;
161 if (i == 10) {
162 dev_dbg(dev->dev,
163 "%s: Init Super Block force break in loop !!!!\n",
164 __func__);
165 status = -1;
166 break;
170 if (status < 0)
171 return status;
173 /* start tuning filter */
174 status = afe_write_byte(dev, SUP_BLK_TUNE3, 0x40);
175 if (status < 0)
176 return status;
178 msleep(5);
180 /* exit tuning */
181 status = afe_write_byte(dev, SUP_BLK_TUNE3, 0x00);
183 return status;
186 int cx231xx_afe_init_channels(struct cx231xx *dev)
188 int status = 0;
190 /* power up all 3 channels, clear pd_buffer */
191 status = afe_write_byte(dev, ADC_PWRDN_CLAMP_CH1, 0x00);
192 status = afe_write_byte(dev, ADC_PWRDN_CLAMP_CH2, 0x00);
193 status = afe_write_byte(dev, ADC_PWRDN_CLAMP_CH3, 0x00);
195 /* Enable quantizer calibration */
196 status = afe_write_byte(dev, ADC_COM_QUANT, 0x02);
198 /* channel initialize, force modulator (fb) reset */
199 status = afe_write_byte(dev, ADC_FB_FRCRST_CH1, 0x17);
200 status = afe_write_byte(dev, ADC_FB_FRCRST_CH2, 0x17);
201 status = afe_write_byte(dev, ADC_FB_FRCRST_CH3, 0x17);
203 /* start quantilizer calibration */
204 status = afe_write_byte(dev, ADC_CAL_ATEST_CH1, 0x10);
205 status = afe_write_byte(dev, ADC_CAL_ATEST_CH2, 0x10);
206 status = afe_write_byte(dev, ADC_CAL_ATEST_CH3, 0x10);
207 msleep(5);
209 /* exit modulator (fb) reset */
210 status = afe_write_byte(dev, ADC_FB_FRCRST_CH1, 0x07);
211 status = afe_write_byte(dev, ADC_FB_FRCRST_CH2, 0x07);
212 status = afe_write_byte(dev, ADC_FB_FRCRST_CH3, 0x07);
214 /* enable the pre_clamp in each channel for single-ended input */
215 status = afe_write_byte(dev, ADC_NTF_PRECLMP_EN_CH1, 0xf0);
216 status = afe_write_byte(dev, ADC_NTF_PRECLMP_EN_CH2, 0xf0);
217 status = afe_write_byte(dev, ADC_NTF_PRECLMP_EN_CH3, 0xf0);
219 /* use diode instead of resistor, so set term_en to 0, res_en to 0 */
220 status = cx231xx_reg_mask_write(dev, AFE_DEVICE_ADDRESS, 8,
221 ADC_QGAIN_RES_TRM_CH1, 3, 7, 0x00);
222 status = cx231xx_reg_mask_write(dev, AFE_DEVICE_ADDRESS, 8,
223 ADC_QGAIN_RES_TRM_CH2, 3, 7, 0x00);
224 status = cx231xx_reg_mask_write(dev, AFE_DEVICE_ADDRESS, 8,
225 ADC_QGAIN_RES_TRM_CH3, 3, 7, 0x00);
227 /* dynamic element matching off */
228 status = afe_write_byte(dev, ADC_DCSERVO_DEM_CH1, 0x03);
229 status = afe_write_byte(dev, ADC_DCSERVO_DEM_CH2, 0x03);
230 status = afe_write_byte(dev, ADC_DCSERVO_DEM_CH3, 0x03);
232 return status;
235 int cx231xx_afe_setup_AFE_for_baseband(struct cx231xx *dev)
237 u8 c_value = 0;
238 int status = 0;
240 status = afe_read_byte(dev, ADC_PWRDN_CLAMP_CH2, &c_value);
241 c_value &= (~(0x50));
242 status = afe_write_byte(dev, ADC_PWRDN_CLAMP_CH2, c_value);
244 return status;
248 The Analog Front End in Cx231xx has 3 channels. These
249 channels are used to share between different inputs
250 like tuner, s-video and composite inputs.
252 channel 1 ----- pin 1 to pin4(in reg is 1-4)
253 channel 2 ----- pin 5 to pin8(in reg is 5-8)
254 channel 3 ----- pin 9 to pin 12(in reg is 9-11)
256 int cx231xx_afe_set_input_mux(struct cx231xx *dev, u32 input_mux)
258 u8 ch1_setting = (u8) input_mux;
259 u8 ch2_setting = (u8) (input_mux >> 8);
260 u8 ch3_setting = (u8) (input_mux >> 16);
261 int status = 0;
262 u8 value = 0;
264 if (ch1_setting != 0) {
265 status = afe_read_byte(dev, ADC_INPUT_CH1, &value);
266 value &= ~INPUT_SEL_MASK;
267 value |= (ch1_setting - 1) << 4;
268 value &= 0xff;
269 status = afe_write_byte(dev, ADC_INPUT_CH1, value);
272 if (ch2_setting != 0) {
273 status = afe_read_byte(dev, ADC_INPUT_CH2, &value);
274 value &= ~INPUT_SEL_MASK;
275 value |= (ch2_setting - 1) << 4;
276 value &= 0xff;
277 status = afe_write_byte(dev, ADC_INPUT_CH2, value);
280 /* For ch3_setting, the value to put in the register is
281 7 less than the input number */
282 if (ch3_setting != 0) {
283 status = afe_read_byte(dev, ADC_INPUT_CH3, &value);
284 value &= ~INPUT_SEL_MASK;
285 value |= (ch3_setting - 1) << 4;
286 value &= 0xff;
287 status = afe_write_byte(dev, ADC_INPUT_CH3, value);
290 return status;
293 int cx231xx_afe_set_mode(struct cx231xx *dev, enum AFE_MODE mode)
295 int status = 0;
298 * FIXME: We need to implement the AFE code for LOW IF and for HI IF.
299 * Currently, only baseband works.
302 switch (mode) {
303 case AFE_MODE_LOW_IF:
304 cx231xx_Setup_AFE_for_LowIF(dev);
305 break;
306 case AFE_MODE_BASEBAND:
307 status = cx231xx_afe_setup_AFE_for_baseband(dev);
308 break;
309 case AFE_MODE_EU_HI_IF:
310 /* SetupAFEforEuHiIF(); */
311 break;
312 case AFE_MODE_US_HI_IF:
313 /* SetupAFEforUsHiIF(); */
314 break;
315 case AFE_MODE_JAPAN_HI_IF:
316 /* SetupAFEforJapanHiIF(); */
317 break;
320 if ((mode != dev->afe_mode) &&
321 (dev->video_input == CX231XX_VMUX_TELEVISION))
322 status = cx231xx_afe_adjust_ref_count(dev,
323 CX231XX_VMUX_TELEVISION);
325 dev->afe_mode = mode;
327 return status;
330 int cx231xx_afe_update_power_control(struct cx231xx *dev,
331 enum AV_MODE avmode)
333 u8 afe_power_status = 0;
334 int status = 0;
336 switch (dev->model) {
337 case CX231XX_BOARD_CNXT_CARRAERA:
338 case CX231XX_BOARD_CNXT_RDE_250:
339 case CX231XX_BOARD_CNXT_SHELBY:
340 case CX231XX_BOARD_CNXT_RDU_250:
341 case CX231XX_BOARD_CNXT_RDE_253S:
342 case CX231XX_BOARD_CNXT_RDU_253S:
343 case CX231XX_BOARD_CNXT_VIDEO_GRABBER:
344 case CX231XX_BOARD_HAUPPAUGE_EXETER:
345 case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:
346 case CX231XX_BOARD_HAUPPAUGE_USBLIVE2:
347 case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
348 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL:
349 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC:
350 case CX231XX_BOARD_OTG102:
351 if (avmode == POLARIS_AVMODE_ANALOGT_TV) {
352 while (afe_power_status != (FLD_PWRDN_TUNING_BIAS |
353 FLD_PWRDN_ENABLE_PLL)) {
354 status = afe_write_byte(dev, SUP_BLK_PWRDN,
355 FLD_PWRDN_TUNING_BIAS |
356 FLD_PWRDN_ENABLE_PLL);
357 status |= afe_read_byte(dev, SUP_BLK_PWRDN,
358 &afe_power_status);
359 if (status < 0)
360 break;
363 status = afe_write_byte(dev, ADC_PWRDN_CLAMP_CH1,
364 0x00);
365 status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH2,
366 0x00);
367 status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH3,
368 0x00);
369 } else if (avmode == POLARIS_AVMODE_DIGITAL) {
370 status = afe_write_byte(dev, ADC_PWRDN_CLAMP_CH1,
371 0x70);
372 status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH2,
373 0x70);
374 status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH3,
375 0x70);
377 status |= afe_read_byte(dev, SUP_BLK_PWRDN,
378 &afe_power_status);
379 afe_power_status |= FLD_PWRDN_PD_BANDGAP |
380 FLD_PWRDN_PD_BIAS |
381 FLD_PWRDN_PD_TUNECK;
382 status |= afe_write_byte(dev, SUP_BLK_PWRDN,
383 afe_power_status);
384 } else if (avmode == POLARIS_AVMODE_ENXTERNAL_AV) {
385 while (afe_power_status != (FLD_PWRDN_TUNING_BIAS |
386 FLD_PWRDN_ENABLE_PLL)) {
387 status = afe_write_byte(dev, SUP_BLK_PWRDN,
388 FLD_PWRDN_TUNING_BIAS |
389 FLD_PWRDN_ENABLE_PLL);
390 status |= afe_read_byte(dev, SUP_BLK_PWRDN,
391 &afe_power_status);
392 if (status < 0)
393 break;
396 status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH1,
397 0x00);
398 status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH2,
399 0x00);
400 status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH3,
401 0x00);
402 } else {
403 dev_dbg(dev->dev, "Invalid AV mode input\n");
404 status = -1;
406 break;
407 default:
408 if (avmode == POLARIS_AVMODE_ANALOGT_TV) {
409 while (afe_power_status != (FLD_PWRDN_TUNING_BIAS |
410 FLD_PWRDN_ENABLE_PLL)) {
411 status = afe_write_byte(dev, SUP_BLK_PWRDN,
412 FLD_PWRDN_TUNING_BIAS |
413 FLD_PWRDN_ENABLE_PLL);
414 status |= afe_read_byte(dev, SUP_BLK_PWRDN,
415 &afe_power_status);
416 if (status < 0)
417 break;
420 status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH1,
421 0x40);
422 status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH2,
423 0x40);
424 status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH3,
425 0x00);
426 } else if (avmode == POLARIS_AVMODE_DIGITAL) {
427 status = afe_write_byte(dev, ADC_PWRDN_CLAMP_CH1,
428 0x70);
429 status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH2,
430 0x70);
431 status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH3,
432 0x70);
434 status |= afe_read_byte(dev, SUP_BLK_PWRDN,
435 &afe_power_status);
436 afe_power_status |= FLD_PWRDN_PD_BANDGAP |
437 FLD_PWRDN_PD_BIAS |
438 FLD_PWRDN_PD_TUNECK;
439 status |= afe_write_byte(dev, SUP_BLK_PWRDN,
440 afe_power_status);
441 } else if (avmode == POLARIS_AVMODE_ENXTERNAL_AV) {
442 while (afe_power_status != (FLD_PWRDN_TUNING_BIAS |
443 FLD_PWRDN_ENABLE_PLL)) {
444 status = afe_write_byte(dev, SUP_BLK_PWRDN,
445 FLD_PWRDN_TUNING_BIAS |
446 FLD_PWRDN_ENABLE_PLL);
447 status |= afe_read_byte(dev, SUP_BLK_PWRDN,
448 &afe_power_status);
449 if (status < 0)
450 break;
453 status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH1,
454 0x00);
455 status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH2,
456 0x00);
457 status |= afe_write_byte(dev, ADC_PWRDN_CLAMP_CH3,
458 0x40);
459 } else {
460 dev_dbg(dev->dev, "Invalid AV mode input\n");
461 status = -1;
463 } /* switch */
465 return status;
468 int cx231xx_afe_adjust_ref_count(struct cx231xx *dev, u32 video_input)
470 u8 input_mode = 0;
471 u8 ntf_mode = 0;
472 int status = 0;
474 dev->video_input = video_input;
476 if (video_input == CX231XX_VMUX_TELEVISION) {
477 status = afe_read_byte(dev, ADC_INPUT_CH3, &input_mode);
478 status = afe_read_byte(dev, ADC_NTF_PRECLMP_EN_CH3,
479 &ntf_mode);
480 } else {
481 status = afe_read_byte(dev, ADC_INPUT_CH1, &input_mode);
482 status = afe_read_byte(dev, ADC_NTF_PRECLMP_EN_CH1,
483 &ntf_mode);
486 input_mode = (ntf_mode & 0x3) | ((input_mode & 0x6) << 1);
488 switch (input_mode) {
489 case SINGLE_ENDED:
490 dev->afe_ref_count = 0x23C;
491 break;
492 case LOW_IF:
493 dev->afe_ref_count = 0x24C;
494 break;
495 case EU_IF:
496 dev->afe_ref_count = 0x258;
497 break;
498 case US_IF:
499 dev->afe_ref_count = 0x260;
500 break;
501 default:
502 break;
505 status = cx231xx_afe_init_super_block(dev, dev->afe_ref_count);
507 return status;
510 /******************************************************************************
511 * 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 *
512 ******************************************************************************/
513 static int vid_blk_write_byte(struct cx231xx *dev, u16 saddr, u8 data)
515 return cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
516 saddr, 2, data, 1);
519 static int vid_blk_read_byte(struct cx231xx *dev, u16 saddr, u8 *data)
521 int status;
522 u32 temp = 0;
524 status = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
525 saddr, 2, &temp, 1);
526 *data = (u8) temp;
527 return status;
530 static int vid_blk_write_word(struct cx231xx *dev, u16 saddr, u32 data)
532 return cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
533 saddr, 2, data, 4);
536 static int vid_blk_read_word(struct cx231xx *dev, u16 saddr, u32 *data)
538 return cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
539 saddr, 2, data, 4);
541 int cx231xx_check_fw(struct cx231xx *dev)
543 u8 temp = 0;
544 int status = 0;
545 status = vid_blk_read_byte(dev, DL_CTL_ADDRESS_LOW, &temp);
546 if (status < 0)
547 return status;
548 else
549 return temp;
553 int cx231xx_set_video_input_mux(struct cx231xx *dev, u8 input)
555 int status = 0;
557 switch (INPUT(input)->type) {
558 case CX231XX_VMUX_COMPOSITE1:
559 case CX231XX_VMUX_SVIDEO:
560 if ((dev->current_pcb_config.type == USB_BUS_POWER) &&
561 (dev->power_mode != POLARIS_AVMODE_ENXTERNAL_AV)) {
562 /* External AV */
563 status = cx231xx_set_power_mode(dev,
564 POLARIS_AVMODE_ENXTERNAL_AV);
565 if (status < 0) {
566 dev_err(dev->dev,
567 "%s: Failed to set Power - errCode [%d]!\n",
568 __func__, status);
569 return status;
572 status = cx231xx_set_decoder_video_input(dev,
573 INPUT(input)->type,
574 INPUT(input)->vmux);
575 break;
576 case CX231XX_VMUX_TELEVISION:
577 case CX231XX_VMUX_CABLE:
578 if ((dev->current_pcb_config.type == USB_BUS_POWER) &&
579 (dev->power_mode != POLARIS_AVMODE_ANALOGT_TV)) {
580 /* Tuner */
581 status = cx231xx_set_power_mode(dev,
582 POLARIS_AVMODE_ANALOGT_TV);
583 if (status < 0) {
584 dev_err(dev->dev,
585 "%s: Failed to set Power - errCode [%d]!\n",
586 __func__, status);
587 return status;
590 if (dev->tuner_type == TUNER_NXP_TDA18271)
591 status = cx231xx_set_decoder_video_input(dev,
592 CX231XX_VMUX_TELEVISION,
593 INPUT(input)->vmux);
594 else
595 status = cx231xx_set_decoder_video_input(dev,
596 CX231XX_VMUX_COMPOSITE1,
597 INPUT(input)->vmux);
599 break;
600 default:
601 dev_err(dev->dev, "%s: Unknown Input %d !\n",
602 __func__, INPUT(input)->type);
603 break;
606 /* save the selection */
607 dev->video_input = input;
609 return status;
612 int cx231xx_set_decoder_video_input(struct cx231xx *dev,
613 u8 pin_type, u8 input)
615 int status = 0;
616 u32 value = 0;
618 if (pin_type != dev->video_input) {
619 status = cx231xx_afe_adjust_ref_count(dev, pin_type);
620 if (status < 0) {
621 dev_err(dev->dev,
622 "%s: adjust_ref_count :Failed to set AFE input mux - errCode [%d]!\n",
623 __func__, status);
624 return status;
628 /* call afe block to set video inputs */
629 status = cx231xx_afe_set_input_mux(dev, input);
630 if (status < 0) {
631 dev_err(dev->dev,
632 "%s: set_input_mux :Failed to set AFE input mux - errCode [%d]!\n",
633 __func__, status);
634 return status;
637 switch (pin_type) {
638 case CX231XX_VMUX_COMPOSITE1:
639 status = vid_blk_read_word(dev, AFE_CTRL, &value);
640 value |= (0 << 13) | (1 << 4);
641 value &= ~(1 << 5);
643 /* set [24:23] [22:15] to 0 */
644 value &= (~(0x1ff8000));
645 /* set FUNC_MODE[24:23] = 2 IF_MOD[22:15] = 0 */
646 value |= 0x1000000;
647 status = vid_blk_write_word(dev, AFE_CTRL, value);
649 status = vid_blk_read_word(dev, OUT_CTRL1, &value);
650 value |= (1 << 7);
651 status = vid_blk_write_word(dev, OUT_CTRL1, value);
653 /* Set output mode */
654 status = cx231xx_read_modify_write_i2c_dword(dev,
655 VID_BLK_I2C_ADDRESS,
656 OUT_CTRL1,
657 FLD_OUT_MODE,
658 dev->board.output_mode);
660 /* Tell DIF object to go to baseband mode */
661 status = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND);
662 if (status < 0) {
663 dev_err(dev->dev,
664 "%s: cx231xx_dif set to By pass mode- errCode [%d]!\n",
665 __func__, status);
666 return status;
669 /* Read the DFE_CTRL1 register */
670 status = vid_blk_read_word(dev, DFE_CTRL1, &value);
672 /* enable the VBI_GATE_EN */
673 value |= FLD_VBI_GATE_EN;
675 /* Enable the auto-VGA enable */
676 value |= FLD_VGA_AUTO_EN;
678 /* Write it back */
679 status = vid_blk_write_word(dev, DFE_CTRL1, value);
681 /* Disable auto config of registers */
682 status = cx231xx_read_modify_write_i2c_dword(dev,
683 VID_BLK_I2C_ADDRESS,
684 MODE_CTRL, FLD_ACFG_DIS,
685 cx231xx_set_field(FLD_ACFG_DIS, 1));
687 /* Set CVBS input mode */
688 status = cx231xx_read_modify_write_i2c_dword(dev,
689 VID_BLK_I2C_ADDRESS,
690 MODE_CTRL, FLD_INPUT_MODE,
691 cx231xx_set_field(FLD_INPUT_MODE, INPUT_MODE_CVBS_0));
692 break;
693 case CX231XX_VMUX_SVIDEO:
694 /* Disable the use of DIF */
696 status = vid_blk_read_word(dev, AFE_CTRL, &value);
698 /* set [24:23] [22:15] to 0 */
699 value &= (~(0x1ff8000));
700 /* set FUNC_MODE[24:23] = 2
701 IF_MOD[22:15] = 0 DCR_BYP_CH2[4:4] = 1; */
702 value |= 0x1000010;
703 status = vid_blk_write_word(dev, AFE_CTRL, value);
705 /* Tell DIF object to go to baseband mode */
706 status = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND);
707 if (status < 0) {
708 dev_err(dev->dev,
709 "%s: cx231xx_dif set to By pass mode- errCode [%d]!\n",
710 __func__, status);
711 return status;
714 /* Read the DFE_CTRL1 register */
715 status = vid_blk_read_word(dev, DFE_CTRL1, &value);
717 /* enable the VBI_GATE_EN */
718 value |= FLD_VBI_GATE_EN;
720 /* Enable the auto-VGA enable */
721 value |= FLD_VGA_AUTO_EN;
723 /* Write it back */
724 status = vid_blk_write_word(dev, DFE_CTRL1, value);
726 /* Disable auto config of registers */
727 status = cx231xx_read_modify_write_i2c_dword(dev,
728 VID_BLK_I2C_ADDRESS,
729 MODE_CTRL, FLD_ACFG_DIS,
730 cx231xx_set_field(FLD_ACFG_DIS, 1));
732 /* Set YC input mode */
733 status = cx231xx_read_modify_write_i2c_dword(dev,
734 VID_BLK_I2C_ADDRESS,
735 MODE_CTRL,
736 FLD_INPUT_MODE,
737 cx231xx_set_field(FLD_INPUT_MODE, INPUT_MODE_YC_1));
739 /* Chroma to ADC2 */
740 status = vid_blk_read_word(dev, AFE_CTRL, &value);
741 value |= FLD_CHROMA_IN_SEL; /* set the chroma in select */
743 /* Clear VGA_SEL_CH2 and VGA_SEL_CH3 (bits 7 and 8)
744 This sets them to use video
745 rather than audio. Only one of the two will be in use. */
746 value &= ~(FLD_VGA_SEL_CH2 | FLD_VGA_SEL_CH3);
748 status = vid_blk_write_word(dev, AFE_CTRL, value);
750 status = cx231xx_afe_set_mode(dev, AFE_MODE_BASEBAND);
751 break;
752 case CX231XX_VMUX_TELEVISION:
753 case CX231XX_VMUX_CABLE:
754 default:
755 /* TODO: Test if this is also needed for xc2028/xc3028 */
756 if (dev->board.tuner_type == TUNER_XC5000) {
757 /* Disable the use of DIF */
759 status = vid_blk_read_word(dev, AFE_CTRL, &value);
760 value |= (0 << 13) | (1 << 4);
761 value &= ~(1 << 5);
763 /* set [24:23] [22:15] to 0 */
764 value &= (~(0x1FF8000));
765 /* set FUNC_MODE[24:23] = 2 IF_MOD[22:15] = 0 */
766 value |= 0x1000000;
767 status = vid_blk_write_word(dev, AFE_CTRL, value);
769 status = vid_blk_read_word(dev, OUT_CTRL1, &value);
770 value |= (1 << 7);
771 status = vid_blk_write_word(dev, OUT_CTRL1, value);
773 /* Set output mode */
774 status = cx231xx_read_modify_write_i2c_dword(dev,
775 VID_BLK_I2C_ADDRESS,
776 OUT_CTRL1, FLD_OUT_MODE,
777 dev->board.output_mode);
779 /* Tell DIF object to go to baseband mode */
780 status = cx231xx_dif_set_standard(dev,
781 DIF_USE_BASEBAND);
782 if (status < 0) {
783 dev_err(dev->dev,
784 "%s: cx231xx_dif set to By pass mode- errCode [%d]!\n",
785 __func__, status);
786 return status;
789 /* Read the DFE_CTRL1 register */
790 status = vid_blk_read_word(dev, DFE_CTRL1, &value);
792 /* enable the VBI_GATE_EN */
793 value |= FLD_VBI_GATE_EN;
795 /* Enable the auto-VGA enable */
796 value |= FLD_VGA_AUTO_EN;
798 /* Write it back */
799 status = vid_blk_write_word(dev, DFE_CTRL1, value);
801 /* Disable auto config of registers */
802 status = cx231xx_read_modify_write_i2c_dword(dev,
803 VID_BLK_I2C_ADDRESS,
804 MODE_CTRL, FLD_ACFG_DIS,
805 cx231xx_set_field(FLD_ACFG_DIS, 1));
807 /* Set CVBS input mode */
808 status = cx231xx_read_modify_write_i2c_dword(dev,
809 VID_BLK_I2C_ADDRESS,
810 MODE_CTRL, FLD_INPUT_MODE,
811 cx231xx_set_field(FLD_INPUT_MODE,
812 INPUT_MODE_CVBS_0));
813 } else {
814 /* Enable the DIF for the tuner */
816 /* Reinitialize the DIF */
817 status = cx231xx_dif_set_standard(dev, dev->norm);
818 if (status < 0) {
819 dev_err(dev->dev,
820 "%s: cx231xx_dif set to By pass mode- errCode [%d]!\n",
821 __func__, status);
822 return status;
825 /* Make sure bypass is cleared */
826 status = vid_blk_read_word(dev, DIF_MISC_CTRL, &value);
828 /* Clear the bypass bit */
829 value &= ~FLD_DIF_DIF_BYPASS;
831 /* Enable the use of the DIF block */
832 status = vid_blk_write_word(dev, DIF_MISC_CTRL, value);
834 /* Read the DFE_CTRL1 register */
835 status = vid_blk_read_word(dev, DFE_CTRL1, &value);
837 /* Disable the VBI_GATE_EN */
838 value &= ~FLD_VBI_GATE_EN;
840 /* Enable the auto-VGA enable, AGC, and
841 set the skip count to 2 */
842 value |= FLD_VGA_AUTO_EN | FLD_AGC_AUTO_EN | 0x00200000;
844 /* Write it back */
845 status = vid_blk_write_word(dev, DFE_CTRL1, value);
847 /* Wait until AGC locks up */
848 msleep(1);
850 /* Disable the auto-VGA enable AGC */
851 value &= ~(FLD_VGA_AUTO_EN);
853 /* Write it back */
854 status = vid_blk_write_word(dev, DFE_CTRL1, value);
856 /* Enable Polaris B0 AGC output */
857 status = vid_blk_read_word(dev, PIN_CTRL, &value);
858 value |= (FLD_OEF_AGC_RF) |
859 (FLD_OEF_AGC_IFVGA) |
860 (FLD_OEF_AGC_IF);
861 status = vid_blk_write_word(dev, PIN_CTRL, value);
863 /* Set output mode */
864 status = cx231xx_read_modify_write_i2c_dword(dev,
865 VID_BLK_I2C_ADDRESS,
866 OUT_CTRL1, FLD_OUT_MODE,
867 dev->board.output_mode);
869 /* Disable auto config of registers */
870 status = cx231xx_read_modify_write_i2c_dword(dev,
871 VID_BLK_I2C_ADDRESS,
872 MODE_CTRL, FLD_ACFG_DIS,
873 cx231xx_set_field(FLD_ACFG_DIS, 1));
875 /* Set CVBS input mode */
876 status = cx231xx_read_modify_write_i2c_dword(dev,
877 VID_BLK_I2C_ADDRESS,
878 MODE_CTRL, FLD_INPUT_MODE,
879 cx231xx_set_field(FLD_INPUT_MODE,
880 INPUT_MODE_CVBS_0));
882 /* Set some bits in AFE_CTRL so that channel 2 or 3
883 * is ready to receive audio */
884 /* Clear clamp for channels 2 and 3 (bit 16-17) */
885 /* Clear droop comp (bit 19-20) */
886 /* Set VGA_SEL (for audio control) (bit 7-8) */
887 status = vid_blk_read_word(dev, AFE_CTRL, &value);
889 /*Set Func mode:01-DIF 10-baseband 11-YUV*/
890 value &= (~(FLD_FUNC_MODE));
891 value |= 0x800000;
893 value |= FLD_VGA_SEL_CH3 | FLD_VGA_SEL_CH2;
895 status = vid_blk_write_word(dev, AFE_CTRL, value);
897 if (dev->tuner_type == TUNER_NXP_TDA18271) {
898 status = vid_blk_read_word(dev, PIN_CTRL,
899 &value);
900 status = vid_blk_write_word(dev, PIN_CTRL,
901 (value & 0xFFFFFFEF));
904 break;
907 break;
910 /* Set raw VBI mode */
911 status = cx231xx_read_modify_write_i2c_dword(dev,
912 VID_BLK_I2C_ADDRESS,
913 OUT_CTRL1, FLD_VBIHACTRAW_EN,
914 cx231xx_set_field(FLD_VBIHACTRAW_EN, 1));
916 status = vid_blk_read_word(dev, OUT_CTRL1, &value);
917 if (value & 0x02) {
918 value |= (1 << 19);
919 status = vid_blk_write_word(dev, OUT_CTRL1, value);
922 return status;
925 void cx231xx_enable656(struct cx231xx *dev)
927 u8 temp = 0;
928 /*enable TS1 data[0:7] as output to export 656*/
930 vid_blk_write_byte(dev, TS1_PIN_CTL0, 0xFF);
932 /*enable TS1 clock as output to export 656*/
934 vid_blk_read_byte(dev, TS1_PIN_CTL1, &temp);
935 temp = temp|0x04;
937 vid_blk_write_byte(dev, TS1_PIN_CTL1, temp);
939 EXPORT_SYMBOL_GPL(cx231xx_enable656);
941 void cx231xx_disable656(struct cx231xx *dev)
943 u8 temp = 0;
945 vid_blk_write_byte(dev, TS1_PIN_CTL0, 0x00);
947 vid_blk_read_byte(dev, TS1_PIN_CTL1, &temp);
948 temp = temp&0xFB;
950 vid_blk_write_byte(dev, TS1_PIN_CTL1, temp);
952 EXPORT_SYMBOL_GPL(cx231xx_disable656);
955 * Handle any video-mode specific overrides that are different
956 * on a per video standards basis after touching the MODE_CTRL
957 * register which resets many values for autodetect
959 int cx231xx_do_mode_ctrl_overrides(struct cx231xx *dev)
961 int status = 0;
963 dev_dbg(dev->dev, "%s: 0x%x\n",
964 __func__, (unsigned int)dev->norm);
966 /* Change the DFE_CTRL3 bp_percent to fix flagging */
967 status = vid_blk_write_word(dev, DFE_CTRL3, 0xCD3F0280);
969 if (dev->norm & (V4L2_STD_NTSC | V4L2_STD_PAL_M)) {
970 dev_dbg(dev->dev, "%s: NTSC\n", __func__);
972 /* Move the close caption lines out of active video,
973 adjust the active video start point */
974 status = cx231xx_read_modify_write_i2c_dword(dev,
975 VID_BLK_I2C_ADDRESS,
976 VERT_TIM_CTRL,
977 FLD_VBLANK_CNT, 0x18);
978 status = cx231xx_read_modify_write_i2c_dword(dev,
979 VID_BLK_I2C_ADDRESS,
980 VERT_TIM_CTRL,
981 FLD_VACTIVE_CNT,
982 0x1E7000);
983 status = cx231xx_read_modify_write_i2c_dword(dev,
984 VID_BLK_I2C_ADDRESS,
985 VERT_TIM_CTRL,
986 FLD_V656BLANK_CNT,
987 0x1C000000);
989 status = cx231xx_read_modify_write_i2c_dword(dev,
990 VID_BLK_I2C_ADDRESS,
991 HORIZ_TIM_CTRL,
992 FLD_HBLANK_CNT,
993 cx231xx_set_field
994 (FLD_HBLANK_CNT, 0x79));
996 } else if (dev->norm & V4L2_STD_SECAM) {
997 dev_dbg(dev->dev, "%s: SECAM\n", __func__);
998 status = cx231xx_read_modify_write_i2c_dword(dev,
999 VID_BLK_I2C_ADDRESS,
1000 VERT_TIM_CTRL,
1001 FLD_VBLANK_CNT, 0x20);
1002 status = cx231xx_read_modify_write_i2c_dword(dev,
1003 VID_BLK_I2C_ADDRESS,
1004 VERT_TIM_CTRL,
1005 FLD_VACTIVE_CNT,
1006 cx231xx_set_field
1007 (FLD_VACTIVE_CNT,
1008 0x244));
1009 status = cx231xx_read_modify_write_i2c_dword(dev,
1010 VID_BLK_I2C_ADDRESS,
1011 VERT_TIM_CTRL,
1012 FLD_V656BLANK_CNT,
1013 cx231xx_set_field
1014 (FLD_V656BLANK_CNT,
1015 0x24));
1016 /* Adjust the active video horizontal start point */
1017 status = cx231xx_read_modify_write_i2c_dword(dev,
1018 VID_BLK_I2C_ADDRESS,
1019 HORIZ_TIM_CTRL,
1020 FLD_HBLANK_CNT,
1021 cx231xx_set_field
1022 (FLD_HBLANK_CNT, 0x85));
1023 } else {
1024 dev_dbg(dev->dev, "%s: PAL\n", __func__);
1025 status = cx231xx_read_modify_write_i2c_dword(dev,
1026 VID_BLK_I2C_ADDRESS,
1027 VERT_TIM_CTRL,
1028 FLD_VBLANK_CNT, 0x20);
1029 status = cx231xx_read_modify_write_i2c_dword(dev,
1030 VID_BLK_I2C_ADDRESS,
1031 VERT_TIM_CTRL,
1032 FLD_VACTIVE_CNT,
1033 cx231xx_set_field
1034 (FLD_VACTIVE_CNT,
1035 0x244));
1036 status = cx231xx_read_modify_write_i2c_dword(dev,
1037 VID_BLK_I2C_ADDRESS,
1038 VERT_TIM_CTRL,
1039 FLD_V656BLANK_CNT,
1040 cx231xx_set_field
1041 (FLD_V656BLANK_CNT,
1042 0x24));
1043 /* Adjust the active video horizontal start point */
1044 status = cx231xx_read_modify_write_i2c_dword(dev,
1045 VID_BLK_I2C_ADDRESS,
1046 HORIZ_TIM_CTRL,
1047 FLD_HBLANK_CNT,
1048 cx231xx_set_field
1049 (FLD_HBLANK_CNT, 0x85));
1053 return status;
1056 int cx231xx_unmute_audio(struct cx231xx *dev)
1058 return vid_blk_write_byte(dev, PATH1_VOL_CTL, 0x24);
1060 EXPORT_SYMBOL_GPL(cx231xx_unmute_audio);
1062 static int stopAudioFirmware(struct cx231xx *dev)
1064 return vid_blk_write_byte(dev, DL_CTL_CONTROL, 0x03);
1067 static int restartAudioFirmware(struct cx231xx *dev)
1069 return vid_blk_write_byte(dev, DL_CTL_CONTROL, 0x13);
1072 int cx231xx_set_audio_input(struct cx231xx *dev, u8 input)
1074 int status = 0;
1075 enum AUDIO_INPUT ainput = AUDIO_INPUT_LINE;
1077 switch (INPUT(input)->amux) {
1078 case CX231XX_AMUX_VIDEO:
1079 ainput = AUDIO_INPUT_TUNER_TV;
1080 break;
1081 case CX231XX_AMUX_LINE_IN:
1082 status = cx231xx_i2s_blk_set_audio_input(dev, input);
1083 ainput = AUDIO_INPUT_LINE;
1084 break;
1085 default:
1086 break;
1089 status = cx231xx_set_audio_decoder_input(dev, ainput);
1091 return status;
1094 int cx231xx_set_audio_decoder_input(struct cx231xx *dev,
1095 enum AUDIO_INPUT audio_input)
1097 u32 dwval;
1098 int status;
1099 u8 gen_ctrl;
1100 u32 value = 0;
1102 /* Put it in soft reset */
1103 status = vid_blk_read_byte(dev, GENERAL_CTL, &gen_ctrl);
1104 gen_ctrl |= 1;
1105 status = vid_blk_write_byte(dev, GENERAL_CTL, gen_ctrl);
1107 switch (audio_input) {
1108 case AUDIO_INPUT_LINE:
1109 /* setup AUD_IO control from Merlin paralle output */
1110 value = cx231xx_set_field(FLD_AUD_CHAN1_SRC,
1111 AUD_CHAN_SRC_PARALLEL);
1112 status = vid_blk_write_word(dev, AUD_IO_CTRL, value);
1114 /* setup input to Merlin, SRC2 connect to AC97
1115 bypass upsample-by-2, slave mode, sony mode, left justify
1116 adr 091c, dat 01000000 */
1117 status = vid_blk_read_word(dev, AC97_CTL, &dwval);
1119 status = vid_blk_write_word(dev, AC97_CTL,
1120 (dwval | FLD_AC97_UP2X_BYPASS));
1122 /* select the parallel1 and SRC3 */
1123 status = vid_blk_write_word(dev, BAND_OUT_SEL,
1124 cx231xx_set_field(FLD_SRC3_IN_SEL, 0x0) |
1125 cx231xx_set_field(FLD_SRC3_CLK_SEL, 0x0) |
1126 cx231xx_set_field(FLD_PARALLEL1_SRC_SEL, 0x0));
1128 /* unmute all, AC97 in, independence mode
1129 adr 08d0, data 0x00063073 */
1130 status = vid_blk_write_word(dev, DL_CTL, 0x3000001);
1131 status = vid_blk_write_word(dev, PATH1_CTL1, 0x00063073);
1133 /* set AVC maximum threshold, adr 08d4, dat ffff0024 */
1134 status = vid_blk_read_word(dev, PATH1_VOL_CTL, &dwval);
1135 status = vid_blk_write_word(dev, PATH1_VOL_CTL,
1136 (dwval | FLD_PATH1_AVC_THRESHOLD));
1138 /* set SC maximum threshold, adr 08ec, dat ffffb3a3 */
1139 status = vid_blk_read_word(dev, PATH1_SC_CTL, &dwval);
1140 status = vid_blk_write_word(dev, PATH1_SC_CTL,
1141 (dwval | FLD_PATH1_SC_THRESHOLD));
1142 break;
1144 case AUDIO_INPUT_TUNER_TV:
1145 default:
1146 status = stopAudioFirmware(dev);
1147 /* Setup SRC sources and clocks */
1148 status = vid_blk_write_word(dev, BAND_OUT_SEL,
1149 cx231xx_set_field(FLD_SRC6_IN_SEL, 0x00) |
1150 cx231xx_set_field(FLD_SRC6_CLK_SEL, 0x01) |
1151 cx231xx_set_field(FLD_SRC5_IN_SEL, 0x00) |
1152 cx231xx_set_field(FLD_SRC5_CLK_SEL, 0x02) |
1153 cx231xx_set_field(FLD_SRC4_IN_SEL, 0x02) |
1154 cx231xx_set_field(FLD_SRC4_CLK_SEL, 0x03) |
1155 cx231xx_set_field(FLD_SRC3_IN_SEL, 0x00) |
1156 cx231xx_set_field(FLD_SRC3_CLK_SEL, 0x00) |
1157 cx231xx_set_field(FLD_BASEBAND_BYPASS_CTL, 0x00) |
1158 cx231xx_set_field(FLD_AC97_SRC_SEL, 0x03) |
1159 cx231xx_set_field(FLD_I2S_SRC_SEL, 0x00) |
1160 cx231xx_set_field(FLD_PARALLEL2_SRC_SEL, 0x02) |
1161 cx231xx_set_field(FLD_PARALLEL1_SRC_SEL, 0x01));
1163 /* Setup the AUD_IO control */
1164 status = vid_blk_write_word(dev, AUD_IO_CTRL,
1165 cx231xx_set_field(FLD_I2S_PORT_DIR, 0x00) |
1166 cx231xx_set_field(FLD_I2S_OUT_SRC, 0x00) |
1167 cx231xx_set_field(FLD_AUD_CHAN3_SRC, 0x00) |
1168 cx231xx_set_field(FLD_AUD_CHAN2_SRC, 0x00) |
1169 cx231xx_set_field(FLD_AUD_CHAN1_SRC, 0x03));
1171 status = vid_blk_write_word(dev, PATH1_CTL1, 0x1F063870);
1173 /* setAudioStandard(_audio_standard); */
1174 status = vid_blk_write_word(dev, PATH1_CTL1, 0x00063870);
1176 status = restartAudioFirmware(dev);
1178 switch (dev->board.tuner_type) {
1179 case TUNER_XC5000:
1180 /* SIF passthrough at 28.6363 MHz sample rate */
1181 status = cx231xx_read_modify_write_i2c_dword(dev,
1182 VID_BLK_I2C_ADDRESS,
1183 CHIP_CTRL,
1184 FLD_SIF_EN,
1185 cx231xx_set_field(FLD_SIF_EN, 1));
1186 break;
1187 case TUNER_NXP_TDA18271:
1188 /* Normal mode: SIF passthrough at 14.32 MHz */
1189 status = cx231xx_read_modify_write_i2c_dword(dev,
1190 VID_BLK_I2C_ADDRESS,
1191 CHIP_CTRL,
1192 FLD_SIF_EN,
1193 cx231xx_set_field(FLD_SIF_EN, 0));
1194 break;
1195 default:
1196 /* This is just a casual suggestion to people adding
1197 new boards in case they use a tuner type we don't
1198 currently know about */
1199 dev_info(dev->dev,
1200 "Unknown tuner type configuring SIF");
1201 break;
1203 break;
1205 case AUDIO_INPUT_TUNER_FM:
1206 /* use SIF for FM radio
1207 setupFM();
1208 setAudioStandard(_audio_standard);
1210 break;
1212 case AUDIO_INPUT_MUTE:
1213 status = vid_blk_write_word(dev, PATH1_CTL1, 0x1F011012);
1214 break;
1217 /* Take it out of soft reset */
1218 status = vid_blk_read_byte(dev, GENERAL_CTL, &gen_ctrl);
1219 gen_ctrl &= ~1;
1220 status = vid_blk_write_byte(dev, GENERAL_CTL, gen_ctrl);
1222 return status;
1225 /******************************************************************************
1226 * C H I P Specific C O N T R O L functions *
1227 ******************************************************************************/
1228 int cx231xx_init_ctrl_pin_status(struct cx231xx *dev)
1230 u32 value;
1231 int status = 0;
1233 status = vid_blk_read_word(dev, PIN_CTRL, &value);
1234 value |= (~dev->board.ctl_pin_status_mask);
1235 status = vid_blk_write_word(dev, PIN_CTRL, value);
1237 return status;
1240 int cx231xx_set_agc_analog_digital_mux_select(struct cx231xx *dev,
1241 u8 analog_or_digital)
1243 int status;
1245 /* first set the direction to output */
1246 status = cx231xx_set_gpio_direction(dev,
1247 dev->board.
1248 agc_analog_digital_select_gpio, 1);
1250 /* 0 - demod ; 1 - Analog mode */
1251 status = cx231xx_set_gpio_value(dev,
1252 dev->board.agc_analog_digital_select_gpio,
1253 analog_or_digital);
1255 if (status < 0)
1256 return status;
1258 return 0;
1261 int cx231xx_enable_i2c_port_3(struct cx231xx *dev, bool is_port_3)
1263 u8 value[4] = { 0, 0, 0, 0 };
1264 int status = 0;
1265 bool current_is_port_3;
1268 * Should this code check dev->port_3_switch_enabled first
1269 * to skip unnecessary reading of the register?
1270 * If yes, the flag dev->port_3_switch_enabled must be initialized
1271 * correctly.
1274 status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1275 PWR_CTL_EN, value, 4);
1276 if (status < 0)
1277 return status;
1279 current_is_port_3 = value[0] & I2C_DEMOD_EN ? true : false;
1281 /* Just return, if already using the right port */
1282 if (current_is_port_3 == is_port_3)
1283 return 0;
1285 if (is_port_3)
1286 value[0] |= I2C_DEMOD_EN;
1287 else
1288 value[0] &= ~I2C_DEMOD_EN;
1290 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1291 PWR_CTL_EN, value, 4);
1293 /* remember status of the switch for usage in is_tuner */
1294 if (status >= 0)
1295 dev->port_3_switch_enabled = is_port_3;
1297 return status;
1300 EXPORT_SYMBOL_GPL(cx231xx_enable_i2c_port_3);
1302 void update_HH_register_after_set_DIF(struct cx231xx *dev)
1305 u8 status = 0;
1306 u32 value = 0;
1308 vid_blk_write_word(dev, PIN_CTRL, 0xA0FFF82F);
1309 vid_blk_write_word(dev, DIF_MISC_CTRL, 0x0A203F11);
1310 vid_blk_write_word(dev, DIF_SRC_PHASE_INC, 0x1BEFBF06);
1312 status = vid_blk_read_word(dev, AFE_CTRL_C2HH_SRC_CTRL, &value);
1313 vid_blk_write_word(dev, AFE_CTRL_C2HH_SRC_CTRL, 0x4485D390);
1314 status = vid_blk_read_word(dev, AFE_CTRL_C2HH_SRC_CTRL, &value);
1318 void cx231xx_dump_HH_reg(struct cx231xx *dev)
1320 u32 value = 0;
1321 u16 i = 0;
1323 value = 0x45005390;
1324 vid_blk_write_word(dev, 0x104, value);
1326 for (i = 0x100; i < 0x140; i++) {
1327 vid_blk_read_word(dev, i, &value);
1328 dev_dbg(dev->dev, "reg0x%x=0x%x\n", i, value);
1329 i = i+3;
1332 for (i = 0x300; i < 0x400; i++) {
1333 vid_blk_read_word(dev, i, &value);
1334 dev_dbg(dev->dev, "reg0x%x=0x%x\n", i, value);
1335 i = i+3;
1338 for (i = 0x400; i < 0x440; i++) {
1339 vid_blk_read_word(dev, i, &value);
1340 dev_dbg(dev->dev, "reg0x%x=0x%x\n", i, value);
1341 i = i+3;
1344 vid_blk_read_word(dev, AFE_CTRL_C2HH_SRC_CTRL, &value);
1345 dev_dbg(dev->dev, "AFE_CTRL_C2HH_SRC_CTRL=0x%x\n", value);
1346 vid_blk_write_word(dev, AFE_CTRL_C2HH_SRC_CTRL, 0x4485D390);
1347 vid_blk_read_word(dev, AFE_CTRL_C2HH_SRC_CTRL, &value);
1348 dev_dbg(dev->dev, "AFE_CTRL_C2HH_SRC_CTRL=0x%x\n", value);
1351 #if 0
1352 static void cx231xx_dump_SC_reg(struct cx231xx *dev)
1354 u8 value[4] = { 0, 0, 0, 0 };
1355 dev_dbg(dev->dev, "%s!\n", __func__);
1357 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, BOARD_CFG_STAT,
1358 value, 4);
1359 dev_dbg(dev->dev,
1360 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", BOARD_CFG_STAT, value[0],
1361 value[1], value[2], value[3]);
1362 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, TS_MODE_REG,
1363 value, 4);
1364 dev_dbg(dev->dev,
1365 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", TS_MODE_REG, value[0],
1366 value[1], value[2], value[3]);
1367 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, TS1_CFG_REG,
1368 value, 4);
1369 dev_dbg(dev->dev,
1370 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", TS1_CFG_REG, value[0],
1371 value[1], value[2], value[3]);
1372 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, TS1_LENGTH_REG,
1373 value, 4);
1374 dev_dbg(dev->dev,
1375 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", TS1_LENGTH_REG, value[0],
1376 value[1], value[2], value[3]);
1378 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, TS2_CFG_REG,
1379 value, 4);
1380 dev_dbg(dev->dev,
1381 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", TS2_CFG_REG, value[0],
1382 value[1], value[2], value[3]);
1383 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, TS2_LENGTH_REG,
1384 value, 4);
1385 dev_dbg(dev->dev,
1386 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", TS2_LENGTH_REG, value[0],
1387 value[1], value[2], value[3]);
1388 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, EP_MODE_SET,
1389 value, 4);
1390 dev_dbg(dev->dev,
1391 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", EP_MODE_SET, value[0],
1392 value[1], value[2], value[3]);
1393 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_PWR_PTN1,
1394 value, 4);
1395 dev_dbg(dev->dev,
1396 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_PWR_PTN1, value[0],
1397 value[1], value[2], value[3]);
1399 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_PWR_PTN2,
1400 value, 4);
1401 dev_dbg(dev->dev,
1402 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_PWR_PTN2, value[0],
1403 value[1], value[2], value[3]);
1404 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_PWR_PTN3,
1405 value, 4);
1406 dev_dbg(dev->dev,
1407 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_PWR_PTN3, value[0],
1408 value[1], value[2], value[3]);
1409 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_PWR_MASK0,
1410 value, 4);
1411 dev_dbg(dev->dev,
1412 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_PWR_MASK0, value[0],
1413 value[1], value[2], value[3]);
1414 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_PWR_MASK1,
1415 value, 4);
1416 dev_dbg(dev->dev,
1417 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_PWR_MASK1, value[0],
1418 value[1], value[2], value[3]);
1420 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_PWR_MASK2,
1421 value, 4);
1422 dev_dbg(dev->dev,
1423 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_PWR_MASK2, value[0],
1424 value[1], value[2], value[3]);
1425 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_GAIN,
1426 value, 4);
1427 dev_dbg(dev->dev,
1428 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_GAIN, value[0],
1429 value[1], value[2], value[3]);
1430 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_CAR_REG,
1431 value, 4);
1432 dev_dbg(dev->dev,
1433 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_CAR_REG, value[0],
1434 value[1], value[2], value[3]);
1435 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_OT_CFG1,
1436 value, 4);
1437 dev_dbg(dev->dev,
1438 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_OT_CFG1, value[0],
1439 value[1], value[2], value[3]);
1441 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, CIR_OT_CFG2,
1442 value, 4);
1443 dev_dbg(dev->dev,
1444 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", CIR_OT_CFG2, value[0],
1445 value[1], value[2], value[3]);
1446 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN,
1447 value, 4);
1448 dev_dbg(dev->dev,
1449 "reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN, value[0],
1450 value[1], value[2], value[3]);
1452 #endif
1454 void cx231xx_Setup_AFE_for_LowIF(struct cx231xx *dev)
1457 u8 value = 0;
1459 afe_read_byte(dev, ADC_STATUS2_CH3, &value);
1460 value = (value & 0xFE)|0x01;
1461 afe_write_byte(dev, ADC_STATUS2_CH3, value);
1463 afe_read_byte(dev, ADC_STATUS2_CH3, &value);
1464 value = (value & 0xFE)|0x00;
1465 afe_write_byte(dev, ADC_STATUS2_CH3, value);
1469 config colibri to lo-if mode
1471 FIXME: ntf_mode = 2'b00 by default. But set 0x1 would reduce
1472 the diff IF input by half,
1474 for low-if agc defect
1477 afe_read_byte(dev, ADC_NTF_PRECLMP_EN_CH3, &value);
1478 value = (value & 0xFC)|0x00;
1479 afe_write_byte(dev, ADC_NTF_PRECLMP_EN_CH3, value);
1481 afe_read_byte(dev, ADC_INPUT_CH3, &value);
1482 value = (value & 0xF9)|0x02;
1483 afe_write_byte(dev, ADC_INPUT_CH3, value);
1485 afe_read_byte(dev, ADC_FB_FRCRST_CH3, &value);
1486 value = (value & 0xFB)|0x04;
1487 afe_write_byte(dev, ADC_FB_FRCRST_CH3, value);
1489 afe_read_byte(dev, ADC_DCSERVO_DEM_CH3, &value);
1490 value = (value & 0xFC)|0x03;
1491 afe_write_byte(dev, ADC_DCSERVO_DEM_CH3, value);
1493 afe_read_byte(dev, ADC_CTRL_DAC1_CH3, &value);
1494 value = (value & 0xFB)|0x04;
1495 afe_write_byte(dev, ADC_CTRL_DAC1_CH3, value);
1497 afe_read_byte(dev, ADC_CTRL_DAC23_CH3, &value);
1498 value = (value & 0xF8)|0x06;
1499 afe_write_byte(dev, ADC_CTRL_DAC23_CH3, value);
1501 afe_read_byte(dev, ADC_CTRL_DAC23_CH3, &value);
1502 value = (value & 0x8F)|0x40;
1503 afe_write_byte(dev, ADC_CTRL_DAC23_CH3, value);
1505 afe_read_byte(dev, ADC_PWRDN_CLAMP_CH3, &value);
1506 value = (value & 0xDF)|0x20;
1507 afe_write_byte(dev, ADC_PWRDN_CLAMP_CH3, value);
1510 void cx231xx_set_Colibri_For_LowIF(struct cx231xx *dev, u32 if_freq,
1511 u8 spectral_invert, u32 mode)
1513 u32 colibri_carrier_offset = 0;
1514 u32 func_mode = 0x01; /* Device has a DIF if this function is called */
1515 u32 standard = 0;
1516 u8 value[4] = { 0, 0, 0, 0 };
1518 dev_dbg(dev->dev, "Enter cx231xx_set_Colibri_For_LowIF()\n");
1519 value[0] = (u8) 0x6F;
1520 value[1] = (u8) 0x6F;
1521 value[2] = (u8) 0x6F;
1522 value[3] = (u8) 0x6F;
1523 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1524 PWR_CTL_EN, value, 4);
1526 /*Set colibri for low IF*/
1527 cx231xx_afe_set_mode(dev, AFE_MODE_LOW_IF);
1529 /* Set C2HH for low IF operation.*/
1530 standard = dev->norm;
1531 cx231xx_dif_configure_C2HH_for_low_IF(dev, dev->active_mode,
1532 func_mode, standard);
1534 /* Get colibri offsets.*/
1535 colibri_carrier_offset = cx231xx_Get_Colibri_CarrierOffset(mode,
1536 standard);
1538 dev_dbg(dev->dev, "colibri_carrier_offset=%d, standard=0x%x\n",
1539 colibri_carrier_offset, standard);
1541 /* Set the band Pass filter for DIF*/
1542 cx231xx_set_DIF_bandpass(dev, (if_freq+colibri_carrier_offset),
1543 spectral_invert, mode);
1546 u32 cx231xx_Get_Colibri_CarrierOffset(u32 mode, u32 standerd)
1548 u32 colibri_carrier_offset = 0;
1550 if (mode == TUNER_MODE_FM_RADIO) {
1551 colibri_carrier_offset = 1100000;
1552 } else if (standerd & (V4L2_STD_MN | V4L2_STD_NTSC_M_JP)) {
1553 colibri_carrier_offset = 4832000; /*4.83MHz */
1554 } else if (standerd & (V4L2_STD_PAL_B | V4L2_STD_PAL_G)) {
1555 colibri_carrier_offset = 2700000; /*2.70MHz */
1556 } else if (standerd & (V4L2_STD_PAL_D | V4L2_STD_PAL_I
1557 | V4L2_STD_SECAM)) {
1558 colibri_carrier_offset = 2100000; /*2.10MHz */
1561 return colibri_carrier_offset;
1564 void cx231xx_set_DIF_bandpass(struct cx231xx *dev, u32 if_freq,
1565 u8 spectral_invert, u32 mode)
1567 unsigned long pll_freq_word;
1568 u32 dif_misc_ctrl_value = 0;
1569 u64 pll_freq_u64 = 0;
1570 u32 i = 0;
1572 dev_dbg(dev->dev, "if_freq=%d;spectral_invert=0x%x;mode=0x%x\n",
1573 if_freq, spectral_invert, mode);
1576 if (mode == TUNER_MODE_FM_RADIO) {
1577 pll_freq_word = 0x905A1CAC;
1578 vid_blk_write_word(dev, DIF_PLL_FREQ_WORD, pll_freq_word);
1580 } else /*KSPROPERTY_TUNER_MODE_TV*/{
1581 /* Calculate the PLL frequency word based on the adjusted if_freq*/
1582 pll_freq_word = if_freq;
1583 pll_freq_u64 = (u64)pll_freq_word << 28L;
1584 do_div(pll_freq_u64, 50000000);
1585 pll_freq_word = (u32)pll_freq_u64;
1586 /*pll_freq_word = 0x3463497;*/
1587 vid_blk_write_word(dev, DIF_PLL_FREQ_WORD, pll_freq_word);
1589 if (spectral_invert) {
1590 if_freq -= 400000;
1591 /* Enable Spectral Invert*/
1592 vid_blk_read_word(dev, DIF_MISC_CTRL,
1593 &dif_misc_ctrl_value);
1594 dif_misc_ctrl_value = dif_misc_ctrl_value | 0x00200000;
1595 vid_blk_write_word(dev, DIF_MISC_CTRL,
1596 dif_misc_ctrl_value);
1597 } else {
1598 if_freq += 400000;
1599 /* Disable Spectral Invert*/
1600 vid_blk_read_word(dev, DIF_MISC_CTRL,
1601 &dif_misc_ctrl_value);
1602 dif_misc_ctrl_value = dif_misc_ctrl_value & 0xFFDFFFFF;
1603 vid_blk_write_word(dev, DIF_MISC_CTRL,
1604 dif_misc_ctrl_value);
1607 if_freq = (if_freq / 100000) * 100000;
1609 if (if_freq < 3000000)
1610 if_freq = 3000000;
1612 if (if_freq > 16000000)
1613 if_freq = 16000000;
1616 dev_dbg(dev->dev, "Enter IF=%zu\n", ARRAY_SIZE(Dif_set_array));
1617 for (i = 0; i < ARRAY_SIZE(Dif_set_array); i++) {
1618 if (Dif_set_array[i].if_freq == if_freq) {
1619 vid_blk_write_word(dev,
1620 Dif_set_array[i].register_address, Dif_set_array[i].value);
1625 /******************************************************************************
1626 * D I F - B L O C K C O N T R O L functions *
1627 ******************************************************************************/
1628 int cx231xx_dif_configure_C2HH_for_low_IF(struct cx231xx *dev, u32 mode,
1629 u32 function_mode, u32 standard)
1631 int status = 0;
1634 if (mode == V4L2_TUNER_RADIO) {
1635 /* C2HH */
1636 /* lo if big signal */
1637 status = cx231xx_reg_mask_write(dev,
1638 VID_BLK_I2C_ADDRESS, 32,
1639 AFE_CTRL_C2HH_SRC_CTRL, 30, 31, 0x1);
1640 /* FUNC_MODE = DIF */
1641 status = cx231xx_reg_mask_write(dev,
1642 VID_BLK_I2C_ADDRESS, 32,
1643 AFE_CTRL_C2HH_SRC_CTRL, 23, 24, function_mode);
1644 /* IF_MODE */
1645 status = cx231xx_reg_mask_write(dev,
1646 VID_BLK_I2C_ADDRESS, 32,
1647 AFE_CTRL_C2HH_SRC_CTRL, 15, 22, 0xFF);
1648 /* no inv */
1649 status = cx231xx_reg_mask_write(dev,
1650 VID_BLK_I2C_ADDRESS, 32,
1651 AFE_CTRL_C2HH_SRC_CTRL, 9, 9, 0x1);
1652 } else if (standard != DIF_USE_BASEBAND) {
1653 if (standard & V4L2_STD_MN) {
1654 /* lo if big signal */
1655 status = cx231xx_reg_mask_write(dev,
1656 VID_BLK_I2C_ADDRESS, 32,
1657 AFE_CTRL_C2HH_SRC_CTRL, 30, 31, 0x1);
1658 /* FUNC_MODE = DIF */
1659 status = cx231xx_reg_mask_write(dev,
1660 VID_BLK_I2C_ADDRESS, 32,
1661 AFE_CTRL_C2HH_SRC_CTRL, 23, 24,
1662 function_mode);
1663 /* IF_MODE */
1664 status = cx231xx_reg_mask_write(dev,
1665 VID_BLK_I2C_ADDRESS, 32,
1666 AFE_CTRL_C2HH_SRC_CTRL, 15, 22, 0xb);
1667 /* no inv */
1668 status = cx231xx_reg_mask_write(dev,
1669 VID_BLK_I2C_ADDRESS, 32,
1670 AFE_CTRL_C2HH_SRC_CTRL, 9, 9, 0x1);
1671 /* 0x124, AUD_CHAN1_SRC = 0x3 */
1672 status = cx231xx_reg_mask_write(dev,
1673 VID_BLK_I2C_ADDRESS, 32,
1674 AUD_IO_CTRL, 0, 31, 0x00000003);
1675 } else if ((standard == V4L2_STD_PAL_I) |
1676 (standard & V4L2_STD_PAL_D) |
1677 (standard & V4L2_STD_SECAM)) {
1678 /* C2HH setup */
1679 /* lo if big signal */
1680 status = cx231xx_reg_mask_write(dev,
1681 VID_BLK_I2C_ADDRESS, 32,
1682 AFE_CTRL_C2HH_SRC_CTRL, 30, 31, 0x1);
1683 /* FUNC_MODE = DIF */
1684 status = cx231xx_reg_mask_write(dev,
1685 VID_BLK_I2C_ADDRESS, 32,
1686 AFE_CTRL_C2HH_SRC_CTRL, 23, 24,
1687 function_mode);
1688 /* IF_MODE */
1689 status = cx231xx_reg_mask_write(dev,
1690 VID_BLK_I2C_ADDRESS, 32,
1691 AFE_CTRL_C2HH_SRC_CTRL, 15, 22, 0xF);
1692 /* no inv */
1693 status = cx231xx_reg_mask_write(dev,
1694 VID_BLK_I2C_ADDRESS, 32,
1695 AFE_CTRL_C2HH_SRC_CTRL, 9, 9, 0x1);
1696 } else {
1697 /* default PAL BG */
1698 /* C2HH setup */
1699 /* lo if big signal */
1700 status = cx231xx_reg_mask_write(dev,
1701 VID_BLK_I2C_ADDRESS, 32,
1702 AFE_CTRL_C2HH_SRC_CTRL, 30, 31, 0x1);
1703 /* FUNC_MODE = DIF */
1704 status = cx231xx_reg_mask_write(dev,
1705 VID_BLK_I2C_ADDRESS, 32,
1706 AFE_CTRL_C2HH_SRC_CTRL, 23, 24,
1707 function_mode);
1708 /* IF_MODE */
1709 status = cx231xx_reg_mask_write(dev,
1710 VID_BLK_I2C_ADDRESS, 32,
1711 AFE_CTRL_C2HH_SRC_CTRL, 15, 22, 0xE);
1712 /* no inv */
1713 status = cx231xx_reg_mask_write(dev,
1714 VID_BLK_I2C_ADDRESS, 32,
1715 AFE_CTRL_C2HH_SRC_CTRL, 9, 9, 0x1);
1719 return status;
1722 int cx231xx_dif_set_standard(struct cx231xx *dev, u32 standard)
1724 int status = 0;
1725 u32 dif_misc_ctrl_value = 0;
1726 u32 func_mode = 0;
1728 dev_dbg(dev->dev, "%s: setStandard to %x\n", __func__, standard);
1730 status = vid_blk_read_word(dev, DIF_MISC_CTRL, &dif_misc_ctrl_value);
1731 if (standard != DIF_USE_BASEBAND)
1732 dev->norm = standard;
1734 switch (dev->model) {
1735 case CX231XX_BOARD_CNXT_CARRAERA:
1736 case CX231XX_BOARD_CNXT_RDE_250:
1737 case CX231XX_BOARD_CNXT_SHELBY:
1738 case CX231XX_BOARD_CNXT_RDU_250:
1739 case CX231XX_BOARD_CNXT_VIDEO_GRABBER:
1740 case CX231XX_BOARD_HAUPPAUGE_EXETER:
1741 case CX231XX_BOARD_OTG102:
1742 func_mode = 0x03;
1743 break;
1744 case CX231XX_BOARD_CNXT_RDE_253S:
1745 case CX231XX_BOARD_CNXT_RDU_253S:
1746 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL:
1747 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC:
1748 func_mode = 0x01;
1749 break;
1750 default:
1751 func_mode = 0x01;
1754 status = cx231xx_dif_configure_C2HH_for_low_IF(dev, dev->active_mode,
1755 func_mode, standard);
1757 if (standard == DIF_USE_BASEBAND) { /* base band */
1758 /* There is a different SRC_PHASE_INC value
1759 for baseband vs. DIF */
1760 status = vid_blk_write_word(dev, DIF_SRC_PHASE_INC, 0xDF7DF83);
1761 status = vid_blk_read_word(dev, DIF_MISC_CTRL,
1762 &dif_misc_ctrl_value);
1763 dif_misc_ctrl_value |= FLD_DIF_DIF_BYPASS;
1764 status = vid_blk_write_word(dev, DIF_MISC_CTRL,
1765 dif_misc_ctrl_value);
1766 } else if (standard & V4L2_STD_PAL_D) {
1767 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1768 DIF_PLL_CTRL, 0, 31, 0x6503bc0c);
1769 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1770 DIF_PLL_CTRL1, 0, 31, 0xbd038c85);
1771 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1772 DIF_PLL_CTRL2, 0, 31, 0x1db4640a);
1773 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1774 DIF_PLL_CTRL3, 0, 31, 0x00008800);
1775 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1776 DIF_AGC_IF_REF, 0, 31, 0x444C1380);
1777 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1778 DIF_AGC_CTRL_IF, 0, 31, 0xDA302600);
1779 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1780 DIF_AGC_CTRL_INT, 0, 31, 0xDA261700);
1781 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1782 DIF_AGC_CTRL_RF, 0, 31, 0xDA262600);
1783 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1784 DIF_AGC_IF_INT_CURRENT, 0, 31,
1785 0x26001700);
1786 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1787 DIF_AGC_RF_CURRENT, 0, 31,
1788 0x00002660);
1789 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1790 DIF_VIDEO_AGC_CTRL, 0, 31,
1791 0x72500800);
1792 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1793 DIF_VID_AUD_OVERRIDE, 0, 31,
1794 0x27000100);
1795 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1796 DIF_AV_SEP_CTRL, 0, 31, 0x3F3934EA);
1797 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1798 DIF_COMP_FLT_CTRL, 0, 31,
1799 0x00000000);
1800 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1801 DIF_SRC_PHASE_INC, 0, 31,
1802 0x1befbf06);
1803 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1804 DIF_SRC_GAIN_CONTROL, 0, 31,
1805 0x000035e8);
1806 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1807 DIF_RPT_VARIANCE, 0, 31, 0x00000000);
1808 /* Save the Spec Inversion value */
1809 dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
1810 dif_misc_ctrl_value |= 0x3a023F11;
1811 } else if (standard & V4L2_STD_PAL_I) {
1812 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1813 DIF_PLL_CTRL, 0, 31, 0x6503bc0c);
1814 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1815 DIF_PLL_CTRL1, 0, 31, 0xbd038c85);
1816 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1817 DIF_PLL_CTRL2, 0, 31, 0x1db4640a);
1818 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1819 DIF_PLL_CTRL3, 0, 31, 0x00008800);
1820 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1821 DIF_AGC_IF_REF, 0, 31, 0x444C1380);
1822 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1823 DIF_AGC_CTRL_IF, 0, 31, 0xDA302600);
1824 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1825 DIF_AGC_CTRL_INT, 0, 31, 0xDA261700);
1826 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1827 DIF_AGC_CTRL_RF, 0, 31, 0xDA262600);
1828 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1829 DIF_AGC_IF_INT_CURRENT, 0, 31,
1830 0x26001700);
1831 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1832 DIF_AGC_RF_CURRENT, 0, 31,
1833 0x00002660);
1834 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1835 DIF_VIDEO_AGC_CTRL, 0, 31,
1836 0x72500800);
1837 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1838 DIF_VID_AUD_OVERRIDE, 0, 31,
1839 0x27000100);
1840 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1841 DIF_AV_SEP_CTRL, 0, 31, 0x5F39A934);
1842 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1843 DIF_COMP_FLT_CTRL, 0, 31,
1844 0x00000000);
1845 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1846 DIF_SRC_PHASE_INC, 0, 31,
1847 0x1befbf06);
1848 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1849 DIF_SRC_GAIN_CONTROL, 0, 31,
1850 0x000035e8);
1851 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1852 DIF_RPT_VARIANCE, 0, 31, 0x00000000);
1853 /* Save the Spec Inversion value */
1854 dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
1855 dif_misc_ctrl_value |= 0x3a033F11;
1856 } else if (standard & V4L2_STD_PAL_M) {
1857 /* improved Low Frequency Phase Noise */
1858 status = vid_blk_write_word(dev, DIF_PLL_CTRL, 0xFF01FF0C);
1859 status = vid_blk_write_word(dev, DIF_PLL_CTRL1, 0xbd038c85);
1860 status = vid_blk_write_word(dev, DIF_PLL_CTRL2, 0x1db4640a);
1861 status = vid_blk_write_word(dev, DIF_PLL_CTRL3, 0x00008800);
1862 status = vid_blk_write_word(dev, DIF_AGC_IF_REF, 0x444C1380);
1863 status = vid_blk_write_word(dev, DIF_AGC_IF_INT_CURRENT,
1864 0x26001700);
1865 status = vid_blk_write_word(dev, DIF_AGC_RF_CURRENT,
1866 0x00002660);
1867 status = vid_blk_write_word(dev, DIF_VIDEO_AGC_CTRL,
1868 0x72500800);
1869 status = vid_blk_write_word(dev, DIF_VID_AUD_OVERRIDE,
1870 0x27000100);
1871 status = vid_blk_write_word(dev, DIF_AV_SEP_CTRL, 0x012c405d);
1872 status = vid_blk_write_word(dev, DIF_COMP_FLT_CTRL,
1873 0x009f50c1);
1874 status = vid_blk_write_word(dev, DIF_SRC_PHASE_INC,
1875 0x1befbf06);
1876 status = vid_blk_write_word(dev, DIF_SRC_GAIN_CONTROL,
1877 0x000035e8);
1878 status = vid_blk_write_word(dev, DIF_SOFT_RST_CTRL_REVB,
1879 0x00000000);
1880 /* Save the Spec Inversion value */
1881 dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
1882 dif_misc_ctrl_value |= 0x3A0A3F10;
1883 } else if (standard & (V4L2_STD_PAL_N | V4L2_STD_PAL_Nc)) {
1884 /* improved Low Frequency Phase Noise */
1885 status = vid_blk_write_word(dev, DIF_PLL_CTRL, 0xFF01FF0C);
1886 status = vid_blk_write_word(dev, DIF_PLL_CTRL1, 0xbd038c85);
1887 status = vid_blk_write_word(dev, DIF_PLL_CTRL2, 0x1db4640a);
1888 status = vid_blk_write_word(dev, DIF_PLL_CTRL3, 0x00008800);
1889 status = vid_blk_write_word(dev, DIF_AGC_IF_REF, 0x444C1380);
1890 status = vid_blk_write_word(dev, DIF_AGC_IF_INT_CURRENT,
1891 0x26001700);
1892 status = vid_blk_write_word(dev, DIF_AGC_RF_CURRENT,
1893 0x00002660);
1894 status = vid_blk_write_word(dev, DIF_VIDEO_AGC_CTRL,
1895 0x72500800);
1896 status = vid_blk_write_word(dev, DIF_VID_AUD_OVERRIDE,
1897 0x27000100);
1898 status = vid_blk_write_word(dev, DIF_AV_SEP_CTRL,
1899 0x012c405d);
1900 status = vid_blk_write_word(dev, DIF_COMP_FLT_CTRL,
1901 0x009f50c1);
1902 status = vid_blk_write_word(dev, DIF_SRC_PHASE_INC,
1903 0x1befbf06);
1904 status = vid_blk_write_word(dev, DIF_SRC_GAIN_CONTROL,
1905 0x000035e8);
1906 status = vid_blk_write_word(dev, DIF_SOFT_RST_CTRL_REVB,
1907 0x00000000);
1908 /* Save the Spec Inversion value */
1909 dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
1910 dif_misc_ctrl_value = 0x3A093F10;
1911 } else if (standard &
1912 (V4L2_STD_SECAM_B | V4L2_STD_SECAM_D | V4L2_STD_SECAM_G |
1913 V4L2_STD_SECAM_K | V4L2_STD_SECAM_K1)) {
1915 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1916 DIF_PLL_CTRL, 0, 31, 0x6503bc0c);
1917 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1918 DIF_PLL_CTRL1, 0, 31, 0xbd038c85);
1919 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1920 DIF_PLL_CTRL2, 0, 31, 0x1db4640a);
1921 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1922 DIF_PLL_CTRL3, 0, 31, 0x00008800);
1923 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1924 DIF_AGC_IF_REF, 0, 31, 0x888C0380);
1925 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1926 DIF_AGC_CTRL_IF, 0, 31, 0xe0262600);
1927 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1928 DIF_AGC_CTRL_INT, 0, 31, 0xc2171700);
1929 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1930 DIF_AGC_CTRL_RF, 0, 31, 0xc2262600);
1931 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1932 DIF_AGC_IF_INT_CURRENT, 0, 31,
1933 0x26001700);
1934 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1935 DIF_AGC_RF_CURRENT, 0, 31,
1936 0x00002660);
1937 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1938 DIF_VID_AUD_OVERRIDE, 0, 31,
1939 0x27000100);
1940 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1941 DIF_AV_SEP_CTRL, 0, 31, 0x3F3530ec);
1942 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1943 DIF_COMP_FLT_CTRL, 0, 31,
1944 0x00000000);
1945 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1946 DIF_SRC_PHASE_INC, 0, 31,
1947 0x1befbf06);
1948 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1949 DIF_SRC_GAIN_CONTROL, 0, 31,
1950 0x000035e8);
1951 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1952 DIF_RPT_VARIANCE, 0, 31, 0x00000000);
1953 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1954 DIF_VIDEO_AGC_CTRL, 0, 31,
1955 0xf4000000);
1957 /* Save the Spec Inversion value */
1958 dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
1959 dif_misc_ctrl_value |= 0x3a023F11;
1960 } else if (standard & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC)) {
1961 /* Is it SECAM_L1? */
1962 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1963 DIF_PLL_CTRL, 0, 31, 0x6503bc0c);
1964 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1965 DIF_PLL_CTRL1, 0, 31, 0xbd038c85);
1966 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1967 DIF_PLL_CTRL2, 0, 31, 0x1db4640a);
1968 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1969 DIF_PLL_CTRL3, 0, 31, 0x00008800);
1970 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1971 DIF_AGC_IF_REF, 0, 31, 0x888C0380);
1972 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1973 DIF_AGC_CTRL_IF, 0, 31, 0xe0262600);
1974 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1975 DIF_AGC_CTRL_INT, 0, 31, 0xc2171700);
1976 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1977 DIF_AGC_CTRL_RF, 0, 31, 0xc2262600);
1978 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1979 DIF_AGC_IF_INT_CURRENT, 0, 31,
1980 0x26001700);
1981 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1982 DIF_AGC_RF_CURRENT, 0, 31,
1983 0x00002660);
1984 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1985 DIF_VID_AUD_OVERRIDE, 0, 31,
1986 0x27000100);
1987 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1988 DIF_AV_SEP_CTRL, 0, 31, 0x3F3530ec);
1989 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1990 DIF_COMP_FLT_CTRL, 0, 31,
1991 0x00000000);
1992 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1993 DIF_SRC_PHASE_INC, 0, 31,
1994 0x1befbf06);
1995 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1996 DIF_SRC_GAIN_CONTROL, 0, 31,
1997 0x000035e8);
1998 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
1999 DIF_RPT_VARIANCE, 0, 31, 0x00000000);
2000 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2001 DIF_VIDEO_AGC_CTRL, 0, 31,
2002 0xf2560000);
2004 /* Save the Spec Inversion value */
2005 dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
2006 dif_misc_ctrl_value |= 0x3a023F11;
2008 } else if (standard & V4L2_STD_NTSC_M) {
2009 /* V4L2_STD_NTSC_M (75 IRE Setup) Or
2010 V4L2_STD_NTSC_M_JP (Japan, 0 IRE Setup) */
2012 /* For NTSC the centre frequency of video coming out of
2013 sidewinder is around 7.1MHz or 3.6MHz depending on the
2014 spectral inversion. so for a non spectrally inverted channel
2015 the pll freq word is 0x03420c49
2018 status = vid_blk_write_word(dev, DIF_PLL_CTRL, 0x6503BC0C);
2019 status = vid_blk_write_word(dev, DIF_PLL_CTRL1, 0xBD038C85);
2020 status = vid_blk_write_word(dev, DIF_PLL_CTRL2, 0x1DB4640A);
2021 status = vid_blk_write_word(dev, DIF_PLL_CTRL3, 0x00008800);
2022 status = vid_blk_write_word(dev, DIF_AGC_IF_REF, 0x444C0380);
2023 status = vid_blk_write_word(dev, DIF_AGC_IF_INT_CURRENT,
2024 0x26001700);
2025 status = vid_blk_write_word(dev, DIF_AGC_RF_CURRENT,
2026 0x00002660);
2027 status = vid_blk_write_word(dev, DIF_VIDEO_AGC_CTRL,
2028 0x04000800);
2029 status = vid_blk_write_word(dev, DIF_VID_AUD_OVERRIDE,
2030 0x27000100);
2031 status = vid_blk_write_word(dev, DIF_AV_SEP_CTRL, 0x01296e1f);
2033 status = vid_blk_write_word(dev, DIF_COMP_FLT_CTRL,
2034 0x009f50c1);
2035 status = vid_blk_write_word(dev, DIF_SRC_PHASE_INC,
2036 0x1befbf06);
2037 status = vid_blk_write_word(dev, DIF_SRC_GAIN_CONTROL,
2038 0x000035e8);
2040 status = vid_blk_write_word(dev, DIF_AGC_CTRL_IF, 0xC2262600);
2041 status = vid_blk_write_word(dev, DIF_AGC_CTRL_INT,
2042 0xC2262600);
2043 status = vid_blk_write_word(dev, DIF_AGC_CTRL_RF, 0xC2262600);
2045 /* Save the Spec Inversion value */
2046 dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
2047 dif_misc_ctrl_value |= 0x3a003F10;
2048 } else {
2049 /* default PAL BG */
2050 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2051 DIF_PLL_CTRL, 0, 31, 0x6503bc0c);
2052 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2053 DIF_PLL_CTRL1, 0, 31, 0xbd038c85);
2054 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2055 DIF_PLL_CTRL2, 0, 31, 0x1db4640a);
2056 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2057 DIF_PLL_CTRL3, 0, 31, 0x00008800);
2058 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2059 DIF_AGC_IF_REF, 0, 31, 0x444C1380);
2060 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2061 DIF_AGC_CTRL_IF, 0, 31, 0xDA302600);
2062 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2063 DIF_AGC_CTRL_INT, 0, 31, 0xDA261700);
2064 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2065 DIF_AGC_CTRL_RF, 0, 31, 0xDA262600);
2066 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2067 DIF_AGC_IF_INT_CURRENT, 0, 31,
2068 0x26001700);
2069 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2070 DIF_AGC_RF_CURRENT, 0, 31,
2071 0x00002660);
2072 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2073 DIF_VIDEO_AGC_CTRL, 0, 31,
2074 0x72500800);
2075 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2076 DIF_VID_AUD_OVERRIDE, 0, 31,
2077 0x27000100);
2078 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2079 DIF_AV_SEP_CTRL, 0, 31, 0x3F3530EC);
2080 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2081 DIF_COMP_FLT_CTRL, 0, 31,
2082 0x00A653A8);
2083 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2084 DIF_SRC_PHASE_INC, 0, 31,
2085 0x1befbf06);
2086 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2087 DIF_SRC_GAIN_CONTROL, 0, 31,
2088 0x000035e8);
2089 status = cx231xx_reg_mask_write(dev, VID_BLK_I2C_ADDRESS, 32,
2090 DIF_RPT_VARIANCE, 0, 31, 0x00000000);
2091 /* Save the Spec Inversion value */
2092 dif_misc_ctrl_value &= FLD_DIF_SPEC_INV;
2093 dif_misc_ctrl_value |= 0x3a013F11;
2096 /* The AGC values should be the same for all standards,
2097 AUD_SRC_SEL[19] should always be disabled */
2098 dif_misc_ctrl_value &= ~FLD_DIF_AUD_SRC_SEL;
2100 /* It is still possible to get Set Standard calls even when we
2101 are in FM mode.
2102 This is done to override the value for FM. */
2103 if (dev->active_mode == V4L2_TUNER_RADIO)
2104 dif_misc_ctrl_value = 0x7a080000;
2106 /* Write the calculated value for misc ontrol register */
2107 status = vid_blk_write_word(dev, DIF_MISC_CTRL, dif_misc_ctrl_value);
2109 return status;
2112 int cx231xx_tuner_pre_channel_change(struct cx231xx *dev)
2114 int status = 0;
2115 u32 dwval;
2117 /* Set the RF and IF k_agc values to 3 */
2118 status = vid_blk_read_word(dev, DIF_AGC_IF_REF, &dwval);
2119 dwval &= ~(FLD_DIF_K_AGC_RF | FLD_DIF_K_AGC_IF);
2120 dwval |= 0x33000000;
2122 status = vid_blk_write_word(dev, DIF_AGC_IF_REF, dwval);
2124 return status;
2127 int cx231xx_tuner_post_channel_change(struct cx231xx *dev)
2129 int status = 0;
2130 u32 dwval;
2131 dev_dbg(dev->dev, "%s: dev->tuner_type =0%d\n",
2132 __func__, dev->tuner_type);
2133 /* Set the RF and IF k_agc values to 4 for PAL/NTSC and 8 for
2134 * SECAM L/B/D standards */
2135 status = vid_blk_read_word(dev, DIF_AGC_IF_REF, &dwval);
2136 dwval &= ~(FLD_DIF_K_AGC_RF | FLD_DIF_K_AGC_IF);
2138 if (dev->norm & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_B |
2139 V4L2_STD_SECAM_D)) {
2140 if (dev->tuner_type == TUNER_NXP_TDA18271) {
2141 dwval &= ~FLD_DIF_IF_REF;
2142 dwval |= 0x88000300;
2143 } else
2144 dwval |= 0x88000000;
2145 } else {
2146 if (dev->tuner_type == TUNER_NXP_TDA18271) {
2147 dwval &= ~FLD_DIF_IF_REF;
2148 dwval |= 0xCC000300;
2149 } else
2150 dwval |= 0x44000000;
2153 status = vid_blk_write_word(dev, DIF_AGC_IF_REF, dwval);
2155 return status == sizeof(dwval) ? 0 : -EIO;
2158 /******************************************************************************
2159 * I 2 S - B L O C K C O N T R O L functions *
2160 ******************************************************************************/
2161 int cx231xx_i2s_blk_initialize(struct cx231xx *dev)
2163 int status = 0;
2164 u32 value;
2166 status = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
2167 CH_PWR_CTRL1, 1, &value, 1);
2168 /* enables clock to delta-sigma and decimation filter */
2169 value |= 0x80;
2170 status = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
2171 CH_PWR_CTRL1, 1, value, 1);
2172 /* power up all channel */
2173 status = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
2174 CH_PWR_CTRL2, 1, 0x00, 1);
2176 return status;
2179 int cx231xx_i2s_blk_update_power_control(struct cx231xx *dev,
2180 enum AV_MODE avmode)
2182 int status = 0;
2183 u32 value = 0;
2185 if (avmode != POLARIS_AVMODE_ENXTERNAL_AV) {
2186 status = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
2187 CH_PWR_CTRL2, 1, &value, 1);
2188 value |= 0xfe;
2189 status = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
2190 CH_PWR_CTRL2, 1, value, 1);
2191 } else {
2192 status = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
2193 CH_PWR_CTRL2, 1, 0x00, 1);
2196 return status;
2199 /* set i2s_blk for audio input types */
2200 int cx231xx_i2s_blk_set_audio_input(struct cx231xx *dev, u8 audio_input)
2202 int status = 0;
2204 switch (audio_input) {
2205 case CX231XX_AMUX_LINE_IN:
2206 status = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
2207 CH_PWR_CTRL2, 1, 0x00, 1);
2208 status = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
2209 CH_PWR_CTRL1, 1, 0x80, 1);
2210 break;
2211 case CX231XX_AMUX_VIDEO:
2212 default:
2213 break;
2216 dev->ctl_ainput = audio_input;
2218 return status;
2221 /******************************************************************************
2222 * P O W E R C O N T R O L functions *
2223 ******************************************************************************/
2224 int cx231xx_set_power_mode(struct cx231xx *dev, enum AV_MODE mode)
2226 u8 value[4] = { 0, 0, 0, 0 };
2227 u32 tmp = 0;
2228 int status = 0;
2230 if (dev->power_mode != mode)
2231 dev->power_mode = mode;
2232 else {
2233 dev_dbg(dev->dev, "%s: mode = %d, No Change req.\n",
2234 __func__, mode);
2235 return 0;
2238 status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN, value,
2240 if (status < 0)
2241 return status;
2243 tmp = le32_to_cpu(*((__le32 *) value));
2245 switch (mode) {
2246 case POLARIS_AVMODE_ENXTERNAL_AV:
2248 tmp &= (~PWR_MODE_MASK);
2250 tmp |= PWR_AV_EN;
2251 value[0] = (u8) tmp;
2252 value[1] = (u8) (tmp >> 8);
2253 value[2] = (u8) (tmp >> 16);
2254 value[3] = (u8) (tmp >> 24);
2255 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
2256 PWR_CTL_EN, value, 4);
2257 msleep(PWR_SLEEP_INTERVAL);
2259 tmp |= PWR_ISO_EN;
2260 value[0] = (u8) tmp;
2261 value[1] = (u8) (tmp >> 8);
2262 value[2] = (u8) (tmp >> 16);
2263 value[3] = (u8) (tmp >> 24);
2264 status =
2265 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, PWR_CTL_EN,
2266 value, 4);
2267 msleep(PWR_SLEEP_INTERVAL);
2269 tmp |= POLARIS_AVMODE_ENXTERNAL_AV;
2270 value[0] = (u8) tmp;
2271 value[1] = (u8) (tmp >> 8);
2272 value[2] = (u8) (tmp >> 16);
2273 value[3] = (u8) (tmp >> 24);
2274 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
2275 PWR_CTL_EN, value, 4);
2277 /* reset state of xceive tuner */
2278 dev->xc_fw_load_done = 0;
2279 break;
2281 case POLARIS_AVMODE_ANALOGT_TV:
2283 tmp |= PWR_DEMOD_EN;
2284 value[0] = (u8) tmp;
2285 value[1] = (u8) (tmp >> 8);
2286 value[2] = (u8) (tmp >> 16);
2287 value[3] = (u8) (tmp >> 24);
2288 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
2289 PWR_CTL_EN, value, 4);
2290 msleep(PWR_SLEEP_INTERVAL);
2292 if (!(tmp & PWR_TUNER_EN)) {
2293 tmp |= (PWR_TUNER_EN);
2294 value[0] = (u8) tmp;
2295 value[1] = (u8) (tmp >> 8);
2296 value[2] = (u8) (tmp >> 16);
2297 value[3] = (u8) (tmp >> 24);
2298 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
2299 PWR_CTL_EN, value, 4);
2300 msleep(PWR_SLEEP_INTERVAL);
2303 if (!(tmp & PWR_AV_EN)) {
2304 tmp |= PWR_AV_EN;
2305 value[0] = (u8) tmp;
2306 value[1] = (u8) (tmp >> 8);
2307 value[2] = (u8) (tmp >> 16);
2308 value[3] = (u8) (tmp >> 24);
2309 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
2310 PWR_CTL_EN, value, 4);
2311 msleep(PWR_SLEEP_INTERVAL);
2313 if (!(tmp & PWR_ISO_EN)) {
2314 tmp |= PWR_ISO_EN;
2315 value[0] = (u8) tmp;
2316 value[1] = (u8) (tmp >> 8);
2317 value[2] = (u8) (tmp >> 16);
2318 value[3] = (u8) (tmp >> 24);
2319 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
2320 PWR_CTL_EN, value, 4);
2321 msleep(PWR_SLEEP_INTERVAL);
2324 if (!(tmp & POLARIS_AVMODE_ANALOGT_TV)) {
2325 tmp |= POLARIS_AVMODE_ANALOGT_TV;
2326 value[0] = (u8) tmp;
2327 value[1] = (u8) (tmp >> 8);
2328 value[2] = (u8) (tmp >> 16);
2329 value[3] = (u8) (tmp >> 24);
2330 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
2331 PWR_CTL_EN, value, 4);
2332 msleep(PWR_SLEEP_INTERVAL);
2335 if (dev->board.tuner_type != TUNER_ABSENT) {
2336 /* reset the Tuner */
2337 if (dev->board.tuner_gpio)
2338 cx231xx_gpio_set(dev, dev->board.tuner_gpio);
2340 if (dev->cx231xx_reset_analog_tuner)
2341 dev->cx231xx_reset_analog_tuner(dev);
2344 break;
2346 case POLARIS_AVMODE_DIGITAL:
2347 if (!(tmp & PWR_TUNER_EN)) {
2348 tmp |= (PWR_TUNER_EN);
2349 value[0] = (u8) tmp;
2350 value[1] = (u8) (tmp >> 8);
2351 value[2] = (u8) (tmp >> 16);
2352 value[3] = (u8) (tmp >> 24);
2353 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
2354 PWR_CTL_EN, value, 4);
2355 msleep(PWR_SLEEP_INTERVAL);
2357 if (!(tmp & PWR_AV_EN)) {
2358 tmp |= PWR_AV_EN;
2359 value[0] = (u8) tmp;
2360 value[1] = (u8) (tmp >> 8);
2361 value[2] = (u8) (tmp >> 16);
2362 value[3] = (u8) (tmp >> 24);
2363 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
2364 PWR_CTL_EN, value, 4);
2365 msleep(PWR_SLEEP_INTERVAL);
2367 if (!(tmp & PWR_ISO_EN)) {
2368 tmp |= PWR_ISO_EN;
2369 value[0] = (u8) tmp;
2370 value[1] = (u8) (tmp >> 8);
2371 value[2] = (u8) (tmp >> 16);
2372 value[3] = (u8) (tmp >> 24);
2373 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
2374 PWR_CTL_EN, value, 4);
2375 msleep(PWR_SLEEP_INTERVAL);
2378 tmp &= (~PWR_AV_MODE);
2379 tmp |= POLARIS_AVMODE_DIGITAL;
2380 value[0] = (u8) tmp;
2381 value[1] = (u8) (tmp >> 8);
2382 value[2] = (u8) (tmp >> 16);
2383 value[3] = (u8) (tmp >> 24);
2384 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
2385 PWR_CTL_EN, value, 4);
2386 msleep(PWR_SLEEP_INTERVAL);
2388 if (!(tmp & PWR_DEMOD_EN)) {
2389 tmp |= PWR_DEMOD_EN;
2390 value[0] = (u8) tmp;
2391 value[1] = (u8) (tmp >> 8);
2392 value[2] = (u8) (tmp >> 16);
2393 value[3] = (u8) (tmp >> 24);
2394 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
2395 PWR_CTL_EN, value, 4);
2396 msleep(PWR_SLEEP_INTERVAL);
2399 if (dev->board.tuner_type != TUNER_ABSENT) {
2400 /* reset the Tuner */
2401 if (dev->board.tuner_gpio)
2402 cx231xx_gpio_set(dev, dev->board.tuner_gpio);
2404 if (dev->cx231xx_reset_analog_tuner)
2405 dev->cx231xx_reset_analog_tuner(dev);
2407 break;
2409 default:
2410 break;
2413 msleep(PWR_SLEEP_INTERVAL);
2415 /* For power saving, only enable Pwr_resetout_n
2416 when digital TV is selected. */
2417 if (mode == POLARIS_AVMODE_DIGITAL) {
2418 tmp |= PWR_RESETOUT_EN;
2419 value[0] = (u8) tmp;
2420 value[1] = (u8) (tmp >> 8);
2421 value[2] = (u8) (tmp >> 16);
2422 value[3] = (u8) (tmp >> 24);
2423 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
2424 PWR_CTL_EN, value, 4);
2425 msleep(PWR_SLEEP_INTERVAL);
2428 /* update power control for afe */
2429 status = cx231xx_afe_update_power_control(dev, mode);
2431 /* update power control for i2s_blk */
2432 status = cx231xx_i2s_blk_update_power_control(dev, mode);
2434 status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN, value,
2437 return status;
2440 int cx231xx_power_suspend(struct cx231xx *dev)
2442 u8 value[4] = { 0, 0, 0, 0 };
2443 u32 tmp = 0;
2444 int status = 0;
2446 status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN,
2447 value, 4);
2448 if (status > 0)
2449 return status;
2451 tmp = le32_to_cpu(*((__le32 *) value));
2452 tmp &= (~PWR_MODE_MASK);
2454 value[0] = (u8) tmp;
2455 value[1] = (u8) (tmp >> 8);
2456 value[2] = (u8) (tmp >> 16);
2457 value[3] = (u8) (tmp >> 24);
2458 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, PWR_CTL_EN,
2459 value, 4);
2461 return status;
2464 /******************************************************************************
2465 * S T R E A M C O N T R O L functions *
2466 ******************************************************************************/
2467 int cx231xx_start_stream(struct cx231xx *dev, u32 ep_mask)
2469 u8 value[4] = { 0x0, 0x0, 0x0, 0x0 };
2470 u32 tmp = 0;
2471 int status = 0;
2473 dev_dbg(dev->dev, "%s: ep_mask = %x\n", __func__, ep_mask);
2474 status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, EP_MODE_SET,
2475 value, 4);
2476 if (status < 0)
2477 return status;
2479 tmp = le32_to_cpu(*((__le32 *) value));
2480 tmp |= ep_mask;
2481 value[0] = (u8) tmp;
2482 value[1] = (u8) (tmp >> 8);
2483 value[2] = (u8) (tmp >> 16);
2484 value[3] = (u8) (tmp >> 24);
2486 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, EP_MODE_SET,
2487 value, 4);
2489 return status;
2492 int cx231xx_stop_stream(struct cx231xx *dev, u32 ep_mask)
2494 u8 value[4] = { 0x0, 0x0, 0x0, 0x0 };
2495 u32 tmp = 0;
2496 int status = 0;
2498 dev_dbg(dev->dev, "%s: ep_mask = %x\n", __func__, ep_mask);
2499 status =
2500 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, EP_MODE_SET, value, 4);
2501 if (status < 0)
2502 return status;
2504 tmp = le32_to_cpu(*((__le32 *) value));
2505 tmp &= (~ep_mask);
2506 value[0] = (u8) tmp;
2507 value[1] = (u8) (tmp >> 8);
2508 value[2] = (u8) (tmp >> 16);
2509 value[3] = (u8) (tmp >> 24);
2511 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, EP_MODE_SET,
2512 value, 4);
2514 return status;
2517 int cx231xx_initialize_stream_xfer(struct cx231xx *dev, u32 media_type)
2519 int status = 0;
2520 u32 value = 0;
2521 u8 val[4] = { 0, 0, 0, 0 };
2523 if (dev->udev->speed == USB_SPEED_HIGH) {
2524 switch (media_type) {
2525 case Audio:
2526 dev_dbg(dev->dev,
2527 "%s: Audio enter HANC\n", __func__);
2528 status =
2529 cx231xx_mode_register(dev, TS_MODE_REG, 0x9300);
2530 break;
2532 case Vbi:
2533 dev_dbg(dev->dev,
2534 "%s: set vanc registers\n", __func__);
2535 status = cx231xx_mode_register(dev, TS_MODE_REG, 0x300);
2536 break;
2538 case Sliced_cc:
2539 dev_dbg(dev->dev,
2540 "%s: set hanc registers\n", __func__);
2541 status =
2542 cx231xx_mode_register(dev, TS_MODE_REG, 0x1300);
2543 break;
2545 case Raw_Video:
2546 dev_dbg(dev->dev,
2547 "%s: set video registers\n", __func__);
2548 status = cx231xx_mode_register(dev, TS_MODE_REG, 0x100);
2549 break;
2551 case TS1_serial_mode:
2552 dev_dbg(dev->dev,
2553 "%s: set ts1 registers", __func__);
2555 if (dev->board.has_417) {
2556 dev_dbg(dev->dev,
2557 "%s: MPEG\n", __func__);
2558 value &= 0xFFFFFFFC;
2559 value |= 0x3;
2561 status = cx231xx_mode_register(dev,
2562 TS_MODE_REG, value);
2564 val[0] = 0x04;
2565 val[1] = 0xA3;
2566 val[2] = 0x3B;
2567 val[3] = 0x00;
2568 status = cx231xx_write_ctrl_reg(dev,
2569 VRT_SET_REGISTER,
2570 TS1_CFG_REG, val, 4);
2572 val[0] = 0x00;
2573 val[1] = 0x08;
2574 val[2] = 0x00;
2575 val[3] = 0x08;
2576 status = cx231xx_write_ctrl_reg(dev,
2577 VRT_SET_REGISTER,
2578 TS1_LENGTH_REG, val, 4);
2579 } else {
2580 dev_dbg(dev->dev, "%s: BDA\n", __func__);
2581 status = cx231xx_mode_register(dev,
2582 TS_MODE_REG, 0x101);
2583 status = cx231xx_mode_register(dev,
2584 TS1_CFG_REG, 0x010);
2586 break;
2588 case TS1_parallel_mode:
2589 dev_dbg(dev->dev,
2590 "%s: set ts1 parallel mode registers\n",
2591 __func__);
2592 status = cx231xx_mode_register(dev, TS_MODE_REG, 0x100);
2593 status = cx231xx_mode_register(dev, TS1_CFG_REG, 0x400);
2594 break;
2596 } else {
2597 status = cx231xx_mode_register(dev, TS_MODE_REG, 0x101);
2600 return status;
2603 int cx231xx_capture_start(struct cx231xx *dev, int start, u8 media_type)
2605 int rc = -1;
2606 u32 ep_mask = -1;
2607 struct pcb_config *pcb_config;
2609 /* get EP for media type */
2610 pcb_config = (struct pcb_config *)&dev->current_pcb_config;
2612 if (pcb_config->config_num) {
2613 switch (media_type) {
2614 case Raw_Video:
2615 ep_mask = ENABLE_EP4; /* ep4 [00:1000] */
2616 break;
2617 case Audio:
2618 ep_mask = ENABLE_EP3; /* ep3 [00:0100] */
2619 break;
2620 case Vbi:
2621 ep_mask = ENABLE_EP5; /* ep5 [01:0000] */
2622 break;
2623 case Sliced_cc:
2624 ep_mask = ENABLE_EP6; /* ep6 [10:0000] */
2625 break;
2626 case TS1_serial_mode:
2627 case TS1_parallel_mode:
2628 ep_mask = ENABLE_EP1; /* ep1 [00:0001] */
2629 break;
2630 case TS2:
2631 ep_mask = ENABLE_EP2; /* ep2 [00:0010] */
2632 break;
2636 if (start) {
2637 rc = cx231xx_initialize_stream_xfer(dev, media_type);
2639 if (rc < 0)
2640 return rc;
2642 /* enable video capture */
2643 if (ep_mask > 0)
2644 rc = cx231xx_start_stream(dev, ep_mask);
2645 } else {
2646 /* disable video capture */
2647 if (ep_mask > 0)
2648 rc = cx231xx_stop_stream(dev, ep_mask);
2651 return rc;
2653 EXPORT_SYMBOL_GPL(cx231xx_capture_start);
2655 /*****************************************************************************
2656 * G P I O B I T control functions *
2657 ******************************************************************************/
2658 static int cx231xx_set_gpio_bit(struct cx231xx *dev, u32 gpio_bit, u32 gpio_val)
2660 int status = 0;
2662 gpio_val = (__force u32)cpu_to_le32(gpio_val);
2663 status = cx231xx_send_gpio_cmd(dev, gpio_bit, (u8 *)&gpio_val, 4, 0, 0);
2665 return status;
2668 static int cx231xx_get_gpio_bit(struct cx231xx *dev, u32 gpio_bit, u32 *gpio_val)
2670 __le32 tmp;
2671 int status = 0;
2673 status = cx231xx_send_gpio_cmd(dev, gpio_bit, (u8 *)&tmp, 4, 0, 1);
2674 *gpio_val = le32_to_cpu(tmp);
2676 return status;
2680 * cx231xx_set_gpio_direction
2681 * Sets the direction of the GPIO pin to input or output
2683 * Parameters :
2684 * pin_number : The GPIO Pin number to program the direction for
2685 * from 0 to 31
2686 * pin_value : The Direction of the GPIO Pin under reference.
2687 * 0 = Input direction
2688 * 1 = Output direction
2690 int cx231xx_set_gpio_direction(struct cx231xx *dev,
2691 int pin_number, int pin_value)
2693 int status = 0;
2694 u32 value = 0;
2696 /* Check for valid pin_number - if 32 , bail out */
2697 if (pin_number >= 32)
2698 return -EINVAL;
2700 /* input */
2701 if (pin_value == 0)
2702 value = dev->gpio_dir & (~(1 << pin_number)); /* clear */
2703 else
2704 value = dev->gpio_dir | (1 << pin_number);
2706 status = cx231xx_set_gpio_bit(dev, value, dev->gpio_val);
2708 /* cache the value for future */
2709 dev->gpio_dir = value;
2711 return status;
2715 * cx231xx_set_gpio_value
2716 * Sets the value of the GPIO pin to Logic high or low. The Pin under
2717 * reference should ALREADY BE SET IN OUTPUT MODE !!!!!!!!!
2719 * Parameters :
2720 * pin_number : The GPIO Pin number to program the direction for
2721 * pin_value : The value of the GPIO Pin under reference.
2722 * 0 = set it to 0
2723 * 1 = set it to 1
2725 int cx231xx_set_gpio_value(struct cx231xx *dev, int pin_number, int pin_value)
2727 int status = 0;
2728 u32 value = 0;
2730 /* Check for valid pin_number - if 0xFF , bail out */
2731 if (pin_number >= 32)
2732 return -EINVAL;
2734 /* first do a sanity check - if the Pin is not output, make it output */
2735 if ((dev->gpio_dir & (1 << pin_number)) == 0x00) {
2736 /* It was in input mode */
2737 value = dev->gpio_dir | (1 << pin_number);
2738 dev->gpio_dir = value;
2739 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
2740 dev->gpio_val);
2741 value = 0;
2744 if (pin_value == 0)
2745 value = dev->gpio_val & (~(1 << pin_number));
2746 else
2747 value = dev->gpio_val | (1 << pin_number);
2749 /* store the value */
2750 dev->gpio_val = value;
2752 /* toggle bit0 of GP_IO */
2753 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
2755 return status;
2758 /*****************************************************************************
2759 * G P I O I2C related functions *
2760 ******************************************************************************/
2761 int cx231xx_gpio_i2c_start(struct cx231xx *dev)
2763 int status = 0;
2765 /* set SCL to output 1 ; set SDA to output 1 */
2766 dev->gpio_dir |= 1 << dev->board.tuner_scl_gpio;
2767 dev->gpio_dir |= 1 << dev->board.tuner_sda_gpio;
2768 dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
2769 dev->gpio_val |= 1 << dev->board.tuner_sda_gpio;
2771 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
2772 if (status < 0)
2773 return -EINVAL;
2775 /* set SCL to output 1; set SDA to output 0 */
2776 dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
2777 dev->gpio_val &= ~(1 << dev->board.tuner_sda_gpio);
2779 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
2780 if (status < 0)
2781 return -EINVAL;
2783 /* set SCL to output 0; set SDA to output 0 */
2784 dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2785 dev->gpio_val &= ~(1 << dev->board.tuner_sda_gpio);
2787 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
2788 if (status < 0)
2789 return -EINVAL;
2791 return status;
2794 int cx231xx_gpio_i2c_end(struct cx231xx *dev)
2796 int status = 0;
2798 /* set SCL to output 0; set SDA to output 0 */
2799 dev->gpio_dir |= 1 << dev->board.tuner_scl_gpio;
2800 dev->gpio_dir |= 1 << dev->board.tuner_sda_gpio;
2802 dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2803 dev->gpio_val &= ~(1 << dev->board.tuner_sda_gpio);
2805 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
2806 if (status < 0)
2807 return -EINVAL;
2809 /* set SCL to output 1; set SDA to output 0 */
2810 dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
2811 dev->gpio_val &= ~(1 << dev->board.tuner_sda_gpio);
2813 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
2814 if (status < 0)
2815 return -EINVAL;
2817 /* set SCL to input ,release SCL cable control
2818 set SDA to input ,release SDA cable control */
2819 dev->gpio_dir &= ~(1 << dev->board.tuner_scl_gpio);
2820 dev->gpio_dir &= ~(1 << dev->board.tuner_sda_gpio);
2822 status =
2823 cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
2824 if (status < 0)
2825 return -EINVAL;
2827 return status;
2830 int cx231xx_gpio_i2c_write_byte(struct cx231xx *dev, u8 data)
2832 int status = 0;
2833 u8 i;
2835 /* set SCL to output ; set SDA to output */
2836 dev->gpio_dir |= 1 << dev->board.tuner_scl_gpio;
2837 dev->gpio_dir |= 1 << dev->board.tuner_sda_gpio;
2839 for (i = 0; i < 8; i++) {
2840 if (((data << i) & 0x80) == 0) {
2841 /* set SCL to output 0; set SDA to output 0 */
2842 dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2843 dev->gpio_val &= ~(1 << dev->board.tuner_sda_gpio);
2844 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
2845 dev->gpio_val);
2847 /* set SCL to output 1; set SDA to output 0 */
2848 dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
2849 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
2850 dev->gpio_val);
2852 /* set SCL to output 0; set SDA to output 0 */
2853 dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2854 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
2855 dev->gpio_val);
2856 } else {
2857 /* set SCL to output 0; set SDA to output 1 */
2858 dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2859 dev->gpio_val |= 1 << dev->board.tuner_sda_gpio;
2860 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
2861 dev->gpio_val);
2863 /* set SCL to output 1; set SDA to output 1 */
2864 dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
2865 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
2866 dev->gpio_val);
2868 /* set SCL to output 0; set SDA to output 1 */
2869 dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2870 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
2871 dev->gpio_val);
2874 return status;
2877 int cx231xx_gpio_i2c_read_byte(struct cx231xx *dev, u8 *buf)
2879 u8 value = 0;
2880 int status = 0;
2881 u32 gpio_logic_value = 0;
2882 u8 i;
2884 /* read byte */
2885 for (i = 0; i < 8; i++) { /* send write I2c addr */
2887 /* set SCL to output 0; set SDA to input */
2888 dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2889 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
2890 dev->gpio_val);
2892 /* set SCL to output 1; set SDA to input */
2893 dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
2894 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir,
2895 dev->gpio_val);
2897 /* get SDA data bit */
2898 gpio_logic_value = dev->gpio_val;
2899 status = cx231xx_get_gpio_bit(dev, dev->gpio_dir,
2900 &dev->gpio_val);
2901 if ((dev->gpio_val & (1 << dev->board.tuner_sda_gpio)) != 0)
2902 value |= (1 << (8 - i - 1));
2904 dev->gpio_val = gpio_logic_value;
2907 /* set SCL to output 0,finish the read latest SCL signal.
2908 !!!set SDA to input, never to modify SDA direction at
2909 the same times */
2910 dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2911 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
2913 /* store the value */
2914 *buf = value & 0xff;
2916 return status;
2919 int cx231xx_gpio_i2c_read_ack(struct cx231xx *dev)
2921 int status = 0;
2922 u32 gpio_logic_value = 0;
2923 int nCnt = 10;
2924 int nInit = nCnt;
2926 /* clock stretch; set SCL to input; set SDA to input;
2927 get SCL value till SCL = 1 */
2928 dev->gpio_dir &= ~(1 << dev->board.tuner_sda_gpio);
2929 dev->gpio_dir &= ~(1 << dev->board.tuner_scl_gpio);
2931 gpio_logic_value = dev->gpio_val;
2932 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
2934 do {
2935 msleep(2);
2936 status = cx231xx_get_gpio_bit(dev, dev->gpio_dir,
2937 &dev->gpio_val);
2938 nCnt--;
2939 } while (((dev->gpio_val &
2940 (1 << dev->board.tuner_scl_gpio)) == 0) &&
2941 (nCnt > 0));
2943 if (nCnt == 0)
2944 dev_dbg(dev->dev,
2945 "No ACK after %d msec -GPIO I2C failed!",
2946 nInit * 10);
2949 * readAck
2950 * through clock stretch, slave has given a SCL signal,
2951 * so the SDA data can be directly read.
2953 status = cx231xx_get_gpio_bit(dev, dev->gpio_dir, &dev->gpio_val);
2955 if ((dev->gpio_val & 1 << dev->board.tuner_sda_gpio) == 0) {
2956 dev->gpio_val = gpio_logic_value;
2957 dev->gpio_val &= ~(1 << dev->board.tuner_sda_gpio);
2958 status = 0;
2959 } else {
2960 dev->gpio_val = gpio_logic_value;
2961 dev->gpio_val |= (1 << dev->board.tuner_sda_gpio);
2964 /* read SDA end, set the SCL to output 0, after this operation,
2965 SDA direction can be changed. */
2966 dev->gpio_val = gpio_logic_value;
2967 dev->gpio_dir |= (1 << dev->board.tuner_scl_gpio);
2968 dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2969 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
2971 return status;
2974 int cx231xx_gpio_i2c_write_ack(struct cx231xx *dev)
2976 int status = 0;
2978 /* set SDA to output */
2979 dev->gpio_dir |= 1 << dev->board.tuner_sda_gpio;
2980 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
2982 /* set SCL = 0 (output); set SDA = 0 (output) */
2983 dev->gpio_val &= ~(1 << dev->board.tuner_sda_gpio);
2984 dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2985 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
2987 /* set SCL = 1 (output); set SDA = 0 (output) */
2988 dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
2989 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
2991 /* set SCL = 0 (output); set SDA = 0 (output) */
2992 dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
2993 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
2995 /* set SDA to input,and then the slave will read data from SDA. */
2996 dev->gpio_dir &= ~(1 << dev->board.tuner_sda_gpio);
2997 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
2999 return status;
3002 int cx231xx_gpio_i2c_write_nak(struct cx231xx *dev)
3004 int status = 0;
3006 /* set scl to output ; set sda to input */
3007 dev->gpio_dir |= 1 << dev->board.tuner_scl_gpio;
3008 dev->gpio_dir &= ~(1 << dev->board.tuner_sda_gpio);
3009 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
3011 /* set scl to output 0; set sda to input */
3012 dev->gpio_val &= ~(1 << dev->board.tuner_scl_gpio);
3013 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
3015 /* set scl to output 1; set sda to input */
3016 dev->gpio_val |= 1 << dev->board.tuner_scl_gpio;
3017 status = cx231xx_set_gpio_bit(dev, dev->gpio_dir, dev->gpio_val);
3019 return status;
3022 /*****************************************************************************
3023 * G P I O I2C related functions *
3024 ******************************************************************************/
3025 /* cx231xx_gpio_i2c_read
3026 * Function to read data from gpio based I2C interface
3028 int cx231xx_gpio_i2c_read(struct cx231xx *dev, u8 dev_addr, u8 *buf, u8 len)
3030 int status = 0;
3031 int i = 0;
3033 /* get the lock */
3034 mutex_lock(&dev->gpio_i2c_lock);
3036 /* start */
3037 status = cx231xx_gpio_i2c_start(dev);
3039 /* write dev_addr */
3040 status = cx231xx_gpio_i2c_write_byte(dev, (dev_addr << 1) + 1);
3042 /* readAck */
3043 status = cx231xx_gpio_i2c_read_ack(dev);
3045 /* read data */
3046 for (i = 0; i < len; i++) {
3047 /* read data */
3048 buf[i] = 0;
3049 status = cx231xx_gpio_i2c_read_byte(dev, &buf[i]);
3051 if ((i + 1) != len) {
3052 /* only do write ack if we more length */
3053 status = cx231xx_gpio_i2c_write_ack(dev);
3057 /* write NAK - inform reads are complete */
3058 status = cx231xx_gpio_i2c_write_nak(dev);
3060 /* write end */
3061 status = cx231xx_gpio_i2c_end(dev);
3063 /* release the lock */
3064 mutex_unlock(&dev->gpio_i2c_lock);
3066 return status;
3069 /* cx231xx_gpio_i2c_write
3070 * Function to write data to gpio based I2C interface
3072 int cx231xx_gpio_i2c_write(struct cx231xx *dev, u8 dev_addr, u8 *buf, u8 len)
3074 int i = 0;
3076 /* get the lock */
3077 mutex_lock(&dev->gpio_i2c_lock);
3079 /* start */
3080 cx231xx_gpio_i2c_start(dev);
3082 /* write dev_addr */
3083 cx231xx_gpio_i2c_write_byte(dev, dev_addr << 1);
3085 /* read Ack */
3086 cx231xx_gpio_i2c_read_ack(dev);
3088 for (i = 0; i < len; i++) {
3089 /* Write data */
3090 cx231xx_gpio_i2c_write_byte(dev, buf[i]);
3092 /* read Ack */
3093 cx231xx_gpio_i2c_read_ack(dev);
3096 /* write End */
3097 cx231xx_gpio_i2c_end(dev);
3099 /* release the lock */
3100 mutex_unlock(&dev->gpio_i2c_lock);
3102 return 0;