blob: 1ae0e8a4068c9c0c764f89a6c5b344811ffc3bb3 [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 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300102 value =
103 cx25821_i2c_read(&dev->i2c_bus[0], MODE_CTRL + (0x200 * i),
104 &tmp);
105 value &= 0xFFFFFFF0;
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300106 /* enable the fast locking mode bit[16] */
107 value |= 0x10001;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300108 ret_val =
109 cx25821_i2c_write(&dev->i2c_bus[0], MODE_CTRL + (0x200 * i),
110 value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300111
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300112 /* resolution NTSC 720x480 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300113 value =
114 cx25821_i2c_read(&dev->i2c_bus[0],
115 HORIZ_TIM_CTRL + (0x200 * i), &tmp);
116 value &= 0x00C00C00;
117 value |= 0x612D0074;
118 ret_val =
119 cx25821_i2c_write(&dev->i2c_bus[0],
120 HORIZ_TIM_CTRL + (0x200 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300121
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300122 value =
123 cx25821_i2c_read(&dev->i2c_bus[0],
124 VERT_TIM_CTRL + (0x200 * i), &tmp);
125 value &= 0x00C00C00;
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300126 value |= 0x1C1E001A; /* vblank_cnt + 2 to get camera ID */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300127 ret_val =
128 cx25821_i2c_write(&dev->i2c_bus[0],
129 VERT_TIM_CTRL + (0x200 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300130
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300131 /* chroma subcarrier step size */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300132 ret_val =
133 cx25821_i2c_write(&dev->i2c_bus[0],
134 SC_STEP_SIZE + (0x200 * i), 0x43E00000);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300135
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300136 /* enable VIP optional active */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300137 value =
138 cx25821_i2c_read(&dev->i2c_bus[0],
139 OUT_CTRL_NS + (0x200 * i), &tmp);
140 value &= 0xFFFBFFFF;
141 value |= 0x00040000;
142 ret_val =
143 cx25821_i2c_write(&dev->i2c_bus[0],
144 OUT_CTRL_NS + (0x200 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300145
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300146 /* enable VIP optional active (VIP_OPT_AL) for direct output. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300147 value =
148 cx25821_i2c_read(&dev->i2c_bus[0], OUT_CTRL1 + (0x200 * i),
149 &tmp);
150 value &= 0xFFFBFFFF;
151 value |= 0x00040000;
152 ret_val =
153 cx25821_i2c_write(&dev->i2c_bus[0], OUT_CTRL1 + (0x200 * i),
154 value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300155
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300156 /*
157 * clear VPRES_VERT_EN bit, fixes the chroma run away problem
158 * when the input switching rate < 16 fields
159 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300160 value =
161 cx25821_i2c_read(&dev->i2c_bus[0],
162 MISC_TIM_CTRL + (0x200 * i), &tmp);
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300163 /* disable special play detection */
164 value = setBitAtPos(value, 14);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300165 value = clearBitAtPos(value, 15);
166 ret_val =
167 cx25821_i2c_write(&dev->i2c_bus[0],
168 MISC_TIM_CTRL + (0x200 * i), value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300169
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300170 /* set vbi_gate_en to 0 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300171 value =
172 cx25821_i2c_read(&dev->i2c_bus[0], DFE_CTRL1 + (0x200 * i),
173 &tmp);
174 value = clearBitAtPos(value, 29);
175 ret_val =
176 cx25821_i2c_write(&dev->i2c_bus[0], DFE_CTRL1 + (0x200 * i),
177 value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300178
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300179 /* Enable the generation of blue field output if no video */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300180 medusa_enable_bluefield_output(dev, i, 1);
181 }
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300182
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300183 for (i = 0; i < MAX_ENCODERS; i++) {
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300184 /* NTSC hclock */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300185 value =
186 cx25821_i2c_read(&dev->i2c_bus[0],
187 DENC_A_REG_1 + (0x100 * i), &tmp);
188 value &= 0xF000FC00;
189 value |= 0x06B402D0;
190 ret_val =
191 cx25821_i2c_write(&dev->i2c_bus[0],
192 DENC_A_REG_1 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300193
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300194 /* burst begin and burst end */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300195 value =
196 cx25821_i2c_read(&dev->i2c_bus[0],
197 DENC_A_REG_2 + (0x100 * i), &tmp);
198 value &= 0xFF000000;
199 value |= 0x007E9054;
200 ret_val =
201 cx25821_i2c_write(&dev->i2c_bus[0],
202 DENC_A_REG_2 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300203
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300204 value =
205 cx25821_i2c_read(&dev->i2c_bus[0],
206 DENC_A_REG_3 + (0x100 * i), &tmp);
207 value &= 0xFC00FE00;
208 value |= 0x00EC00F0;
209 ret_val =
210 cx25821_i2c_write(&dev->i2c_bus[0],
211 DENC_A_REG_3 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300212
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300213 /* set NTSC vblank, no phase alternation, 7.5 IRE pedestal */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300214 value =
215 cx25821_i2c_read(&dev->i2c_bus[0],
216 DENC_A_REG_4 + (0x100 * i), &tmp);
217 value &= 0x00FCFFFF;
218 value |= 0x13020000;
219 ret_val =
220 cx25821_i2c_write(&dev->i2c_bus[0],
221 DENC_A_REG_4 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300222
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300223 value =
224 cx25821_i2c_read(&dev->i2c_bus[0],
225 DENC_A_REG_5 + (0x100 * i), &tmp);
226 value &= 0xFFFF0000;
227 value |= 0x0000E575;
228 ret_val =
229 cx25821_i2c_write(&dev->i2c_bus[0],
230 DENC_A_REG_5 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300231
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300232 ret_val =
233 cx25821_i2c_write(&dev->i2c_bus[0],
234 DENC_A_REG_6 + (0x100 * i), 0x009A89C1);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300235
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300236 /* Subcarrier Increment */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300237 ret_val =
238 cx25821_i2c_write(&dev->i2c_bus[0],
239 DENC_A_REG_7 + (0x100 * i), 0x21F07C1F);
240 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300241
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300242 /* set picture resolutions */
243 /* 0 - 720 */
244 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], HSCALE_CTRL, 0x0);
245 /* 0 - 480 */
246 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], VSCALE_CTRL, 0x0);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300247
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300248 /* set Bypass input format to NTSC 525 lines */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300249 value = cx25821_i2c_read(&dev->i2c_bus[0], BYP_AB_CTRL, &tmp);
250 value |= 0x00080200;
251 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], BYP_AB_CTRL, value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300252
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300253 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300254
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300255 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300256}
257
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300258static int medusa_PALCombInit(struct cx25821_dev *dev, int dec)
259{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300260 int ret_val = -1;
261 u32 value = 0, tmp = 0;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300262
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300263 /* Setup for 2D threshold */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300264 ret_val =
265 cx25821_i2c_write(&dev->i2c_bus[0], COMB_2D_HFS_CFG + (0x200 * dec),
266 0x20002861);
267 ret_val =
268 cx25821_i2c_write(&dev->i2c_bus[0], COMB_2D_HFD_CFG + (0x200 * dec),
269 0x20002861);
270 ret_val =
271 cx25821_i2c_write(&dev->i2c_bus[0], COMB_2D_LF_CFG + (0x200 * dec),
272 0x200A1023);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300273
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300274 /* Setup flat chroma and luma thresholds */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300275 value =
276 cx25821_i2c_read(&dev->i2c_bus[0],
277 COMB_FLAT_THRESH_CTRL + (0x200 * dec), &tmp);
278 value &= 0x06230000;
279 ret_val =
280 cx25821_i2c_write(&dev->i2c_bus[0],
281 COMB_FLAT_THRESH_CTRL + (0x200 * dec), value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300282
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300283 /* set comb 2D blend */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300284 ret_val =
285 cx25821_i2c_write(&dev->i2c_bus[0], COMB_2D_BLEND + (0x200 * dec),
286 0x210F0F0F);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300287
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300288 /* COMB MISC CONTROL */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300289 ret_val =
290 cx25821_i2c_write(&dev->i2c_bus[0], COMB_MISC_CTRL + (0x200 * dec),
291 0x41120A7F);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300292
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300293 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300294}
295
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300296static int medusa_initialize_pal(struct cx25821_dev *dev)
297{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300298 int ret_val = 0;
299 int i = 0;
300 u32 value = 0;
301 u32 tmp = 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300302
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300303 mutex_lock(&dev->lock);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300304
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300305 for (i = 0; i < MAX_DECODERS; i++) {
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300306 /* set video format PAL-BDGHI */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300307 value =
308 cx25821_i2c_read(&dev->i2c_bus[0], MODE_CTRL + (0x200 * i),
309 &tmp);
310 value &= 0xFFFFFFF0;
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300311 /* enable the fast locking mode bit[16] */
312 value |= 0x10004;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300313 ret_val =
314 cx25821_i2c_write(&dev->i2c_bus[0], MODE_CTRL + (0x200 * i),
315 value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300316
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300317 /* resolution PAL 720x576 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300318 value =
319 cx25821_i2c_read(&dev->i2c_bus[0],
320 HORIZ_TIM_CTRL + (0x200 * i), &tmp);
321 value &= 0x00C00C00;
322 value |= 0x632D007D;
323 ret_val =
324 cx25821_i2c_write(&dev->i2c_bus[0],
325 HORIZ_TIM_CTRL + (0x200 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300326
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300327 /* vblank656_cnt=x26, vactive_cnt=240h, vblank_cnt=x24 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300328 value =
329 cx25821_i2c_read(&dev->i2c_bus[0],
330 VERT_TIM_CTRL + (0x200 * i), &tmp);
331 value &= 0x00C00C00;
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300332 value |= 0x28240026; /* vblank_cnt + 2 to get camera ID */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300333 ret_val =
334 cx25821_i2c_write(&dev->i2c_bus[0],
335 VERT_TIM_CTRL + (0x200 * i), value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300336
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300337 /* chroma subcarrier step size */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300338 ret_val =
339 cx25821_i2c_write(&dev->i2c_bus[0],
340 SC_STEP_SIZE + (0x200 * i), 0x5411E2D0);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300341
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300342 /* enable VIP optional active */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300343 value =
344 cx25821_i2c_read(&dev->i2c_bus[0],
345 OUT_CTRL_NS + (0x200 * i), &tmp);
346 value &= 0xFFFBFFFF;
347 value |= 0x00040000;
348 ret_val =
349 cx25821_i2c_write(&dev->i2c_bus[0],
350 OUT_CTRL_NS + (0x200 * i), value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300351
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300352 /* enable VIP optional active (VIP_OPT_AL) for direct output. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300353 value =
354 cx25821_i2c_read(&dev->i2c_bus[0], OUT_CTRL1 + (0x200 * i),
355 &tmp);
356 value &= 0xFFFBFFFF;
357 value |= 0x00040000;
358 ret_val =
359 cx25821_i2c_write(&dev->i2c_bus[0], OUT_CTRL1 + (0x200 * i),
360 value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300361
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300362 /*
363 * clear VPRES_VERT_EN bit, fixes the chroma run away problem
364 * when the input switching rate < 16 fields
365 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300366 value =
367 cx25821_i2c_read(&dev->i2c_bus[0],
368 MISC_TIM_CTRL + (0x200 * i), &tmp);
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300369 /* disable special play detection */
370 value = setBitAtPos(value, 14);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300371 value = clearBitAtPos(value, 15);
372 ret_val =
373 cx25821_i2c_write(&dev->i2c_bus[0],
374 MISC_TIM_CTRL + (0x200 * i), value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300375
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300376 /* set vbi_gate_en to 0 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300377 value =
378 cx25821_i2c_read(&dev->i2c_bus[0], DFE_CTRL1 + (0x200 * i),
379 &tmp);
380 value = clearBitAtPos(value, 29);
381 ret_val =
382 cx25821_i2c_write(&dev->i2c_bus[0], DFE_CTRL1 + (0x200 * i),
383 value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300384
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300385 medusa_PALCombInit(dev, i);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300386
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300387 /* Enable the generation of blue field output if no video */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300388 medusa_enable_bluefield_output(dev, i, 1);
389 }
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300390
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300391 for (i = 0; i < MAX_ENCODERS; i++) {
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300392 /* PAL hclock */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300393 value =
394 cx25821_i2c_read(&dev->i2c_bus[0],
395 DENC_A_REG_1 + (0x100 * i), &tmp);
396 value &= 0xF000FC00;
397 value |= 0x06C002D0;
398 ret_val =
399 cx25821_i2c_write(&dev->i2c_bus[0],
400 DENC_A_REG_1 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300401
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300402 /* burst begin and burst end */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300403 value =
404 cx25821_i2c_read(&dev->i2c_bus[0],
405 DENC_A_REG_2 + (0x100 * i), &tmp);
406 value &= 0xFF000000;
407 value |= 0x007E9754;
408 ret_val =
409 cx25821_i2c_write(&dev->i2c_bus[0],
410 DENC_A_REG_2 + (0x100 * i), value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300411
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300412 /* hblank and vactive */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300413 value =
414 cx25821_i2c_read(&dev->i2c_bus[0],
415 DENC_A_REG_3 + (0x100 * i), &tmp);
416 value &= 0xFC00FE00;
417 value |= 0x00FC0120;
418 ret_val =
419 cx25821_i2c_write(&dev->i2c_bus[0],
420 DENC_A_REG_3 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300421
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300422 /* set PAL vblank, phase alternation, 0 IRE pedestal */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300423 value =
424 cx25821_i2c_read(&dev->i2c_bus[0],
425 DENC_A_REG_4 + (0x100 * i), &tmp);
426 value &= 0x00FCFFFF;
427 value |= 0x14010000;
428 ret_val =
429 cx25821_i2c_write(&dev->i2c_bus[0],
430 DENC_A_REG_4 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300431
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300432 value =
433 cx25821_i2c_read(&dev->i2c_bus[0],
434 DENC_A_REG_5 + (0x100 * i), &tmp);
435 value &= 0xFFFF0000;
436 value |= 0x0000F078;
437 ret_val =
438 cx25821_i2c_write(&dev->i2c_bus[0],
439 DENC_A_REG_5 + (0x100 * i), value);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300440
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300441 ret_val =
442 cx25821_i2c_write(&dev->i2c_bus[0],
443 DENC_A_REG_6 + (0x100 * i), 0x00A493CF);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300444
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300445 /* Subcarrier Increment */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300446 ret_val =
447 cx25821_i2c_write(&dev->i2c_bus[0],
448 DENC_A_REG_7 + (0x100 * i), 0x2A098ACB);
449 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300450
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300451 /* set picture resolutions */
452 /* 0 - 720 */
453 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], HSCALE_CTRL, 0x0);
454 /* 0 - 576 */
455 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], VSCALE_CTRL, 0x0);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300456
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300457 /* set Bypass input format to PAL 625 lines */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300458 value = cx25821_i2c_read(&dev->i2c_bus[0], BYP_AB_CTRL, &tmp);
459 value &= 0xFFF7FDFF;
460 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], BYP_AB_CTRL, value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300461
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300462 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300463
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300464 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300465}
466
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300467int medusa_set_videostandard(struct cx25821_dev *dev)
468{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300469 int status = STATUS_SUCCESS;
470 u32 value = 0, tmp = 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300471
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300472 if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300473 status = medusa_initialize_pal(dev);
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300474 else
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300475 status = medusa_initialize_ntsc(dev);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300476
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300477 /* Enable DENC_A output */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300478 value = cx25821_i2c_read(&dev->i2c_bus[0], DENC_A_REG_4, &tmp);
479 value = setBitAtPos(value, 4);
480 status = cx25821_i2c_write(&dev->i2c_bus[0], DENC_A_REG_4, value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300481
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300482 /* Enable DENC_B output */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300483 value = cx25821_i2c_read(&dev->i2c_bus[0], DENC_B_REG_4, &tmp);
484 value = setBitAtPos(value, 4);
485 status = cx25821_i2c_write(&dev->i2c_bus[0], DENC_B_REG_4, value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300486
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300487 return status;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300488}
489
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300490void medusa_set_resolution(struct cx25821_dev *dev, int width,
491 int decoder_select)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300492{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300493 int decoder = 0;
494 int decoder_count = 0;
495 int ret_val = 0;
496 u32 hscale = 0x0;
497 u32 vscale = 0x0;
498 const int MAX_WIDTH = 720;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300499
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300500 mutex_lock(&dev->lock);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300501
Leonid V. Fedorenchik09c35442011-10-22 01:43:38 -0300502 /* validate the width */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300503 if (width > MAX_WIDTH) {
Joe Perches36d89f72010-11-07 17:48:21 -0300504 pr_info("%s(): width %d > MAX_WIDTH %d ! resetting to MAX_WIDTH\n",
505 __func__, width, MAX_WIDTH);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300506 width = MAX_WIDTH;
507 }
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300508
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300509 if (decoder_select <= 7 && decoder_select >= 0) {
510 decoder = decoder_select;
511 decoder_count = decoder_select + 1;
512 } else {
513 decoder = 0;
514 decoder_count = _num_decoders;
515 }
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300516
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300517 switch (width) {
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300518 case 320:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300519 hscale = 0x13E34B;
520 vscale = 0x0;
521 break;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300522
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300523 case 352:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300524 hscale = 0x10A273;
525 vscale = 0x0;
526 break;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300527
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300528 case 176:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300529 hscale = 0x3115B2;
530 vscale = 0x1E00;
531 break;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300532
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300533 case 160:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300534 hscale = 0x378D84;
535 vscale = 0x1E00;
536 break;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300537
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300538 default: /* 720 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300539 hscale = 0x0;
540 vscale = 0x0;
541 break;
542 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300543
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300544 for (; decoder < decoder_count; decoder++) {
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300545 /* write scaling values for each decoder */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300546 ret_val =
547 cx25821_i2c_write(&dev->i2c_bus[0],
548 HSCALE_CTRL + (0x200 * decoder), hscale);
549 ret_val =
550 cx25821_i2c_write(&dev->i2c_bus[0],
551 VSCALE_CTRL + (0x200 * decoder), vscale);
552 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300553
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300554 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300555}
556
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300557static void medusa_set_decoderduration(struct cx25821_dev *dev, int decoder,
558 int duration)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300559{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300560 int ret_val = 0;
561 u32 fld_cnt = 0;
562 u32 tmp = 0;
563 u32 disp_cnt_reg = DISP_AB_CNT;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300564
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300565 mutex_lock(&dev->lock);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300566
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300567 /* no support */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300568 if (decoder < VDEC_A && decoder > VDEC_H) {
569 mutex_unlock(&dev->lock);
570 return;
571 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300572
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300573 switch (decoder) {
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300574 default:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300575 break;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300576 case VDEC_C:
577 case VDEC_D:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300578 disp_cnt_reg = DISP_CD_CNT;
579 break;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300580 case VDEC_E:
581 case VDEC_F:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300582 disp_cnt_reg = DISP_EF_CNT;
583 break;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300584 case VDEC_G:
585 case VDEC_H:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300586 disp_cnt_reg = DISP_GH_CNT;
587 break;
588 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300589
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300590 _display_field_cnt[decoder] = duration;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300591
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300592 /* update hardware */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300593 fld_cnt = cx25821_i2c_read(&dev->i2c_bus[0], disp_cnt_reg, &tmp);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300594
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300595 if (!(decoder % 2)) { /* EVEN decoder */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300596 fld_cnt &= 0xFFFF0000;
597 fld_cnt |= duration;
598 } else {
599 fld_cnt &= 0x0000FFFF;
600 fld_cnt |= ((u32) duration) << 16;
601 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300602
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300603 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], disp_cnt_reg, fld_cnt);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300604
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300605 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300606}
607
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300608/* Map to Medusa register setting */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300609static int mapM(int srcMin,
610 int srcMax, int srcVal, int dstMin, int dstMax, int *dstVal)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300611{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300612 int numerator;
613 int denominator;
614 int quotient;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300615
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300616 if ((srcMin == srcMax) || (srcVal < srcMin) || (srcVal > srcMax))
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300617 return -1;
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300618 /*
619 * This is the overall expression used:
620 * *dstVal =
621 * (srcVal - srcMin)*(dstMax - dstMin) / (srcMax - srcMin) + dstMin;
622 * but we need to account for rounding so below we use the modulus
623 * operator to find the remainder and increment if necessary.
624 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300625 numerator = (srcVal - srcMin) * (dstMax - dstMin);
626 denominator = srcMax - srcMin;
627 quotient = numerator / denominator;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300628
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300629 if (2 * (numerator % denominator) >= denominator)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300630 quotient++;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300631
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300632 *dstVal = quotient + dstMin;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300633
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300634 return 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300635}
636
637static unsigned long convert_to_twos(long numeric, unsigned long bits_len)
638{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300639 unsigned char temp;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300640
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300641 if (numeric >= 0)
642 return numeric;
643 else {
644 temp = ~(abs(numeric) & 0xFF);
645 temp += 1;
646 return temp;
647 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300648}
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300649
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300650int medusa_set_brightness(struct cx25821_dev *dev, int brightness, int decoder)
651{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300652 int ret_val = 0;
653 int value = 0;
654 u32 val = 0, tmp = 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300655
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300656 mutex_lock(&dev->lock);
Leonid V. Fedorenchik49faae82011-10-22 01:43:39 -0300657 if ((brightness > VIDEO_PROCAMP_MAX) ||
658 (brightness < VIDEO_PROCAMP_MIN)) {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300659 mutex_unlock(&dev->lock);
660 return -1;
661 }
662 ret_val =
663 mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, brightness,
664 SIGNED_BYTE_MIN, SIGNED_BYTE_MAX, &value);
665 value = convert_to_twos(value, 8);
666 val =
667 cx25821_i2c_read(&dev->i2c_bus[0],
668 VDEC_A_BRITE_CTRL + (0x200 * decoder), &tmp);
669 val &= 0xFFFFFF00;
670 ret_val |=
671 cx25821_i2c_write(&dev->i2c_bus[0],
672 VDEC_A_BRITE_CTRL + (0x200 * decoder),
673 val | value);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300674 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300675 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300676}
677
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300678int medusa_set_contrast(struct cx25821_dev *dev, int contrast, int decoder)
679{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300680 int ret_val = 0;
681 int value = 0;
682 u32 val = 0, tmp = 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300683
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300684 mutex_lock(&dev->lock);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300685
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300686 if ((contrast > VIDEO_PROCAMP_MAX) || (contrast < VIDEO_PROCAMP_MIN)) {
687 mutex_unlock(&dev->lock);
688 return -1;
689 }
690
691 ret_val =
692 mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, contrast,
693 UNSIGNED_BYTE_MIN, UNSIGNED_BYTE_MAX, &value);
694 val =
695 cx25821_i2c_read(&dev->i2c_bus[0],
696 VDEC_A_CNTRST_CTRL + (0x200 * decoder), &tmp);
697 val &= 0xFFFFFF00;
698 ret_val |=
699 cx25821_i2c_write(&dev->i2c_bus[0],
700 VDEC_A_CNTRST_CTRL + (0x200 * decoder),
701 val | value);
702
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300703 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300704 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300705}
706
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300707int medusa_set_hue(struct cx25821_dev *dev, int hue, int decoder)
708{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300709 int ret_val = 0;
710 int value = 0;
711 u32 val = 0, tmp = 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300712
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300713 mutex_lock(&dev->lock);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300714
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300715 if ((hue > VIDEO_PROCAMP_MAX) || (hue < VIDEO_PROCAMP_MIN)) {
716 mutex_unlock(&dev->lock);
717 return -1;
718 }
719
720 ret_val =
721 mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, hue, SIGNED_BYTE_MIN,
722 SIGNED_BYTE_MAX, &value);
723
724 value = convert_to_twos(value, 8);
725 val =
726 cx25821_i2c_read(&dev->i2c_bus[0],
727 VDEC_A_HUE_CTRL + (0x200 * decoder), &tmp);
728 val &= 0xFFFFFF00;
729
730 ret_val |=
731 cx25821_i2c_write(&dev->i2c_bus[0],
732 VDEC_A_HUE_CTRL + (0x200 * decoder), val | value);
733
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300734 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300735 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300736}
737
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300738int medusa_set_saturation(struct cx25821_dev *dev, int saturation, int decoder)
739{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300740 int ret_val = 0;
741 int value = 0;
742 u32 val = 0, tmp = 0;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300743
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300744 mutex_lock(&dev->lock);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300745
Leonid V. Fedorenchik49faae82011-10-22 01:43:39 -0300746 if ((saturation > VIDEO_PROCAMP_MAX) ||
747 (saturation < VIDEO_PROCAMP_MIN)) {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300748 mutex_unlock(&dev->lock);
749 return -1;
750 }
751
752 ret_val =
753 mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, saturation,
754 UNSIGNED_BYTE_MIN, UNSIGNED_BYTE_MAX, &value);
755
756 val =
757 cx25821_i2c_read(&dev->i2c_bus[0],
758 VDEC_A_USAT_CTRL + (0x200 * decoder), &tmp);
759 val &= 0xFFFFFF00;
760 ret_val |=
761 cx25821_i2c_write(&dev->i2c_bus[0],
762 VDEC_A_USAT_CTRL + (0x200 * decoder),
763 val | value);
764
765 val =
766 cx25821_i2c_read(&dev->i2c_bus[0],
767 VDEC_A_VSAT_CTRL + (0x200 * decoder), &tmp);
768 val &= 0xFFFFFF00;
769 ret_val |=
770 cx25821_i2c_write(&dev->i2c_bus[0],
771 VDEC_A_VSAT_CTRL + (0x200 * decoder),
772 val | value);
773
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300774 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300775 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300776}
777
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300778/* Program the display sequence and monitor output. */
779
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300780int medusa_video_init(struct cx25821_dev *dev)
781{
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300782 u32 value = 0, tmp = 0;
783 int ret_val = 0;
784 int i = 0;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300785
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300786 mutex_lock(&dev->lock);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300787
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300788 _num_decoders = dev->_max_num_decoders;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300789
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300790 /* disable Auto source selection on all video decoders */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300791 value = cx25821_i2c_read(&dev->i2c_bus[0], MON_A_CTRL, &tmp);
792 value &= 0xFFFFF0FF;
793 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], MON_A_CTRL, value);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300794
Mauro Carvalho Chehab64561a32010-05-05 12:30:01 -0300795 if (ret_val < 0)
796 goto error;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300797
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300798 /* Turn off Master source switch enable */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300799 value = cx25821_i2c_read(&dev->i2c_bus[0], MON_A_CTRL, &tmp);
800 value &= 0xFFFFFFDF;
801 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], MON_A_CTRL, value);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300802
Dan Carpenter7a02f542009-12-28 18:59:46 +0200803 if (ret_val < 0)
Mauro Carvalho Chehab64561a32010-05-05 12:30:01 -0300804 goto error;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300805
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300806 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300807
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300808 for (i = 0; i < _num_decoders; i++)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300809 medusa_set_decoderduration(dev, i, _display_field_cnt[i]);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300810
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300811 mutex_lock(&dev->lock);
812
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300813 /* Select monitor as DENC A input, power up the DAC */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300814 value = cx25821_i2c_read(&dev->i2c_bus[0], DENC_AB_CTRL, &tmp);
815 value &= 0xFF70FF70;
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300816 value |= 0x00090008; /* set en_active */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300817 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], DENC_AB_CTRL, value);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300818
Mauro Carvalho Chehab64561a32010-05-05 12:30:01 -0300819 if (ret_val < 0)
820 goto error;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300821
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300822 /* enable input is VIP/656 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300823 value = cx25821_i2c_read(&dev->i2c_bus[0], BYP_AB_CTRL, &tmp);
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300824 value |= 0x00040100; /* enable VIP */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300825 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], BYP_AB_CTRL, value);
826
Mauro Carvalho Chehab64561a32010-05-05 12:30:01 -0300827 if (ret_val < 0)
828 goto error;
829
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300830 /* select AFE clock to output mode */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300831 value = cx25821_i2c_read(&dev->i2c_bus[0], AFE_AB_DIAG_CTRL, &tmp);
832 value &= 0x83FFFFFF;
Ruslan Pisareve4115bb2010-09-27 10:01:36 -0300833 ret_val =
Mauro Carvalho Chehab3e9442c2010-07-04 15:37:05 -0300834 cx25821_i2c_write(&dev->i2c_bus[0], AFE_AB_DIAG_CTRL,
835 value | 0x10000000);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300836
Mauro Carvalho Chehab64561a32010-05-05 12:30:01 -0300837 if (ret_val < 0)
838 goto error;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300839
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300840 /* Turn on all of the data out and control output pins. */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300841 value = cx25821_i2c_read(&dev->i2c_bus[0], PIN_OE_CTRL, &tmp);
842 value &= 0xFEF0FE00;
843 if (_num_decoders == MAX_DECODERS) {
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300844 /*
845 * Note: The octal board does not support control pins(bit16-19)
846 * These bits are ignored in the octal board.
847 *
848 * disable VDEC A-C port, default to Mobilygen Interface
849 */
850 value |= 0x010001F8;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300851 } else {
Olimpiu Pascariu0dd8be82010-03-21 15:46:26 -0300852 /* disable VDEC A-C port, default to Mobilygen Interface */
853 value |= 0x010F0108;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300854 }
855
856 value |= 7;
857 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], PIN_OE_CTRL, value);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300858
Mauro Carvalho Chehab64561a32010-05-05 12:30:01 -0300859 if (ret_val < 0)
860 goto error;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300861
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300862
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300863 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300864
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300865 ret_val = medusa_set_videostandard(dev);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300866
Mauro Carvalho Chehab64561a32010-05-05 12:30:01 -0300867 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300868
Mauro Carvalho Chehab64561a32010-05-05 12:30:01 -0300869error:
870 mutex_unlock(&dev->lock);
871 return ret_val;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300872}