blob: c0308c16e21fd92c67f502dc37a9f7712187d85e [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>
Hans Verkuil83811912010-05-09 06:50:25 -030038#include <media/v4l2-mediabus.h>
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030039#include <media/v4l2-chip-ident.h>
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030040#include <media/tvp514x.h>
41
42#include "tvp514x_regs.h"
43
44/* Module Name */
45#define TVP514X_MODULE_NAME "tvp514x"
46
47/* Private macros for TVP */
48#define I2C_RETRY_COUNT (5)
49#define LOCK_RETRY_COUNT (5)
50#define LOCK_RETRY_DELAY (200)
51
52/* Debug functions */
53static int debug;
54module_param(debug, bool, 0644);
55MODULE_PARM_DESC(debug, "Debug level (0-1)");
56
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030057MODULE_AUTHOR("Texas Instruments");
58MODULE_DESCRIPTION("TVP514X linux decoder driver");
59MODULE_LICENSE("GPL");
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030060
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030061/* enum tvp514x_std - enum for supported standards */
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030062enum tvp514x_std {
63 STD_NTSC_MJ = 0,
64 STD_PAL_BDGHIN,
65 STD_INVALID
66};
67
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030068/**
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030069 * struct tvp514x_std_info - Structure to store standard informations
70 * @width: Line width in pixels
71 * @height:Number of active lines
72 * @video_std: Value to write in REG_VIDEO_STD register
73 * @standard: v4l2 standard structure information
74 */
75struct tvp514x_std_info {
76 unsigned long width;
77 unsigned long height;
78 u8 video_std;
79 struct v4l2_standard standard;
80};
81
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030082static struct tvp514x_reg tvp514x_reg_list_default[0x40];
Vaibhav Hiremath63b59ce2010-03-27 09:37:54 -030083
84static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable);
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030085/**
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030086 * struct tvp514x_decoder - TVP5146/47 decoder object
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030087 * @sd: Subdevice Slave handle
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030088 * @tvp514x_regs: copy of hw's regs with preset values.
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030089 * @pdata: Board specific
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030090 * @ver: Chip version
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030091 * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030092 * @current_std: Current standard
93 * @num_stds: Number of standards
94 * @std_list: Standards list
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030095 * @input: Input routing at chip level
96 * @output: Output routing at chip level
Vaibhav Hiremath07b17472008-12-05 10:19:36 -030097 */
98struct tvp514x_decoder {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030099 struct v4l2_subdev sd;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300100 struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300101 const struct tvp514x_platform_data *pdata;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300102
103 int ver;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300104 int streaming;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300105
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300106 enum tvp514x_std current_std;
107 int num_stds;
Hans Verkuila75ffc12010-05-09 06:32:47 -0300108 const struct tvp514x_std_info *std_list;
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300109 /* Input and Output Routing parameters */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300110 u32 input;
111 u32 output;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300112};
113
114/* TVP514x default register values */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300115static struct tvp514x_reg tvp514x_reg_list_default[] = {
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300116 /* Composite selected */
117 {TOK_WRITE, REG_INPUT_SEL, 0x05},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300118 {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300119 /* Auto mode */
120 {TOK_WRITE, REG_VIDEO_STD, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300121 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
122 {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
123 {TOK_WRITE, REG_COLOR_KILLER, 0x10},
124 {TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
125 {TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
126 {TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
127 {TOK_WRITE, REG_BRIGHTNESS, 0x80},
128 {TOK_WRITE, REG_CONTRAST, 0x80},
129 {TOK_WRITE, REG_SATURATION, 0x80},
130 {TOK_WRITE, REG_HUE, 0x00},
131 {TOK_WRITE, REG_CHROMA_CONTROL1, 0x00},
132 {TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300133 /* Reserved */
134 {TOK_SKIP, 0x0F, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300135 {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
136 {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
137 {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300138 /* Reserved */
139 {TOK_SKIP, 0x13, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300140 {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300141 /* Reserved */
142 {TOK_SKIP, 0x15, 0x00},
143 /* NTSC timing */
144 {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300145 {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
146 {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
147 {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300148 /* NTSC timing */
149 {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300150 {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
151 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
152 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300153 /* NTSC timing */
154 {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300155 {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
156 {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
157 {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300158 /* NTSC timing */
159 {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300160 {TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
161 {TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
162 {TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300163 /* Reserved */
164 {TOK_SKIP, 0x26, 0x00},
165 /* Reserved */
166 {TOK_SKIP, 0x27, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300167 {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300168 /* Reserved */
169 {TOK_SKIP, 0x29, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300170 {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300171 /* Reserved */
172 {TOK_SKIP, 0x2B, 0x00},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300173 {TOK_SKIP, REG_SCART_DELAY, 0x00},
174 {TOK_SKIP, REG_CTI_DELAY, 0x00},
175 {TOK_SKIP, REG_CTI_CONTROL, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300176 /* Reserved */
177 {TOK_SKIP, 0x2F, 0x00},
178 /* Reserved */
179 {TOK_SKIP, 0x30, 0x00},
180 /* Reserved */
181 {TOK_SKIP, 0x31, 0x00},
182 /* HS, VS active high */
183 {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
184 /* 10-bit BT.656 */
185 {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00},
186 /* Enable clk & data */
187 {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11},
188 /* Enable AVID & FLD */
189 {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE},
190 /* Enable VS & HS */
191 {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300192 {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
193 {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300194 /* Clear status */
195 {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300196 {TOK_TERM, 0, 0},
197};
198
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300199/**
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300200 * Supported standards -
201 *
202 * Currently supports two standards only, need to add support for rest of the
203 * modes, like SECAM, etc...
204 */
Hans Verkuila75ffc12010-05-09 06:32:47 -0300205static const struct tvp514x_std_info tvp514x_std_list[] = {
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300206 /* Standard: STD_NTSC_MJ */
207 [STD_NTSC_MJ] = {
208 .width = NTSC_NUM_ACTIVE_PIXELS,
209 .height = NTSC_NUM_ACTIVE_LINES,
210 .video_std = VIDEO_STD_NTSC_MJ_BIT,
211 .standard = {
212 .index = 0,
213 .id = V4L2_STD_NTSC,
214 .name = "NTSC",
215 .frameperiod = {1001, 30000},
216 .framelines = 525
217 },
218 /* Standard: STD_PAL_BDGHIN */
219 },
220 [STD_PAL_BDGHIN] = {
221 .width = PAL_NUM_ACTIVE_PIXELS,
222 .height = PAL_NUM_ACTIVE_LINES,
223 .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
224 .standard = {
225 .index = 1,
226 .id = V4L2_STD_PAL,
227 .name = "PAL",
228 .frameperiod = {1, 25},
229 .framelines = 625
230 },
231 },
232 /* Standard: need to add for additional standard */
233};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300234
235
236static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
237{
238 return container_of(sd, struct tvp514x_decoder, sd);
239}
240
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300241
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300242/**
243 * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
244 * @sd: ptr to v4l2_subdev struct
245 * @reg: TVP5146/47 register address
246 *
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300247 * Returns value read if successful, or non-zero (-1) otherwise.
248 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300249static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300250{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300251 int err, retry = 0;
252 struct i2c_client *client = v4l2_get_subdevdata(sd);
253
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300254read_again:
255
256 err = i2c_smbus_read_byte_data(client, reg);
Sebastian Andrzej Siewior6f901a92009-11-04 15:35:09 -0300257 if (err < 0) {
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300258 if (retry <= I2C_RETRY_COUNT) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300259 v4l2_warn(sd, "Read: retry ... %d\n", retry);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300260 retry++;
261 msleep_interruptible(10);
262 goto read_again;
263 }
264 }
265
266 return err;
267}
268
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300269/**
270 * dump_reg() - dump the register content of TVP5146/47.
271 * @sd: ptr to v4l2_subdev struct
272 * @reg: TVP5146/47 register address
273 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300274static void dump_reg(struct v4l2_subdev *sd, u8 reg)
275{
276 u32 val;
277
278 val = tvp514x_read_reg(sd, reg);
279 v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
280}
281
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300282/**
283 * tvp514x_write_reg() - Write a value to a register in TVP5146/47
284 * @sd: ptr to v4l2_subdev struct
285 * @reg: TVP5146/47 register address
286 * @val: value to be written to the register
287 *
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300288 * Write a value to a register in an TVP5146/47 decoder device.
289 * Returns zero if successful, or non-zero otherwise.
290 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300291static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300292{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300293 int err, retry = 0;
294 struct i2c_client *client = v4l2_get_subdevdata(sd);
295
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300296write_again:
297
298 err = i2c_smbus_write_byte_data(client, reg, val);
299 if (err) {
300 if (retry <= I2C_RETRY_COUNT) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300301 v4l2_warn(sd, "Write: retry ... %d\n", retry);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300302 retry++;
303 msleep_interruptible(10);
304 goto write_again;
305 }
306 }
307
308 return err;
309}
310
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300311/**
312 * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
313 * @sd: ptr to v4l2_subdev struct
314 * @reglist: list of TVP5146/47 registers and values
315 *
316 * Initializes a list of TVP5146/47 registers:-
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300317 * if token is TOK_TERM, then entire write operation terminates
318 * if token is TOK_DELAY, then a delay of 'val' msec is introduced
319 * if token is TOK_SKIP, then the register write is skipped
320 * if token is TOK_WRITE, then the register write is performed
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300321 * Returns zero if successful, or non-zero otherwise.
322 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300323static int tvp514x_write_regs(struct v4l2_subdev *sd,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300324 const struct tvp514x_reg reglist[])
325{
326 int err;
327 const struct tvp514x_reg *next = reglist;
328
329 for (; next->token != TOK_TERM; next++) {
330 if (next->token == TOK_DELAY) {
331 msleep(next->val);
332 continue;
333 }
334
335 if (next->token == TOK_SKIP)
336 continue;
337
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300338 err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300339 if (err) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300340 v4l2_err(sd, "Write failed. Err[%d]\n", err);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300341 return err;
342 }
343 }
344 return 0;
345}
346
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300347/**
Hans Verkuil2db4e782010-05-09 06:30:15 -0300348 * tvp514x_query_current_std() : Query the current standard detected by TVP5146/47
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300349 * @sd: ptr to v4l2_subdev struct
350 *
Hans Verkuil2db4e782010-05-09 06:30:15 -0300351 * Returns the current standard detected by TVP5146/47, STD_INVALID if there is no
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300352 * standard detected.
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300353 */
Hans Verkuil2db4e782010-05-09 06:30:15 -0300354static enum tvp514x_std tvp514x_query_current_std(struct v4l2_subdev *sd)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300355{
356 u8 std, std_status;
357
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300358 std = tvp514x_read_reg(sd, REG_VIDEO_STD);
359 if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300360 /* use the standard status register */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300361 std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
362 else
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300363 /* use the standard register itself */
364 std_status = std;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300365
366 switch (std_status & VIDEO_STD_MASK) {
367 case VIDEO_STD_NTSC_MJ_BIT:
368 return STD_NTSC_MJ;
369
370 case VIDEO_STD_PAL_BDGHIN_BIT:
371 return STD_PAL_BDGHIN;
372
373 default:
374 return STD_INVALID;
375 }
376
377 return STD_INVALID;
378}
379
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300380/* TVP5146/47 register dump function */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300381static void tvp514x_reg_dump(struct v4l2_subdev *sd)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300382{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300383 dump_reg(sd, REG_INPUT_SEL);
384 dump_reg(sd, REG_AFE_GAIN_CTRL);
385 dump_reg(sd, REG_VIDEO_STD);
386 dump_reg(sd, REG_OPERATION_MODE);
387 dump_reg(sd, REG_COLOR_KILLER);
388 dump_reg(sd, REG_LUMA_CONTROL1);
389 dump_reg(sd, REG_LUMA_CONTROL2);
390 dump_reg(sd, REG_LUMA_CONTROL3);
391 dump_reg(sd, REG_BRIGHTNESS);
392 dump_reg(sd, REG_CONTRAST);
393 dump_reg(sd, REG_SATURATION);
394 dump_reg(sd, REG_HUE);
395 dump_reg(sd, REG_CHROMA_CONTROL1);
396 dump_reg(sd, REG_CHROMA_CONTROL2);
397 dump_reg(sd, REG_COMP_PR_SATURATION);
398 dump_reg(sd, REG_COMP_Y_CONTRAST);
399 dump_reg(sd, REG_COMP_PB_SATURATION);
400 dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
401 dump_reg(sd, REG_AVID_START_PIXEL_LSB);
402 dump_reg(sd, REG_AVID_START_PIXEL_MSB);
403 dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
404 dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
405 dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
406 dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
407 dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
408 dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
409 dump_reg(sd, REG_VSYNC_START_LINE_LSB);
410 dump_reg(sd, REG_VSYNC_START_LINE_MSB);
411 dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
412 dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
413 dump_reg(sd, REG_VBLK_START_LINE_LSB);
414 dump_reg(sd, REG_VBLK_START_LINE_MSB);
415 dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
416 dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
417 dump_reg(sd, REG_SYNC_CONTROL);
418 dump_reg(sd, REG_OUTPUT_FORMATTER1);
419 dump_reg(sd, REG_OUTPUT_FORMATTER2);
420 dump_reg(sd, REG_OUTPUT_FORMATTER3);
421 dump_reg(sd, REG_OUTPUT_FORMATTER4);
422 dump_reg(sd, REG_OUTPUT_FORMATTER5);
423 dump_reg(sd, REG_OUTPUT_FORMATTER6);
424 dump_reg(sd, REG_CLEAR_LOST_LOCK);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300425}
426
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300427/**
428 * tvp514x_configure() - Configure the TVP5146/47 registers
429 * @sd: ptr to v4l2_subdev struct
430 * @decoder: ptr to tvp514x_decoder structure
431 *
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300432 * Returns zero if successful, or non-zero otherwise.
433 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300434static int tvp514x_configure(struct v4l2_subdev *sd,
435 struct tvp514x_decoder *decoder)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300436{
437 int err;
438
439 /* common register initialization */
440 err =
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300441 tvp514x_write_regs(sd, decoder->tvp514x_regs);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300442 if (err)
443 return err;
444
445 if (debug)
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300446 tvp514x_reg_dump(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300447
448 return 0;
449}
450
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300451/**
452 * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
453 * @sd: pointer to standard V4L2 sub-device structure
454 * @decoder: pointer to tvp514x_decoder structure
455 *
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300456 * A device is considered to be detected if the chip ID (LSB and MSB)
457 * registers match the expected values.
458 * Any value of the rom version register is accepted.
459 * Returns ENODEV error number if no device is detected, or zero
460 * if a device is detected.
461 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300462static int tvp514x_detect(struct v4l2_subdev *sd,
463 struct tvp514x_decoder *decoder)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300464{
465 u8 chip_id_msb, chip_id_lsb, rom_ver;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300466 struct i2c_client *client = v4l2_get_subdevdata(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300467
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300468 chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
469 chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
470 rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300471
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300472 v4l2_dbg(1, debug, sd,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300473 "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
474 chip_id_msb, chip_id_lsb, rom_ver);
475 if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
476 || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
477 && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
478 /* We didn't read the values we expected, so this must not be
479 * an TVP5146/47.
480 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300481 v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
482 chip_id_msb, chip_id_lsb);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300483 return -ENODEV;
484 }
485
486 decoder->ver = rom_ver;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300487
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300488 v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
489 client->name, decoder->ver,
490 client->addr << 1, client->adapter->name);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300491 return 0;
492}
493
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300494/**
495 * tvp514x_querystd() - V4L2 decoder interface handler for querystd
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300496 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300497 * @std_id: standard V4L2 std_id ioctl enum
498 *
499 * Returns the current standard detected by TVP5146/47. If no active input is
Hans Verkuil2db4e782010-05-09 06:30:15 -0300500 * detected then *std_id is set to 0 and the function returns 0.
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300501 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300502static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300503{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300504 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300505 enum tvp514x_std current_std;
506 enum tvp514x_input input_sel;
507 u8 sync_lock_status, lock_mask;
508
509 if (std_id == NULL)
510 return -EINVAL;
511
Hans Verkuil2db4e782010-05-09 06:30:15 -0300512 *std_id = V4L2_STD_UNKNOWN;
513
514 /* query the current standard */
515 current_std = tvp514x_query_current_std(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300516 if (current_std == STD_INVALID)
Hans Verkuil2db4e782010-05-09 06:30:15 -0300517 return 0;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300518
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300519 input_sel = decoder->input;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300520
521 switch (input_sel) {
522 case INPUT_CVBS_VI1A:
523 case INPUT_CVBS_VI1B:
524 case INPUT_CVBS_VI1C:
525 case INPUT_CVBS_VI2A:
526 case INPUT_CVBS_VI2B:
527 case INPUT_CVBS_VI2C:
528 case INPUT_CVBS_VI3A:
529 case INPUT_CVBS_VI3B:
530 case INPUT_CVBS_VI3C:
531 case INPUT_CVBS_VI4A:
532 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
533 STATUS_HORZ_SYNC_LOCK_BIT |
534 STATUS_VIRT_SYNC_LOCK_BIT;
535 break;
536
537 case INPUT_SVIDEO_VI2A_VI1A:
538 case INPUT_SVIDEO_VI2B_VI1B:
539 case INPUT_SVIDEO_VI2C_VI1C:
540 case INPUT_SVIDEO_VI2A_VI3A:
541 case INPUT_SVIDEO_VI2B_VI3B:
542 case INPUT_SVIDEO_VI2C_VI3C:
543 case INPUT_SVIDEO_VI4A_VI1A:
544 case INPUT_SVIDEO_VI4A_VI1B:
545 case INPUT_SVIDEO_VI4A_VI1C:
546 case INPUT_SVIDEO_VI4A_VI3A:
547 case INPUT_SVIDEO_VI4A_VI3B:
548 case INPUT_SVIDEO_VI4A_VI3C:
549 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
550 STATUS_VIRT_SYNC_LOCK_BIT;
551 break;
552 /*Need to add other interfaces*/
553 default:
554 return -EINVAL;
555 }
556 /* check whether signal is locked */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300557 sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300558 if (lock_mask != (sync_lock_status & lock_mask))
Hans Verkuil2db4e782010-05-09 06:30:15 -0300559 return 0; /* No input detected */
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300560
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300561 *std_id = decoder->std_list[current_std].standard.id;
562
Hans Verkuil2db4e782010-05-09 06:30:15 -0300563 v4l2_dbg(1, debug, sd, "Current STD: %s\n",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300564 decoder->std_list[current_std].standard.name);
565 return 0;
566}
567
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300568/**
569 * tvp514x_s_std() - V4L2 decoder interface handler for s_std
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300570 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300571 * @std_id: standard V4L2 v4l2_std_id ioctl enum
572 *
573 * If std_id is supported, sets the requested standard. Otherwise, returns
574 * -EINVAL
575 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300576static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300577{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300578 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300579 int err, i;
580
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300581 for (i = 0; i < decoder->num_stds; i++)
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300582 if (std_id & decoder->std_list[i].standard.id)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300583 break;
584
585 if ((i == decoder->num_stds) || (i == STD_INVALID))
586 return -EINVAL;
587
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300588 err = tvp514x_write_reg(sd, REG_VIDEO_STD,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300589 decoder->std_list[i].video_std);
590 if (err)
591 return err;
592
593 decoder->current_std = i;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300594 decoder->tvp514x_regs[REG_VIDEO_STD].val =
595 decoder->std_list[i].video_std;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300596
Hans Verkuil3907b072010-05-09 06:39:44 -0300597 v4l2_dbg(1, debug, sd, "Standard set to: %s\n",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300598 decoder->std_list[i].standard.name);
599 return 0;
600}
601
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300602/**
603 * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300604 * @sd: pointer to standard V4L2 sub-device structure
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300605 * @input: input selector for routing the signal
606 * @output: output selector for routing the signal
607 * @config: config value. Not used
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300608 *
609 * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
610 * the input is not supported or there is no active signal present in the
611 * selected input.
612 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300613static int tvp514x_s_routing(struct v4l2_subdev *sd,
614 u32 input, u32 output, u32 config)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300615{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300616 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300617 int err;
618 enum tvp514x_input input_sel;
619 enum tvp514x_output output_sel;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300620 u8 sync_lock_status, lock_mask;
621 int try_count = LOCK_RETRY_COUNT;
622
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300623 if ((input >= INPUT_INVALID) ||
624 (output >= OUTPUT_INVALID))
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300625 /* Index out of bound */
626 return -EINVAL;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300627
Vaibhav Hiremath63b59ce2010-03-27 09:37:54 -0300628 /*
629 * For the sequence streamon -> streamoff and again s_input
630 * it fails to lock the signal, since streamoff puts TVP514x
631 * into power off state which leads to failure in sub-sequent s_input.
632 *
633 * So power up the TVP514x device here, since it is important to lock
634 * the signal at this stage.
635 */
636 if (!decoder->streaming)
637 tvp514x_s_stream(sd, 1);
638
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300639 input_sel = input;
640 output_sel = output;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300641
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300642 err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300643 if (err)
644 return err;
645
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300646 output_sel |= tvp514x_read_reg(sd,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300647 REG_OUTPUT_FORMATTER1) & 0x7;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300648 err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300649 output_sel);
650 if (err)
651 return err;
652
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300653 decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
654 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300655
656 /* Clear status */
657 msleep(LOCK_RETRY_DELAY);
658 err =
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300659 tvp514x_write_reg(sd, REG_CLEAR_LOST_LOCK, 0x01);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300660 if (err)
661 return err;
662
663 switch (input_sel) {
664 case INPUT_CVBS_VI1A:
665 case INPUT_CVBS_VI1B:
666 case INPUT_CVBS_VI1C:
667 case INPUT_CVBS_VI2A:
668 case INPUT_CVBS_VI2B:
669 case INPUT_CVBS_VI2C:
670 case INPUT_CVBS_VI3A:
671 case INPUT_CVBS_VI3B:
672 case INPUT_CVBS_VI3C:
673 case INPUT_CVBS_VI4A:
674 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
675 STATUS_HORZ_SYNC_LOCK_BIT |
676 STATUS_VIRT_SYNC_LOCK_BIT;
677 break;
678
679 case INPUT_SVIDEO_VI2A_VI1A:
680 case INPUT_SVIDEO_VI2B_VI1B:
681 case INPUT_SVIDEO_VI2C_VI1C:
682 case INPUT_SVIDEO_VI2A_VI3A:
683 case INPUT_SVIDEO_VI2B_VI3B:
684 case INPUT_SVIDEO_VI2C_VI3C:
685 case INPUT_SVIDEO_VI4A_VI1A:
686 case INPUT_SVIDEO_VI4A_VI1B:
687 case INPUT_SVIDEO_VI4A_VI1C:
688 case INPUT_SVIDEO_VI4A_VI3A:
689 case INPUT_SVIDEO_VI4A_VI3B:
690 case INPUT_SVIDEO_VI4A_VI3C:
691 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
692 STATUS_VIRT_SYNC_LOCK_BIT;
693 break;
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300694 /* Need to add other interfaces*/
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300695 default:
696 return -EINVAL;
697 }
698
699 while (try_count-- > 0) {
700 /* Allow decoder to sync up with new input */
701 msleep(LOCK_RETRY_DELAY);
702
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300703 sync_lock_status = tvp514x_read_reg(sd,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300704 REG_STATUS1);
705 if (lock_mask == (sync_lock_status & lock_mask))
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300706 /* Input detected */
707 break;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300708 }
709
Hans Verkuil2db4e782010-05-09 06:30:15 -0300710 if (try_count < 0)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300711 return -EINVAL;
712
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300713 decoder->input = input;
714 decoder->output = output;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300715
Hans Verkuil2db4e782010-05-09 06:30:15 -0300716 v4l2_dbg(1, debug, sd, "Input set to: %d\n", input_sel);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300717
718 return 0;
719}
720
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300721/**
722 * tvp514x_queryctrl() - V4L2 decoder interface handler for queryctrl
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300723 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300724 * @qctrl: standard V4L2 v4l2_queryctrl structure
725 *
726 * If the requested control is supported, returns the control information.
727 * Otherwise, returns -EINVAL if the control is not supported.
728 */
729static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300730tvp514x_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qctrl)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300731{
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300732 int err = -EINVAL;
733
734 if (qctrl == NULL)
735 return err;
736
737 switch (qctrl->id) {
738 case V4L2_CID_BRIGHTNESS:
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300739 /* Brightness supported is (0-255), */
Hans Verkuil10afbef2009-02-21 18:47:24 -0300740 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300741 break;
742 case V4L2_CID_CONTRAST:
743 case V4L2_CID_SATURATION:
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300744 /**
745 * Saturation and Contrast supported is -
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300746 * Contrast: 0 - 255 (Default - 128)
747 * Saturation: 0 - 255 (Default - 128)
748 */
749 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
750 break;
751 case V4L2_CID_HUE:
752 /* Hue Supported is -
753 * Hue - -180 - +180 (Default - 0, Step - +180)
754 */
755 err = v4l2_ctrl_query_fill(qctrl, -180, 180, 180, 0);
756 break;
757 case V4L2_CID_AUTOGAIN:
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300758 /**
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300759 * Auto Gain supported is -
760 * 0 - 1 (Default - 1)
761 */
762 err = v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 1);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300763 break;
764 default:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300765 v4l2_err(sd, "invalid control id %d\n", qctrl->id);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300766 return err;
767 }
768
Hans Verkuil3907b072010-05-09 06:39:44 -0300769 v4l2_dbg(1, debug, sd, "Query Control:%s: Min - %d, Max - %d, Def - %d\n",
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300770 qctrl->name, qctrl->minimum, qctrl->maximum,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300771 qctrl->default_value);
772
773 return err;
774}
775
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300776/**
777 * tvp514x_g_ctrl() - V4L2 decoder interface handler for g_ctrl
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300778 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300779 * @ctrl: pointer to v4l2_control structure
780 *
781 * If the requested control is supported, returns the control's current
782 * value from the decoder. Otherwise, returns -EINVAL if the control is not
783 * supported.
784 */
785static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300786tvp514x_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300787{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300788 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300789
790 if (ctrl == NULL)
791 return -EINVAL;
792
793 switch (ctrl->id) {
794 case V4L2_CID_BRIGHTNESS:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300795 ctrl->value = decoder->tvp514x_regs[REG_BRIGHTNESS].val;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300796 break;
797 case V4L2_CID_CONTRAST:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300798 ctrl->value = decoder->tvp514x_regs[REG_CONTRAST].val;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300799 break;
800 case V4L2_CID_SATURATION:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300801 ctrl->value = decoder->tvp514x_regs[REG_SATURATION].val;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300802 break;
803 case V4L2_CID_HUE:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300804 ctrl->value = decoder->tvp514x_regs[REG_HUE].val;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300805 if (ctrl->value == 0x7F)
806 ctrl->value = 180;
807 else if (ctrl->value == 0x80)
808 ctrl->value = -180;
809 else
810 ctrl->value = 0;
811
812 break;
813 case V4L2_CID_AUTOGAIN:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300814 ctrl->value = decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300815 if ((ctrl->value & 0x3) == 3)
816 ctrl->value = 1;
817 else
818 ctrl->value = 0;
819
820 break;
821 default:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300822 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300823 return -EINVAL;
824 }
825
Hans Verkuil3907b072010-05-09 06:39:44 -0300826 v4l2_dbg(1, debug, sd, "Get Control: ID - %d - %d\n",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300827 ctrl->id, ctrl->value);
828 return 0;
829}
830
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300831/**
832 * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300833 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300834 * @ctrl: pointer to v4l2_control structure
835 *
836 * If the requested control is supported, sets the control's current
837 * value in HW. Otherwise, returns -EINVAL if the control is not supported.
838 */
839static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300840tvp514x_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300841{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300842 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300843 int err = -EINVAL, value;
844
845 if (ctrl == NULL)
846 return err;
847
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300848 value = ctrl->value;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300849
850 switch (ctrl->id) {
851 case V4L2_CID_BRIGHTNESS:
852 if (ctrl->value < 0 || ctrl->value > 255) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300853 v4l2_err(sd, "invalid brightness setting %d\n",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300854 ctrl->value);
855 return -ERANGE;
856 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300857 err = tvp514x_write_reg(sd, REG_BRIGHTNESS,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300858 value);
859 if (err)
860 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300861
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300862 decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300863 break;
864 case V4L2_CID_CONTRAST:
865 if (ctrl->value < 0 || ctrl->value > 255) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300866 v4l2_err(sd, "invalid contrast setting %d\n",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300867 ctrl->value);
868 return -ERANGE;
869 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300870 err = tvp514x_write_reg(sd, REG_CONTRAST, value);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300871 if (err)
872 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300873
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300874 decoder->tvp514x_regs[REG_CONTRAST].val = value;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300875 break;
876 case V4L2_CID_SATURATION:
877 if (ctrl->value < 0 || ctrl->value > 255) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300878 v4l2_err(sd, "invalid saturation setting %d\n",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300879 ctrl->value);
880 return -ERANGE;
881 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300882 err = tvp514x_write_reg(sd, REG_SATURATION, value);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300883 if (err)
884 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300885
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300886 decoder->tvp514x_regs[REG_SATURATION].val = value;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300887 break;
888 case V4L2_CID_HUE:
889 if (value == 180)
890 value = 0x7F;
891 else if (value == -180)
892 value = 0x80;
893 else if (value == 0)
894 value = 0;
895 else {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300896 v4l2_err(sd, "invalid hue setting %d\n", ctrl->value);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300897 return -ERANGE;
898 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300899 err = tvp514x_write_reg(sd, REG_HUE, value);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300900 if (err)
901 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300902
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300903 decoder->tvp514x_regs[REG_HUE].val = value;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300904 break;
905 case V4L2_CID_AUTOGAIN:
906 if (value == 1)
907 value = 0x0F;
908 else if (value == 0)
909 value = 0x0C;
910 else {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300911 v4l2_err(sd, "invalid auto gain setting %d\n",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300912 ctrl->value);
913 return -ERANGE;
914 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300915 err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300916 if (err)
917 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300918
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300919 decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300920 break;
921 default:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300922 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300923 return err;
924 }
925
Hans Verkuil3907b072010-05-09 06:39:44 -0300926 v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d\n",
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300927 ctrl->id, ctrl->value);
928
929 return err;
930}
931
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300932/**
Hans Verkuil83811912010-05-09 06:50:25 -0300933 * tvp514x_enum_mbus_fmt() - V4L2 decoder interface handler for enum_mbus_fmt
934 * @sd: pointer to standard V4L2 sub-device structure
935 * @index: index of pixelcode to retrieve
936 * @code: receives the pixelcode
937 *
938 * Enumerates supported mediabus formats
939 */
940static int
941tvp514x_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index,
942 enum v4l2_mbus_pixelcode *code)
943{
944 if (index)
945 return -EINVAL;
946
947 *code = V4L2_MBUS_FMT_YUYV10_2X10;
948 return 0;
949}
950
951/**
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300952 * tvp514x_enum_fmt_cap() - V4L2 decoder interface handler for enum_fmt
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300953 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300954 * @fmt: standard V4L2 VIDIOC_ENUM_FMT ioctl structure
955 *
956 * Implement the VIDIOC_ENUM_FMT ioctl to enumerate supported formats
957 */
958static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300959tvp514x_enum_fmt_cap(struct v4l2_subdev *sd, struct v4l2_fmtdesc *fmt)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300960{
Hans Verkuilc2fc8092010-05-09 06:37:14 -0300961 if (fmt == NULL || fmt->index)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300962 return -EINVAL;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300963
964 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300965 /* only capture is supported */
966 return -EINVAL;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300967
Hans Verkuilc2fc8092010-05-09 06:37:14 -0300968 /* only one format */
969 fmt->flags = 0;
970 strlcpy(fmt->description, "8-bit UYVY 4:2:2 Format",
971 sizeof(fmt->description));
972 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -0300973 return 0;
974}
975
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300976/**
Hans Verkuil83811912010-05-09 06:50:25 -0300977 * tvp514x_mbus_fmt_cap() - V4L2 decoder interface handler for try/s/g_mbus_fmt
978 * @sd: pointer to standard V4L2 sub-device structure
979 * @f: pointer to the mediabus format structure
980 *
981 * Negotiates the image capture size and mediabus format.
982 */
983static int
984tvp514x_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f)
985{
986 struct tvp514x_decoder *decoder = to_decoder(sd);
987 enum tvp514x_std current_std;
988
989 if (f == NULL)
990 return -EINVAL;
991
992 /* Calculate height and width based on current standard */
993 current_std = decoder->current_std;
994
995 f->code = V4L2_MBUS_FMT_YUYV10_2X10;
996 f->width = decoder->std_list[current_std].width;
997 f->height = decoder->std_list[current_std].height;
998 f->field = V4L2_FIELD_INTERLACED;
999 f->colorspace = V4L2_COLORSPACE_SMPTE170M;
1000
1001 v4l2_dbg(1, debug, sd, "MBUS_FMT: Width - %d, Height - %d\n",
1002 f->width, f->height);
1003 return 0;
1004}
1005
1006/**
Hans Verkuil283d6372010-05-09 06:44:16 -03001007 * tvp514x_fmt_cap() - V4L2 decoder interface handler for try/s/g_fmt
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001008 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001009 * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
1010 *
Hans Verkuil283d6372010-05-09 06:44:16 -03001011 * Implement the VIDIOC_TRY/S/G_FMT ioctl for the CAPTURE buffer type. This
1012 * ioctl is used to negotiate the image capture size and pixel format.
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001013 */
1014static int
Hans Verkuil283d6372010-05-09 06:44:16 -03001015tvp514x_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001016{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001017 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001018 struct v4l2_pix_format *pix;
1019 enum tvp514x_std current_std;
1020
1021 if (f == NULL)
1022 return -EINVAL;
1023
1024 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Hans Verkuil283d6372010-05-09 06:44:16 -03001025 return -EINVAL;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001026
1027 pix = &f->fmt.pix;
1028
1029 /* Calculate height and width based on current standard */
Hans Verkuil2db4e782010-05-09 06:30:15 -03001030 current_std = decoder->current_std;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001031
Hans Verkuilc2fc8092010-05-09 06:37:14 -03001032 pix->pixelformat = V4L2_PIX_FMT_UYVY;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001033 pix->width = decoder->std_list[current_std].width;
1034 pix->height = decoder->std_list[current_std].height;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001035 pix->field = V4L2_FIELD_INTERLACED;
1036 pix->bytesperline = pix->width * 2;
1037 pix->sizeimage = pix->bytesperline * pix->height;
1038 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1039 pix->priv = 0;
1040
Hans Verkuil283d6372010-05-09 06:44:16 -03001041 v4l2_dbg(1, debug, sd, "FMT: bytesperline - %d"
Hans Verkuil3907b072010-05-09 06:39:44 -03001042 "Width - %d, Height - %d\n",
Hans Verkuilc2fc8092010-05-09 06:37:14 -03001043 pix->bytesperline,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001044 pix->width, pix->height);
1045 return 0;
1046}
1047
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001048/**
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001049 * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001050 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001051 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
1052 *
1053 * Returns the decoder's video CAPTURE parameters.
1054 */
1055static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001056tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001057{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001058 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001059 struct v4l2_captureparm *cparm;
1060 enum tvp514x_std current_std;
1061
1062 if (a == NULL)
1063 return -EINVAL;
1064
1065 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001066 /* only capture is supported */
1067 return -EINVAL;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001068
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001069 /* get the current standard */
Hans Verkuil2db4e782010-05-09 06:30:15 -03001070 current_std = decoder->current_std;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001071
1072 cparm = &a->parm.capture;
1073 cparm->capability = V4L2_CAP_TIMEPERFRAME;
1074 cparm->timeperframe =
1075 decoder->std_list[current_std].standard.frameperiod;
1076
1077 return 0;
1078}
1079
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001080/**
1081 * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001082 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001083 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
1084 *
1085 * Configures the decoder to use the input parameters, if possible. If
1086 * not possible, returns the appropriate error code.
1087 */
1088static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001089tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001090{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001091 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001092 struct v4l2_fract *timeperframe;
1093 enum tvp514x_std current_std;
1094
1095 if (a == NULL)
1096 return -EINVAL;
1097
1098 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001099 /* only capture is supported */
1100 return -EINVAL;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001101
1102 timeperframe = &a->parm.capture.timeperframe;
1103
1104 /* get the current standard */
Hans Verkuil2db4e782010-05-09 06:30:15 -03001105 current_std = decoder->current_std;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001106
1107 *timeperframe =
1108 decoder->std_list[current_std].standard.frameperiod;
1109
1110 return 0;
1111}
1112
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001113/**
1114 * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001115 * @sd: pointer to standard V4L2 sub-device structure
1116 * @enable: streaming enable or disable
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001117 *
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001118 * Sets streaming to enable or disable, if possible.
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001119 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001120static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001121{
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001122 int err = 0;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001123 struct i2c_client *client = v4l2_get_subdevdata(sd);
1124 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001125
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001126 if (decoder->streaming == enable)
1127 return 0;
1128
1129 switch (enable) {
1130 case 0:
1131 {
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001132 /* Power Down Sequence */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001133 err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
1134 if (err) {
1135 v4l2_err(sd, "Unable to turn off decoder\n");
1136 return err;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001137 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001138 decoder->streaming = enable;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001139 break;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001140 }
1141 case 1:
1142 {
1143 struct tvp514x_reg *int_seq = (struct tvp514x_reg *)
1144 client->driver->id_table->driver_data;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001145
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001146 /* Power Up Sequence */
1147 err = tvp514x_write_regs(sd, int_seq);
1148 if (err) {
1149 v4l2_err(sd, "Unable to turn on decoder\n");
1150 return err;
1151 }
1152 /* Detect if not already detected */
1153 err = tvp514x_detect(sd, decoder);
1154 if (err) {
1155 v4l2_err(sd, "Unable to detect decoder\n");
1156 return err;
1157 }
1158 err = tvp514x_configure(sd, decoder);
1159 if (err) {
1160 v4l2_err(sd, "Unable to configure decoder\n");
1161 return err;
1162 }
1163 decoder->streaming = enable;
1164 break;
1165 }
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001166 default:
1167 err = -ENODEV;
1168 break;
1169 }
1170
1171 return err;
1172}
1173
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001174static const struct v4l2_subdev_core_ops tvp514x_core_ops = {
1175 .queryctrl = tvp514x_queryctrl,
1176 .g_ctrl = tvp514x_g_ctrl,
1177 .s_ctrl = tvp514x_s_ctrl,
1178 .s_std = tvp514x_s_std,
1179};
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001180
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001181static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
1182 .s_routing = tvp514x_s_routing,
1183 .querystd = tvp514x_querystd,
1184 .enum_fmt = tvp514x_enum_fmt_cap,
Hans Verkuil283d6372010-05-09 06:44:16 -03001185 .g_fmt = tvp514x_fmt_cap,
1186 .try_fmt = tvp514x_fmt_cap,
1187 .s_fmt = tvp514x_fmt_cap,
Hans Verkuil83811912010-05-09 06:50:25 -03001188 .enum_mbus_fmt = tvp514x_enum_mbus_fmt,
1189 .g_mbus_fmt = tvp514x_mbus_fmt,
1190 .try_mbus_fmt = tvp514x_mbus_fmt,
1191 .s_mbus_fmt = tvp514x_mbus_fmt,
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001192 .g_parm = tvp514x_g_parm,
1193 .s_parm = tvp514x_s_parm,
1194 .s_stream = tvp514x_s_stream,
1195};
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001196
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001197static const struct v4l2_subdev_ops tvp514x_ops = {
1198 .core = &tvp514x_core_ops,
1199 .video = &tvp514x_video_ops,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001200};
1201
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001202static struct tvp514x_decoder tvp514x_dev = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001203 .streaming = 0,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001204 .current_std = STD_NTSC_MJ,
1205 .std_list = tvp514x_std_list,
1206 .num_stds = ARRAY_SIZE(tvp514x_std_list),
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001207
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001208};
1209
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001210/**
1211 * tvp514x_probe() - decoder driver i2c probe handler
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001212 * @client: i2c driver client device structure
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001213 * @id: i2c driver id table
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001214 *
1215 * Register decoder as an i2c client device and V4L2
1216 * device.
1217 */
1218static int
1219tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1220{
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001221 struct tvp514x_decoder *decoder;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001222 struct v4l2_subdev *sd;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001223
1224 /* Check if the adapter supports the needed features */
1225 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1226 return -EIO;
1227
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001228 if (!client->dev.platform_data) {
1229 v4l2_err(client, "No platform data!!\n");
1230 return -ENODEV;
1231 }
1232
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001233 decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
1234 if (!decoder)
1235 return -ENOMEM;
1236
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001237 /* Initialize the tvp514x_decoder with default configuration */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001238 *decoder = tvp514x_dev;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001239 /* Copy default register configuration */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001240 memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
1241 sizeof(tvp514x_reg_list_default));
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001242
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001243 /* Copy board specific information here */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001244 decoder->pdata = client->dev.platform_data;
1245
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001246 /**
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001247 * Fetch platform specific data, and configure the
1248 * tvp514x_reg_list[] accordingly. Since this is one
1249 * time configuration, no need to preserve.
1250 */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001251 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001252 (decoder->pdata->clk_polarity << 1);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001253 decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001254 ((decoder->pdata->hs_polarity << 2) |
1255 (decoder->pdata->vs_polarity << 3));
1256 /* Set default standard to auto */
1257 decoder->tvp514x_regs[REG_VIDEO_STD].val =
1258 VIDEO_STD_AUTO_SWITCH_BIT;
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001259
1260 /* Register with V4L2 layer as slave device */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001261 sd = &decoder->sd;
1262 v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001263
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001264 v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
1265
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001266 return 0;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001267
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001268}
1269
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001270/**
1271 * tvp514x_remove() - decoder driver i2c remove handler
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001272 * @client: i2c driver client device structure
1273 *
1274 * Unregister decoder as an i2c client device and V4L2
1275 * device. Complement of tvp514x_probe().
1276 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001277static int tvp514x_remove(struct i2c_client *client)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001278{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001279 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1280 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001281
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001282 v4l2_device_unregister_subdev(sd);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001283 kfree(decoder);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001284 return 0;
1285}
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001286/* TVP5146 Init/Power on Sequence */
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001287static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
1288 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1289 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1290 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1291 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1292 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1293 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1294 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1295 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1296 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1297 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1298 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001299 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001300};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001301
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001302/* TVP5147 Init/Power on Sequence */
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001303static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
1304 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1305 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1306 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1307 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1308 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1309 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1310 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1311 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1312 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
1313 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1314 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
1315 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
1316 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1317 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1318 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1319 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1320 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1321 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001322 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001323};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001324
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001325/* TVP5146M2/TVP5147M1 Init/Power on Sequence */
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001326static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1327 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1328 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001329 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001330};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001331
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001332/**
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001333 * I2C Device Table -
1334 *
1335 * name - Name of the actual device/chip.
1336 * driver_data - Driver data
1337 */
1338static const struct i2c_device_id tvp514x_id[] = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001339 {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
1340 {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
1341 {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
1342 {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001343 {},
1344};
1345
1346MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1347
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001348static struct i2c_driver tvp514x_driver = {
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001349 .driver = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001350 .owner = THIS_MODULE,
1351 .name = TVP514X_MODULE_NAME,
1352 },
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001353 .probe = tvp514x_probe,
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001354 .remove = tvp514x_remove,
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001355 .id_table = tvp514x_id,
1356};
1357
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001358static int __init tvp514x_init(void)
1359{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001360 return i2c_add_driver(&tvp514x_driver);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001361}
1362
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001363static void __exit tvp514x_exit(void)
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001364{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001365 i2c_del_driver(&tvp514x_driver);
Vaibhav Hiremath07b17472008-12-05 10:19:36 -03001366}
1367
1368module_init(tvp514x_init);
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001369module_exit(tvp514x_exit);