blob: e4815a1806e30e03482c55ca17390f6e4c95ba4c [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030033#include <linux/delay.h>
34#include <linux/videodev2.h>
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030035
36#include <media/v4l2-device.h>
37#include <media/v4l2-common.h>
38#include <media/v4l2-chip-ident.h>
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030039#include <media/tvp514x.h>
40
41#include "tvp514x_regs.h"
42
43/* Module Name */
44#define TVP514X_MODULE_NAME "tvp514x"
45
46/* Private macros for TVP */
47#define I2C_RETRY_COUNT (5)
48#define LOCK_RETRY_COUNT (5)
49#define LOCK_RETRY_DELAY (200)
50
51/* Debug functions */
52static int debug;
53module_param(debug, bool, 0644);
54MODULE_PARM_DESC(debug, "Debug level (0-1)");
55
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030056MODULE_AUTHOR("Texas Instruments");
57MODULE_DESCRIPTION("TVP514X linux decoder driver");
58MODULE_LICENSE("GPL");
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030059
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030060/* enum tvp514x_std - enum for supported standards */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030061enum tvp514x_std {
62 STD_NTSC_MJ = 0,
63 STD_PAL_BDGHIN,
64 STD_INVALID
65};
66
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030067/**
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030068 * struct tvp514x_std_info - Structure to store standard informations
69 * @width: Line width in pixels
70 * @height:Number of active lines
71 * @video_std: Value to write in REG_VIDEO_STD register
72 * @standard: v4l2 standard structure information
73 */
74struct tvp514x_std_info {
75 unsigned long width;
76 unsigned long height;
77 u8 video_std;
78 struct v4l2_standard standard;
79};
80
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030081static struct tvp514x_reg tvp514x_reg_list_default[0x40];
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -030082/**
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030083 * struct tvp514x_decoder - TVP5146/47 decoder object
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030084 * @sd: Subdevice Slave handle
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -030085 * @tvp514x_regs: copy of hw's regs with preset values.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030086 * @pdata: Board specific
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030087 * @ver: Chip version
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -030088 * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -030089 * @pix: Current pixel format
90 * @num_fmts: Number of formats
91 * @fmt_list: Format list
92 * @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 Hiremath07b1747c2008-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 Hiremath07b1747c2008-12-05 10:19:36 -0300101 const struct tvp514x_platform_data *pdata;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300102
103 int ver;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300104 int streaming;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300105
106 struct v4l2_pix_format pix;
107 int num_fmts;
108 const struct v4l2_fmtdesc *fmt_list;
109
110 enum tvp514x_std current_std;
111 int num_stds;
112 struct tvp514x_std_info *std_list;
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300113 /* Input and Output Routing parameters */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300114 u32 input;
115 u32 output;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300116};
117
118/* TVP514x default register values */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300119static struct tvp514x_reg tvp514x_reg_list_default[] = {
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300120 /* Composite selected */
121 {TOK_WRITE, REG_INPUT_SEL, 0x05},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300122 {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300123 /* Auto mode */
124 {TOK_WRITE, REG_VIDEO_STD, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300125 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
126 {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
127 {TOK_WRITE, REG_COLOR_KILLER, 0x10},
128 {TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
129 {TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
130 {TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
131 {TOK_WRITE, REG_BRIGHTNESS, 0x80},
132 {TOK_WRITE, REG_CONTRAST, 0x80},
133 {TOK_WRITE, REG_SATURATION, 0x80},
134 {TOK_WRITE, REG_HUE, 0x00},
135 {TOK_WRITE, REG_CHROMA_CONTROL1, 0x00},
136 {TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300137 /* Reserved */
138 {TOK_SKIP, 0x0F, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300139 {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
140 {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
141 {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300142 /* Reserved */
143 {TOK_SKIP, 0x13, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300144 {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300145 /* Reserved */
146 {TOK_SKIP, 0x15, 0x00},
147 /* NTSC timing */
148 {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300149 {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
150 {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
151 {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300152 /* NTSC timing */
153 {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300154 {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
155 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
156 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300157 /* NTSC timing */
158 {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300159 {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
160 {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
161 {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300162 /* NTSC timing */
163 {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300164 {TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
165 {TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
166 {TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300167 /* Reserved */
168 {TOK_SKIP, 0x26, 0x00},
169 /* Reserved */
170 {TOK_SKIP, 0x27, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300171 {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300172 /* Reserved */
173 {TOK_SKIP, 0x29, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300174 {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300175 /* Reserved */
176 {TOK_SKIP, 0x2B, 0x00},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300177 {TOK_SKIP, REG_SCART_DELAY, 0x00},
178 {TOK_SKIP, REG_CTI_DELAY, 0x00},
179 {TOK_SKIP, REG_CTI_CONTROL, 0x00},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300180 /* Reserved */
181 {TOK_SKIP, 0x2F, 0x00},
182 /* Reserved */
183 {TOK_SKIP, 0x30, 0x00},
184 /* Reserved */
185 {TOK_SKIP, 0x31, 0x00},
186 /* HS, VS active high */
187 {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
188 /* 10-bit BT.656 */
189 {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00},
190 /* Enable clk & data */
191 {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11},
192 /* Enable AVID & FLD */
193 {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE},
194 /* Enable VS & HS */
195 {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300196 {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
197 {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300198 /* Clear status */
199 {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300200 {TOK_TERM, 0, 0},
201};
202
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300203/**
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300204 * List of image formats supported by TVP5146/47 decoder
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300205 * Currently we are using 8 bit mode only, but can be
206 * extended to 10/20 bit mode.
207 */
208static const struct v4l2_fmtdesc tvp514x_fmt_list[] = {
209 {
210 .index = 0,
211 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
212 .flags = 0,
213 .description = "8-bit UYVY 4:2:2 Format",
214 .pixelformat = V4L2_PIX_FMT_UYVY,
215 },
216};
217
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300218/**
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300219 * Supported standards -
220 *
221 * Currently supports two standards only, need to add support for rest of the
222 * modes, like SECAM, etc...
223 */
224static struct tvp514x_std_info tvp514x_std_list[] = {
225 /* Standard: STD_NTSC_MJ */
226 [STD_NTSC_MJ] = {
227 .width = NTSC_NUM_ACTIVE_PIXELS,
228 .height = NTSC_NUM_ACTIVE_LINES,
229 .video_std = VIDEO_STD_NTSC_MJ_BIT,
230 .standard = {
231 .index = 0,
232 .id = V4L2_STD_NTSC,
233 .name = "NTSC",
234 .frameperiod = {1001, 30000},
235 .framelines = 525
236 },
237 /* Standard: STD_PAL_BDGHIN */
238 },
239 [STD_PAL_BDGHIN] = {
240 .width = PAL_NUM_ACTIVE_PIXELS,
241 .height = PAL_NUM_ACTIVE_LINES,
242 .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
243 .standard = {
244 .index = 1,
245 .id = V4L2_STD_PAL,
246 .name = "PAL",
247 .frameperiod = {1, 25},
248 .framelines = 625
249 },
250 },
251 /* Standard: need to add for additional standard */
252};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300253
254
255static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
256{
257 return container_of(sd, struct tvp514x_decoder, sd);
258}
259
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300260
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300261/**
262 * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
263 * @sd: ptr to v4l2_subdev struct
264 * @reg: TVP5146/47 register address
265 *
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300266 * Returns value read if successful, or non-zero (-1) otherwise.
267 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300268static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300269{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300270 int err, retry = 0;
271 struct i2c_client *client = v4l2_get_subdevdata(sd);
272
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300273read_again:
274
275 err = i2c_smbus_read_byte_data(client, reg);
Sebastian Andrzej Siewior6f901a92009-11-04 15:35:09 -0300276 if (err < 0) {
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300277 if (retry <= I2C_RETRY_COUNT) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300278 v4l2_warn(sd, "Read: retry ... %d\n", retry);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300279 retry++;
280 msleep_interruptible(10);
281 goto read_again;
282 }
283 }
284
285 return err;
286}
287
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300288/**
289 * dump_reg() - dump the register content of TVP5146/47.
290 * @sd: ptr to v4l2_subdev struct
291 * @reg: TVP5146/47 register address
292 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300293static void dump_reg(struct v4l2_subdev *sd, u8 reg)
294{
295 u32 val;
296
297 val = tvp514x_read_reg(sd, reg);
298 v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
299}
300
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300301/**
302 * tvp514x_write_reg() - Write a value to a register in TVP5146/47
303 * @sd: ptr to v4l2_subdev struct
304 * @reg: TVP5146/47 register address
305 * @val: value to be written to the register
306 *
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300307 * Write a value to a register in an TVP5146/47 decoder device.
308 * Returns zero if successful, or non-zero otherwise.
309 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300310static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300311{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300312 int err, retry = 0;
313 struct i2c_client *client = v4l2_get_subdevdata(sd);
314
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300315write_again:
316
317 err = i2c_smbus_write_byte_data(client, reg, val);
318 if (err) {
319 if (retry <= I2C_RETRY_COUNT) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300320 v4l2_warn(sd, "Write: retry ... %d\n", retry);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300321 retry++;
322 msleep_interruptible(10);
323 goto write_again;
324 }
325 }
326
327 return err;
328}
329
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300330/**
331 * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
332 * @sd: ptr to v4l2_subdev struct
333 * @reglist: list of TVP5146/47 registers and values
334 *
335 * Initializes a list of TVP5146/47 registers:-
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300336 * if token is TOK_TERM, then entire write operation terminates
337 * if token is TOK_DELAY, then a delay of 'val' msec is introduced
338 * if token is TOK_SKIP, then the register write is skipped
339 * if token is TOK_WRITE, then the register write is performed
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300340 * Returns zero if successful, or non-zero otherwise.
341 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300342static int tvp514x_write_regs(struct v4l2_subdev *sd,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300343 const struct tvp514x_reg reglist[])
344{
345 int err;
346 const struct tvp514x_reg *next = reglist;
347
348 for (; next->token != TOK_TERM; next++) {
349 if (next->token == TOK_DELAY) {
350 msleep(next->val);
351 continue;
352 }
353
354 if (next->token == TOK_SKIP)
355 continue;
356
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300357 err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300358 if (err) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300359 v4l2_err(sd, "Write failed. Err[%d]\n", err);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300360 return err;
361 }
362 }
363 return 0;
364}
365
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300366/**
367 * tvp514x_get_current_std() : Get the current standard detected by TVP5146/47
368 * @sd: ptr to v4l2_subdev struct
369 *
370 * Get current standard detected by TVP5146/47, STD_INVALID if there is no
371 * standard detected.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300372 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300373static enum tvp514x_std tvp514x_get_current_std(struct v4l2_subdev *sd)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300374{
375 u8 std, std_status;
376
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300377 std = tvp514x_read_reg(sd, REG_VIDEO_STD);
378 if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300379 /* use the standard status register */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300380 std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
381 else
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300382 /* use the standard register itself */
383 std_status = std;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300384
385 switch (std_status & VIDEO_STD_MASK) {
386 case VIDEO_STD_NTSC_MJ_BIT:
387 return STD_NTSC_MJ;
388
389 case VIDEO_STD_PAL_BDGHIN_BIT:
390 return STD_PAL_BDGHIN;
391
392 default:
393 return STD_INVALID;
394 }
395
396 return STD_INVALID;
397}
398
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300399/* TVP5146/47 register dump function */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300400static void tvp514x_reg_dump(struct v4l2_subdev *sd)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300401{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300402 dump_reg(sd, REG_INPUT_SEL);
403 dump_reg(sd, REG_AFE_GAIN_CTRL);
404 dump_reg(sd, REG_VIDEO_STD);
405 dump_reg(sd, REG_OPERATION_MODE);
406 dump_reg(sd, REG_COLOR_KILLER);
407 dump_reg(sd, REG_LUMA_CONTROL1);
408 dump_reg(sd, REG_LUMA_CONTROL2);
409 dump_reg(sd, REG_LUMA_CONTROL3);
410 dump_reg(sd, REG_BRIGHTNESS);
411 dump_reg(sd, REG_CONTRAST);
412 dump_reg(sd, REG_SATURATION);
413 dump_reg(sd, REG_HUE);
414 dump_reg(sd, REG_CHROMA_CONTROL1);
415 dump_reg(sd, REG_CHROMA_CONTROL2);
416 dump_reg(sd, REG_COMP_PR_SATURATION);
417 dump_reg(sd, REG_COMP_Y_CONTRAST);
418 dump_reg(sd, REG_COMP_PB_SATURATION);
419 dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
420 dump_reg(sd, REG_AVID_START_PIXEL_LSB);
421 dump_reg(sd, REG_AVID_START_PIXEL_MSB);
422 dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
423 dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
424 dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
425 dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
426 dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
427 dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
428 dump_reg(sd, REG_VSYNC_START_LINE_LSB);
429 dump_reg(sd, REG_VSYNC_START_LINE_MSB);
430 dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
431 dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
432 dump_reg(sd, REG_VBLK_START_LINE_LSB);
433 dump_reg(sd, REG_VBLK_START_LINE_MSB);
434 dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
435 dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
436 dump_reg(sd, REG_SYNC_CONTROL);
437 dump_reg(sd, REG_OUTPUT_FORMATTER1);
438 dump_reg(sd, REG_OUTPUT_FORMATTER2);
439 dump_reg(sd, REG_OUTPUT_FORMATTER3);
440 dump_reg(sd, REG_OUTPUT_FORMATTER4);
441 dump_reg(sd, REG_OUTPUT_FORMATTER5);
442 dump_reg(sd, REG_OUTPUT_FORMATTER6);
443 dump_reg(sd, REG_CLEAR_LOST_LOCK);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300444}
445
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300446/**
447 * tvp514x_configure() - Configure the TVP5146/47 registers
448 * @sd: ptr to v4l2_subdev struct
449 * @decoder: ptr to tvp514x_decoder structure
450 *
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300451 * Returns zero if successful, or non-zero otherwise.
452 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300453static int tvp514x_configure(struct v4l2_subdev *sd,
454 struct tvp514x_decoder *decoder)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300455{
456 int err;
457
458 /* common register initialization */
459 err =
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300460 tvp514x_write_regs(sd, decoder->tvp514x_regs);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300461 if (err)
462 return err;
463
464 if (debug)
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300465 tvp514x_reg_dump(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300466
467 return 0;
468}
469
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300470/**
471 * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
472 * @sd: pointer to standard V4L2 sub-device structure
473 * @decoder: pointer to tvp514x_decoder structure
474 *
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300475 * A device is considered to be detected if the chip ID (LSB and MSB)
476 * registers match the expected values.
477 * Any value of the rom version register is accepted.
478 * Returns ENODEV error number if no device is detected, or zero
479 * if a device is detected.
480 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300481static int tvp514x_detect(struct v4l2_subdev *sd,
482 struct tvp514x_decoder *decoder)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300483{
484 u8 chip_id_msb, chip_id_lsb, rom_ver;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300485 struct i2c_client *client = v4l2_get_subdevdata(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300486
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300487 chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
488 chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
489 rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300490
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300491 v4l2_dbg(1, debug, sd,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300492 "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
493 chip_id_msb, chip_id_lsb, rom_ver);
494 if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
495 || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
496 && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
497 /* We didn't read the values we expected, so this must not be
498 * an TVP5146/47.
499 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300500 v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
501 chip_id_msb, chip_id_lsb);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300502 return -ENODEV;
503 }
504
505 decoder->ver = rom_ver;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300506
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300507 v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
508 client->name, decoder->ver,
509 client->addr << 1, client->adapter->name);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300510 return 0;
511}
512
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300513/**
514 * tvp514x_querystd() - V4L2 decoder interface handler for querystd
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300515 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300516 * @std_id: standard V4L2 std_id ioctl enum
517 *
518 * Returns the current standard detected by TVP5146/47. If no active input is
519 * detected, returns -EINVAL
520 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300521static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300522{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300523 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300524 enum tvp514x_std current_std;
525 enum tvp514x_input input_sel;
526 u8 sync_lock_status, lock_mask;
527
528 if (std_id == NULL)
529 return -EINVAL;
530
531 /* get the current standard */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300532 current_std = tvp514x_get_current_std(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300533 if (current_std == STD_INVALID)
534 return -EINVAL;
535
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300536 input_sel = decoder->input;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300537
538 switch (input_sel) {
539 case INPUT_CVBS_VI1A:
540 case INPUT_CVBS_VI1B:
541 case INPUT_CVBS_VI1C:
542 case INPUT_CVBS_VI2A:
543 case INPUT_CVBS_VI2B:
544 case INPUT_CVBS_VI2C:
545 case INPUT_CVBS_VI3A:
546 case INPUT_CVBS_VI3B:
547 case INPUT_CVBS_VI3C:
548 case INPUT_CVBS_VI4A:
549 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
550 STATUS_HORZ_SYNC_LOCK_BIT |
551 STATUS_VIRT_SYNC_LOCK_BIT;
552 break;
553
554 case INPUT_SVIDEO_VI2A_VI1A:
555 case INPUT_SVIDEO_VI2B_VI1B:
556 case INPUT_SVIDEO_VI2C_VI1C:
557 case INPUT_SVIDEO_VI2A_VI3A:
558 case INPUT_SVIDEO_VI2B_VI3B:
559 case INPUT_SVIDEO_VI2C_VI3C:
560 case INPUT_SVIDEO_VI4A_VI1A:
561 case INPUT_SVIDEO_VI4A_VI1B:
562 case INPUT_SVIDEO_VI4A_VI1C:
563 case INPUT_SVIDEO_VI4A_VI3A:
564 case INPUT_SVIDEO_VI4A_VI3B:
565 case INPUT_SVIDEO_VI4A_VI3C:
566 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
567 STATUS_VIRT_SYNC_LOCK_BIT;
568 break;
569 /*Need to add other interfaces*/
570 default:
571 return -EINVAL;
572 }
573 /* check whether signal is locked */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300574 sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300575 if (lock_mask != (sync_lock_status & lock_mask))
576 return -EINVAL; /* No input detected */
577
578 decoder->current_std = current_std;
579 *std_id = decoder->std_list[current_std].standard.id;
580
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300581 v4l2_dbg(1, debug, sd, "Current STD: %s",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300582 decoder->std_list[current_std].standard.name);
583 return 0;
584}
585
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300586/**
587 * tvp514x_s_std() - V4L2 decoder interface handler for s_std
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300588 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300589 * @std_id: standard V4L2 v4l2_std_id ioctl enum
590 *
591 * If std_id is supported, sets the requested standard. Otherwise, returns
592 * -EINVAL
593 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300594static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300595{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300596 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300597 int err, i;
598
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300599 for (i = 0; i < decoder->num_stds; i++)
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300600 if (std_id & decoder->std_list[i].standard.id)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300601 break;
602
603 if ((i == decoder->num_stds) || (i == STD_INVALID))
604 return -EINVAL;
605
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300606 err = tvp514x_write_reg(sd, REG_VIDEO_STD,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300607 decoder->std_list[i].video_std);
608 if (err)
609 return err;
610
611 decoder->current_std = i;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300612 decoder->tvp514x_regs[REG_VIDEO_STD].val =
613 decoder->std_list[i].video_std;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300614
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300615 v4l2_dbg(1, debug, sd, "Standard set to: %s",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300616 decoder->std_list[i].standard.name);
617 return 0;
618}
619
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300620/**
621 * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300622 * @sd: pointer to standard V4L2 sub-device structure
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300623 * @input: input selector for routing the signal
624 * @output: output selector for routing the signal
625 * @config: config value. Not used
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300626 *
627 * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
628 * the input is not supported or there is no active signal present in the
629 * selected input.
630 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300631static int tvp514x_s_routing(struct v4l2_subdev *sd,
632 u32 input, u32 output, u32 config)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300633{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300634 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300635 int err;
636 enum tvp514x_input input_sel;
637 enum tvp514x_output output_sel;
638 enum tvp514x_std current_std = STD_INVALID;
639 u8 sync_lock_status, lock_mask;
640 int try_count = LOCK_RETRY_COUNT;
641
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300642 if ((input >= INPUT_INVALID) ||
643 (output >= OUTPUT_INVALID))
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300644 /* Index out of bound */
645 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300646
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300647 input_sel = input;
648 output_sel = output;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300649
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300650 err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300651 if (err)
652 return err;
653
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300654 output_sel |= tvp514x_read_reg(sd,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300655 REG_OUTPUT_FORMATTER1) & 0x7;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300656 err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300657 output_sel);
658 if (err)
659 return err;
660
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300661 decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
662 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300663
664 /* Clear status */
665 msleep(LOCK_RETRY_DELAY);
666 err =
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300667 tvp514x_write_reg(sd, REG_CLEAR_LOST_LOCK, 0x01);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300668 if (err)
669 return err;
670
671 switch (input_sel) {
672 case INPUT_CVBS_VI1A:
673 case INPUT_CVBS_VI1B:
674 case INPUT_CVBS_VI1C:
675 case INPUT_CVBS_VI2A:
676 case INPUT_CVBS_VI2B:
677 case INPUT_CVBS_VI2C:
678 case INPUT_CVBS_VI3A:
679 case INPUT_CVBS_VI3B:
680 case INPUT_CVBS_VI3C:
681 case INPUT_CVBS_VI4A:
682 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
683 STATUS_HORZ_SYNC_LOCK_BIT |
684 STATUS_VIRT_SYNC_LOCK_BIT;
685 break;
686
687 case INPUT_SVIDEO_VI2A_VI1A:
688 case INPUT_SVIDEO_VI2B_VI1B:
689 case INPUT_SVIDEO_VI2C_VI1C:
690 case INPUT_SVIDEO_VI2A_VI3A:
691 case INPUT_SVIDEO_VI2B_VI3B:
692 case INPUT_SVIDEO_VI2C_VI3C:
693 case INPUT_SVIDEO_VI4A_VI1A:
694 case INPUT_SVIDEO_VI4A_VI1B:
695 case INPUT_SVIDEO_VI4A_VI1C:
696 case INPUT_SVIDEO_VI4A_VI3A:
697 case INPUT_SVIDEO_VI4A_VI3B:
698 case INPUT_SVIDEO_VI4A_VI3C:
699 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
700 STATUS_VIRT_SYNC_LOCK_BIT;
701 break;
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300702 /* Need to add other interfaces*/
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300703 default:
704 return -EINVAL;
705 }
706
707 while (try_count-- > 0) {
708 /* Allow decoder to sync up with new input */
709 msleep(LOCK_RETRY_DELAY);
710
711 /* get the current standard for future reference */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300712 current_std = tvp514x_get_current_std(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300713 if (current_std == STD_INVALID)
714 continue;
715
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300716 sync_lock_status = tvp514x_read_reg(sd,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300717 REG_STATUS1);
718 if (lock_mask == (sync_lock_status & lock_mask))
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300719 /* Input detected */
720 break;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300721 }
722
Roel Kluin76b08112009-06-11 11:28:17 -0300723 if ((current_std == STD_INVALID) || (try_count < 0))
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300724 return -EINVAL;
725
726 decoder->current_std = current_std;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300727 decoder->input = input;
728 decoder->output = output;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300729
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300730 v4l2_dbg(1, debug, sd, "Input set to: %d, std : %d",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300731 input_sel, current_std);
732
733 return 0;
734}
735
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300736/**
737 * tvp514x_queryctrl() - V4L2 decoder interface handler for queryctrl
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300738 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300739 * @qctrl: standard V4L2 v4l2_queryctrl structure
740 *
741 * If the requested control is supported, returns the control information.
742 * Otherwise, returns -EINVAL if the control is not supported.
743 */
744static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300745tvp514x_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qctrl)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300746{
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300747 int err = -EINVAL;
748
749 if (qctrl == NULL)
750 return err;
751
752 switch (qctrl->id) {
753 case V4L2_CID_BRIGHTNESS:
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300754 /* Brightness supported is (0-255), */
Hans Verkuil10afbef2009-02-21 18:47:24 -0300755 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300756 break;
757 case V4L2_CID_CONTRAST:
758 case V4L2_CID_SATURATION:
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300759 /**
760 * Saturation and Contrast supported is -
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300761 * Contrast: 0 - 255 (Default - 128)
762 * Saturation: 0 - 255 (Default - 128)
763 */
764 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
765 break;
766 case V4L2_CID_HUE:
767 /* Hue Supported is -
768 * Hue - -180 - +180 (Default - 0, Step - +180)
769 */
770 err = v4l2_ctrl_query_fill(qctrl, -180, 180, 180, 0);
771 break;
772 case V4L2_CID_AUTOGAIN:
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300773 /**
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300774 * Auto Gain supported is -
775 * 0 - 1 (Default - 1)
776 */
777 err = v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 1);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300778 break;
779 default:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300780 v4l2_err(sd, "invalid control id %d\n", qctrl->id);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300781 return err;
782 }
783
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300784 v4l2_dbg(1, debug, sd, "Query Control:%s: Min - %d, Max - %d, Def - %d",
785 qctrl->name, qctrl->minimum, qctrl->maximum,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300786 qctrl->default_value);
787
788 return err;
789}
790
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300791/**
792 * tvp514x_g_ctrl() - V4L2 decoder interface handler for g_ctrl
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300793 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300794 * @ctrl: pointer to v4l2_control structure
795 *
796 * If the requested control is supported, returns the control's current
797 * value from the decoder. Otherwise, returns -EINVAL if the control is not
798 * supported.
799 */
800static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300801tvp514x_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300802{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300803 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300804
805 if (ctrl == NULL)
806 return -EINVAL;
807
808 switch (ctrl->id) {
809 case V4L2_CID_BRIGHTNESS:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300810 ctrl->value = decoder->tvp514x_regs[REG_BRIGHTNESS].val;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300811 break;
812 case V4L2_CID_CONTRAST:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300813 ctrl->value = decoder->tvp514x_regs[REG_CONTRAST].val;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300814 break;
815 case V4L2_CID_SATURATION:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300816 ctrl->value = decoder->tvp514x_regs[REG_SATURATION].val;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300817 break;
818 case V4L2_CID_HUE:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300819 ctrl->value = decoder->tvp514x_regs[REG_HUE].val;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300820 if (ctrl->value == 0x7F)
821 ctrl->value = 180;
822 else if (ctrl->value == 0x80)
823 ctrl->value = -180;
824 else
825 ctrl->value = 0;
826
827 break;
828 case V4L2_CID_AUTOGAIN:
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300829 ctrl->value = decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300830 if ((ctrl->value & 0x3) == 3)
831 ctrl->value = 1;
832 else
833 ctrl->value = 0;
834
835 break;
836 default:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300837 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300838 return -EINVAL;
839 }
840
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300841 v4l2_dbg(1, debug, sd, "Get Control: ID - %d - %d",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300842 ctrl->id, ctrl->value);
843 return 0;
844}
845
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300846/**
847 * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300848 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300849 * @ctrl: pointer to v4l2_control structure
850 *
851 * If the requested control is supported, sets the control's current
852 * value in HW. Otherwise, returns -EINVAL if the control is not supported.
853 */
854static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300855tvp514x_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300856{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300857 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300858 int err = -EINVAL, value;
859
860 if (ctrl == NULL)
861 return err;
862
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300863 value = ctrl->value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300864
865 switch (ctrl->id) {
866 case V4L2_CID_BRIGHTNESS:
867 if (ctrl->value < 0 || ctrl->value > 255) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300868 v4l2_err(sd, "invalid brightness setting %d\n",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300869 ctrl->value);
870 return -ERANGE;
871 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300872 err = tvp514x_write_reg(sd, REG_BRIGHTNESS,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300873 value);
874 if (err)
875 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300876
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300877 decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300878 break;
879 case V4L2_CID_CONTRAST:
880 if (ctrl->value < 0 || ctrl->value > 255) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300881 v4l2_err(sd, "invalid contrast setting %d\n",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300882 ctrl->value);
883 return -ERANGE;
884 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300885 err = tvp514x_write_reg(sd, REG_CONTRAST, value);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300886 if (err)
887 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300888
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300889 decoder->tvp514x_regs[REG_CONTRAST].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300890 break;
891 case V4L2_CID_SATURATION:
892 if (ctrl->value < 0 || ctrl->value > 255) {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300893 v4l2_err(sd, "invalid saturation setting %d\n",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300894 ctrl->value);
895 return -ERANGE;
896 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300897 err = tvp514x_write_reg(sd, REG_SATURATION, value);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300898 if (err)
899 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300900
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300901 decoder->tvp514x_regs[REG_SATURATION].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300902 break;
903 case V4L2_CID_HUE:
904 if (value == 180)
905 value = 0x7F;
906 else if (value == -180)
907 value = 0x80;
908 else if (value == 0)
909 value = 0;
910 else {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300911 v4l2_err(sd, "invalid hue setting %d\n", ctrl->value);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300912 return -ERANGE;
913 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300914 err = tvp514x_write_reg(sd, REG_HUE, value);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300915 if (err)
916 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300917
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300918 decoder->tvp514x_regs[REG_HUE].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300919 break;
920 case V4L2_CID_AUTOGAIN:
921 if (value == 1)
922 value = 0x0F;
923 else if (value == 0)
924 value = 0x0C;
925 else {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300926 v4l2_err(sd, "invalid auto gain setting %d\n",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300927 ctrl->value);
928 return -ERANGE;
929 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300930 err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300931 if (err)
932 return err;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300933
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -0300934 decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300935 break;
936 default:
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300937 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300938 return err;
939 }
940
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300941 v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300942 ctrl->id, ctrl->value);
943
944 return err;
945}
946
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300947/**
948 * tvp514x_enum_fmt_cap() - V4L2 decoder interface handler for enum_fmt
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300949 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300950 * @fmt: standard V4L2 VIDIOC_ENUM_FMT ioctl structure
951 *
952 * Implement the VIDIOC_ENUM_FMT ioctl to enumerate supported formats
953 */
954static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300955tvp514x_enum_fmt_cap(struct v4l2_subdev *sd, struct v4l2_fmtdesc *fmt)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300956{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300957 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300958 int index;
959
960 if (fmt == NULL)
961 return -EINVAL;
962
963 index = fmt->index;
964 if ((index >= decoder->num_fmts) || (index < 0))
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300965 /* Index out of bound */
966 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300967
968 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300969 /* only capture is supported */
970 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300971
972 memcpy(fmt, &decoder->fmt_list[index],
973 sizeof(struct v4l2_fmtdesc));
974
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300975 v4l2_dbg(1, debug, sd, "Current FMT: index - %d (%s)",
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300976 decoder->fmt_list[index].index,
977 decoder->fmt_list[index].description);
978 return 0;
979}
980
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -0300981/**
982 * tvp514x_try_fmt_cap() - V4L2 decoder interface handler for try_fmt
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300983 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300984 * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
985 *
986 * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
987 * ioctl is used to negotiate the image capture size and pixel format
988 * without actually making it take effect.
989 */
990static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300991tvp514x_try_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300992{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -0300993 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -0300994 int ifmt;
995 struct v4l2_pix_format *pix;
996 enum tvp514x_std current_std;
997
998 if (f == NULL)
999 return -EINVAL;
1000
1001 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001002 /* only capture is supported */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001003 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1004
1005 pix = &f->fmt.pix;
1006
1007 /* Calculate height and width based on current standard */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001008 current_std = tvp514x_get_current_std(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001009 if (current_std == STD_INVALID)
1010 return -EINVAL;
1011
1012 decoder->current_std = current_std;
1013 pix->width = decoder->std_list[current_std].width;
1014 pix->height = decoder->std_list[current_std].height;
1015
1016 for (ifmt = 0; ifmt < decoder->num_fmts; ifmt++) {
1017 if (pix->pixelformat ==
1018 decoder->fmt_list[ifmt].pixelformat)
1019 break;
1020 }
1021 if (ifmt == decoder->num_fmts)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001022 /* None of the format matched, select default */
1023 ifmt = 0;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001024 pix->pixelformat = decoder->fmt_list[ifmt].pixelformat;
1025
1026 pix->field = V4L2_FIELD_INTERLACED;
1027 pix->bytesperline = pix->width * 2;
1028 pix->sizeimage = pix->bytesperline * pix->height;
1029 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1030 pix->priv = 0;
1031
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001032 v4l2_dbg(1, debug, sd, "Try FMT: pixelformat - %s, bytesperline - %d"
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001033 "Width - %d, Height - %d",
1034 decoder->fmt_list[ifmt].description, pix->bytesperline,
1035 pix->width, pix->height);
1036 return 0;
1037}
1038
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001039/**
1040 * tvp514x_s_fmt_cap() - V4L2 decoder interface handler for s_fmt
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001041 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001042 * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
1043 *
1044 * If the requested format is supported, configures the HW to use that
1045 * format, returns error code if format not supported or HW can't be
1046 * correctly configured.
1047 */
1048static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001049tvp514x_s_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001050{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001051 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001052 struct v4l2_pix_format *pix;
1053 int rval;
1054
1055 if (f == NULL)
1056 return -EINVAL;
1057
1058 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001059 /* only capture is supported */
1060 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001061
1062 pix = &f->fmt.pix;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001063 rval = tvp514x_try_fmt_cap(sd, f);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001064 if (rval)
1065 return rval;
1066
1067 decoder->pix = *pix;
1068
1069 return rval;
1070}
1071
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001072/**
1073 * tvp514x_g_fmt_cap() - V4L2 decoder interface handler for tvp514x_g_fmt_cap
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001074 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001075 * @f: pointer to standard V4L2 v4l2_format structure
1076 *
1077 * Returns the decoder's current pixel format in the v4l2_format
1078 * parameter.
1079 */
1080static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001081tvp514x_g_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001082{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001083 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001084
1085 if (f == NULL)
1086 return -EINVAL;
1087
1088 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001089 /* only capture is supported */
1090 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001091
1092 f->fmt.pix = decoder->pix;
1093
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001094 v4l2_dbg(1, debug, sd, "Current FMT: bytesperline - %d"
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001095 "Width - %d, Height - %d",
1096 decoder->pix.bytesperline,
1097 decoder->pix.width, decoder->pix.height);
1098 return 0;
1099}
1100
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001101/**
1102 * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001103 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001104 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
1105 *
1106 * Returns the decoder's video CAPTURE parameters.
1107 */
1108static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001109tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001110{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001111 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001112 struct v4l2_captureparm *cparm;
1113 enum tvp514x_std current_std;
1114
1115 if (a == NULL)
1116 return -EINVAL;
1117
1118 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001119 /* only capture is supported */
1120 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001121
1122 memset(a, 0, sizeof(*a));
1123 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1124
1125 /* get the current standard */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001126 current_std = tvp514x_get_current_std(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001127 if (current_std == STD_INVALID)
1128 return -EINVAL;
1129
1130 decoder->current_std = current_std;
1131
1132 cparm = &a->parm.capture;
1133 cparm->capability = V4L2_CAP_TIMEPERFRAME;
1134 cparm->timeperframe =
1135 decoder->std_list[current_std].standard.frameperiod;
1136
1137 return 0;
1138}
1139
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001140/**
1141 * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001142 * @sd: pointer to standard V4L2 sub-device structure
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001143 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
1144 *
1145 * Configures the decoder to use the input parameters, if possible. If
1146 * not possible, returns the appropriate error code.
1147 */
1148static int
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001149tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001150{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001151 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001152 struct v4l2_fract *timeperframe;
1153 enum tvp514x_std current_std;
1154
1155 if (a == NULL)
1156 return -EINVAL;
1157
1158 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001159 /* only capture is supported */
1160 return -EINVAL;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001161
1162 timeperframe = &a->parm.capture.timeperframe;
1163
1164 /* get the current standard */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001165 current_std = tvp514x_get_current_std(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001166 if (current_std == STD_INVALID)
1167 return -EINVAL;
1168
1169 decoder->current_std = current_std;
1170
1171 *timeperframe =
1172 decoder->std_list[current_std].standard.frameperiod;
1173
1174 return 0;
1175}
1176
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001177/**
1178 * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001179 * @sd: pointer to standard V4L2 sub-device structure
1180 * @enable: streaming enable or disable
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001181 *
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001182 * Sets streaming to enable or disable, if possible.
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001183 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001184static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001185{
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001186 int err = 0;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001187 struct i2c_client *client = v4l2_get_subdevdata(sd);
1188 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001189
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001190 if (decoder->streaming == enable)
1191 return 0;
1192
1193 switch (enable) {
1194 case 0:
1195 {
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001196 /* Power Down Sequence */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001197 err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
1198 if (err) {
1199 v4l2_err(sd, "Unable to turn off decoder\n");
1200 return err;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001201 }
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001202 decoder->streaming = enable;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001203 break;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001204 }
1205 case 1:
1206 {
1207 struct tvp514x_reg *int_seq = (struct tvp514x_reg *)
1208 client->driver->id_table->driver_data;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001209
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001210 /* Power Up Sequence */
1211 err = tvp514x_write_regs(sd, int_seq);
1212 if (err) {
1213 v4l2_err(sd, "Unable to turn on decoder\n");
1214 return err;
1215 }
1216 /* Detect if not already detected */
1217 err = tvp514x_detect(sd, decoder);
1218 if (err) {
1219 v4l2_err(sd, "Unable to detect decoder\n");
1220 return err;
1221 }
1222 err = tvp514x_configure(sd, decoder);
1223 if (err) {
1224 v4l2_err(sd, "Unable to configure decoder\n");
1225 return err;
1226 }
1227 decoder->streaming = enable;
1228 break;
1229 }
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001230 default:
1231 err = -ENODEV;
1232 break;
1233 }
1234
1235 return err;
1236}
1237
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001238static const struct v4l2_subdev_core_ops tvp514x_core_ops = {
1239 .queryctrl = tvp514x_queryctrl,
1240 .g_ctrl = tvp514x_g_ctrl,
1241 .s_ctrl = tvp514x_s_ctrl,
1242 .s_std = tvp514x_s_std,
1243};
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001244
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001245static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
1246 .s_routing = tvp514x_s_routing,
1247 .querystd = tvp514x_querystd,
1248 .enum_fmt = tvp514x_enum_fmt_cap,
1249 .g_fmt = tvp514x_g_fmt_cap,
1250 .try_fmt = tvp514x_try_fmt_cap,
1251 .s_fmt = tvp514x_s_fmt_cap,
1252 .g_parm = tvp514x_g_parm,
1253 .s_parm = tvp514x_s_parm,
1254 .s_stream = tvp514x_s_stream,
1255};
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001256
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001257static const struct v4l2_subdev_ops tvp514x_ops = {
1258 .core = &tvp514x_core_ops,
1259 .video = &tvp514x_video_ops,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001260};
1261
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001262static struct tvp514x_decoder tvp514x_dev = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001263 .streaming = 0,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001264
1265 .fmt_list = tvp514x_fmt_list,
1266 .num_fmts = ARRAY_SIZE(tvp514x_fmt_list),
1267
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001268 .pix = {
1269 /* Default to NTSC 8-bit YUV 422 */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001270 .width = NTSC_NUM_ACTIVE_PIXELS,
1271 .height = NTSC_NUM_ACTIVE_LINES,
1272 .pixelformat = V4L2_PIX_FMT_UYVY,
1273 .field = V4L2_FIELD_INTERLACED,
1274 .bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2,
1275 .sizeimage =
1276 NTSC_NUM_ACTIVE_PIXELS * 2 * NTSC_NUM_ACTIVE_LINES,
1277 .colorspace = V4L2_COLORSPACE_SMPTE170M,
1278 },
1279
1280 .current_std = STD_NTSC_MJ,
1281 .std_list = tvp514x_std_list,
1282 .num_stds = ARRAY_SIZE(tvp514x_std_list),
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001283
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001284};
1285
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001286/**
1287 * tvp514x_probe() - decoder driver i2c probe handler
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001288 * @client: i2c driver client device structure
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001289 * @id: i2c driver id table
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001290 *
1291 * Register decoder as an i2c client device and V4L2
1292 * device.
1293 */
1294static int
1295tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1296{
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001297 struct tvp514x_decoder *decoder;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001298 struct v4l2_subdev *sd;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001299
1300 /* Check if the adapter supports the needed features */
1301 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1302 return -EIO;
1303
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001304 if (!client->dev.platform_data) {
1305 v4l2_err(client, "No platform data!!\n");
1306 return -ENODEV;
1307 }
1308
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001309 decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
1310 if (!decoder)
1311 return -ENOMEM;
1312
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001313 /* Initialize the tvp514x_decoder with default configuration */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001314 *decoder = tvp514x_dev;
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001315 /* Copy default register configuration */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001316 memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
1317 sizeof(tvp514x_reg_list_default));
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001318
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001319 /* Copy board specific information here */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001320 decoder->pdata = client->dev.platform_data;
1321
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001322 /**
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001323 * Fetch platform specific data, and configure the
1324 * tvp514x_reg_list[] accordingly. Since this is one
1325 * time configuration, no need to preserve.
1326 */
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001327 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001328 (decoder->pdata->clk_polarity << 1);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001329 decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001330 ((decoder->pdata->hs_polarity << 2) |
1331 (decoder->pdata->vs_polarity << 3));
1332 /* Set default standard to auto */
1333 decoder->tvp514x_regs[REG_VIDEO_STD].val =
1334 VIDEO_STD_AUTO_SWITCH_BIT;
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001335
1336 /* Register with V4L2 layer as slave device */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001337 sd = &decoder->sd;
1338 v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001339
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001340 v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
1341
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001342 return 0;
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001343
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001344}
1345
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001346/**
1347 * tvp514x_remove() - decoder driver i2c remove handler
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001348 * @client: i2c driver client device structure
1349 *
1350 * Unregister decoder as an i2c client device and V4L2
1351 * device. Complement of tvp514x_probe().
1352 */
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001353static int tvp514x_remove(struct i2c_client *client)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001354{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001355 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1356 struct tvp514x_decoder *decoder = to_decoder(sd);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001357
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001358 v4l2_device_unregister_subdev(sd);
Sebastian Andrzej Siewior6722e0e2009-01-12 06:17:43 -03001359 kfree(decoder);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001360 return 0;
1361}
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001362/* TVP5146 Init/Power on Sequence */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001363static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
1364 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1365 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1366 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1367 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1368 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1369 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1370 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1371 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1372 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1373 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1374 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001375 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001376};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001377
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001378/* TVP5147 Init/Power on Sequence */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001379static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
1380 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1381 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1382 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1383 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1384 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1385 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1386 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1387 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1388 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
1389 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1390 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
1391 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
1392 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1393 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1394 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1395 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1396 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1397 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001398 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001399};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001400
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001401/* TVP5146M2/TVP5147M1 Init/Power on Sequence */
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001402static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1403 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1404 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001405 {TOK_TERM, 0, 0},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001406};
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001407
Muralidharan Karicheric1c9d092009-07-01 04:20:43 -03001408/**
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001409 * I2C Device Table -
1410 *
1411 * name - Name of the actual device/chip.
1412 * driver_data - Driver data
1413 */
1414static const struct i2c_device_id tvp514x_id[] = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001415 {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
1416 {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
1417 {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
1418 {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001419 {},
1420};
1421
1422MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1423
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001424static struct i2c_driver tvp514x_driver = {
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001425 .driver = {
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001426 .owner = THIS_MODULE,
1427 .name = TVP514X_MODULE_NAME,
1428 },
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001429 .probe = tvp514x_probe,
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001430 .remove = tvp514x_remove,
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001431 .id_table = tvp514x_id,
1432};
1433
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001434static int __init tvp514x_init(void)
1435{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001436 return i2c_add_driver(&tvp514x_driver);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001437}
1438
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001439static void __exit tvp514x_exit(void)
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001440{
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001441 i2c_del_driver(&tvp514x_driver);
Vaibhav Hiremath07b1747c2008-12-05 10:19:36 -03001442}
1443
1444module_init(tvp514x_init);
Muralidharan Karicheri62ef80a2009-06-19 07:13:44 -03001445module_exit(tvp514x_exit);