blob: 26b4e718cd6d74226cd0320c1a633cedae16041c [file] [log] [blame]
Vaibhav Hiremath07b1747c2008-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>
32#include <linux/delay.h>
33#include <linux/videodev2.h>
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030034
35#include <media/v4l2-device.h>
36#include <media/v4l2-common.h>
37#include <media/v4l2-chip-ident.h>
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030038#include <media/tvp514x.h>
39
40#include "tvp514x_regs.h"
41
42/* Module Name */
43#define TVP514X_MODULE_NAME "tvp514x"
44
45/* Private macros for TVP */
46#define I2C_RETRY_COUNT (5)
47#define LOCK_RETRY_COUNT (5)
48#define LOCK_RETRY_DELAY (200)
49
50/* Debug functions */
51static int debug;
52module_param(debug, bool, 0644);
53MODULE_PARM_DESC(debug, "Debug level (0-1)");
54
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030055MODULE_AUTHOR("Texas Instruments");
56MODULE_DESCRIPTION("TVP514X linux decoder driver");
57MODULE_LICENSE("GPL");
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030058
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030059/* enum tvp514x_std - enum for supported standards */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030060enum tvp514x_std {
61 STD_NTSC_MJ = 0,
62 STD_PAL_BDGHIN,
63 STD_INVALID
64};
65
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030066/**
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030067 * struct tvp514x_std_info - Structure to store standard informations
68 * @width: Line width in pixels
69 * @height:Number of active lines
70 * @video_std: Value to write in REG_VIDEO_STD register
71 * @standard: v4l2 standard structure information
72 */
73struct tvp514x_std_info {
74 unsigned long width;
75 unsigned long height;
76 u8 video_std;
77 struct v4l2_standard standard;
78};
79
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030080static struct tvp514x_reg tvp514x_reg_list_default[0x40];
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030081/**
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030082 * struct tvp514x_decoder - TVP5146/47 decoder object
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030083 * @sd: Subdevice Slave handle
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030084 * @tvp514x_regs: copy of hw's regs with preset values.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030085 * @pdata: Board specific
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030086 * @ver: Chip version
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030087 * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030088 * @pix: Current pixel format
89 * @num_fmts: Number of formats
90 * @fmt_list: Format list
91 * @current_std: Current standard
92 * @num_stds: Number of standards
93 * @std_list: Standards list
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030094 * @input: Input routing at chip level
95 * @output: Output routing at chip level
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030096 */
97struct tvp514x_decoder {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030098 struct v4l2_subdev sd;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030099 struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300100 const struct tvp514x_platform_data *pdata;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300101
102 int ver;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300103 int streaming;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300104
105 struct v4l2_pix_format pix;
106 int num_fmts;
107 const struct v4l2_fmtdesc *fmt_list;
108
109 enum tvp514x_std current_std;
110 int num_stds;
111 struct tvp514x_std_info *std_list;
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300112 /* Input and Output Routing parameters */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300113 u32 input;
114 u32 output;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300115};
116
117/* TVP514x default register values */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300118static struct tvp514x_reg tvp514x_reg_list_default[] = {
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300119 /* Composite selected */
120 {TOK_WRITE, REG_INPUT_SEL, 0x05},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300121 {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300122 /* Auto mode */
123 {TOK_WRITE, REG_VIDEO_STD, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300124 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
125 {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
126 {TOK_WRITE, REG_COLOR_KILLER, 0x10},
127 {TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
128 {TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
129 {TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
130 {TOK_WRITE, REG_BRIGHTNESS, 0x80},
131 {TOK_WRITE, REG_CONTRAST, 0x80},
132 {TOK_WRITE, REG_SATURATION, 0x80},
133 {TOK_WRITE, REG_HUE, 0x00},
134 {TOK_WRITE, REG_CHROMA_CONTROL1, 0x00},
135 {TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300136 /* Reserved */
137 {TOK_SKIP, 0x0F, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300138 {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
139 {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
140 {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300141 /* Reserved */
142 {TOK_SKIP, 0x13, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300143 {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300144 /* Reserved */
145 {TOK_SKIP, 0x15, 0x00},
146 /* NTSC timing */
147 {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300148 {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
149 {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
150 {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300151 /* NTSC timing */
152 {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300153 {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
154 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
155 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300156 /* NTSC timing */
157 {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300158 {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
159 {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
160 {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300161 /* NTSC timing */
162 {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300163 {TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
164 {TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
165 {TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300166 /* Reserved */
167 {TOK_SKIP, 0x26, 0x00},
168 /* Reserved */
169 {TOK_SKIP, 0x27, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300170 {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300171 /* Reserved */
172 {TOK_SKIP, 0x29, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300173 {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300174 /* Reserved */
175 {TOK_SKIP, 0x2B, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300176 {TOK_SKIP, REG_SCART_DELAY, 0x00},
177 {TOK_SKIP, REG_CTI_DELAY, 0x00},
178 {TOK_SKIP, REG_CTI_CONTROL, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300179 /* Reserved */
180 {TOK_SKIP, 0x2F, 0x00},
181 /* Reserved */
182 {TOK_SKIP, 0x30, 0x00},
183 /* Reserved */
184 {TOK_SKIP, 0x31, 0x00},
185 /* HS, VS active high */
186 {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
187 /* 10-bit BT.656 */
188 {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00},
189 /* Enable clk & data */
190 {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11},
191 /* Enable AVID & FLD */
192 {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE},
193 /* Enable VS & HS */
194 {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300195 {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
196 {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300197 /* Clear status */
198 {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300199 {TOK_TERM, 0, 0},
200};
201
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300202/**
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300203 * List of image formats supported by TVP5146/47 decoder
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300204 * Currently we are using 8 bit mode only, but can be
205 * extended to 10/20 bit mode.
206 */
207static const struct v4l2_fmtdesc tvp514x_fmt_list[] = {
208 {
209 .index = 0,
210 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
211 .flags = 0,
212 .description = "8-bit UYVY 4:2:2 Format",
213 .pixelformat = V4L2_PIX_FMT_UYVY,
214 },
215};
216
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300217/**
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300218 * Supported standards -
219 *
220 * Currently supports two standards only, need to add support for rest of the
221 * modes, like SECAM, etc...
222 */
223static struct tvp514x_std_info tvp514x_std_list[] = {
224 /* Standard: STD_NTSC_MJ */
225 [STD_NTSC_MJ] = {
226 .width = NTSC_NUM_ACTIVE_PIXELS,
227 .height = NTSC_NUM_ACTIVE_LINES,
228 .video_std = VIDEO_STD_NTSC_MJ_BIT,
229 .standard = {
230 .index = 0,
231 .id = V4L2_STD_NTSC,
232 .name = "NTSC",
233 .frameperiod = {1001, 30000},
234 .framelines = 525
235 },
236 /* Standard: STD_PAL_BDGHIN */
237 },
238 [STD_PAL_BDGHIN] = {
239 .width = PAL_NUM_ACTIVE_PIXELS,
240 .height = PAL_NUM_ACTIVE_LINES,
241 .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
242 .standard = {
243 .index = 1,
244 .id = V4L2_STD_PAL,
245 .name = "PAL",
246 .frameperiod = {1, 25},
247 .framelines = 625
248 },
249 },
250 /* Standard: need to add for additional standard */
251};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300252
253
254static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
255{
256 return container_of(sd, struct tvp514x_decoder, sd);
257}
258
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300259
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300260/**
261 * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
262 * @sd: ptr to v4l2_subdev struct
263 * @reg: TVP5146/47 register address
264 *
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300265 * Returns value read if successful, or non-zero (-1) otherwise.
266 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300267static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300268{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300269 int err, retry = 0;
270 struct i2c_client *client = v4l2_get_subdevdata(sd);
271
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300272read_again:
273
274 err = i2c_smbus_read_byte_data(client, reg);
Sebastian Andrzej Siewior6f901a92009-11-04 15:35:09 -0300275 if (err < 0) {
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300276 if (retry <= I2C_RETRY_COUNT) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300277 v4l2_warn(sd, "Read: retry ... %d\n", retry);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300278 retry++;
279 msleep_interruptible(10);
280 goto read_again;
281 }
282 }
283
284 return err;
285}
286
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300287/**
288 * dump_reg() - dump the register content of TVP5146/47.
289 * @sd: ptr to v4l2_subdev struct
290 * @reg: TVP5146/47 register address
291 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300292static void dump_reg(struct v4l2_subdev *sd, u8 reg)
293{
294 u32 val;
295
296 val = tvp514x_read_reg(sd, reg);
297 v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
298}
299
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300300/**
301 * tvp514x_write_reg() - Write a value to a register in TVP5146/47
302 * @sd: ptr to v4l2_subdev struct
303 * @reg: TVP5146/47 register address
304 * @val: value to be written to the register
305 *
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300306 * Write a value to a register in an TVP5146/47 decoder device.
307 * Returns zero if successful, or non-zero otherwise.
308 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300309static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300310{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300311 int err, retry = 0;
312 struct i2c_client *client = v4l2_get_subdevdata(sd);
313
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300314write_again:
315
316 err = i2c_smbus_write_byte_data(client, reg, val);
317 if (err) {
318 if (retry <= I2C_RETRY_COUNT) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300319 v4l2_warn(sd, "Write: retry ... %d\n", retry);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300320 retry++;
321 msleep_interruptible(10);
322 goto write_again;
323 }
324 }
325
326 return err;
327}
328
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300329/**
330 * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
331 * @sd: ptr to v4l2_subdev struct
332 * @reglist: list of TVP5146/47 registers and values
333 *
334 * Initializes a list of TVP5146/47 registers:-
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300335 * if token is TOK_TERM, then entire write operation terminates
336 * if token is TOK_DELAY, then a delay of 'val' msec is introduced
337 * if token is TOK_SKIP, then the register write is skipped
338 * if token is TOK_WRITE, then the register write is performed
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300339 * Returns zero if successful, or non-zero otherwise.
340 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300341static int tvp514x_write_regs(struct v4l2_subdev *sd,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300342 const struct tvp514x_reg reglist[])
343{
344 int err;
345 const struct tvp514x_reg *next = reglist;
346
347 for (; next->token != TOK_TERM; next++) {
348 if (next->token == TOK_DELAY) {
349 msleep(next->val);
350 continue;
351 }
352
353 if (next->token == TOK_SKIP)
354 continue;
355
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300356 err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300357 if (err) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300358 v4l2_err(sd, "Write failed. Err[%d]\n", err);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300359 return err;
360 }
361 }
362 return 0;
363}
364
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300365/**
366 * tvp514x_get_current_std() : Get the current standard detected by TVP5146/47
367 * @sd: ptr to v4l2_subdev struct
368 *
369 * Get current standard detected by TVP5146/47, STD_INVALID if there is no
370 * standard detected.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300371 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300372static enum tvp514x_std tvp514x_get_current_std(struct v4l2_subdev *sd)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300373{
374 u8 std, std_status;
375
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300376 std = tvp514x_read_reg(sd, REG_VIDEO_STD);
377 if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300378 /* use the standard status register */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300379 std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
380 else
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300381 /* use the standard register itself */
382 std_status = std;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300383
384 switch (std_status & VIDEO_STD_MASK) {
385 case VIDEO_STD_NTSC_MJ_BIT:
386 return STD_NTSC_MJ;
387
388 case VIDEO_STD_PAL_BDGHIN_BIT:
389 return STD_PAL_BDGHIN;
390
391 default:
392 return STD_INVALID;
393 }
394
395 return STD_INVALID;
396}
397
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300398/* TVP5146/47 register dump function */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300399static void tvp514x_reg_dump(struct v4l2_subdev *sd)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300400{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300401 dump_reg(sd, REG_INPUT_SEL);
402 dump_reg(sd, REG_AFE_GAIN_CTRL);
403 dump_reg(sd, REG_VIDEO_STD);
404 dump_reg(sd, REG_OPERATION_MODE);
405 dump_reg(sd, REG_COLOR_KILLER);
406 dump_reg(sd, REG_LUMA_CONTROL1);
407 dump_reg(sd, REG_LUMA_CONTROL2);
408 dump_reg(sd, REG_LUMA_CONTROL3);
409 dump_reg(sd, REG_BRIGHTNESS);
410 dump_reg(sd, REG_CONTRAST);
411 dump_reg(sd, REG_SATURATION);
412 dump_reg(sd, REG_HUE);
413 dump_reg(sd, REG_CHROMA_CONTROL1);
414 dump_reg(sd, REG_CHROMA_CONTROL2);
415 dump_reg(sd, REG_COMP_PR_SATURATION);
416 dump_reg(sd, REG_COMP_Y_CONTRAST);
417 dump_reg(sd, REG_COMP_PB_SATURATION);
418 dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
419 dump_reg(sd, REG_AVID_START_PIXEL_LSB);
420 dump_reg(sd, REG_AVID_START_PIXEL_MSB);
421 dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
422 dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
423 dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
424 dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
425 dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
426 dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
427 dump_reg(sd, REG_VSYNC_START_LINE_LSB);
428 dump_reg(sd, REG_VSYNC_START_LINE_MSB);
429 dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
430 dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
431 dump_reg(sd, REG_VBLK_START_LINE_LSB);
432 dump_reg(sd, REG_VBLK_START_LINE_MSB);
433 dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
434 dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
435 dump_reg(sd, REG_SYNC_CONTROL);
436 dump_reg(sd, REG_OUTPUT_FORMATTER1);
437 dump_reg(sd, REG_OUTPUT_FORMATTER2);
438 dump_reg(sd, REG_OUTPUT_FORMATTER3);
439 dump_reg(sd, REG_OUTPUT_FORMATTER4);
440 dump_reg(sd, REG_OUTPUT_FORMATTER5);
441 dump_reg(sd, REG_OUTPUT_FORMATTER6);
442 dump_reg(sd, REG_CLEAR_LOST_LOCK);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300443}
444
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300445/**
446 * tvp514x_configure() - Configure the TVP5146/47 registers
447 * @sd: ptr to v4l2_subdev struct
448 * @decoder: ptr to tvp514x_decoder structure
449 *
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300450 * Returns zero if successful, or non-zero otherwise.
451 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300452static int tvp514x_configure(struct v4l2_subdev *sd,
453 struct tvp514x_decoder *decoder)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300454{
455 int err;
456
457 /* common register initialization */
458 err =
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300459 tvp514x_write_regs(sd, decoder->tvp514x_regs);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300460 if (err)
461 return err;
462
463 if (debug)
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300464 tvp514x_reg_dump(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300465
466 return 0;
467}
468
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300469/**
470 * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
471 * @sd: pointer to standard V4L2 sub-device structure
472 * @decoder: pointer to tvp514x_decoder structure
473 *
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300474 * A device is considered to be detected if the chip ID (LSB and MSB)
475 * registers match the expected values.
476 * Any value of the rom version register is accepted.
477 * Returns ENODEV error number if no device is detected, or zero
478 * if a device is detected.
479 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300480static int tvp514x_detect(struct v4l2_subdev *sd,
481 struct tvp514x_decoder *decoder)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300482{
483 u8 chip_id_msb, chip_id_lsb, rom_ver;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300484 struct i2c_client *client = v4l2_get_subdevdata(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300485
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300486 chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
487 chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
488 rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300489
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300490 v4l2_dbg(1, debug, sd,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300491 "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
492 chip_id_msb, chip_id_lsb, rom_ver);
493 if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
494 || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
495 && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
496 /* We didn't read the values we expected, so this must not be
497 * an TVP5146/47.
498 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300499 v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
500 chip_id_msb, chip_id_lsb);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300501 return -ENODEV;
502 }
503
504 decoder->ver = rom_ver;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300505
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300506 v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
507 client->name, decoder->ver,
508 client->addr << 1, client->adapter->name);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300509 return 0;
510}
511
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300512/**
513 * tvp514x_querystd() - V4L2 decoder interface handler for querystd
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300514 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300515 * @std_id: standard V4L2 std_id ioctl enum
516 *
517 * Returns the current standard detected by TVP5146/47. If no active input is
518 * detected, returns -EINVAL
519 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300520static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300521{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300522 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300523 enum tvp514x_std current_std;
524 enum tvp514x_input input_sel;
525 u8 sync_lock_status, lock_mask;
526
527 if (std_id == NULL)
528 return -EINVAL;
529
530 /* get the current standard */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300531 current_std = tvp514x_get_current_std(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300532 if (current_std == STD_INVALID)
533 return -EINVAL;
534
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300535 input_sel = decoder->input;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300536
537 switch (input_sel) {
538 case INPUT_CVBS_VI1A:
539 case INPUT_CVBS_VI1B:
540 case INPUT_CVBS_VI1C:
541 case INPUT_CVBS_VI2A:
542 case INPUT_CVBS_VI2B:
543 case INPUT_CVBS_VI2C:
544 case INPUT_CVBS_VI3A:
545 case INPUT_CVBS_VI3B:
546 case INPUT_CVBS_VI3C:
547 case INPUT_CVBS_VI4A:
548 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
549 STATUS_HORZ_SYNC_LOCK_BIT |
550 STATUS_VIRT_SYNC_LOCK_BIT;
551 break;
552
553 case INPUT_SVIDEO_VI2A_VI1A:
554 case INPUT_SVIDEO_VI2B_VI1B:
555 case INPUT_SVIDEO_VI2C_VI1C:
556 case INPUT_SVIDEO_VI2A_VI3A:
557 case INPUT_SVIDEO_VI2B_VI3B:
558 case INPUT_SVIDEO_VI2C_VI3C:
559 case INPUT_SVIDEO_VI4A_VI1A:
560 case INPUT_SVIDEO_VI4A_VI1B:
561 case INPUT_SVIDEO_VI4A_VI1C:
562 case INPUT_SVIDEO_VI4A_VI3A:
563 case INPUT_SVIDEO_VI4A_VI3B:
564 case INPUT_SVIDEO_VI4A_VI3C:
565 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
566 STATUS_VIRT_SYNC_LOCK_BIT;
567 break;
568 /*Need to add other interfaces*/
569 default:
570 return -EINVAL;
571 }
572 /* check whether signal is locked */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300573 sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300574 if (lock_mask != (sync_lock_status & lock_mask))
575 return -EINVAL; /* No input detected */
576
577 decoder->current_std = current_std;
578 *std_id = decoder->std_list[current_std].standard.id;
579
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300580 v4l2_dbg(1, debug, sd, "Current STD: %s",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300581 decoder->std_list[current_std].standard.name);
582 return 0;
583}
584
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300585/**
586 * tvp514x_s_std() - V4L2 decoder interface handler for s_std
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300587 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300588 * @std_id: standard V4L2 v4l2_std_id ioctl enum
589 *
590 * If std_id is supported, sets the requested standard. Otherwise, returns
591 * -EINVAL
592 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300593static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300594{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300595 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300596 int err, i;
597
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300598 for (i = 0; i < decoder->num_stds; i++)
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300599 if (std_id & decoder->std_list[i].standard.id)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300600 break;
601
602 if ((i == decoder->num_stds) || (i == STD_INVALID))
603 return -EINVAL;
604
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300605 err = tvp514x_write_reg(sd, REG_VIDEO_STD,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300606 decoder->std_list[i].video_std);
607 if (err)
608 return err;
609
610 decoder->current_std = i;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300611 decoder->tvp514x_regs[REG_VIDEO_STD].val =
612 decoder->std_list[i].video_std;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300613
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300614 v4l2_dbg(1, debug, sd, "Standard set to: %s",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300615 decoder->std_list[i].standard.name);
616 return 0;
617}
618
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300619/**
620 * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300621 * @sd: pointer to standard V4L2 sub-device structure
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300622 * @input: input selector for routing the signal
623 * @output: output selector for routing the signal
624 * @config: config value. Not used
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300625 *
626 * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
627 * the input is not supported or there is no active signal present in the
628 * selected input.
629 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300630static int tvp514x_s_routing(struct v4l2_subdev *sd,
631 u32 input, u32 output, u32 config)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300632{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300633 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300634 int err;
635 enum tvp514x_input input_sel;
636 enum tvp514x_output output_sel;
637 enum tvp514x_std current_std = STD_INVALID;
638 u8 sync_lock_status, lock_mask;
639 int try_count = LOCK_RETRY_COUNT;
640
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300641 if ((input >= INPUT_INVALID) ||
642 (output >= OUTPUT_INVALID))
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300643 /* Index out of bound */
644 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300645
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300646 input_sel = input;
647 output_sel = output;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300648
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300649 err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300650 if (err)
651 return err;
652
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300653 output_sel |= tvp514x_read_reg(sd,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300654 REG_OUTPUT_FORMATTER1) & 0x7;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300655 err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300656 output_sel);
657 if (err)
658 return err;
659
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300660 decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
661 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300662
663 /* Clear status */
664 msleep(LOCK_RETRY_DELAY);
665 err =
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300666 tvp514x_write_reg(sd, REG_CLEAR_LOST_LOCK, 0x01);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300667 if (err)
668 return err;
669
670 switch (input_sel) {
671 case INPUT_CVBS_VI1A:
672 case INPUT_CVBS_VI1B:
673 case INPUT_CVBS_VI1C:
674 case INPUT_CVBS_VI2A:
675 case INPUT_CVBS_VI2B:
676 case INPUT_CVBS_VI2C:
677 case INPUT_CVBS_VI3A:
678 case INPUT_CVBS_VI3B:
679 case INPUT_CVBS_VI3C:
680 case INPUT_CVBS_VI4A:
681 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
682 STATUS_HORZ_SYNC_LOCK_BIT |
683 STATUS_VIRT_SYNC_LOCK_BIT;
684 break;
685
686 case INPUT_SVIDEO_VI2A_VI1A:
687 case INPUT_SVIDEO_VI2B_VI1B:
688 case INPUT_SVIDEO_VI2C_VI1C:
689 case INPUT_SVIDEO_VI2A_VI3A:
690 case INPUT_SVIDEO_VI2B_VI3B:
691 case INPUT_SVIDEO_VI2C_VI3C:
692 case INPUT_SVIDEO_VI4A_VI1A:
693 case INPUT_SVIDEO_VI4A_VI1B:
694 case INPUT_SVIDEO_VI4A_VI1C:
695 case INPUT_SVIDEO_VI4A_VI3A:
696 case INPUT_SVIDEO_VI4A_VI3B:
697 case INPUT_SVIDEO_VI4A_VI3C:
698 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
699 STATUS_VIRT_SYNC_LOCK_BIT;
700 break;
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300701 /* Need to add other interfaces*/
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300702 default:
703 return -EINVAL;
704 }
705
706 while (try_count-- > 0) {
707 /* Allow decoder to sync up with new input */
708 msleep(LOCK_RETRY_DELAY);
709
710 /* get the current standard for future reference */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300711 current_std = tvp514x_get_current_std(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300712 if (current_std == STD_INVALID)
713 continue;
714
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300715 sync_lock_status = tvp514x_read_reg(sd,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300716 REG_STATUS1);
717 if (lock_mask == (sync_lock_status & lock_mask))
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300718 /* Input detected */
719 break;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300720 }
721
Roel Kluin76b08112009-06-11 11:28:17 -0300722 if ((current_std == STD_INVALID) || (try_count < 0))
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300723 return -EINVAL;
724
725 decoder->current_std = current_std;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300726 decoder->input = input;
727 decoder->output = output;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300728
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300729 v4l2_dbg(1, debug, sd, "Input set to: %d, std : %d",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300730 input_sel, current_std);
731
732 return 0;
733}
734
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300735/**
736 * tvp514x_queryctrl() - V4L2 decoder interface handler for queryctrl
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300737 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300738 * @qctrl: standard V4L2 v4l2_queryctrl structure
739 *
740 * If the requested control is supported, returns the control information.
741 * Otherwise, returns -EINVAL if the control is not supported.
742 */
743static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300744tvp514x_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qctrl)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300745{
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300746 int err = -EINVAL;
747
748 if (qctrl == NULL)
749 return err;
750
751 switch (qctrl->id) {
752 case V4L2_CID_BRIGHTNESS:
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300753 /* Brightness supported is (0-255), */
Hans Verkuil10afbef2009-02-21 18:47:24 -0300754 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300755 break;
756 case V4L2_CID_CONTRAST:
757 case V4L2_CID_SATURATION:
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300758 /**
759 * Saturation and Contrast supported is -
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300760 * Contrast: 0 - 255 (Default - 128)
761 * Saturation: 0 - 255 (Default - 128)
762 */
763 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
764 break;
765 case V4L2_CID_HUE:
766 /* Hue Supported is -
767 * Hue - -180 - +180 (Default - 0, Step - +180)
768 */
769 err = v4l2_ctrl_query_fill(qctrl, -180, 180, 180, 0);
770 break;
771 case V4L2_CID_AUTOGAIN:
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300772 /**
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300773 * Auto Gain supported is -
774 * 0 - 1 (Default - 1)
775 */
776 err = v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 1);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300777 break;
778 default:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300779 v4l2_err(sd, "invalid control id %d\n", qctrl->id);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300780 return err;
781 }
782
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300783 v4l2_dbg(1, debug, sd, "Query Control:%s: Min - %d, Max - %d, Def - %d",
784 qctrl->name, qctrl->minimum, qctrl->maximum,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300785 qctrl->default_value);
786
787 return err;
788}
789
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300790/**
791 * tvp514x_g_ctrl() - V4L2 decoder interface handler for g_ctrl
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300792 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300793 * @ctrl: pointer to v4l2_control structure
794 *
795 * If the requested control is supported, returns the control's current
796 * value from the decoder. Otherwise, returns -EINVAL if the control is not
797 * supported.
798 */
799static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300800tvp514x_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300801{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300802 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300803
804 if (ctrl == NULL)
805 return -EINVAL;
806
807 switch (ctrl->id) {
808 case V4L2_CID_BRIGHTNESS:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300809 ctrl->value = decoder->tvp514x_regs[REG_BRIGHTNESS].val;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300810 break;
811 case V4L2_CID_CONTRAST:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300812 ctrl->value = decoder->tvp514x_regs[REG_CONTRAST].val;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300813 break;
814 case V4L2_CID_SATURATION:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300815 ctrl->value = decoder->tvp514x_regs[REG_SATURATION].val;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300816 break;
817 case V4L2_CID_HUE:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300818 ctrl->value = decoder->tvp514x_regs[REG_HUE].val;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300819 if (ctrl->value == 0x7F)
820 ctrl->value = 180;
821 else if (ctrl->value == 0x80)
822 ctrl->value = -180;
823 else
824 ctrl->value = 0;
825
826 break;
827 case V4L2_CID_AUTOGAIN:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300828 ctrl->value = decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300829 if ((ctrl->value & 0x3) == 3)
830 ctrl->value = 1;
831 else
832 ctrl->value = 0;
833
834 break;
835 default:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300836 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300837 return -EINVAL;
838 }
839
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300840 v4l2_dbg(1, debug, sd, "Get Control: ID - %d - %d",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300841 ctrl->id, ctrl->value);
842 return 0;
843}
844
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300845/**
846 * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300847 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300848 * @ctrl: pointer to v4l2_control structure
849 *
850 * If the requested control is supported, sets the control's current
851 * value in HW. Otherwise, returns -EINVAL if the control is not supported.
852 */
853static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300854tvp514x_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300855{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300856 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300857 int err = -EINVAL, value;
858
859 if (ctrl == NULL)
860 return err;
861
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300862 value = ctrl->value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300863
864 switch (ctrl->id) {
865 case V4L2_CID_BRIGHTNESS:
866 if (ctrl->value < 0 || ctrl->value > 255) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300867 v4l2_err(sd, "invalid brightness setting %d\n",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300868 ctrl->value);
869 return -ERANGE;
870 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300871 err = tvp514x_write_reg(sd, REG_BRIGHTNESS,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300872 value);
873 if (err)
874 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300875
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300876 decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300877 break;
878 case V4L2_CID_CONTRAST:
879 if (ctrl->value < 0 || ctrl->value > 255) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300880 v4l2_err(sd, "invalid contrast setting %d\n",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300881 ctrl->value);
882 return -ERANGE;
883 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300884 err = tvp514x_write_reg(sd, REG_CONTRAST, value);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300885 if (err)
886 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300887
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300888 decoder->tvp514x_regs[REG_CONTRAST].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300889 break;
890 case V4L2_CID_SATURATION:
891 if (ctrl->value < 0 || ctrl->value > 255) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300892 v4l2_err(sd, "invalid saturation setting %d\n",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300893 ctrl->value);
894 return -ERANGE;
895 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300896 err = tvp514x_write_reg(sd, REG_SATURATION, value);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300897 if (err)
898 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300899
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300900 decoder->tvp514x_regs[REG_SATURATION].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300901 break;
902 case V4L2_CID_HUE:
903 if (value == 180)
904 value = 0x7F;
905 else if (value == -180)
906 value = 0x80;
907 else if (value == 0)
908 value = 0;
909 else {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300910 v4l2_err(sd, "invalid hue setting %d\n", ctrl->value);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300911 return -ERANGE;
912 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300913 err = tvp514x_write_reg(sd, REG_HUE, value);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300914 if (err)
915 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300916
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300917 decoder->tvp514x_regs[REG_HUE].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300918 break;
919 case V4L2_CID_AUTOGAIN:
920 if (value == 1)
921 value = 0x0F;
922 else if (value == 0)
923 value = 0x0C;
924 else {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300925 v4l2_err(sd, "invalid auto gain setting %d\n",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300926 ctrl->value);
927 return -ERANGE;
928 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300929 err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300930 if (err)
931 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300932
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300933 decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300934 break;
935 default:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300936 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300937 return err;
938 }
939
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300940 v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300941 ctrl->id, ctrl->value);
942
943 return err;
944}
945
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300946/**
947 * tvp514x_enum_fmt_cap() - V4L2 decoder interface handler for enum_fmt
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300948 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300949 * @fmt: standard V4L2 VIDIOC_ENUM_FMT ioctl structure
950 *
951 * Implement the VIDIOC_ENUM_FMT ioctl to enumerate supported formats
952 */
953static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300954tvp514x_enum_fmt_cap(struct v4l2_subdev *sd, struct v4l2_fmtdesc *fmt)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300955{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300956 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300957 int index;
958
959 if (fmt == NULL)
960 return -EINVAL;
961
962 index = fmt->index;
963 if ((index >= decoder->num_fmts) || (index < 0))
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300964 /* Index out of bound */
965 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300966
967 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300968 /* only capture is supported */
969 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300970
971 memcpy(fmt, &decoder->fmt_list[index],
972 sizeof(struct v4l2_fmtdesc));
973
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300974 v4l2_dbg(1, debug, sd, "Current FMT: index - %d (%s)",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300975 decoder->fmt_list[index].index,
976 decoder->fmt_list[index].description);
977 return 0;
978}
979
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300980/**
981 * tvp514x_try_fmt_cap() - V4L2 decoder interface handler for try_fmt
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300982 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300983 * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
984 *
985 * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
986 * ioctl is used to negotiate the image capture size and pixel format
987 * without actually making it take effect.
988 */
989static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300990tvp514x_try_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300991{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300992 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300993 int ifmt;
994 struct v4l2_pix_format *pix;
995 enum tvp514x_std current_std;
996
997 if (f == NULL)
998 return -EINVAL;
999
1000 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001001 /* only capture is supported */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001002 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1003
1004 pix = &f->fmt.pix;
1005
1006 /* Calculate height and width based on current standard */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001007 current_std = tvp514x_get_current_std(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001008 if (current_std == STD_INVALID)
1009 return -EINVAL;
1010
1011 decoder->current_std = current_std;
1012 pix->width = decoder->std_list[current_std].width;
1013 pix->height = decoder->std_list[current_std].height;
1014
1015 for (ifmt = 0; ifmt < decoder->num_fmts; ifmt++) {
1016 if (pix->pixelformat ==
1017 decoder->fmt_list[ifmt].pixelformat)
1018 break;
1019 }
1020 if (ifmt == decoder->num_fmts)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001021 /* None of the format matched, select default */
1022 ifmt = 0;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001023 pix->pixelformat = decoder->fmt_list[ifmt].pixelformat;
1024
1025 pix->field = V4L2_FIELD_INTERLACED;
1026 pix->bytesperline = pix->width * 2;
1027 pix->sizeimage = pix->bytesperline * pix->height;
1028 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1029 pix->priv = 0;
1030
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001031 v4l2_dbg(1, debug, sd, "Try FMT: pixelformat - %s, bytesperline - %d"
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001032 "Width - %d, Height - %d",
1033 decoder->fmt_list[ifmt].description, pix->bytesperline,
1034 pix->width, pix->height);
1035 return 0;
1036}
1037
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001038/**
1039 * tvp514x_s_fmt_cap() - V4L2 decoder interface handler for s_fmt
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001040 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001041 * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
1042 *
1043 * If the requested format is supported, configures the HW to use that
1044 * format, returns error code if format not supported or HW can't be
1045 * correctly configured.
1046 */
1047static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001048tvp514x_s_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001049{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001050 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001051 struct v4l2_pix_format *pix;
1052 int rval;
1053
1054 if (f == NULL)
1055 return -EINVAL;
1056
1057 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001058 /* only capture is supported */
1059 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001060
1061 pix = &f->fmt.pix;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001062 rval = tvp514x_try_fmt_cap(sd, f);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001063 if (rval)
1064 return rval;
1065
1066 decoder->pix = *pix;
1067
1068 return rval;
1069}
1070
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001071/**
1072 * tvp514x_g_fmt_cap() - V4L2 decoder interface handler for tvp514x_g_fmt_cap
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001073 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001074 * @f: pointer to standard V4L2 v4l2_format structure
1075 *
1076 * Returns the decoder's current pixel format in the v4l2_format
1077 * parameter.
1078 */
1079static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001080tvp514x_g_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001081{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001082 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001083
1084 if (f == NULL)
1085 return -EINVAL;
1086
1087 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001088 /* only capture is supported */
1089 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001090
1091 f->fmt.pix = decoder->pix;
1092
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001093 v4l2_dbg(1, debug, sd, "Current FMT: bytesperline - %d"
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001094 "Width - %d, Height - %d",
1095 decoder->pix.bytesperline,
1096 decoder->pix.width, decoder->pix.height);
1097 return 0;
1098}
1099
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001100/**
1101 * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001102 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001103 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
1104 *
1105 * Returns the decoder's video CAPTURE parameters.
1106 */
1107static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001108tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001109{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001110 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001111 struct v4l2_captureparm *cparm;
1112 enum tvp514x_std current_std;
1113
1114 if (a == NULL)
1115 return -EINVAL;
1116
1117 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001118 /* only capture is supported */
1119 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001120
1121 memset(a, 0, sizeof(*a));
1122 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1123
1124 /* get the current standard */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001125 current_std = tvp514x_get_current_std(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001126 if (current_std == STD_INVALID)
1127 return -EINVAL;
1128
1129 decoder->current_std = current_std;
1130
1131 cparm = &a->parm.capture;
1132 cparm->capability = V4L2_CAP_TIMEPERFRAME;
1133 cparm->timeperframe =
1134 decoder->std_list[current_std].standard.frameperiod;
1135
1136 return 0;
1137}
1138
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001139/**
1140 * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001141 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001142 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
1143 *
1144 * Configures the decoder to use the input parameters, if possible. If
1145 * not possible, returns the appropriate error code.
1146 */
1147static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001148tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001149{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001150 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001151 struct v4l2_fract *timeperframe;
1152 enum tvp514x_std current_std;
1153
1154 if (a == NULL)
1155 return -EINVAL;
1156
1157 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001158 /* only capture is supported */
1159 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001160
1161 timeperframe = &a->parm.capture.timeperframe;
1162
1163 /* get the current standard */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001164 current_std = tvp514x_get_current_std(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001165 if (current_std == STD_INVALID)
1166 return -EINVAL;
1167
1168 decoder->current_std = current_std;
1169
1170 *timeperframe =
1171 decoder->std_list[current_std].standard.frameperiod;
1172
1173 return 0;
1174}
1175
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001176/**
1177 * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001178 * @sd: pointer to standard V4L2 sub-device structure
1179 * @enable: streaming enable or disable
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001180 *
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001181 * Sets streaming to enable or disable, if possible.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001182 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001183static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001184{
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001185 int err = 0;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001186 struct i2c_client *client = v4l2_get_subdevdata(sd);
1187 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001188
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001189 if (decoder->streaming == enable)
1190 return 0;
1191
1192 switch (enable) {
1193 case 0:
1194 {
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001195 /* Power Down Sequence */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001196 err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
1197 if (err) {
1198 v4l2_err(sd, "Unable to turn off decoder\n");
1199 return err;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001200 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001201 decoder->streaming = enable;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001202 break;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001203 }
1204 case 1:
1205 {
1206 struct tvp514x_reg *int_seq = (struct tvp514x_reg *)
1207 client->driver->id_table->driver_data;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001208
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001209 /* Power Up Sequence */
1210 err = tvp514x_write_regs(sd, int_seq);
1211 if (err) {
1212 v4l2_err(sd, "Unable to turn on decoder\n");
1213 return err;
1214 }
1215 /* Detect if not already detected */
1216 err = tvp514x_detect(sd, decoder);
1217 if (err) {
1218 v4l2_err(sd, "Unable to detect decoder\n");
1219 return err;
1220 }
1221 err = tvp514x_configure(sd, decoder);
1222 if (err) {
1223 v4l2_err(sd, "Unable to configure decoder\n");
1224 return err;
1225 }
1226 decoder->streaming = enable;
1227 break;
1228 }
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001229 default:
1230 err = -ENODEV;
1231 break;
1232 }
1233
1234 return err;
1235}
1236
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001237static const struct v4l2_subdev_core_ops tvp514x_core_ops = {
1238 .queryctrl = tvp514x_queryctrl,
1239 .g_ctrl = tvp514x_g_ctrl,
1240 .s_ctrl = tvp514x_s_ctrl,
1241 .s_std = tvp514x_s_std,
1242};
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001243
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001244static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
1245 .s_routing = tvp514x_s_routing,
1246 .querystd = tvp514x_querystd,
1247 .enum_fmt = tvp514x_enum_fmt_cap,
1248 .g_fmt = tvp514x_g_fmt_cap,
1249 .try_fmt = tvp514x_try_fmt_cap,
1250 .s_fmt = tvp514x_s_fmt_cap,
1251 .g_parm = tvp514x_g_parm,
1252 .s_parm = tvp514x_s_parm,
1253 .s_stream = tvp514x_s_stream,
1254};
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001255
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001256static const struct v4l2_subdev_ops tvp514x_ops = {
1257 .core = &tvp514x_core_ops,
1258 .video = &tvp514x_video_ops,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001259};
1260
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001261static struct tvp514x_decoder tvp514x_dev = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001262 .streaming = 0,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001263
1264 .fmt_list = tvp514x_fmt_list,
1265 .num_fmts = ARRAY_SIZE(tvp514x_fmt_list),
1266
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001267 .pix = {
1268 /* Default to NTSC 8-bit YUV 422 */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001269 .width = NTSC_NUM_ACTIVE_PIXELS,
1270 .height = NTSC_NUM_ACTIVE_LINES,
1271 .pixelformat = V4L2_PIX_FMT_UYVY,
1272 .field = V4L2_FIELD_INTERLACED,
1273 .bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2,
1274 .sizeimage =
1275 NTSC_NUM_ACTIVE_PIXELS * 2 * NTSC_NUM_ACTIVE_LINES,
1276 .colorspace = V4L2_COLORSPACE_SMPTE170M,
1277 },
1278
1279 .current_std = STD_NTSC_MJ,
1280 .std_list = tvp514x_std_list,
1281 .num_stds = ARRAY_SIZE(tvp514x_std_list),
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001282
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001283};
1284
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001285/**
1286 * tvp514x_probe() - decoder driver i2c probe handler
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001287 * @client: i2c driver client device structure
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001288 * @id: i2c driver id table
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001289 *
1290 * Register decoder as an i2c client device and V4L2
1291 * device.
1292 */
1293static int
1294tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1295{
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001296 struct tvp514x_decoder *decoder;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001297 struct v4l2_subdev *sd;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001298
1299 /* Check if the adapter supports the needed features */
1300 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1301 return -EIO;
1302
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001303 if (!client->dev.platform_data) {
1304 v4l2_err(client, "No platform data!!\n");
1305 return -ENODEV;
1306 }
1307
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001308 decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
1309 if (!decoder)
1310 return -ENOMEM;
1311
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001312 /* Initialize the tvp514x_decoder with default configuration */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001313 *decoder = tvp514x_dev;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001314 /* Copy default register configuration */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001315 memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
1316 sizeof(tvp514x_reg_list_default));
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001317
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001318 /* Copy board specific information here */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001319 decoder->pdata = client->dev.platform_data;
1320
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001321 /**
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001322 * Fetch platform specific data, and configure the
1323 * tvp514x_reg_list[] accordingly. Since this is one
1324 * time configuration, no need to preserve.
1325 */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001326 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001327 (decoder->pdata->clk_polarity << 1);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001328 decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001329 ((decoder->pdata->hs_polarity << 2) |
1330 (decoder->pdata->vs_polarity << 3));
1331 /* Set default standard to auto */
1332 decoder->tvp514x_regs[REG_VIDEO_STD].val =
1333 VIDEO_STD_AUTO_SWITCH_BIT;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001334
1335 /* Register with V4L2 layer as slave device */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001336 sd = &decoder->sd;
1337 v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001338
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001339 v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
1340
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001341 return 0;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001342
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001343}
1344
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001345/**
1346 * tvp514x_remove() - decoder driver i2c remove handler
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001347 * @client: i2c driver client device structure
1348 *
1349 * Unregister decoder as an i2c client device and V4L2
1350 * device. Complement of tvp514x_probe().
1351 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001352static int tvp514x_remove(struct i2c_client *client)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001353{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001354 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1355 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001356
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001357 v4l2_device_unregister_subdev(sd);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001358 kfree(decoder);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001359 return 0;
1360}
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001361/* TVP5146 Init/Power on Sequence */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001362static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
1363 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1364 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1365 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1366 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1367 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1368 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1369 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1370 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1371 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1372 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1373 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001374 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001375};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001376
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001377/* TVP5147 Init/Power on Sequence */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001378static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
1379 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1380 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1381 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1382 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1383 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1384 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1385 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1386 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1387 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
1388 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1389 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
1390 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
1391 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1392 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1393 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1394 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
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 Hiremath07b1747c2008-12-05 10:19:36 -03001398};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001399
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001400/* TVP5146M2/TVP5147M1 Init/Power on Sequence */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001401static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1402 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1403 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001404 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001405};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001406
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001407/**
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001408 * I2C Device Table -
1409 *
1410 * name - Name of the actual device/chip.
1411 * driver_data - Driver data
1412 */
1413static const struct i2c_device_id tvp514x_id[] = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001414 {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
1415 {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
1416 {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
1417 {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001418 {},
1419};
1420
1421MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1422
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001423static struct i2c_driver tvp514x_driver = {
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001424 .driver = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001425 .owner = THIS_MODULE,
1426 .name = TVP514X_MODULE_NAME,
1427 },
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001428 .probe = tvp514x_probe,
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001429 .remove = tvp514x_remove,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001430 .id_table = tvp514x_id,
1431};
1432
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001433static int __init tvp514x_init(void)
1434{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001435 return i2c_add_driver(&tvp514x_driver);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001436}
1437
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001438static void __exit tvp514x_exit(void)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001439{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001440 i2c_del_driver(&tvp514x_driver);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001441}
1442
1443module_init(tvp514x_init);
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001444module_exit(tvp514x_exit);