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