blob: 40d84d73edf809c2d60fedbc31eb36339dc997f6 [file] [log] [blame]
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001/*
2 * drivers/media/video/tvp514x.c
3 *
4 * TI TVP5146/47 decoder driver
5 *
6 * Copyright (C) 2008 Texas Instruments Inc
7 * Author: Vaibhav Hiremath <hvaibhav@ti.com>
8 *
9 * Contributors:
10 * Sivaraj R <sivaraj@ti.com>
11 * Brijesh R Jadav <brijesh.j@ti.com>
12 * Hardik Shah <hardik.shah@ti.com>
13 * Manjunath Hadli <mrh@ti.com>
14 * Karicheri Muralidharan <m-karicheri2@ti.com>
15 *
16 * This package is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
29 */
30
31#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030033#include <linux/delay.h>
34#include <linux/videodev2.h>
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030035
36#include <media/v4l2-device.h>
37#include <media/v4l2-common.h>
38#include <media/v4l2-chip-ident.h>
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030039#include <media/tvp514x.h>
40
41#include "tvp514x_regs.h"
42
43/* Module Name */
44#define TVP514X_MODULE_NAME "tvp514x"
45
46/* Private macros for TVP */
47#define I2C_RETRY_COUNT (5)
48#define LOCK_RETRY_COUNT (5)
49#define LOCK_RETRY_DELAY (200)
50
51/* Debug functions */
52static int debug;
53module_param(debug, bool, 0644);
54MODULE_PARM_DESC(debug, "Debug level (0-1)");
55
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030056MODULE_AUTHOR("Texas Instruments");
57MODULE_DESCRIPTION("TVP514X linux decoder driver");
58MODULE_LICENSE("GPL");
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030059
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030060/* enum tvp514x_std - enum for supported standards */
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030061enum tvp514x_std {
62 STD_NTSC_MJ = 0,
63 STD_PAL_BDGHIN,
64 STD_INVALID
65};
66
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030067/**
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030068 * struct tvp514x_std_info - Structure to store standard informations
69 * @width: Line width in pixels
70 * @height:Number of active lines
71 * @video_std: Value to write in REG_VIDEO_STD register
72 * @standard: v4l2 standard structure information
73 */
74struct tvp514x_std_info {
75 unsigned long width;
76 unsigned long height;
77 u8 video_std;
78 struct v4l2_standard standard;
79};
80
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030081static struct tvp514x_reg tvp514x_reg_list_default[0x40];
Vaibhav Hiremath63b59ce2010-03-27 09:37:54 -030082
83static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable);
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030084/**
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030085 * struct tvp514x_decoder - TVP5146/47 decoder object
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030086 * @sd: Subdevice Slave handle
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030087 * @tvp514x_regs: copy of hw's regs with preset values.
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030088 * @pdata: Board specific
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030089 * @ver: Chip version
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030090 * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030091 * @pix: Current pixel format
92 * @num_fmts: Number of formats
93 * @fmt_list: Format list
94 * @current_std: Current standard
95 * @num_stds: Number of standards
96 * @std_list: Standards list
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030097 * @input: Input routing at chip level
98 * @output: Output routing at chip level
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030099 */
100struct tvp514x_decoder {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300101 struct v4l2_subdev sd;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300102 struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300103 const struct tvp514x_platform_data *pdata;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300104
105 int ver;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300106 int streaming;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300107
108 struct v4l2_pix_format pix;
109 int num_fmts;
110 const struct v4l2_fmtdesc *fmt_list;
111
112 enum tvp514x_std current_std;
113 int num_stds;
114 struct tvp514x_std_info *std_list;
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300115 /* Input and Output Routing parameters */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300116 u32 input;
117 u32 output;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300118};
119
120/* TVP514x default register values */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300121static struct tvp514x_reg tvp514x_reg_list_default[] = {
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300122 /* Composite selected */
123 {TOK_WRITE, REG_INPUT_SEL, 0x05},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300124 {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300125 /* Auto mode */
126 {TOK_WRITE, REG_VIDEO_STD, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300127 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
128 {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
129 {TOK_WRITE, REG_COLOR_KILLER, 0x10},
130 {TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
131 {TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
132 {TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
133 {TOK_WRITE, REG_BRIGHTNESS, 0x80},
134 {TOK_WRITE, REG_CONTRAST, 0x80},
135 {TOK_WRITE, REG_SATURATION, 0x80},
136 {TOK_WRITE, REG_HUE, 0x00},
137 {TOK_WRITE, REG_CHROMA_CONTROL1, 0x00},
138 {TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300139 /* Reserved */
140 {TOK_SKIP, 0x0F, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300141 {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
142 {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
143 {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300144 /* Reserved */
145 {TOK_SKIP, 0x13, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300146 {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300147 /* Reserved */
148 {TOK_SKIP, 0x15, 0x00},
149 /* NTSC timing */
150 {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300151 {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
152 {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
153 {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300154 /* NTSC timing */
155 {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300156 {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
157 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
158 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300159 /* NTSC timing */
160 {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300161 {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
162 {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
163 {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300164 /* NTSC timing */
165 {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300166 {TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
167 {TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
168 {TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300169 /* Reserved */
170 {TOK_SKIP, 0x26, 0x00},
171 /* Reserved */
172 {TOK_SKIP, 0x27, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300173 {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300174 /* Reserved */
175 {TOK_SKIP, 0x29, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300176 {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300177 /* Reserved */
178 {TOK_SKIP, 0x2B, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300179 {TOK_SKIP, REG_SCART_DELAY, 0x00},
180 {TOK_SKIP, REG_CTI_DELAY, 0x00},
181 {TOK_SKIP, REG_CTI_CONTROL, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300182 /* Reserved */
183 {TOK_SKIP, 0x2F, 0x00},
184 /* Reserved */
185 {TOK_SKIP, 0x30, 0x00},
186 /* Reserved */
187 {TOK_SKIP, 0x31, 0x00},
188 /* HS, VS active high */
189 {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
190 /* 10-bit BT.656 */
191 {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00},
192 /* Enable clk & data */
193 {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11},
194 /* Enable AVID & FLD */
195 {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE},
196 /* Enable VS & HS */
197 {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300198 {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
199 {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300200 /* Clear status */
201 {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300202 {TOK_TERM, 0, 0},
203};
204
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300205/**
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300206 * List of image formats supported by TVP5146/47 decoder
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300207 * Currently we are using 8 bit mode only, but can be
208 * extended to 10/20 bit mode.
209 */
210static const struct v4l2_fmtdesc tvp514x_fmt_list[] = {
211 {
212 .index = 0,
213 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
214 .flags = 0,
215 .description = "8-bit UYVY 4:2:2 Format",
216 .pixelformat = V4L2_PIX_FMT_UYVY,
217 },
218};
219
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300220/**
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300221 * Supported standards -
222 *
223 * Currently supports two standards only, need to add support for rest of the
224 * modes, like SECAM, etc...
225 */
226static struct tvp514x_std_info tvp514x_std_list[] = {
227 /* Standard: STD_NTSC_MJ */
228 [STD_NTSC_MJ] = {
229 .width = NTSC_NUM_ACTIVE_PIXELS,
230 .height = NTSC_NUM_ACTIVE_LINES,
231 .video_std = VIDEO_STD_NTSC_MJ_BIT,
232 .standard = {
233 .index = 0,
234 .id = V4L2_STD_NTSC,
235 .name = "NTSC",
236 .frameperiod = {1001, 30000},
237 .framelines = 525
238 },
239 /* Standard: STD_PAL_BDGHIN */
240 },
241 [STD_PAL_BDGHIN] = {
242 .width = PAL_NUM_ACTIVE_PIXELS,
243 .height = PAL_NUM_ACTIVE_LINES,
244 .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
245 .standard = {
246 .index = 1,
247 .id = V4L2_STD_PAL,
248 .name = "PAL",
249 .frameperiod = {1, 25},
250 .framelines = 625
251 },
252 },
253 /* Standard: need to add for additional standard */
254};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300255
256
257static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
258{
259 return container_of(sd, struct tvp514x_decoder, sd);
260}
261
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300262
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300263/**
264 * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
265 * @sd: ptr to v4l2_subdev struct
266 * @reg: TVP5146/47 register address
267 *
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300268 * Returns value read if successful, or non-zero (-1) otherwise.
269 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300270static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300271{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300272 int err, retry = 0;
273 struct i2c_client *client = v4l2_get_subdevdata(sd);
274
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300275read_again:
276
277 err = i2c_smbus_read_byte_data(client, reg);
Sebastian Andrzej Siewior6f901a92009-11-04 15:35:09 -0300278 if (err < 0) {
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300279 if (retry <= I2C_RETRY_COUNT) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300280 v4l2_warn(sd, "Read: retry ... %d\n", retry);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300281 retry++;
282 msleep_interruptible(10);
283 goto read_again;
284 }
285 }
286
287 return err;
288}
289
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300290/**
291 * dump_reg() - dump the register content of TVP5146/47.
292 * @sd: ptr to v4l2_subdev struct
293 * @reg: TVP5146/47 register address
294 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300295static void dump_reg(struct v4l2_subdev *sd, u8 reg)
296{
297 u32 val;
298
299 val = tvp514x_read_reg(sd, reg);
300 v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
301}
302
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300303/**
304 * tvp514x_write_reg() - Write a value to a register in TVP5146/47
305 * @sd: ptr to v4l2_subdev struct
306 * @reg: TVP5146/47 register address
307 * @val: value to be written to the register
308 *
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300309 * Write a value to a register in an TVP5146/47 decoder device.
310 * Returns zero if successful, or non-zero otherwise.
311 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300312static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300313{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300314 int err, retry = 0;
315 struct i2c_client *client = v4l2_get_subdevdata(sd);
316
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300317write_again:
318
319 err = i2c_smbus_write_byte_data(client, reg, val);
320 if (err) {
321 if (retry <= I2C_RETRY_COUNT) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300322 v4l2_warn(sd, "Write: retry ... %d\n", retry);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300323 retry++;
324 msleep_interruptible(10);
325 goto write_again;
326 }
327 }
328
329 return err;
330}
331
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300332/**
333 * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
334 * @sd: ptr to v4l2_subdev struct
335 * @reglist: list of TVP5146/47 registers and values
336 *
337 * Initializes a list of TVP5146/47 registers:-
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300338 * if token is TOK_TERM, then entire write operation terminates
339 * if token is TOK_DELAY, then a delay of 'val' msec is introduced
340 * if token is TOK_SKIP, then the register write is skipped
341 * if token is TOK_WRITE, then the register write is performed
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300342 * Returns zero if successful, or non-zero otherwise.
343 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300344static int tvp514x_write_regs(struct v4l2_subdev *sd,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300345 const struct tvp514x_reg reglist[])
346{
347 int err;
348 const struct tvp514x_reg *next = reglist;
349
350 for (; next->token != TOK_TERM; next++) {
351 if (next->token == TOK_DELAY) {
352 msleep(next->val);
353 continue;
354 }
355
356 if (next->token == TOK_SKIP)
357 continue;
358
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300359 err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300360 if (err) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300361 v4l2_err(sd, "Write failed. Err[%d]\n", err);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300362 return err;
363 }
364 }
365 return 0;
366}
367
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300368/**
Hans Verkuil2db4e782010-05-09 06:30:15 -0300369 * tvp514x_query_current_std() : Query the current standard detected by TVP5146/47
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300370 * @sd: ptr to v4l2_subdev struct
371 *
Hans Verkuil2db4e782010-05-09 06:30:15 -0300372 * Returns the current standard detected by TVP5146/47, STD_INVALID if there is no
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300373 * standard detected.
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300374 */
Hans Verkuil2db4e782010-05-09 06:30:15 -0300375static enum tvp514x_std tvp514x_query_current_std(struct v4l2_subdev *sd)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300376{
377 u8 std, std_status;
378
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300379 std = tvp514x_read_reg(sd, REG_VIDEO_STD);
380 if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300381 /* use the standard status register */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300382 std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
383 else
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300384 /* use the standard register itself */
385 std_status = std;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300386
387 switch (std_status & VIDEO_STD_MASK) {
388 case VIDEO_STD_NTSC_MJ_BIT:
389 return STD_NTSC_MJ;
390
391 case VIDEO_STD_PAL_BDGHIN_BIT:
392 return STD_PAL_BDGHIN;
393
394 default:
395 return STD_INVALID;
396 }
397
398 return STD_INVALID;
399}
400
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300401/* TVP5146/47 register dump function */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300402static void tvp514x_reg_dump(struct v4l2_subdev *sd)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300403{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300404 dump_reg(sd, REG_INPUT_SEL);
405 dump_reg(sd, REG_AFE_GAIN_CTRL);
406 dump_reg(sd, REG_VIDEO_STD);
407 dump_reg(sd, REG_OPERATION_MODE);
408 dump_reg(sd, REG_COLOR_KILLER);
409 dump_reg(sd, REG_LUMA_CONTROL1);
410 dump_reg(sd, REG_LUMA_CONTROL2);
411 dump_reg(sd, REG_LUMA_CONTROL3);
412 dump_reg(sd, REG_BRIGHTNESS);
413 dump_reg(sd, REG_CONTRAST);
414 dump_reg(sd, REG_SATURATION);
415 dump_reg(sd, REG_HUE);
416 dump_reg(sd, REG_CHROMA_CONTROL1);
417 dump_reg(sd, REG_CHROMA_CONTROL2);
418 dump_reg(sd, REG_COMP_PR_SATURATION);
419 dump_reg(sd, REG_COMP_Y_CONTRAST);
420 dump_reg(sd, REG_COMP_PB_SATURATION);
421 dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
422 dump_reg(sd, REG_AVID_START_PIXEL_LSB);
423 dump_reg(sd, REG_AVID_START_PIXEL_MSB);
424 dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
425 dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
426 dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
427 dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
428 dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
429 dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
430 dump_reg(sd, REG_VSYNC_START_LINE_LSB);
431 dump_reg(sd, REG_VSYNC_START_LINE_MSB);
432 dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
433 dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
434 dump_reg(sd, REG_VBLK_START_LINE_LSB);
435 dump_reg(sd, REG_VBLK_START_LINE_MSB);
436 dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
437 dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
438 dump_reg(sd, REG_SYNC_CONTROL);
439 dump_reg(sd, REG_OUTPUT_FORMATTER1);
440 dump_reg(sd, REG_OUTPUT_FORMATTER2);
441 dump_reg(sd, REG_OUTPUT_FORMATTER3);
442 dump_reg(sd, REG_OUTPUT_FORMATTER4);
443 dump_reg(sd, REG_OUTPUT_FORMATTER5);
444 dump_reg(sd, REG_OUTPUT_FORMATTER6);
445 dump_reg(sd, REG_CLEAR_LOST_LOCK);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300446}
447
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300448/**
449 * tvp514x_configure() - Configure the TVP5146/47 registers
450 * @sd: ptr to v4l2_subdev struct
451 * @decoder: ptr to tvp514x_decoder structure
452 *
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300453 * Returns zero if successful, or non-zero otherwise.
454 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300455static int tvp514x_configure(struct v4l2_subdev *sd,
456 struct tvp514x_decoder *decoder)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300457{
458 int err;
459
460 /* common register initialization */
461 err =
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300462 tvp514x_write_regs(sd, decoder->tvp514x_regs);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300463 if (err)
464 return err;
465
466 if (debug)
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300467 tvp514x_reg_dump(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300468
469 return 0;
470}
471
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300472/**
473 * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
474 * @sd: pointer to standard V4L2 sub-device structure
475 * @decoder: pointer to tvp514x_decoder structure
476 *
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300477 * A device is considered to be detected if the chip ID (LSB and MSB)
478 * registers match the expected values.
479 * Any value of the rom version register is accepted.
480 * Returns ENODEV error number if no device is detected, or zero
481 * if a device is detected.
482 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300483static int tvp514x_detect(struct v4l2_subdev *sd,
484 struct tvp514x_decoder *decoder)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300485{
486 u8 chip_id_msb, chip_id_lsb, rom_ver;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300487 struct i2c_client *client = v4l2_get_subdevdata(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300488
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300489 chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
490 chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
491 rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300492
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300493 v4l2_dbg(1, debug, sd,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300494 "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
495 chip_id_msb, chip_id_lsb, rom_ver);
496 if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
497 || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
498 && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
499 /* We didn't read the values we expected, so this must not be
500 * an TVP5146/47.
501 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300502 v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
503 chip_id_msb, chip_id_lsb);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300504 return -ENODEV;
505 }
506
507 decoder->ver = rom_ver;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300508
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300509 v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
510 client->name, decoder->ver,
511 client->addr << 1, client->adapter->name);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300512 return 0;
513}
514
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300515/**
516 * tvp514x_querystd() - V4L2 decoder interface handler for querystd
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300517 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300518 * @std_id: standard V4L2 std_id ioctl enum
519 *
520 * Returns the current standard detected by TVP5146/47. If no active input is
Hans Verkuil2db4e782010-05-09 06:30:15 -0300521 * detected then *std_id is set to 0 and the function returns 0.
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300522 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300523static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300524{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300525 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300526 enum tvp514x_std current_std;
527 enum tvp514x_input input_sel;
528 u8 sync_lock_status, lock_mask;
529
530 if (std_id == NULL)
531 return -EINVAL;
532
Hans Verkuil2db4e782010-05-09 06:30:15 -0300533 *std_id = V4L2_STD_UNKNOWN;
534
535 /* query the current standard */
536 current_std = tvp514x_query_current_std(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300537 if (current_std == STD_INVALID)
Hans Verkuil2db4e782010-05-09 06:30:15 -0300538 return 0;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300539
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300540 input_sel = decoder->input;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300541
542 switch (input_sel) {
543 case INPUT_CVBS_VI1A:
544 case INPUT_CVBS_VI1B:
545 case INPUT_CVBS_VI1C:
546 case INPUT_CVBS_VI2A:
547 case INPUT_CVBS_VI2B:
548 case INPUT_CVBS_VI2C:
549 case INPUT_CVBS_VI3A:
550 case INPUT_CVBS_VI3B:
551 case INPUT_CVBS_VI3C:
552 case INPUT_CVBS_VI4A:
553 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
554 STATUS_HORZ_SYNC_LOCK_BIT |
555 STATUS_VIRT_SYNC_LOCK_BIT;
556 break;
557
558 case INPUT_SVIDEO_VI2A_VI1A:
559 case INPUT_SVIDEO_VI2B_VI1B:
560 case INPUT_SVIDEO_VI2C_VI1C:
561 case INPUT_SVIDEO_VI2A_VI3A:
562 case INPUT_SVIDEO_VI2B_VI3B:
563 case INPUT_SVIDEO_VI2C_VI3C:
564 case INPUT_SVIDEO_VI4A_VI1A:
565 case INPUT_SVIDEO_VI4A_VI1B:
566 case INPUT_SVIDEO_VI4A_VI1C:
567 case INPUT_SVIDEO_VI4A_VI3A:
568 case INPUT_SVIDEO_VI4A_VI3B:
569 case INPUT_SVIDEO_VI4A_VI3C:
570 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
571 STATUS_VIRT_SYNC_LOCK_BIT;
572 break;
573 /*Need to add other interfaces*/
574 default:
575 return -EINVAL;
576 }
577 /* check whether signal is locked */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300578 sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300579 if (lock_mask != (sync_lock_status & lock_mask))
Hans Verkuil2db4e782010-05-09 06:30:15 -0300580 return 0; /* No input detected */
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300581
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300582 *std_id = decoder->std_list[current_std].standard.id;
583
Hans Verkuil2db4e782010-05-09 06:30:15 -0300584 v4l2_dbg(1, debug, sd, "Current STD: %s\n",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300585 decoder->std_list[current_std].standard.name);
586 return 0;
587}
588
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300589/**
590 * tvp514x_s_std() - V4L2 decoder interface handler for s_std
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300591 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300592 * @std_id: standard V4L2 v4l2_std_id ioctl enum
593 *
594 * If std_id is supported, sets the requested standard. Otherwise, returns
595 * -EINVAL
596 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300597static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300598{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300599 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300600 int err, i;
601
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300602 for (i = 0; i < decoder->num_stds; i++)
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300603 if (std_id & decoder->std_list[i].standard.id)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300604 break;
605
606 if ((i == decoder->num_stds) || (i == STD_INVALID))
607 return -EINVAL;
608
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300609 err = tvp514x_write_reg(sd, REG_VIDEO_STD,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300610 decoder->std_list[i].video_std);
611 if (err)
612 return err;
613
614 decoder->current_std = i;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300615 decoder->tvp514x_regs[REG_VIDEO_STD].val =
616 decoder->std_list[i].video_std;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300617
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300618 v4l2_dbg(1, debug, sd, "Standard set to: %s",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300619 decoder->std_list[i].standard.name);
620 return 0;
621}
622
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300623/**
624 * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300625 * @sd: pointer to standard V4L2 sub-device structure
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300626 * @input: input selector for routing the signal
627 * @output: output selector for routing the signal
628 * @config: config value. Not used
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300629 *
630 * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
631 * the input is not supported or there is no active signal present in the
632 * selected input.
633 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300634static int tvp514x_s_routing(struct v4l2_subdev *sd,
635 u32 input, u32 output, u32 config)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300636{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300637 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300638 int err;
639 enum tvp514x_input input_sel;
640 enum tvp514x_output output_sel;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300641 u8 sync_lock_status, lock_mask;
642 int try_count = LOCK_RETRY_COUNT;
643
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300644 if ((input >= INPUT_INVALID) ||
645 (output >= OUTPUT_INVALID))
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300646 /* Index out of bound */
647 return -EINVAL;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300648
Vaibhav Hiremath63b59ce2010-03-27 09:37:54 -0300649 /*
650 * For the sequence streamon -> streamoff and again s_input
651 * it fails to lock the signal, since streamoff puts TVP514x
652 * into power off state which leads to failure in sub-sequent s_input.
653 *
654 * So power up the TVP514x device here, since it is important to lock
655 * the signal at this stage.
656 */
657 if (!decoder->streaming)
658 tvp514x_s_stream(sd, 1);
659
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300660 input_sel = input;
661 output_sel = output;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300662
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300663 err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300664 if (err)
665 return err;
666
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300667 output_sel |= tvp514x_read_reg(sd,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300668 REG_OUTPUT_FORMATTER1) & 0x7;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300669 err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300670 output_sel);
671 if (err)
672 return err;
673
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300674 decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
675 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300676
677 /* Clear status */
678 msleep(LOCK_RETRY_DELAY);
679 err =
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300680 tvp514x_write_reg(sd, REG_CLEAR_LOST_LOCK, 0x01);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300681 if (err)
682 return err;
683
684 switch (input_sel) {
685 case INPUT_CVBS_VI1A:
686 case INPUT_CVBS_VI1B:
687 case INPUT_CVBS_VI1C:
688 case INPUT_CVBS_VI2A:
689 case INPUT_CVBS_VI2B:
690 case INPUT_CVBS_VI2C:
691 case INPUT_CVBS_VI3A:
692 case INPUT_CVBS_VI3B:
693 case INPUT_CVBS_VI3C:
694 case INPUT_CVBS_VI4A:
695 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
696 STATUS_HORZ_SYNC_LOCK_BIT |
697 STATUS_VIRT_SYNC_LOCK_BIT;
698 break;
699
700 case INPUT_SVIDEO_VI2A_VI1A:
701 case INPUT_SVIDEO_VI2B_VI1B:
702 case INPUT_SVIDEO_VI2C_VI1C:
703 case INPUT_SVIDEO_VI2A_VI3A:
704 case INPUT_SVIDEO_VI2B_VI3B:
705 case INPUT_SVIDEO_VI2C_VI3C:
706 case INPUT_SVIDEO_VI4A_VI1A:
707 case INPUT_SVIDEO_VI4A_VI1B:
708 case INPUT_SVIDEO_VI4A_VI1C:
709 case INPUT_SVIDEO_VI4A_VI3A:
710 case INPUT_SVIDEO_VI4A_VI3B:
711 case INPUT_SVIDEO_VI4A_VI3C:
712 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
713 STATUS_VIRT_SYNC_LOCK_BIT;
714 break;
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300715 /* Need to add other interfaces*/
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300716 default:
717 return -EINVAL;
718 }
719
720 while (try_count-- > 0) {
721 /* Allow decoder to sync up with new input */
722 msleep(LOCK_RETRY_DELAY);
723
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300724 sync_lock_status = tvp514x_read_reg(sd,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300725 REG_STATUS1);
726 if (lock_mask == (sync_lock_status & lock_mask))
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300727 /* Input detected */
728 break;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300729 }
730
Hans Verkuil2db4e782010-05-09 06:30:15 -0300731 if (try_count < 0)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300732 return -EINVAL;
733
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300734 decoder->input = input;
735 decoder->output = output;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300736
Hans Verkuil2db4e782010-05-09 06:30:15 -0300737 v4l2_dbg(1, debug, sd, "Input set to: %d\n", input_sel);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300738
739 return 0;
740}
741
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300742/**
743 * tvp514x_queryctrl() - V4L2 decoder interface handler for queryctrl
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300744 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300745 * @qctrl: standard V4L2 v4l2_queryctrl structure
746 *
747 * If the requested control is supported, returns the control information.
748 * Otherwise, returns -EINVAL if the control is not supported.
749 */
750static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300751tvp514x_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qctrl)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300752{
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300753 int err = -EINVAL;
754
755 if (qctrl == NULL)
756 return err;
757
758 switch (qctrl->id) {
759 case V4L2_CID_BRIGHTNESS:
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300760 /* Brightness supported is (0-255), */
Hans Verkuil10afbef2009-02-21 18:47:24 -0300761 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300762 break;
763 case V4L2_CID_CONTRAST:
764 case V4L2_CID_SATURATION:
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300765 /**
766 * Saturation and Contrast supported is -
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300767 * Contrast: 0 - 255 (Default - 128)
768 * Saturation: 0 - 255 (Default - 128)
769 */
770 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
771 break;
772 case V4L2_CID_HUE:
773 /* Hue Supported is -
774 * Hue - -180 - +180 (Default - 0, Step - +180)
775 */
776 err = v4l2_ctrl_query_fill(qctrl, -180, 180, 180, 0);
777 break;
778 case V4L2_CID_AUTOGAIN:
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300779 /**
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300780 * Auto Gain supported is -
781 * 0 - 1 (Default - 1)
782 */
783 err = v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 1);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300784 break;
785 default:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300786 v4l2_err(sd, "invalid control id %d\n", qctrl->id);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300787 return err;
788 }
789
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300790 v4l2_dbg(1, debug, sd, "Query Control:%s: Min - %d, Max - %d, Def - %d",
791 qctrl->name, qctrl->minimum, qctrl->maximum,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300792 qctrl->default_value);
793
794 return err;
795}
796
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300797/**
798 * tvp514x_g_ctrl() - V4L2 decoder interface handler for g_ctrl
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300799 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300800 * @ctrl: pointer to v4l2_control structure
801 *
802 * If the requested control is supported, returns the control's current
803 * value from the decoder. Otherwise, returns -EINVAL if the control is not
804 * supported.
805 */
806static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300807tvp514x_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300808{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300809 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300810
811 if (ctrl == NULL)
812 return -EINVAL;
813
814 switch (ctrl->id) {
815 case V4L2_CID_BRIGHTNESS:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300816 ctrl->value = decoder->tvp514x_regs[REG_BRIGHTNESS].val;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300817 break;
818 case V4L2_CID_CONTRAST:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300819 ctrl->value = decoder->tvp514x_regs[REG_CONTRAST].val;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300820 break;
821 case V4L2_CID_SATURATION:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300822 ctrl->value = decoder->tvp514x_regs[REG_SATURATION].val;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300823 break;
824 case V4L2_CID_HUE:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300825 ctrl->value = decoder->tvp514x_regs[REG_HUE].val;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300826 if (ctrl->value == 0x7F)
827 ctrl->value = 180;
828 else if (ctrl->value == 0x80)
829 ctrl->value = -180;
830 else
831 ctrl->value = 0;
832
833 break;
834 case V4L2_CID_AUTOGAIN:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300835 ctrl->value = decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300836 if ((ctrl->value & 0x3) == 3)
837 ctrl->value = 1;
838 else
839 ctrl->value = 0;
840
841 break;
842 default:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300843 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300844 return -EINVAL;
845 }
846
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300847 v4l2_dbg(1, debug, sd, "Get Control: ID - %d - %d",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300848 ctrl->id, ctrl->value);
849 return 0;
850}
851
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300852/**
853 * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300854 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300855 * @ctrl: pointer to v4l2_control structure
856 *
857 * If the requested control is supported, sets the control's current
858 * value in HW. Otherwise, returns -EINVAL if the control is not supported.
859 */
860static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300861tvp514x_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300862{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300863 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300864 int err = -EINVAL, value;
865
866 if (ctrl == NULL)
867 return err;
868
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300869 value = ctrl->value;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300870
871 switch (ctrl->id) {
872 case V4L2_CID_BRIGHTNESS:
873 if (ctrl->value < 0 || ctrl->value > 255) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300874 v4l2_err(sd, "invalid brightness setting %d\n",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300875 ctrl->value);
876 return -ERANGE;
877 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300878 err = tvp514x_write_reg(sd, REG_BRIGHTNESS,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300879 value);
880 if (err)
881 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300882
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300883 decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300884 break;
885 case V4L2_CID_CONTRAST:
886 if (ctrl->value < 0 || ctrl->value > 255) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300887 v4l2_err(sd, "invalid contrast setting %d\n",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300888 ctrl->value);
889 return -ERANGE;
890 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300891 err = tvp514x_write_reg(sd, REG_CONTRAST, value);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300892 if (err)
893 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300894
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300895 decoder->tvp514x_regs[REG_CONTRAST].val = value;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300896 break;
897 case V4L2_CID_SATURATION:
898 if (ctrl->value < 0 || ctrl->value > 255) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300899 v4l2_err(sd, "invalid saturation setting %d\n",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300900 ctrl->value);
901 return -ERANGE;
902 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300903 err = tvp514x_write_reg(sd, REG_SATURATION, value);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300904 if (err)
905 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300906
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300907 decoder->tvp514x_regs[REG_SATURATION].val = value;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300908 break;
909 case V4L2_CID_HUE:
910 if (value == 180)
911 value = 0x7F;
912 else if (value == -180)
913 value = 0x80;
914 else if (value == 0)
915 value = 0;
916 else {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300917 v4l2_err(sd, "invalid hue setting %d\n", ctrl->value);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300918 return -ERANGE;
919 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300920 err = tvp514x_write_reg(sd, REG_HUE, value);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300921 if (err)
922 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300923
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300924 decoder->tvp514x_regs[REG_HUE].val = value;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300925 break;
926 case V4L2_CID_AUTOGAIN:
927 if (value == 1)
928 value = 0x0F;
929 else if (value == 0)
930 value = 0x0C;
931 else {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300932 v4l2_err(sd, "invalid auto gain setting %d\n",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300933 ctrl->value);
934 return -ERANGE;
935 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300936 err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300937 if (err)
938 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300939
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300940 decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300941 break;
942 default:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300943 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300944 return err;
945 }
946
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300947 v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300948 ctrl->id, ctrl->value);
949
950 return err;
951}
952
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300953/**
954 * tvp514x_enum_fmt_cap() - V4L2 decoder interface handler for enum_fmt
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300955 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300956 * @fmt: standard V4L2 VIDIOC_ENUM_FMT ioctl structure
957 *
958 * Implement the VIDIOC_ENUM_FMT ioctl to enumerate supported formats
959 */
960static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300961tvp514x_enum_fmt_cap(struct v4l2_subdev *sd, struct v4l2_fmtdesc *fmt)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300962{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300963 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300964 int index;
965
966 if (fmt == NULL)
967 return -EINVAL;
968
969 index = fmt->index;
970 if ((index >= decoder->num_fmts) || (index < 0))
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300971 /* Index out of bound */
972 return -EINVAL;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300973
974 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300975 /* only capture is supported */
976 return -EINVAL;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300977
978 memcpy(fmt, &decoder->fmt_list[index],
979 sizeof(struct v4l2_fmtdesc));
980
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300981 v4l2_dbg(1, debug, sd, "Current FMT: index - %d (%s)",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300982 decoder->fmt_list[index].index,
983 decoder->fmt_list[index].description);
984 return 0;
985}
986
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300987/**
988 * tvp514x_try_fmt_cap() - V4L2 decoder interface handler for try_fmt
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300989 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300990 * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
991 *
992 * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
993 * ioctl is used to negotiate the image capture size and pixel format
994 * without actually making it take effect.
995 */
996static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300997tvp514x_try_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300998{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300999 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001000 int ifmt;
1001 struct v4l2_pix_format *pix;
1002 enum tvp514x_std current_std;
1003
1004 if (f == NULL)
1005 return -EINVAL;
1006
1007 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001008 /* only capture is supported */
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001009 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1010
1011 pix = &f->fmt.pix;
1012
1013 /* Calculate height and width based on current standard */
Hans Verkuil2db4e782010-05-09 06:30:15 -03001014 current_std = decoder->current_std;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001015
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001016 pix->width = decoder->std_list[current_std].width;
1017 pix->height = decoder->std_list[current_std].height;
1018
1019 for (ifmt = 0; ifmt < decoder->num_fmts; ifmt++) {
1020 if (pix->pixelformat ==
1021 decoder->fmt_list[ifmt].pixelformat)
1022 break;
1023 }
1024 if (ifmt == decoder->num_fmts)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001025 /* None of the format matched, select default */
1026 ifmt = 0;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001027 pix->pixelformat = decoder->fmt_list[ifmt].pixelformat;
1028
1029 pix->field = V4L2_FIELD_INTERLACED;
1030 pix->bytesperline = pix->width * 2;
1031 pix->sizeimage = pix->bytesperline * pix->height;
1032 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1033 pix->priv = 0;
1034
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001035 v4l2_dbg(1, debug, sd, "Try FMT: pixelformat - %s, bytesperline - %d"
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001036 "Width - %d, Height - %d",
1037 decoder->fmt_list[ifmt].description, pix->bytesperline,
1038 pix->width, pix->height);
1039 return 0;
1040}
1041
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001042/**
1043 * tvp514x_s_fmt_cap() - V4L2 decoder interface handler for s_fmt
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001044 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001045 * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
1046 *
1047 * If the requested format is supported, configures the HW to use that
1048 * format, returns error code if format not supported or HW can't be
1049 * correctly configured.
1050 */
1051static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001052tvp514x_s_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001053{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001054 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001055 struct v4l2_pix_format *pix;
1056 int rval;
1057
1058 if (f == NULL)
1059 return -EINVAL;
1060
1061 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001062 /* only capture is supported */
1063 return -EINVAL;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001064
1065 pix = &f->fmt.pix;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001066 rval = tvp514x_try_fmt_cap(sd, f);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001067 if (rval)
1068 return rval;
1069
1070 decoder->pix = *pix;
1071
1072 return rval;
1073}
1074
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001075/**
1076 * tvp514x_g_fmt_cap() - V4L2 decoder interface handler for tvp514x_g_fmt_cap
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001077 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001078 * @f: pointer to standard V4L2 v4l2_format structure
1079 *
1080 * Returns the decoder's current pixel format in the v4l2_format
1081 * parameter.
1082 */
1083static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001084tvp514x_g_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001085{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001086 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001087
1088 if (f == NULL)
1089 return -EINVAL;
1090
1091 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001092 /* only capture is supported */
1093 return -EINVAL;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001094
1095 f->fmt.pix = decoder->pix;
1096
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001097 v4l2_dbg(1, debug, sd, "Current FMT: bytesperline - %d"
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001098 "Width - %d, Height - %d",
1099 decoder->pix.bytesperline,
1100 decoder->pix.width, decoder->pix.height);
1101 return 0;
1102}
1103
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001104/**
1105 * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001106 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001107 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
1108 *
1109 * Returns the decoder's video CAPTURE parameters.
1110 */
1111static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001112tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001113{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001114 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001115 struct v4l2_captureparm *cparm;
1116 enum tvp514x_std current_std;
1117
1118 if (a == NULL)
1119 return -EINVAL;
1120
1121 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001122 /* only capture is supported */
1123 return -EINVAL;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001124
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001125 /* get the current standard */
Hans Verkuil2db4e782010-05-09 06:30:15 -03001126 current_std = decoder->current_std;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001127
1128 cparm = &a->parm.capture;
1129 cparm->capability = V4L2_CAP_TIMEPERFRAME;
1130 cparm->timeperframe =
1131 decoder->std_list[current_std].standard.frameperiod;
1132
1133 return 0;
1134}
1135
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001136/**
1137 * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001138 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001139 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
1140 *
1141 * Configures the decoder to use the input parameters, if possible. If
1142 * not possible, returns the appropriate error code.
1143 */
1144static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001145tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001146{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001147 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001148 struct v4l2_fract *timeperframe;
1149 enum tvp514x_std current_std;
1150
1151 if (a == NULL)
1152 return -EINVAL;
1153
1154 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001155 /* only capture is supported */
1156 return -EINVAL;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001157
1158 timeperframe = &a->parm.capture.timeperframe;
1159
1160 /* get the current standard */
Hans Verkuil2db4e782010-05-09 06:30:15 -03001161 current_std = decoder->current_std;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001162
1163 *timeperframe =
1164 decoder->std_list[current_std].standard.frameperiod;
1165
1166 return 0;
1167}
1168
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001169/**
1170 * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001171 * @sd: pointer to standard V4L2 sub-device structure
1172 * @enable: streaming enable or disable
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001173 *
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001174 * Sets streaming to enable or disable, if possible.
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001175 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001176static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001177{
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001178 int err = 0;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001179 struct i2c_client *client = v4l2_get_subdevdata(sd);
1180 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001181
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001182 if (decoder->streaming == enable)
1183 return 0;
1184
1185 switch (enable) {
1186 case 0:
1187 {
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001188 /* Power Down Sequence */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001189 err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
1190 if (err) {
1191 v4l2_err(sd, "Unable to turn off decoder\n");
1192 return err;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001193 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001194 decoder->streaming = enable;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001195 break;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001196 }
1197 case 1:
1198 {
1199 struct tvp514x_reg *int_seq = (struct tvp514x_reg *)
1200 client->driver->id_table->driver_data;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001201
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001202 /* Power Up Sequence */
1203 err = tvp514x_write_regs(sd, int_seq);
1204 if (err) {
1205 v4l2_err(sd, "Unable to turn on decoder\n");
1206 return err;
1207 }
1208 /* Detect if not already detected */
1209 err = tvp514x_detect(sd, decoder);
1210 if (err) {
1211 v4l2_err(sd, "Unable to detect decoder\n");
1212 return err;
1213 }
1214 err = tvp514x_configure(sd, decoder);
1215 if (err) {
1216 v4l2_err(sd, "Unable to configure decoder\n");
1217 return err;
1218 }
1219 decoder->streaming = enable;
1220 break;
1221 }
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001222 default:
1223 err = -ENODEV;
1224 break;
1225 }
1226
1227 return err;
1228}
1229
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001230static const struct v4l2_subdev_core_ops tvp514x_core_ops = {
1231 .queryctrl = tvp514x_queryctrl,
1232 .g_ctrl = tvp514x_g_ctrl,
1233 .s_ctrl = tvp514x_s_ctrl,
1234 .s_std = tvp514x_s_std,
1235};
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001236
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001237static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
1238 .s_routing = tvp514x_s_routing,
1239 .querystd = tvp514x_querystd,
1240 .enum_fmt = tvp514x_enum_fmt_cap,
1241 .g_fmt = tvp514x_g_fmt_cap,
1242 .try_fmt = tvp514x_try_fmt_cap,
1243 .s_fmt = tvp514x_s_fmt_cap,
1244 .g_parm = tvp514x_g_parm,
1245 .s_parm = tvp514x_s_parm,
1246 .s_stream = tvp514x_s_stream,
1247};
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001248
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001249static const struct v4l2_subdev_ops tvp514x_ops = {
1250 .core = &tvp514x_core_ops,
1251 .video = &tvp514x_video_ops,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001252};
1253
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001254static struct tvp514x_decoder tvp514x_dev = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001255 .streaming = 0,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001256
1257 .fmt_list = tvp514x_fmt_list,
1258 .num_fmts = ARRAY_SIZE(tvp514x_fmt_list),
1259
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001260 .pix = {
1261 /* Default to NTSC 8-bit YUV 422 */
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001262 .width = NTSC_NUM_ACTIVE_PIXELS,
1263 .height = NTSC_NUM_ACTIVE_LINES,
1264 .pixelformat = V4L2_PIX_FMT_UYVY,
1265 .field = V4L2_FIELD_INTERLACED,
1266 .bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2,
1267 .sizeimage =
1268 NTSC_NUM_ACTIVE_PIXELS * 2 * NTSC_NUM_ACTIVE_LINES,
1269 .colorspace = V4L2_COLORSPACE_SMPTE170M,
1270 },
1271
1272 .current_std = STD_NTSC_MJ,
1273 .std_list = tvp514x_std_list,
1274 .num_stds = ARRAY_SIZE(tvp514x_std_list),
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001275
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001276};
1277
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001278/**
1279 * tvp514x_probe() - decoder driver i2c probe handler
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001280 * @client: i2c driver client device structure
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001281 * @id: i2c driver id table
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001282 *
1283 * Register decoder as an i2c client device and V4L2
1284 * device.
1285 */
1286static int
1287tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1288{
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001289 struct tvp514x_decoder *decoder;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001290 struct v4l2_subdev *sd;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001291
1292 /* Check if the adapter supports the needed features */
1293 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1294 return -EIO;
1295
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001296 if (!client->dev.platform_data) {
1297 v4l2_err(client, "No platform data!!\n");
1298 return -ENODEV;
1299 }
1300
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001301 decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
1302 if (!decoder)
1303 return -ENOMEM;
1304
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001305 /* Initialize the tvp514x_decoder with default configuration */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001306 *decoder = tvp514x_dev;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001307 /* Copy default register configuration */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001308 memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
1309 sizeof(tvp514x_reg_list_default));
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001310
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001311 /* Copy board specific information here */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001312 decoder->pdata = client->dev.platform_data;
1313
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001314 /**
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001315 * Fetch platform specific data, and configure the
1316 * tvp514x_reg_list[] accordingly. Since this is one
1317 * time configuration, no need to preserve.
1318 */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001319 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001320 (decoder->pdata->clk_polarity << 1);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001321 decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001322 ((decoder->pdata->hs_polarity << 2) |
1323 (decoder->pdata->vs_polarity << 3));
1324 /* Set default standard to auto */
1325 decoder->tvp514x_regs[REG_VIDEO_STD].val =
1326 VIDEO_STD_AUTO_SWITCH_BIT;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001327
1328 /* Register with V4L2 layer as slave device */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001329 sd = &decoder->sd;
1330 v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001331
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001332 v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
1333
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001334 return 0;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001335
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001336}
1337
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001338/**
1339 * tvp514x_remove() - decoder driver i2c remove handler
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001340 * @client: i2c driver client device structure
1341 *
1342 * Unregister decoder as an i2c client device and V4L2
1343 * device. Complement of tvp514x_probe().
1344 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001345static int tvp514x_remove(struct i2c_client *client)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001346{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001347 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1348 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001349
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001350 v4l2_device_unregister_subdev(sd);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001351 kfree(decoder);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001352 return 0;
1353}
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001354/* TVP5146 Init/Power on Sequence */
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001355static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
1356 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1357 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1358 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1359 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1360 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1361 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1362 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1363 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1364 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1365 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1366 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001367 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001368};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001369
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001370/* TVP5147 Init/Power on Sequence */
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001371static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
1372 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1373 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1374 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1375 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1376 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1377 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1378 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1379 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1380 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
1381 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1382 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
1383 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
1384 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1385 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1386 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1387 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1388 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1389 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001390 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001391};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001392
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001393/* TVP5146M2/TVP5147M1 Init/Power on Sequence */
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001394static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1395 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1396 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001397 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001398};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001399
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001400/**
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001401 * I2C Device Table -
1402 *
1403 * name - Name of the actual device/chip.
1404 * driver_data - Driver data
1405 */
1406static const struct i2c_device_id tvp514x_id[] = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001407 {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
1408 {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
1409 {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
1410 {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001411 {},
1412};
1413
1414MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1415
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001416static struct i2c_driver tvp514x_driver = {
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001417 .driver = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001418 .owner = THIS_MODULE,
1419 .name = TVP514X_MODULE_NAME,
1420 },
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001421 .probe = tvp514x_probe,
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001422 .remove = tvp514x_remove,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001423 .id_table = tvp514x_id,
1424};
1425
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001426static int __init tvp514x_init(void)
1427{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001428 return i2c_add_driver(&tvp514x_driver);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001429}
1430
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001431static void __exit tvp514x_exit(void)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001432{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001433 i2c_del_driver(&tvp514x_driver);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001434}
1435
1436module_init(tvp514x_init);
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001437module_exit(tvp514x_exit);