blob: 298a68d98c2f3ac4bab0846715f440410f85d185 [file] [log] [blame]
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -03001/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -03004 * Copyright (C) 2009 Conexant Systems Inc.
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -03005 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
Joe Perches36d89f72010-11-07 17:48:21 -030023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030025#include "cx25821.h"
26#include "cx25821-medusa-video.h"
27#include "cx25821-biffuncs.h"
28
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -030029/*
30 * medusa_enable_bluefield_output()
31 *
32 * Enable the generation of blue filed output if no video
33 *
34 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030035static void medusa_enable_bluefield_output(struct cx25821_dev *dev, int channel,
36 int enable)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030037{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030038 int ret_val = 1;
39 u32 value = 0;
40 u32 tmp = 0;
41 int out_ctrl = OUT_CTRL1;
42 int out_ctrl_ns = OUT_CTRL_NS;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030043
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030044 switch (channel) {
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -030045 default:
46 case VDEC_A:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030047 break;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -030048 case VDEC_B:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030049 out_ctrl = VDEC_B_OUT_CTRL1;
50 out_ctrl_ns = VDEC_B_OUT_CTRL_NS;
51 break;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -030052 case VDEC_C:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030053 out_ctrl = VDEC_C_OUT_CTRL1;
54 out_ctrl_ns = VDEC_C_OUT_CTRL_NS;
55 break;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -030056 case VDEC_D:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030057 out_ctrl = VDEC_D_OUT_CTRL1;
58 out_ctrl_ns = VDEC_D_OUT_CTRL_NS;
59 break;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -030060 case VDEC_E:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030061 out_ctrl = VDEC_E_OUT_CTRL1;
62 out_ctrl_ns = VDEC_E_OUT_CTRL_NS;
63 return;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -030064 case VDEC_F:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030065 out_ctrl = VDEC_F_OUT_CTRL1;
66 out_ctrl_ns = VDEC_F_OUT_CTRL_NS;
67 return;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -030068 case VDEC_G:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030069 out_ctrl = VDEC_G_OUT_CTRL1;
70 out_ctrl_ns = VDEC_G_OUT_CTRL_NS;
71 return;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -030072 case VDEC_H:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030073 out_ctrl = VDEC_H_OUT_CTRL1;
74 out_ctrl_ns = VDEC_H_OUT_CTRL_NS;
75 return;
76 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030077
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030078 value = cx25821_i2c_read(&dev->i2c_bus[0], out_ctrl, &tmp);
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -030079 value &= 0xFFFFFF7F; /* clear BLUE_FIELD_EN */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030080 if (enable)
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -030081 value |= 0x00000080; /* set BLUE_FIELD_EN */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030082 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], out_ctrl, value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030083
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030084 value = cx25821_i2c_read(&dev->i2c_bus[0], out_ctrl_ns, &tmp);
85 value &= 0xFFFFFF7F;
86 if (enable)
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -030087 value |= 0x00000080; /* set BLUE_FIELD_EN */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030088 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], out_ctrl_ns, value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030089}
90
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030091static int medusa_initialize_ntsc(struct cx25821_dev *dev)
92{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030093 int ret_val = 0;
94 int i = 0;
95 u32 value = 0;
96 u32 tmp = 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030097
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030098 mutex_lock(&dev->lock);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030099
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300100 for (i = 0; i < MAX_DECODERS; i++) {
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300101 /* set video format NTSC-M */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300102 value = cx25821_i2c_read(&dev->i2c_bus[0],
103 MODE_CTRL + (0x200 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300104 value &= 0xFFFFFFF0;
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300105 /* enable the fast locking mode bit[16] */
106 value |= 0x10001;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300107 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
108 MODE_CTRL + (0x200 * i), value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300109
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300110 /* resolution NTSC 720x480 */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300111 value = cx25821_i2c_read(&dev->i2c_bus[0],
112 HORIZ_TIM_CTRL + (0x200 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300113 value &= 0x00C00C00;
114 value |= 0x612D0074;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300115 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
116 HORIZ_TIM_CTRL + (0x200 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300117
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300118 value = cx25821_i2c_read(&dev->i2c_bus[0],
119 VERT_TIM_CTRL + (0x200 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300120 value &= 0x00C00C00;
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300121 value |= 0x1C1E001A; /* vblank_cnt + 2 to get camera ID */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300122 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
123 VERT_TIM_CTRL + (0x200 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300124
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300125 /* chroma subcarrier step size */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300126 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
127 SC_STEP_SIZE + (0x200 * i), 0x43E00000);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300128
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300129 /* enable VIP optional active */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300130 value = cx25821_i2c_read(&dev->i2c_bus[0],
131 OUT_CTRL_NS + (0x200 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300132 value &= 0xFFFBFFFF;
133 value |= 0x00040000;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300134 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
135 OUT_CTRL_NS + (0x200 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300136
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300137 /* enable VIP optional active (VIP_OPT_AL) for direct output. */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300138 value = cx25821_i2c_read(&dev->i2c_bus[0],
139 OUT_CTRL1 + (0x200 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300140 value &= 0xFFFBFFFF;
141 value |= 0x00040000;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300142 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
143 OUT_CTRL1 + (0x200 * i), value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300144
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300145 /*
146 * clear VPRES_VERT_EN bit, fixes the chroma run away problem
147 * when the input switching rate < 16 fields
148 */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300149 value = cx25821_i2c_read(&dev->i2c_bus[0],
150 MISC_TIM_CTRL + (0x200 * i), &tmp);
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300151 /* disable special play detection */
152 value = setBitAtPos(value, 14);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300153 value = clearBitAtPos(value, 15);
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300154 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
155 MISC_TIM_CTRL + (0x200 * i), value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300156
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300157 /* set vbi_gate_en to 0 */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300158 value = cx25821_i2c_read(&dev->i2c_bus[0],
159 DFE_CTRL1 + (0x200 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300160 value = clearBitAtPos(value, 29);
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300161 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
162 DFE_CTRL1 + (0x200 * i), value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300163
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300164 /* Enable the generation of blue field output if no video */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300165 medusa_enable_bluefield_output(dev, i, 1);
166 }
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300167
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300168 for (i = 0; i < MAX_ENCODERS; i++) {
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300169 /* NTSC hclock */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300170 value = cx25821_i2c_read(&dev->i2c_bus[0],
171 DENC_A_REG_1 + (0x100 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300172 value &= 0xF000FC00;
173 value |= 0x06B402D0;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300174 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
175 DENC_A_REG_1 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300176
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300177 /* burst begin and burst end */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300178 value = cx25821_i2c_read(&dev->i2c_bus[0],
179 DENC_A_REG_2 + (0x100 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300180 value &= 0xFF000000;
181 value |= 0x007E9054;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300182 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
183 DENC_A_REG_2 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300184
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300185 value = cx25821_i2c_read(&dev->i2c_bus[0],
186 DENC_A_REG_3 + (0x100 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300187 value &= 0xFC00FE00;
188 value |= 0x00EC00F0;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300189 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
190 DENC_A_REG_3 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300191
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300192 /* set NTSC vblank, no phase alternation, 7.5 IRE pedestal */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300193 value = cx25821_i2c_read(&dev->i2c_bus[0],
194 DENC_A_REG_4 + (0x100 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300195 value &= 0x00FCFFFF;
196 value |= 0x13020000;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300197 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
198 DENC_A_REG_4 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300199
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300200 value = cx25821_i2c_read(&dev->i2c_bus[0],
201 DENC_A_REG_5 + (0x100 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300202 value &= 0xFFFF0000;
203 value |= 0x0000E575;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300204 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
205 DENC_A_REG_5 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300206
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300207 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
208 DENC_A_REG_6 + (0x100 * i), 0x009A89C1);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300209
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300210 /* Subcarrier Increment */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300211 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
212 DENC_A_REG_7 + (0x100 * i), 0x21F07C1F);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300213 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300214
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300215 /* set picture resolutions */
216 /* 0 - 720 */
217 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], HSCALE_CTRL, 0x0);
218 /* 0 - 480 */
219 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], VSCALE_CTRL, 0x0);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300220
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300221 /* set Bypass input format to NTSC 525 lines */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300222 value = cx25821_i2c_read(&dev->i2c_bus[0], BYP_AB_CTRL, &tmp);
223 value |= 0x00080200;
224 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], BYP_AB_CTRL, value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300225
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300226 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300227
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300228 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300229}
230
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300231static int medusa_PALCombInit(struct cx25821_dev *dev, int dec)
232{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300233 int ret_val = -1;
234 u32 value = 0, tmp = 0;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300235
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300236 /* Setup for 2D threshold */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300237 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
238 COMB_2D_HFS_CFG + (0x200 * dec), 0x20002861);
239 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
240 COMB_2D_HFD_CFG + (0x200 * dec), 0x20002861);
241 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
242 COMB_2D_LF_CFG + (0x200 * dec), 0x200A1023);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300243
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300244 /* Setup flat chroma and luma thresholds */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300245 value = cx25821_i2c_read(&dev->i2c_bus[0],
246 COMB_FLAT_THRESH_CTRL + (0x200 * dec), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300247 value &= 0x06230000;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300248 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
249 COMB_FLAT_THRESH_CTRL + (0x200 * dec), value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300250
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300251 /* set comb 2D blend */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300252 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
253 COMB_2D_BLEND + (0x200 * dec), 0x210F0F0F);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300254
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300255 /* COMB MISC CONTROL */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300256 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
257 COMB_MISC_CTRL + (0x200 * dec), 0x41120A7F);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300258
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300259 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300260}
261
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300262static int medusa_initialize_pal(struct cx25821_dev *dev)
263{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300264 int ret_val = 0;
265 int i = 0;
266 u32 value = 0;
267 u32 tmp = 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300268
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300269 mutex_lock(&dev->lock);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300270
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300271 for (i = 0; i < MAX_DECODERS; i++) {
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300272 /* set video format PAL-BDGHI */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300273 value = cx25821_i2c_read(&dev->i2c_bus[0],
274 MODE_CTRL + (0x200 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300275 value &= 0xFFFFFFF0;
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300276 /* enable the fast locking mode bit[16] */
277 value |= 0x10004;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300278 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
279 MODE_CTRL + (0x200 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300280
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300281 /* resolution PAL 720x576 */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300282 value = cx25821_i2c_read(&dev->i2c_bus[0],
283 HORIZ_TIM_CTRL + (0x200 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300284 value &= 0x00C00C00;
285 value |= 0x632D007D;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300286 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
287 HORIZ_TIM_CTRL + (0x200 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300288
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300289 /* vblank656_cnt=x26, vactive_cnt=240h, vblank_cnt=x24 */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300290 value = cx25821_i2c_read(&dev->i2c_bus[0],
291 VERT_TIM_CTRL + (0x200 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300292 value &= 0x00C00C00;
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300293 value |= 0x28240026; /* vblank_cnt + 2 to get camera ID */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300294 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
295 VERT_TIM_CTRL + (0x200 * i), value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300296
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300297 /* chroma subcarrier step size */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300298 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
299 SC_STEP_SIZE + (0x200 * i), 0x5411E2D0);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300300
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300301 /* enable VIP optional active */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300302 value = cx25821_i2c_read(&dev->i2c_bus[0],
303 OUT_CTRL_NS + (0x200 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300304 value &= 0xFFFBFFFF;
305 value |= 0x00040000;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300306 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
307 OUT_CTRL_NS + (0x200 * i), value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300308
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300309 /* enable VIP optional active (VIP_OPT_AL) for direct output. */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300310 value = cx25821_i2c_read(&dev->i2c_bus[0],
311 OUT_CTRL1 + (0x200 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300312 value &= 0xFFFBFFFF;
313 value |= 0x00040000;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300314 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
315 OUT_CTRL1 + (0x200 * i), value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300316
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300317 /*
318 * clear VPRES_VERT_EN bit, fixes the chroma run away problem
319 * when the input switching rate < 16 fields
320 */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300321 value = cx25821_i2c_read(&dev->i2c_bus[0],
322 MISC_TIM_CTRL + (0x200 * i), &tmp);
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300323 /* disable special play detection */
324 value = setBitAtPos(value, 14);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300325 value = clearBitAtPos(value, 15);
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300326 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
327 MISC_TIM_CTRL + (0x200 * i), value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300328
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300329 /* set vbi_gate_en to 0 */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300330 value = cx25821_i2c_read(&dev->i2c_bus[0],
331 DFE_CTRL1 + (0x200 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300332 value = clearBitAtPos(value, 29);
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300333 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
334 DFE_CTRL1 + (0x200 * i), value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300335
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300336 medusa_PALCombInit(dev, i);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300337
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300338 /* Enable the generation of blue field output if no video */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300339 medusa_enable_bluefield_output(dev, i, 1);
340 }
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300341
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300342 for (i = 0; i < MAX_ENCODERS; i++) {
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300343 /* PAL hclock */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300344 value = cx25821_i2c_read(&dev->i2c_bus[0],
345 DENC_A_REG_1 + (0x100 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300346 value &= 0xF000FC00;
347 value |= 0x06C002D0;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300348 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
349 DENC_A_REG_1 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300350
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300351 /* burst begin and burst end */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300352 value = cx25821_i2c_read(&dev->i2c_bus[0],
353 DENC_A_REG_2 + (0x100 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300354 value &= 0xFF000000;
355 value |= 0x007E9754;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300356 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
357 DENC_A_REG_2 + (0x100 * i), value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300358
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300359 /* hblank and vactive */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300360 value = cx25821_i2c_read(&dev->i2c_bus[0],
361 DENC_A_REG_3 + (0x100 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300362 value &= 0xFC00FE00;
363 value |= 0x00FC0120;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300364 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
365 DENC_A_REG_3 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300366
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300367 /* set PAL vblank, phase alternation, 0 IRE pedestal */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300368 value = cx25821_i2c_read(&dev->i2c_bus[0],
369 DENC_A_REG_4 + (0x100 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300370 value &= 0x00FCFFFF;
371 value |= 0x14010000;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300372 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
373 DENC_A_REG_4 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300374
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300375 value = cx25821_i2c_read(&dev->i2c_bus[0],
376 DENC_A_REG_5 + (0x100 * i), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300377 value &= 0xFFFF0000;
378 value |= 0x0000F078;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300379 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
380 DENC_A_REG_5 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300381
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300382 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
383 DENC_A_REG_6 + (0x100 * i), 0x00A493CF);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300384
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300385 /* Subcarrier Increment */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300386 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
387 DENC_A_REG_7 + (0x100 * i), 0x2A098ACB);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300388 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300389
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300390 /* set picture resolutions */
391 /* 0 - 720 */
392 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], HSCALE_CTRL, 0x0);
393 /* 0 - 576 */
394 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], VSCALE_CTRL, 0x0);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300395
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300396 /* set Bypass input format to PAL 625 lines */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300397 value = cx25821_i2c_read(&dev->i2c_bus[0], BYP_AB_CTRL, &tmp);
398 value &= 0xFFF7FDFF;
399 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], BYP_AB_CTRL, value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300400
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300401 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300402
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300403 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300404}
405
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300406int medusa_set_videostandard(struct cx25821_dev *dev)
407{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300408 int status = STATUS_SUCCESS;
409 u32 value = 0, tmp = 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300410
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300411 if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300412 status = medusa_initialize_pal(dev);
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300413 else
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300414 status = medusa_initialize_ntsc(dev);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300415
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300416 /* Enable DENC_A output */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300417 value = cx25821_i2c_read(&dev->i2c_bus[0], DENC_A_REG_4, &tmp);
418 value = setBitAtPos(value, 4);
419 status = cx25821_i2c_write(&dev->i2c_bus[0], DENC_A_REG_4, value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300420
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300421 /* Enable DENC_B output */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300422 value = cx25821_i2c_read(&dev->i2c_bus[0], DENC_B_REG_4, &tmp);
423 value = setBitAtPos(value, 4);
424 status = cx25821_i2c_write(&dev->i2c_bus[0], DENC_B_REG_4, value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300425
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300426 return status;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300427}
428
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300429void medusa_set_resolution(struct cx25821_dev *dev, int width,
430 int decoder_select)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300431{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300432 int decoder = 0;
433 int decoder_count = 0;
434 int ret_val = 0;
435 u32 hscale = 0x0;
436 u32 vscale = 0x0;
437 const int MAX_WIDTH = 720;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300438
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300439 mutex_lock(&dev->lock);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300440
Leonid V. Fedorenchik09c35442011-10-22 01:43:38 -0300441 /* validate the width */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300442 if (width > MAX_WIDTH) {
Joe Perches36d89f72010-11-07 17:48:21 -0300443 pr_info("%s(): width %d > MAX_WIDTH %d ! resetting to MAX_WIDTH\n",
444 __func__, width, MAX_WIDTH);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300445 width = MAX_WIDTH;
446 }
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300447
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300448 if (decoder_select <= 7 && decoder_select >= 0) {
449 decoder = decoder_select;
450 decoder_count = decoder_select + 1;
451 } else {
452 decoder = 0;
453 decoder_count = _num_decoders;
454 }
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300455
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300456 switch (width) {
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300457 case 320:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300458 hscale = 0x13E34B;
459 vscale = 0x0;
460 break;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300461
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300462 case 352:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300463 hscale = 0x10A273;
464 vscale = 0x0;
465 break;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300466
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300467 case 176:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300468 hscale = 0x3115B2;
469 vscale = 0x1E00;
470 break;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300471
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300472 case 160:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300473 hscale = 0x378D84;
474 vscale = 0x1E00;
475 break;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300476
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300477 default: /* 720 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300478 hscale = 0x0;
479 vscale = 0x0;
480 break;
481 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300482
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300483 for (; decoder < decoder_count; decoder++) {
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300484 /* write scaling values for each decoder */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300485 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
486 HSCALE_CTRL + (0x200 * decoder), hscale);
487 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
488 VSCALE_CTRL + (0x200 * decoder), vscale);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300489 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300490
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300491 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300492}
493
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300494static void medusa_set_decoderduration(struct cx25821_dev *dev, int decoder,
495 int duration)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300496{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300497 int ret_val = 0;
498 u32 fld_cnt = 0;
499 u32 tmp = 0;
500 u32 disp_cnt_reg = DISP_AB_CNT;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300501
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300502 mutex_lock(&dev->lock);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300503
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300504 /* no support */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300505 if (decoder < VDEC_A && decoder > VDEC_H) {
506 mutex_unlock(&dev->lock);
507 return;
508 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300509
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300510 switch (decoder) {
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300511 default:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300512 break;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300513 case VDEC_C:
514 case VDEC_D:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300515 disp_cnt_reg = DISP_CD_CNT;
516 break;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300517 case VDEC_E:
518 case VDEC_F:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300519 disp_cnt_reg = DISP_EF_CNT;
520 break;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300521 case VDEC_G:
522 case VDEC_H:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300523 disp_cnt_reg = DISP_GH_CNT;
524 break;
525 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300526
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300527 _display_field_cnt[decoder] = duration;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300528
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300529 /* update hardware */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300530 fld_cnt = cx25821_i2c_read(&dev->i2c_bus[0], disp_cnt_reg, &tmp);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300531
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300532 if (!(decoder % 2)) { /* EVEN decoder */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300533 fld_cnt &= 0xFFFF0000;
534 fld_cnt |= duration;
535 } else {
536 fld_cnt &= 0x0000FFFF;
537 fld_cnt |= ((u32) duration) << 16;
538 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300539
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300540 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], disp_cnt_reg, fld_cnt);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300541
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300542 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300543}
544
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300545/* Map to Medusa register setting */
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300546static int mapM(int srcMin, int srcMax, int srcVal, int dstMin, int dstMax,
547 int *dstVal)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300548{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300549 int numerator;
550 int denominator;
551 int quotient;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300552
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300553 if ((srcMin == srcMax) || (srcVal < srcMin) || (srcVal > srcMax))
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300554 return -1;
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300555 /*
556 * This is the overall expression used:
557 * *dstVal =
558 * (srcVal - srcMin)*(dstMax - dstMin) / (srcMax - srcMin) + dstMin;
559 * but we need to account for rounding so below we use the modulus
560 * operator to find the remainder and increment if necessary.
561 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300562 numerator = (srcVal - srcMin) * (dstMax - dstMin);
563 denominator = srcMax - srcMin;
564 quotient = numerator / denominator;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300565
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300566 if (2 * (numerator % denominator) >= denominator)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300567 quotient++;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300568
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300569 *dstVal = quotient + dstMin;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300570
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300571 return 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300572}
573
574static unsigned long convert_to_twos(long numeric, unsigned long bits_len)
575{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300576 unsigned char temp;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300577
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300578 if (numeric >= 0)
579 return numeric;
580 else {
581 temp = ~(abs(numeric) & 0xFF);
582 temp += 1;
583 return temp;
584 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300585}
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300586
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300587int medusa_set_brightness(struct cx25821_dev *dev, int brightness, int decoder)
588{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300589 int ret_val = 0;
590 int value = 0;
591 u32 val = 0, tmp = 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300592
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300593 mutex_lock(&dev->lock);
Leonid V. Fedorenchik49faae82011-10-22 01:43:39 -0300594 if ((brightness > VIDEO_PROCAMP_MAX) ||
595 (brightness < VIDEO_PROCAMP_MIN)) {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300596 mutex_unlock(&dev->lock);
597 return -1;
598 }
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300599 ret_val = mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, brightness,
600 SIGNED_BYTE_MIN, SIGNED_BYTE_MAX, &value);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300601 value = convert_to_twos(value, 8);
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300602 val = cx25821_i2c_read(&dev->i2c_bus[0],
603 VDEC_A_BRITE_CTRL + (0x200 * decoder), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300604 val &= 0xFFFFFF00;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300605 ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
606 VDEC_A_BRITE_CTRL + (0x200 * decoder), val | value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300607 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300608 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300609}
610
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300611int medusa_set_contrast(struct cx25821_dev *dev, int contrast, int decoder)
612{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300613 int ret_val = 0;
614 int value = 0;
615 u32 val = 0, tmp = 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300616
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300617 mutex_lock(&dev->lock);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300618
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300619 if ((contrast > VIDEO_PROCAMP_MAX) || (contrast < VIDEO_PROCAMP_MIN)) {
620 mutex_unlock(&dev->lock);
621 return -1;
622 }
623
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300624 ret_val = mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, contrast,
625 UNSIGNED_BYTE_MIN, UNSIGNED_BYTE_MAX, &value);
626 val = cx25821_i2c_read(&dev->i2c_bus[0],
627 VDEC_A_CNTRST_CTRL + (0x200 * decoder), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300628 val &= 0xFFFFFF00;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300629 ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
630 VDEC_A_CNTRST_CTRL + (0x200 * decoder), val | value);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300631
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300632 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300633 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300634}
635
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300636int medusa_set_hue(struct cx25821_dev *dev, int hue, int decoder)
637{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300638 int ret_val = 0;
639 int value = 0;
640 u32 val = 0, tmp = 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300641
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300642 mutex_lock(&dev->lock);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300643
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300644 if ((hue > VIDEO_PROCAMP_MAX) || (hue < VIDEO_PROCAMP_MIN)) {
645 mutex_unlock(&dev->lock);
646 return -1;
647 }
648
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300649 ret_val = mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, hue,
650 SIGNED_BYTE_MIN, SIGNED_BYTE_MAX, &value);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300651
652 value = convert_to_twos(value, 8);
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300653 val = cx25821_i2c_read(&dev->i2c_bus[0],
654 VDEC_A_HUE_CTRL + (0x200 * decoder), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300655 val &= 0xFFFFFF00;
656
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300657 ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
658 VDEC_A_HUE_CTRL + (0x200 * decoder), val | value);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300659
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300660 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300661 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300662}
663
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300664int medusa_set_saturation(struct cx25821_dev *dev, int saturation, int decoder)
665{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300666 int ret_val = 0;
667 int value = 0;
668 u32 val = 0, tmp = 0;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300669
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300670 mutex_lock(&dev->lock);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300671
Leonid V. Fedorenchik49faae82011-10-22 01:43:39 -0300672 if ((saturation > VIDEO_PROCAMP_MAX) ||
673 (saturation < VIDEO_PROCAMP_MIN)) {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300674 mutex_unlock(&dev->lock);
675 return -1;
676 }
677
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300678 ret_val = mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, saturation,
679 UNSIGNED_BYTE_MIN, UNSIGNED_BYTE_MAX, &value);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300680
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300681 val = cx25821_i2c_read(&dev->i2c_bus[0],
682 VDEC_A_USAT_CTRL + (0x200 * decoder), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300683 val &= 0xFFFFFF00;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300684 ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
685 VDEC_A_USAT_CTRL + (0x200 * decoder), val | value);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300686
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300687 val = cx25821_i2c_read(&dev->i2c_bus[0],
688 VDEC_A_VSAT_CTRL + (0x200 * decoder), &tmp);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300689 val &= 0xFFFFFF00;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300690 ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
691 VDEC_A_VSAT_CTRL + (0x200 * decoder), val | value);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300692
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300693 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300694 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300695}
696
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300697/* Program the display sequence and monitor output. */
698
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300699int medusa_video_init(struct cx25821_dev *dev)
700{
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300701 u32 value = 0, tmp = 0;
702 int ret_val = 0;
703 int i = 0;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300704
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300705 mutex_lock(&dev->lock);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300706
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300707 _num_decoders = dev->_max_num_decoders;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300708
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300709 /* disable Auto source selection on all video decoders */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300710 value = cx25821_i2c_read(&dev->i2c_bus[0], MON_A_CTRL, &tmp);
711 value &= 0xFFFFF0FF;
712 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], MON_A_CTRL, value);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300713
Mauro Carvalho Chehab64561a32010-05-05 12:30:01 -0300714 if (ret_val < 0)
715 goto error;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300716
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300717 /* Turn off Master source switch enable */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300718 value = cx25821_i2c_read(&dev->i2c_bus[0], MON_A_CTRL, &tmp);
719 value &= 0xFFFFFFDF;
720 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], MON_A_CTRL, value);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300721
Dan Carpenter7a02f542009-12-28 18:59:46 +0200722 if (ret_val < 0)
Mauro Carvalho Chehab64561a32010-05-05 12:30:01 -0300723 goto error;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300724
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300725 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300726
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300727 for (i = 0; i < _num_decoders; i++)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300728 medusa_set_decoderduration(dev, i, _display_field_cnt[i]);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300729
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300730 mutex_lock(&dev->lock);
731
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300732 /* Select monitor as DENC A input, power up the DAC */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300733 value = cx25821_i2c_read(&dev->i2c_bus[0], DENC_AB_CTRL, &tmp);
734 value &= 0xFF70FF70;
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300735 value |= 0x00090008; /* set en_active */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300736 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], DENC_AB_CTRL, value);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300737
Mauro Carvalho Chehab64561a32010-05-05 12:30:01 -0300738 if (ret_val < 0)
739 goto error;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300740
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300741 /* enable input is VIP/656 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300742 value = cx25821_i2c_read(&dev->i2c_bus[0], BYP_AB_CTRL, &tmp);
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300743 value |= 0x00040100; /* enable VIP */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300744 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], BYP_AB_CTRL, value);
745
Mauro Carvalho Chehab64561a32010-05-05 12:30:01 -0300746 if (ret_val < 0)
747 goto error;
748
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300749 /* select AFE clock to output mode */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300750 value = cx25821_i2c_read(&dev->i2c_bus[0], AFE_AB_DIAG_CTRL, &tmp);
751 value &= 0x83FFFFFF;
Leonid V. Fedorenchik14098bc2011-10-22 01:43:40 -0300752 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], AFE_AB_DIAG_CTRL,
753 value | 0x10000000);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300754
Mauro Carvalho Chehab64561a32010-05-05 12:30:01 -0300755 if (ret_val < 0)
756 goto error;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300757
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300758 /* Turn on all of the data out and control output pins. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300759 value = cx25821_i2c_read(&dev->i2c_bus[0], PIN_OE_CTRL, &tmp);
760 value &= 0xFEF0FE00;
761 if (_num_decoders == MAX_DECODERS) {
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300762 /*
763 * Note: The octal board does not support control pins(bit16-19)
764 * These bits are ignored in the octal board.
765 *
766 * disable VDEC A-C port, default to Mobilygen Interface
767 */
768 value |= 0x010001F8;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300769 } else {
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300770 /* disable VDEC A-C port, default to Mobilygen Interface */
771 value |= 0x010F0108;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300772 }
773
774 value |= 7;
775 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], PIN_OE_CTRL, value);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300776
Mauro Carvalho Chehab64561a32010-05-05 12:30:01 -0300777 if (ret_val < 0)
778 goto error;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300779
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300780
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300781 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300782
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300783 ret_val = medusa_set_videostandard(dev);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300784
Mauro Carvalho Chehab64561a32010-05-05 12:30:01 -0300785 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300786
Mauro Carvalho Chehab64561a32010-05-05 12:30:01 -0300787error:
788 mutex_unlock(&dev->lock);
789 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300790}