]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/video/tvp514x.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[net-next-2.6.git] / drivers / media / video / tvp514x.c
CommitLineData
07b1747c
VH
1/*
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>
5a0e3ad6 32#include <linux/slab.h>
07b1747c
VH
33#include <linux/delay.h>
34#include <linux/videodev2.h>
62ef80a1
MK
35
36#include <media/v4l2-device.h>
37#include <media/v4l2-common.h>
38#include <media/v4l2-chip-ident.h>
07b1747c
VH
39#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
62ef80a1
MK
56MODULE_AUTHOR("Texas Instruments");
57MODULE_DESCRIPTION("TVP514X linux decoder driver");
58MODULE_LICENSE("GPL");
07b1747c 59
c1c9d09c 60/* enum tvp514x_std - enum for supported standards */
07b1747c
VH
61enum tvp514x_std {
62 STD_NTSC_MJ = 0,
63 STD_PAL_BDGHIN,
64 STD_INVALID
65};
66
c1c9d09c 67/**
07b1747c
VH
68 * 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
6722e0ef 81static struct tvp514x_reg tvp514x_reg_list_default[0x40];
63b59cec
VH
82
83static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable);
c1c9d09c 84/**
6722e0ef 85 * struct tvp514x_decoder - TVP5146/47 decoder object
62ef80a1 86 * @sd: Subdevice Slave handle
6722e0ef 87 * @tvp514x_regs: copy of hw's regs with preset values.
07b1747c 88 * @pdata: Board specific
07b1747c 89 * @ver: Chip version
62ef80a1 90 * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
07b1747c
VH
91 * @current_std: Current standard
92 * @num_stds: Number of standards
93 * @std_list: Standards list
62ef80a1
MK
94 * @input: Input routing at chip level
95 * @output: Output routing at chip level
07b1747c
VH
96 */
97struct tvp514x_decoder {
62ef80a1 98 struct v4l2_subdev sd;
6722e0ef 99 struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
07b1747c 100 const struct tvp514x_platform_data *pdata;
07b1747c
VH
101
102 int ver;
62ef80a1 103 int streaming;
07b1747c 104
07b1747c
VH
105 enum tvp514x_std current_std;
106 int num_stds;
a75ffc12 107 const struct tvp514x_std_info *std_list;
c1c9d09c 108 /* Input and Output Routing parameters */
62ef80a1
MK
109 u32 input;
110 u32 output;
07b1747c
VH
111};
112
113/* TVP514x default register values */
6722e0ef 114static struct tvp514x_reg tvp514x_reg_list_default[] = {
c1c9d09c
MK
115 /* Composite selected */
116 {TOK_WRITE, REG_INPUT_SEL, 0x05},
07b1747c 117 {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
c1c9d09c
MK
118 /* Auto mode */
119 {TOK_WRITE, REG_VIDEO_STD, 0x00},
07b1747c
VH
120 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
121 {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
122 {TOK_WRITE, REG_COLOR_KILLER, 0x10},
123 {TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
124 {TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
125 {TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
126 {TOK_WRITE, REG_BRIGHTNESS, 0x80},
127 {TOK_WRITE, REG_CONTRAST, 0x80},
128 {TOK_WRITE, REG_SATURATION, 0x80},
129 {TOK_WRITE, REG_HUE, 0x00},
130 {TOK_WRITE, REG_CHROMA_CONTROL1, 0x00},
131 {TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E},
c1c9d09c
MK
132 /* Reserved */
133 {TOK_SKIP, 0x0F, 0x00},
07b1747c
VH
134 {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
135 {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
136 {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
c1c9d09c
MK
137 /* Reserved */
138 {TOK_SKIP, 0x13, 0x00},
07b1747c 139 {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
c1c9d09c
MK
140 /* Reserved */
141 {TOK_SKIP, 0x15, 0x00},
142 /* NTSC timing */
143 {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
07b1747c
VH
144 {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
145 {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
146 {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
c1c9d09c
MK
147 /* NTSC timing */
148 {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
07b1747c
VH
149 {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
150 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
151 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
c1c9d09c
MK
152 /* NTSC timing */
153 {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
07b1747c
VH
154 {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
155 {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
156 {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
c1c9d09c
MK
157 /* NTSC timing */
158 {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
07b1747c
VH
159 {TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
160 {TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
161 {TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
c1c9d09c
MK
162 /* Reserved */
163 {TOK_SKIP, 0x26, 0x00},
164 /* Reserved */
165 {TOK_SKIP, 0x27, 0x00},
07b1747c 166 {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
c1c9d09c
MK
167 /* Reserved */
168 {TOK_SKIP, 0x29, 0x00},
07b1747c 169 {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
c1c9d09c
MK
170 /* Reserved */
171 {TOK_SKIP, 0x2B, 0x00},
07b1747c
VH
172 {TOK_SKIP, REG_SCART_DELAY, 0x00},
173 {TOK_SKIP, REG_CTI_DELAY, 0x00},
174 {TOK_SKIP, REG_CTI_CONTROL, 0x00},
c1c9d09c
MK
175 /* Reserved */
176 {TOK_SKIP, 0x2F, 0x00},
177 /* Reserved */
178 {TOK_SKIP, 0x30, 0x00},
179 /* Reserved */
180 {TOK_SKIP, 0x31, 0x00},
181 /* HS, VS active high */
182 {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
183 /* 10-bit BT.656 */
184 {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00},
185 /* Enable clk & data */
186 {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11},
187 /* Enable AVID & FLD */
188 {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE},
189 /* Enable VS & HS */
190 {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
07b1747c
VH
191 {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
192 {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
c1c9d09c
MK
193 /* Clear status */
194 {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
07b1747c
VH
195 {TOK_TERM, 0, 0},
196};
197
c1c9d09c 198/**
07b1747c
VH
199 * Supported standards -
200 *
201 * Currently supports two standards only, need to add support for rest of the
202 * modes, like SECAM, etc...
203 */
a75ffc12 204static const struct tvp514x_std_info tvp514x_std_list[] = {
07b1747c
VH
205 /* Standard: STD_NTSC_MJ */
206 [STD_NTSC_MJ] = {
207 .width = NTSC_NUM_ACTIVE_PIXELS,
208 .height = NTSC_NUM_ACTIVE_LINES,
209 .video_std = VIDEO_STD_NTSC_MJ_BIT,
210 .standard = {
211 .index = 0,
212 .id = V4L2_STD_NTSC,
213 .name = "NTSC",
214 .frameperiod = {1001, 30000},
215 .framelines = 525
216 },
217 /* Standard: STD_PAL_BDGHIN */
218 },
219 [STD_PAL_BDGHIN] = {
220 .width = PAL_NUM_ACTIVE_PIXELS,
221 .height = PAL_NUM_ACTIVE_LINES,
222 .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
223 .standard = {
224 .index = 1,
225 .id = V4L2_STD_PAL,
226 .name = "PAL",
227 .frameperiod = {1, 25},
228 .framelines = 625
229 },
230 },
231 /* Standard: need to add for additional standard */
232};
62ef80a1
MK
233
234
235static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
236{
237 return container_of(sd, struct tvp514x_decoder, sd);
238}
239
07b1747c 240
c1c9d09c
MK
241/**
242 * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
243 * @sd: ptr to v4l2_subdev struct
244 * @reg: TVP5146/47 register address
245 *
07b1747c
VH
246 * Returns value read if successful, or non-zero (-1) otherwise.
247 */
62ef80a1 248static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
07b1747c 249{
62ef80a1
MK
250 int err, retry = 0;
251 struct i2c_client *client = v4l2_get_subdevdata(sd);
252
07b1747c
VH
253read_again:
254
255 err = i2c_smbus_read_byte_data(client, reg);
6f901a99 256 if (err < 0) {
07b1747c 257 if (retry <= I2C_RETRY_COUNT) {
62ef80a1 258 v4l2_warn(sd, "Read: retry ... %d\n", retry);
07b1747c
VH
259 retry++;
260 msleep_interruptible(10);
261 goto read_again;
262 }
263 }
264
265 return err;
266}
267
c1c9d09c
MK
268/**
269 * dump_reg() - dump the register content of TVP5146/47.
270 * @sd: ptr to v4l2_subdev struct
271 * @reg: TVP5146/47 register address
272 */
62ef80a1
MK
273static void dump_reg(struct v4l2_subdev *sd, u8 reg)
274{
275 u32 val;
276
277 val = tvp514x_read_reg(sd, reg);
278 v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
279}
280
c1c9d09c
MK
281/**
282 * tvp514x_write_reg() - Write a value to a register in TVP5146/47
283 * @sd: ptr to v4l2_subdev struct
284 * @reg: TVP5146/47 register address
285 * @val: value to be written to the register
286 *
07b1747c
VH
287 * Write a value to a register in an TVP5146/47 decoder device.
288 * Returns zero if successful, or non-zero otherwise.
289 */
62ef80a1 290static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
07b1747c 291{
62ef80a1
MK
292 int err, retry = 0;
293 struct i2c_client *client = v4l2_get_subdevdata(sd);
294
07b1747c
VH
295write_again:
296
297 err = i2c_smbus_write_byte_data(client, reg, val);
298 if (err) {
299 if (retry <= I2C_RETRY_COUNT) {
62ef80a1 300 v4l2_warn(sd, "Write: retry ... %d\n", retry);
07b1747c
VH
301 retry++;
302 msleep_interruptible(10);
303 goto write_again;
304 }
305 }
306
307 return err;
308}
309
c1c9d09c
MK
310/**
311 * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
312 * @sd: ptr to v4l2_subdev struct
313 * @reglist: list of TVP5146/47 registers and values
314 *
315 * Initializes a list of TVP5146/47 registers:-
07b1747c
VH
316 * if token is TOK_TERM, then entire write operation terminates
317 * if token is TOK_DELAY, then a delay of 'val' msec is introduced
318 * if token is TOK_SKIP, then the register write is skipped
319 * if token is TOK_WRITE, then the register write is performed
07b1747c
VH
320 * Returns zero if successful, or non-zero otherwise.
321 */
62ef80a1 322static int tvp514x_write_regs(struct v4l2_subdev *sd,
07b1747c
VH
323 const struct tvp514x_reg reglist[])
324{
325 int err;
326 const struct tvp514x_reg *next = reglist;
327
328 for (; next->token != TOK_TERM; next++) {
329 if (next->token == TOK_DELAY) {
330 msleep(next->val);
331 continue;
332 }
333
334 if (next->token == TOK_SKIP)
335 continue;
336
62ef80a1 337 err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
07b1747c 338 if (err) {
62ef80a1 339 v4l2_err(sd, "Write failed. Err[%d]\n", err);
07b1747c
VH
340 return err;
341 }
342 }
343 return 0;
344}
345
c1c9d09c 346/**
2db4e78f 347 * tvp514x_query_current_std() : Query the current standard detected by TVP5146/47
c1c9d09c
MK
348 * @sd: ptr to v4l2_subdev struct
349 *
2db4e78f 350 * Returns the current standard detected by TVP5146/47, STD_INVALID if there is no
c1c9d09c 351 * standard detected.
07b1747c 352 */
2db4e78f 353static enum tvp514x_std tvp514x_query_current_std(struct v4l2_subdev *sd)
07b1747c
VH
354{
355 u8 std, std_status;
356
62ef80a1
MK
357 std = tvp514x_read_reg(sd, REG_VIDEO_STD);
358 if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
07b1747c 359 /* use the standard status register */
62ef80a1
MK
360 std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
361 else
c1c9d09c
MK
362 /* use the standard register itself */
363 std_status = std;
07b1747c
VH
364
365 switch (std_status & VIDEO_STD_MASK) {
366 case VIDEO_STD_NTSC_MJ_BIT:
367 return STD_NTSC_MJ;
368
369 case VIDEO_STD_PAL_BDGHIN_BIT:
370 return STD_PAL_BDGHIN;
371
372 default:
373 return STD_INVALID;
374 }
375
376 return STD_INVALID;
377}
378
c1c9d09c 379/* TVP5146/47 register dump function */
62ef80a1 380static void tvp514x_reg_dump(struct v4l2_subdev *sd)
07b1747c 381{
62ef80a1
MK
382 dump_reg(sd, REG_INPUT_SEL);
383 dump_reg(sd, REG_AFE_GAIN_CTRL);
384 dump_reg(sd, REG_VIDEO_STD);
385 dump_reg(sd, REG_OPERATION_MODE);
386 dump_reg(sd, REG_COLOR_KILLER);
387 dump_reg(sd, REG_LUMA_CONTROL1);
388 dump_reg(sd, REG_LUMA_CONTROL2);
389 dump_reg(sd, REG_LUMA_CONTROL3);
390 dump_reg(sd, REG_BRIGHTNESS);
391 dump_reg(sd, REG_CONTRAST);
392 dump_reg(sd, REG_SATURATION);
393 dump_reg(sd, REG_HUE);
394 dump_reg(sd, REG_CHROMA_CONTROL1);
395 dump_reg(sd, REG_CHROMA_CONTROL2);
396 dump_reg(sd, REG_COMP_PR_SATURATION);
397 dump_reg(sd, REG_COMP_Y_CONTRAST);
398 dump_reg(sd, REG_COMP_PB_SATURATION);
399 dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
400 dump_reg(sd, REG_AVID_START_PIXEL_LSB);
401 dump_reg(sd, REG_AVID_START_PIXEL_MSB);
402 dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
403 dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
404 dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
405 dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
406 dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
407 dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
408 dump_reg(sd, REG_VSYNC_START_LINE_LSB);
409 dump_reg(sd, REG_VSYNC_START_LINE_MSB);
410 dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
411 dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
412 dump_reg(sd, REG_VBLK_START_LINE_LSB);
413 dump_reg(sd, REG_VBLK_START_LINE_MSB);
414 dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
415 dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
416 dump_reg(sd, REG_SYNC_CONTROL);
417 dump_reg(sd, REG_OUTPUT_FORMATTER1);
418 dump_reg(sd, REG_OUTPUT_FORMATTER2);
419 dump_reg(sd, REG_OUTPUT_FORMATTER3);
420 dump_reg(sd, REG_OUTPUT_FORMATTER4);
421 dump_reg(sd, REG_OUTPUT_FORMATTER5);
422 dump_reg(sd, REG_OUTPUT_FORMATTER6);
423 dump_reg(sd, REG_CLEAR_LOST_LOCK);
07b1747c
VH
424}
425
c1c9d09c
MK
426/**
427 * tvp514x_configure() - Configure the TVP5146/47 registers
428 * @sd: ptr to v4l2_subdev struct
429 * @decoder: ptr to tvp514x_decoder structure
430 *
07b1747c
VH
431 * Returns zero if successful, or non-zero otherwise.
432 */
62ef80a1
MK
433static int tvp514x_configure(struct v4l2_subdev *sd,
434 struct tvp514x_decoder *decoder)
07b1747c
VH
435{
436 int err;
437
438 /* common register initialization */
439 err =
62ef80a1 440 tvp514x_write_regs(sd, decoder->tvp514x_regs);
07b1747c
VH
441 if (err)
442 return err;
443
444 if (debug)
62ef80a1 445 tvp514x_reg_dump(sd);
07b1747c
VH
446
447 return 0;
448}
449
c1c9d09c
MK
450/**
451 * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
452 * @sd: pointer to standard V4L2 sub-device structure
453 * @decoder: pointer to tvp514x_decoder structure
454 *
07b1747c
VH
455 * A device is considered to be detected if the chip ID (LSB and MSB)
456 * registers match the expected values.
457 * Any value of the rom version register is accepted.
458 * Returns ENODEV error number if no device is detected, or zero
459 * if a device is detected.
460 */
62ef80a1
MK
461static int tvp514x_detect(struct v4l2_subdev *sd,
462 struct tvp514x_decoder *decoder)
07b1747c
VH
463{
464 u8 chip_id_msb, chip_id_lsb, rom_ver;
62ef80a1 465 struct i2c_client *client = v4l2_get_subdevdata(sd);
07b1747c 466
62ef80a1
MK
467 chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
468 chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
469 rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
07b1747c 470
62ef80a1 471 v4l2_dbg(1, debug, sd,
07b1747c
VH
472 "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
473 chip_id_msb, chip_id_lsb, rom_ver);
474 if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
475 || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
476 && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
477 /* We didn't read the values we expected, so this must not be
478 * an TVP5146/47.
479 */
62ef80a1
MK
480 v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
481 chip_id_msb, chip_id_lsb);
07b1747c
VH
482 return -ENODEV;
483 }
484
485 decoder->ver = rom_ver;
07b1747c 486
62ef80a1
MK
487 v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
488 client->name, decoder->ver,
489 client->addr << 1, client->adapter->name);
07b1747c
VH
490 return 0;
491}
492
c1c9d09c
MK
493/**
494 * tvp514x_querystd() - V4L2 decoder interface handler for querystd
62ef80a1 495 * @sd: pointer to standard V4L2 sub-device structure
07b1747c
VH
496 * @std_id: standard V4L2 std_id ioctl enum
497 *
498 * Returns the current standard detected by TVP5146/47. If no active input is
2db4e78f 499 * detected then *std_id is set to 0 and the function returns 0.
07b1747c 500 */
62ef80a1 501static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
07b1747c 502{
62ef80a1 503 struct tvp514x_decoder *decoder = to_decoder(sd);
07b1747c
VH
504 enum tvp514x_std current_std;
505 enum tvp514x_input input_sel;
506 u8 sync_lock_status, lock_mask;
507
508 if (std_id == NULL)
509 return -EINVAL;
510
2db4e78f
HV
511 *std_id = V4L2_STD_UNKNOWN;
512
513 /* query the current standard */
514 current_std = tvp514x_query_current_std(sd);
07b1747c 515 if (current_std == STD_INVALID)
2db4e78f 516 return 0;
07b1747c 517
62ef80a1 518 input_sel = decoder->input;
07b1747c
VH
519
520 switch (input_sel) {
521 case INPUT_CVBS_VI1A:
522 case INPUT_CVBS_VI1B:
523 case INPUT_CVBS_VI1C:
524 case INPUT_CVBS_VI2A:
525 case INPUT_CVBS_VI2B:
526 case INPUT_CVBS_VI2C:
527 case INPUT_CVBS_VI3A:
528 case INPUT_CVBS_VI3B:
529 case INPUT_CVBS_VI3C:
530 case INPUT_CVBS_VI4A:
531 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
532 STATUS_HORZ_SYNC_LOCK_BIT |
533 STATUS_VIRT_SYNC_LOCK_BIT;
534 break;
535
536 case INPUT_SVIDEO_VI2A_VI1A:
537 case INPUT_SVIDEO_VI2B_VI1B:
538 case INPUT_SVIDEO_VI2C_VI1C:
539 case INPUT_SVIDEO_VI2A_VI3A:
540 case INPUT_SVIDEO_VI2B_VI3B:
541 case INPUT_SVIDEO_VI2C_VI3C:
542 case INPUT_SVIDEO_VI4A_VI1A:
543 case INPUT_SVIDEO_VI4A_VI1B:
544 case INPUT_SVIDEO_VI4A_VI1C:
545 case INPUT_SVIDEO_VI4A_VI3A:
546 case INPUT_SVIDEO_VI4A_VI3B:
547 case INPUT_SVIDEO_VI4A_VI3C:
548 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
549 STATUS_VIRT_SYNC_LOCK_BIT;
550 break;
551 /*Need to add other interfaces*/
552 default:
553 return -EINVAL;
554 }
555 /* check whether signal is locked */
62ef80a1 556 sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
07b1747c 557 if (lock_mask != (sync_lock_status & lock_mask))
2db4e78f 558 return 0; /* No input detected */
07b1747c 559
07b1747c
VH
560 *std_id = decoder->std_list[current_std].standard.id;
561
2db4e78f 562 v4l2_dbg(1, debug, sd, "Current STD: %s\n",
07b1747c
VH
563 decoder->std_list[current_std].standard.name);
564 return 0;
565}
566
c1c9d09c
MK
567/**
568 * tvp514x_s_std() - V4L2 decoder interface handler for s_std
62ef80a1 569 * @sd: pointer to standard V4L2 sub-device structure
07b1747c
VH
570 * @std_id: standard V4L2 v4l2_std_id ioctl enum
571 *
572 * If std_id is supported, sets the requested standard. Otherwise, returns
573 * -EINVAL
574 */
62ef80a1 575static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
07b1747c 576{
62ef80a1 577 struct tvp514x_decoder *decoder = to_decoder(sd);
07b1747c
VH
578 int err, i;
579
07b1747c 580 for (i = 0; i < decoder->num_stds; i++)
62ef80a1 581 if (std_id & decoder->std_list[i].standard.id)
07b1747c
VH
582 break;
583
584 if ((i == decoder->num_stds) || (i == STD_INVALID))
585 return -EINVAL;
586
62ef80a1 587 err = tvp514x_write_reg(sd, REG_VIDEO_STD,
07b1747c
VH
588 decoder->std_list[i].video_std);
589 if (err)
590 return err;
591
592 decoder->current_std = i;
6722e0ef
SAS
593 decoder->tvp514x_regs[REG_VIDEO_STD].val =
594 decoder->std_list[i].video_std;
07b1747c 595
3907b072 596 v4l2_dbg(1, debug, sd, "Standard set to: %s\n",
07b1747c
VH
597 decoder->std_list[i].standard.name);
598 return 0;
599}
600
c1c9d09c
MK
601/**
602 * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
62ef80a1 603 * @sd: pointer to standard V4L2 sub-device structure
c1c9d09c
MK
604 * @input: input selector for routing the signal
605 * @output: output selector for routing the signal
606 * @config: config value. Not used
07b1747c
VH
607 *
608 * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
609 * the input is not supported or there is no active signal present in the
610 * selected input.
611 */
62ef80a1
MK
612static int tvp514x_s_routing(struct v4l2_subdev *sd,
613 u32 input, u32 output, u32 config)
07b1747c 614{
62ef80a1 615 struct tvp514x_decoder *decoder = to_decoder(sd);
07b1747c
VH
616 int err;
617 enum tvp514x_input input_sel;
618 enum tvp514x_output output_sel;
07b1747c
VH
619 u8 sync_lock_status, lock_mask;
620 int try_count = LOCK_RETRY_COUNT;
621
62ef80a1
MK
622 if ((input >= INPUT_INVALID) ||
623 (output >= OUTPUT_INVALID))
c1c9d09c
MK
624 /* Index out of bound */
625 return -EINVAL;
07b1747c 626
63b59cec
VH
627 /*
628 * For the sequence streamon -> streamoff and again s_input
629 * it fails to lock the signal, since streamoff puts TVP514x
630 * into power off state which leads to failure in sub-sequent s_input.
631 *
632 * So power up the TVP514x device here, since it is important to lock
633 * the signal at this stage.
634 */
635 if (!decoder->streaming)
636 tvp514x_s_stream(sd, 1);
637
62ef80a1
MK
638 input_sel = input;
639 output_sel = output;
07b1747c 640
62ef80a1 641 err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
07b1747c
VH
642 if (err)
643 return err;
644
62ef80a1 645 output_sel |= tvp514x_read_reg(sd,
07b1747c 646 REG_OUTPUT_FORMATTER1) & 0x7;
62ef80a1 647 err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
07b1747c
VH
648 output_sel);
649 if (err)
650 return err;
651
6722e0ef
SAS
652 decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
653 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
07b1747c
VH
654
655 /* Clear status */
656 msleep(LOCK_RETRY_DELAY);
657 err =
62ef80a1 658 tvp514x_write_reg(sd, REG_CLEAR_LOST_LOCK, 0x01);
07b1747c
VH
659 if (err)
660 return err;
661
662 switch (input_sel) {
663 case INPUT_CVBS_VI1A:
664 case INPUT_CVBS_VI1B:
665 case INPUT_CVBS_VI1C:
666 case INPUT_CVBS_VI2A:
667 case INPUT_CVBS_VI2B:
668 case INPUT_CVBS_VI2C:
669 case INPUT_CVBS_VI3A:
670 case INPUT_CVBS_VI3B:
671 case INPUT_CVBS_VI3C:
672 case INPUT_CVBS_VI4A:
673 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
674 STATUS_HORZ_SYNC_LOCK_BIT |
675 STATUS_VIRT_SYNC_LOCK_BIT;
676 break;
677
678 case INPUT_SVIDEO_VI2A_VI1A:
679 case INPUT_SVIDEO_VI2B_VI1B:
680 case INPUT_SVIDEO_VI2C_VI1C:
681 case INPUT_SVIDEO_VI2A_VI3A:
682 case INPUT_SVIDEO_VI2B_VI3B:
683 case INPUT_SVIDEO_VI2C_VI3C:
684 case INPUT_SVIDEO_VI4A_VI1A:
685 case INPUT_SVIDEO_VI4A_VI1B:
686 case INPUT_SVIDEO_VI4A_VI1C:
687 case INPUT_SVIDEO_VI4A_VI3A:
688 case INPUT_SVIDEO_VI4A_VI3B:
689 case INPUT_SVIDEO_VI4A_VI3C:
690 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
691 STATUS_VIRT_SYNC_LOCK_BIT;
692 break;
c1c9d09c 693 /* Need to add other interfaces*/
07b1747c
VH
694 default:
695 return -EINVAL;
696 }
697
698 while (try_count-- > 0) {
699 /* Allow decoder to sync up with new input */
700 msleep(LOCK_RETRY_DELAY);
701
62ef80a1 702 sync_lock_status = tvp514x_read_reg(sd,
07b1747c
VH
703 REG_STATUS1);
704 if (lock_mask == (sync_lock_status & lock_mask))
c1c9d09c
MK
705 /* Input detected */
706 break;
07b1747c
VH
707 }
708
2db4e78f 709 if (try_count < 0)
07b1747c
VH
710 return -EINVAL;
711
62ef80a1
MK
712 decoder->input = input;
713 decoder->output = output;
07b1747c 714
2db4e78f 715 v4l2_dbg(1, debug, sd, "Input set to: %d\n", input_sel);
07b1747c
VH
716
717 return 0;
718}
719
c1c9d09c
MK
720/**
721 * tvp514x_queryctrl() - V4L2 decoder interface handler for queryctrl
62ef80a1 722 * @sd: pointer to standard V4L2 sub-device structure
07b1747c
VH
723 * @qctrl: standard V4L2 v4l2_queryctrl structure
724 *
725 * If the requested control is supported, returns the control information.
726 * Otherwise, returns -EINVAL if the control is not supported.
727 */
728static int
62ef80a1 729tvp514x_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qctrl)
07b1747c 730{
07b1747c
VH
731 int err = -EINVAL;
732
733 if (qctrl == NULL)
734 return err;
735
736 switch (qctrl->id) {
737 case V4L2_CID_BRIGHTNESS:
c1c9d09c 738 /* Brightness supported is (0-255), */
10afbef1 739 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
07b1747c
VH
740 break;
741 case V4L2_CID_CONTRAST:
742 case V4L2_CID_SATURATION:
c1c9d09c
MK
743 /**
744 * Saturation and Contrast supported is -
07b1747c
VH
745 * Contrast: 0 - 255 (Default - 128)
746 * Saturation: 0 - 255 (Default - 128)
747 */
748 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
749 break;
750 case V4L2_CID_HUE:
751 /* Hue Supported is -
752 * Hue - -180 - +180 (Default - 0, Step - +180)
753 */
754 err = v4l2_ctrl_query_fill(qctrl, -180, 180, 180, 0);
755 break;
756 case V4L2_CID_AUTOGAIN:
c1c9d09c 757 /**
62ef80a1
MK
758 * Auto Gain supported is -
759 * 0 - 1 (Default - 1)
760 */
761 err = v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 1);
07b1747c
VH
762 break;
763 default:
62ef80a1 764 v4l2_err(sd, "invalid control id %d\n", qctrl->id);
07b1747c
VH
765 return err;
766 }
767
3907b072 768 v4l2_dbg(1, debug, sd, "Query Control:%s: Min - %d, Max - %d, Def - %d\n",
62ef80a1 769 qctrl->name, qctrl->minimum, qctrl->maximum,
07b1747c
VH
770 qctrl->default_value);
771
772 return err;
773}
774
c1c9d09c
MK
775/**
776 * tvp514x_g_ctrl() - V4L2 decoder interface handler for g_ctrl
62ef80a1 777 * @sd: pointer to standard V4L2 sub-device structure
07b1747c
VH
778 * @ctrl: pointer to v4l2_control structure
779 *
780 * If the requested control is supported, returns the control's current
781 * value from the decoder. Otherwise, returns -EINVAL if the control is not
782 * supported.
783 */
784static int
62ef80a1 785tvp514x_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
07b1747c 786{
62ef80a1 787 struct tvp514x_decoder *decoder = to_decoder(sd);
07b1747c
VH
788
789 if (ctrl == NULL)
790 return -EINVAL;
791
792 switch (ctrl->id) {
793 case V4L2_CID_BRIGHTNESS:
6722e0ef 794 ctrl->value = decoder->tvp514x_regs[REG_BRIGHTNESS].val;
07b1747c
VH
795 break;
796 case V4L2_CID_CONTRAST:
6722e0ef 797 ctrl->value = decoder->tvp514x_regs[REG_CONTRAST].val;
07b1747c
VH
798 break;
799 case V4L2_CID_SATURATION:
6722e0ef 800 ctrl->value = decoder->tvp514x_regs[REG_SATURATION].val;
07b1747c
VH
801 break;
802 case V4L2_CID_HUE:
6722e0ef 803 ctrl->value = decoder->tvp514x_regs[REG_HUE].val;
07b1747c
VH
804 if (ctrl->value == 0x7F)
805 ctrl->value = 180;
806 else if (ctrl->value == 0x80)
807 ctrl->value = -180;
808 else
809 ctrl->value = 0;
810
811 break;
812 case V4L2_CID_AUTOGAIN:
6722e0ef 813 ctrl->value = decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val;
07b1747c
VH
814 if ((ctrl->value & 0x3) == 3)
815 ctrl->value = 1;
816 else
817 ctrl->value = 0;
818
819 break;
820 default:
62ef80a1 821 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
07b1747c
VH
822 return -EINVAL;
823 }
824
3907b072 825 v4l2_dbg(1, debug, sd, "Get Control: ID - %d - %d\n",
07b1747c
VH
826 ctrl->id, ctrl->value);
827 return 0;
828}
829
c1c9d09c
MK
830/**
831 * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
62ef80a1 832 * @sd: pointer to standard V4L2 sub-device structure
07b1747c
VH
833 * @ctrl: pointer to v4l2_control structure
834 *
835 * If the requested control is supported, sets the control's current
836 * value in HW. Otherwise, returns -EINVAL if the control is not supported.
837 */
838static int
62ef80a1 839tvp514x_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
07b1747c 840{
62ef80a1 841 struct tvp514x_decoder *decoder = to_decoder(sd);
07b1747c
VH
842 int err = -EINVAL, value;
843
844 if (ctrl == NULL)
845 return err;
846
62ef80a1 847 value = ctrl->value;
07b1747c
VH
848
849 switch (ctrl->id) {
850 case V4L2_CID_BRIGHTNESS:
851 if (ctrl->value < 0 || ctrl->value > 255) {
62ef80a1 852 v4l2_err(sd, "invalid brightness setting %d\n",
07b1747c
VH
853 ctrl->value);
854 return -ERANGE;
855 }
62ef80a1 856 err = tvp514x_write_reg(sd, REG_BRIGHTNESS,
07b1747c
VH
857 value);
858 if (err)
859 return err;
62ef80a1 860
6722e0ef 861 decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
07b1747c
VH
862 break;
863 case V4L2_CID_CONTRAST:
864 if (ctrl->value < 0 || ctrl->value > 255) {
62ef80a1 865 v4l2_err(sd, "invalid contrast setting %d\n",
07b1747c
VH
866 ctrl->value);
867 return -ERANGE;
868 }
62ef80a1 869 err = tvp514x_write_reg(sd, REG_CONTRAST, value);
07b1747c
VH
870 if (err)
871 return err;
62ef80a1 872
6722e0ef 873 decoder->tvp514x_regs[REG_CONTRAST].val = value;
07b1747c
VH
874 break;
875 case V4L2_CID_SATURATION:
876 if (ctrl->value < 0 || ctrl->value > 255) {
62ef80a1 877 v4l2_err(sd, "invalid saturation setting %d\n",
07b1747c
VH
878 ctrl->value);
879 return -ERANGE;
880 }
62ef80a1 881 err = tvp514x_write_reg(sd, REG_SATURATION, value);
07b1747c
VH
882 if (err)
883 return err;
62ef80a1 884
6722e0ef 885 decoder->tvp514x_regs[REG_SATURATION].val = value;
07b1747c
VH
886 break;
887 case V4L2_CID_HUE:
888 if (value == 180)
889 value = 0x7F;
890 else if (value == -180)
891 value = 0x80;
892 else if (value == 0)
893 value = 0;
894 else {
62ef80a1 895 v4l2_err(sd, "invalid hue setting %d\n", ctrl->value);
07b1747c
VH
896 return -ERANGE;
897 }
62ef80a1 898 err = tvp514x_write_reg(sd, REG_HUE, value);
07b1747c
VH
899 if (err)
900 return err;
62ef80a1 901
6722e0ef 902 decoder->tvp514x_regs[REG_HUE].val = value;
07b1747c
VH
903 break;
904 case V4L2_CID_AUTOGAIN:
905 if (value == 1)
906 value = 0x0F;
907 else if (value == 0)
908 value = 0x0C;
909 else {
62ef80a1 910 v4l2_err(sd, "invalid auto gain setting %d\n",
07b1747c
VH
911 ctrl->value);
912 return -ERANGE;
913 }
62ef80a1 914 err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value);
07b1747c
VH
915 if (err)
916 return err;
62ef80a1 917
6722e0ef 918 decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
07b1747c
VH
919 break;
920 default:
62ef80a1 921 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
07b1747c
VH
922 return err;
923 }
924
3907b072 925 v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d\n",
07b1747c
VH
926 ctrl->id, ctrl->value);
927
928 return err;
929}
930
c1c9d09c
MK
931/**
932 * tvp514x_enum_fmt_cap() - V4L2 decoder interface handler for enum_fmt
62ef80a1 933 * @sd: pointer to standard V4L2 sub-device structure
07b1747c
VH
934 * @fmt: standard V4L2 VIDIOC_ENUM_FMT ioctl structure
935 *
936 * Implement the VIDIOC_ENUM_FMT ioctl to enumerate supported formats
937 */
938static int
62ef80a1 939tvp514x_enum_fmt_cap(struct v4l2_subdev *sd, struct v4l2_fmtdesc *fmt)
07b1747c 940{
c2fc8096 941 if (fmt == NULL || fmt->index)
c1c9d09c 942 return -EINVAL;
07b1747c
VH
943
944 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
c1c9d09c
MK
945 /* only capture is supported */
946 return -EINVAL;
07b1747c 947
c2fc8096
HV
948 /* only one format */
949 fmt->flags = 0;
950 strlcpy(fmt->description, "8-bit UYVY 4:2:2 Format",
951 sizeof(fmt->description));
952 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
07b1747c
VH
953 return 0;
954}
955
c1c9d09c 956/**
283d637b 957 * tvp514x_fmt_cap() - V4L2 decoder interface handler for try/s/g_fmt
62ef80a1 958 * @sd: pointer to standard V4L2 sub-device structure
07b1747c
VH
959 * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
960 *
283d637b
HV
961 * Implement the VIDIOC_TRY/S/G_FMT ioctl for the CAPTURE buffer type. This
962 * ioctl is used to negotiate the image capture size and pixel format.
07b1747c
VH
963 */
964static int
283d637b 965tvp514x_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
07b1747c 966{
62ef80a1 967 struct tvp514x_decoder *decoder = to_decoder(sd);
07b1747c
VH
968 struct v4l2_pix_format *pix;
969 enum tvp514x_std current_std;
970
971 if (f == NULL)
972 return -EINVAL;
973
974 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
283d637b 975 return -EINVAL;
07b1747c
VH
976
977 pix = &f->fmt.pix;
978
979 /* Calculate height and width based on current standard */
2db4e78f 980 current_std = decoder->current_std;
07b1747c 981
c2fc8096 982 pix->pixelformat = V4L2_PIX_FMT_UYVY;
07b1747c
VH
983 pix->width = decoder->std_list[current_std].width;
984 pix->height = decoder->std_list[current_std].height;
07b1747c
VH
985 pix->field = V4L2_FIELD_INTERLACED;
986 pix->bytesperline = pix->width * 2;
987 pix->sizeimage = pix->bytesperline * pix->height;
988 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
989 pix->priv = 0;
990
283d637b 991 v4l2_dbg(1, debug, sd, "FMT: bytesperline - %d"
3907b072 992 "Width - %d, Height - %d\n",
c2fc8096 993 pix->bytesperline,
07b1747c
VH
994 pix->width, pix->height);
995 return 0;
996}
997
c1c9d09c
MK
998/**
999 * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
62ef80a1 1000 * @sd: pointer to standard V4L2 sub-device structure
07b1747c
VH
1001 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
1002 *
1003 * Returns the decoder's video CAPTURE parameters.
1004 */
1005static int
62ef80a1 1006tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
07b1747c 1007{
62ef80a1 1008 struct tvp514x_decoder *decoder = to_decoder(sd);
07b1747c
VH
1009 struct v4l2_captureparm *cparm;
1010 enum tvp514x_std current_std;
1011
1012 if (a == NULL)
1013 return -EINVAL;
1014
1015 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
c1c9d09c
MK
1016 /* only capture is supported */
1017 return -EINVAL;
07b1747c 1018
07b1747c 1019 /* get the current standard */
2db4e78f 1020 current_std = decoder->current_std;
07b1747c
VH
1021
1022 cparm = &a->parm.capture;
1023 cparm->capability = V4L2_CAP_TIMEPERFRAME;
1024 cparm->timeperframe =
1025 decoder->std_list[current_std].standard.frameperiod;
1026
1027 return 0;
1028}
1029
c1c9d09c
MK
1030/**
1031 * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
62ef80a1 1032 * @sd: pointer to standard V4L2 sub-device structure
07b1747c
VH
1033 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
1034 *
1035 * Configures the decoder to use the input parameters, if possible. If
1036 * not possible, returns the appropriate error code.
1037 */
1038static int
62ef80a1 1039tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
07b1747c 1040{
62ef80a1 1041 struct tvp514x_decoder *decoder = to_decoder(sd);
07b1747c
VH
1042 struct v4l2_fract *timeperframe;
1043 enum tvp514x_std current_std;
1044
1045 if (a == NULL)
1046 return -EINVAL;
1047
1048 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
c1c9d09c
MK
1049 /* only capture is supported */
1050 return -EINVAL;
07b1747c
VH
1051
1052 timeperframe = &a->parm.capture.timeperframe;
1053
1054 /* get the current standard */
2db4e78f 1055 current_std = decoder->current_std;
07b1747c
VH
1056
1057 *timeperframe =
1058 decoder->std_list[current_std].standard.frameperiod;
1059
1060 return 0;
1061}
1062
c1c9d09c
MK
1063/**
1064 * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
62ef80a1
MK
1065 * @sd: pointer to standard V4L2 sub-device structure
1066 * @enable: streaming enable or disable
07b1747c 1067 *
62ef80a1 1068 * Sets streaming to enable or disable, if possible.
07b1747c 1069 */
62ef80a1 1070static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
07b1747c 1071{
07b1747c 1072 int err = 0;
62ef80a1
MK
1073 struct i2c_client *client = v4l2_get_subdevdata(sd);
1074 struct tvp514x_decoder *decoder = to_decoder(sd);
07b1747c 1075
62ef80a1
MK
1076 if (decoder->streaming == enable)
1077 return 0;
07b1747c 1078
62ef80a1
MK
1079 switch (enable) {
1080 case 0:
1081 {
1082 /* Power Down Sequence */
1083 err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
1084 if (err) {
1085 v4l2_err(sd, "Unable to turn off decoder\n");
1086 return err;
1087 }
1088 decoder->streaming = enable;
07b1747c 1089 break;
62ef80a1
MK
1090 }
1091 case 1:
1092 {
1093 struct tvp514x_reg *int_seq = (struct tvp514x_reg *)
1094 client->driver->id_table->driver_data;
07b1747c 1095
62ef80a1
MK
1096 /* Power Up Sequence */
1097 err = tvp514x_write_regs(sd, int_seq);
1098 if (err) {
1099 v4l2_err(sd, "Unable to turn on decoder\n");
1100 return err;
1101 }
1102 /* Detect if not already detected */
1103 err = tvp514x_detect(sd, decoder);
1104 if (err) {
1105 v4l2_err(sd, "Unable to detect decoder\n");
1106 return err;
07b1747c 1107 }
62ef80a1
MK
1108 err = tvp514x_configure(sd, decoder);
1109 if (err) {
1110 v4l2_err(sd, "Unable to configure decoder\n");
1111 return err;
1112 }
1113 decoder->streaming = enable;
07b1747c 1114 break;
62ef80a1 1115 }
07b1747c
VH
1116 default:
1117 err = -ENODEV;
1118 break;
1119 }
1120
1121 return err;
1122}
1123
62ef80a1
MK
1124static const struct v4l2_subdev_core_ops tvp514x_core_ops = {
1125 .queryctrl = tvp514x_queryctrl,
1126 .g_ctrl = tvp514x_g_ctrl,
1127 .s_ctrl = tvp514x_s_ctrl,
1128 .s_std = tvp514x_s_std,
1129};
07b1747c 1130
62ef80a1
MK
1131static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
1132 .s_routing = tvp514x_s_routing,
1133 .querystd = tvp514x_querystd,
1134 .enum_fmt = tvp514x_enum_fmt_cap,
283d637b
HV
1135 .g_fmt = tvp514x_fmt_cap,
1136 .try_fmt = tvp514x_fmt_cap,
1137 .s_fmt = tvp514x_fmt_cap,
62ef80a1
MK
1138 .g_parm = tvp514x_g_parm,
1139 .s_parm = tvp514x_s_parm,
1140 .s_stream = tvp514x_s_stream,
1141};
07b1747c 1142
62ef80a1
MK
1143static const struct v4l2_subdev_ops tvp514x_ops = {
1144 .core = &tvp514x_core_ops,
1145 .video = &tvp514x_video_ops,
07b1747c
VH
1146};
1147
07b1747c 1148static struct tvp514x_decoder tvp514x_dev = {
62ef80a1 1149 .streaming = 0,
07b1747c
VH
1150 .current_std = STD_NTSC_MJ,
1151 .std_list = tvp514x_std_list,
1152 .num_stds = ARRAY_SIZE(tvp514x_std_list),
62ef80a1 1153
07b1747c
VH
1154};
1155
c1c9d09c
MK
1156/**
1157 * tvp514x_probe() - decoder driver i2c probe handler
07b1747c 1158 * @client: i2c driver client device structure
62ef80a1 1159 * @id: i2c driver id table
07b1747c
VH
1160 *
1161 * Register decoder as an i2c client device and V4L2
1162 * device.
1163 */
1164static int
1165tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1166{
6722e0ef 1167 struct tvp514x_decoder *decoder;
62ef80a1 1168 struct v4l2_subdev *sd;
07b1747c
VH
1169
1170 /* Check if the adapter supports the needed features */
1171 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1172 return -EIO;
1173
62ef80a1
MK
1174 if (!client->dev.platform_data) {
1175 v4l2_err(client, "No platform data!!\n");
1176 return -ENODEV;
1177 }
1178
6722e0ef
SAS
1179 decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
1180 if (!decoder)
1181 return -ENOMEM;
1182
c1c9d09c 1183 /* Initialize the tvp514x_decoder with default configuration */
6722e0ef 1184 *decoder = tvp514x_dev;
62ef80a1 1185 /* Copy default register configuration */
6722e0ef
SAS
1186 memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
1187 sizeof(tvp514x_reg_list_default));
62ef80a1 1188
c1c9d09c 1189 /* Copy board specific information here */
62ef80a1
MK
1190 decoder->pdata = client->dev.platform_data;
1191
c1c9d09c 1192 /**
07b1747c
VH
1193 * Fetch platform specific data, and configure the
1194 * tvp514x_reg_list[] accordingly. Since this is one
1195 * time configuration, no need to preserve.
1196 */
6722e0ef 1197 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
62ef80a1 1198 (decoder->pdata->clk_polarity << 1);
6722e0ef 1199 decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
62ef80a1
MK
1200 ((decoder->pdata->hs_polarity << 2) |
1201 (decoder->pdata->vs_polarity << 3));
1202 /* Set default standard to auto */
1203 decoder->tvp514x_regs[REG_VIDEO_STD].val =
1204 VIDEO_STD_AUTO_SWITCH_BIT;
07b1747c
VH
1205
1206 /* Register with V4L2 layer as slave device */
62ef80a1
MK
1207 sd = &decoder->sd;
1208 v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
1209
1210 v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
1211
07b1747c 1212 return 0;
6722e0ef 1213
07b1747c
VH
1214}
1215
c1c9d09c
MK
1216/**
1217 * tvp514x_remove() - decoder driver i2c remove handler
07b1747c
VH
1218 * @client: i2c driver client device structure
1219 *
1220 * Unregister decoder as an i2c client device and V4L2
1221 * device. Complement of tvp514x_probe().
1222 */
62ef80a1 1223static int tvp514x_remove(struct i2c_client *client)
07b1747c 1224{
62ef80a1
MK
1225 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1226 struct tvp514x_decoder *decoder = to_decoder(sd);
07b1747c 1227
62ef80a1 1228 v4l2_device_unregister_subdev(sd);
6722e0ef 1229 kfree(decoder);
07b1747c
VH
1230 return 0;
1231}
c1c9d09c 1232/* TVP5146 Init/Power on Sequence */
07b1747c
VH
1233static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
1234 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1235 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1236 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1237 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1238 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1239 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1240 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1241 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1242 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1243 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1244 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
62ef80a1 1245 {TOK_TERM, 0, 0},
07b1747c 1246};
62ef80a1 1247
c1c9d09c 1248/* TVP5147 Init/Power on Sequence */
07b1747c
VH
1249static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
1250 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1251 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1252 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1253 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1254 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1255 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1256 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1257 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1258 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
1259 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1260 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
1261 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
1262 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1263 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1264 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1265 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1266 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1267 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
62ef80a1 1268 {TOK_TERM, 0, 0},
07b1747c 1269};
62ef80a1 1270
c1c9d09c 1271/* TVP5146M2/TVP5147M1 Init/Power on Sequence */
07b1747c
VH
1272static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1273 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1274 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
62ef80a1 1275 {TOK_TERM, 0, 0},
07b1747c 1276};
62ef80a1 1277
c1c9d09c 1278/**
07b1747c
VH
1279 * I2C Device Table -
1280 *
1281 * name - Name of the actual device/chip.
1282 * driver_data - Driver data
1283 */
1284static const struct i2c_device_id tvp514x_id[] = {
62ef80a1
MK
1285 {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
1286 {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
1287 {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
1288 {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
07b1747c
VH
1289 {},
1290};
1291
1292MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1293
62ef80a1 1294static struct i2c_driver tvp514x_driver = {
07b1747c 1295 .driver = {
62ef80a1
MK
1296 .owner = THIS_MODULE,
1297 .name = TVP514X_MODULE_NAME,
1298 },
07b1747c 1299 .probe = tvp514x_probe,
62ef80a1 1300 .remove = tvp514x_remove,
07b1747c
VH
1301 .id_table = tvp514x_id,
1302};
1303
07b1747c
VH
1304static int __init tvp514x_init(void)
1305{
62ef80a1 1306 return i2c_add_driver(&tvp514x_driver);
07b1747c
VH
1307}
1308
62ef80a1 1309static void __exit tvp514x_exit(void)
07b1747c 1310{
62ef80a1 1311 i2c_del_driver(&tvp514x_driver);
07b1747c
VH
1312}
1313
1314module_init(tvp514x_init);
62ef80a1 1315module_exit(tvp514x_exit);