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