2 * drivers/media/video/tvp514x.c
4 * TI TVP5146/47 decoder driver
6 * Copyright (C) 2008 Texas Instruments Inc
7 * Author: Vaibhav Hiremath <hvaibhav@ti.com>
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>
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.
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.
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.
31 #include <linux/i2c.h>
32 #include <linux/slab.h>
33 #include <linux/delay.h>
34 #include <linux/videodev2.h>
36 #include <media/v4l2-device.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-chip-ident.h>
39 #include <media/tvp514x.h>
41 #include "tvp514x_regs.h"
44 #define TVP514X_MODULE_NAME "tvp514x"
46 /* Private macros for TVP */
47 #define I2C_RETRY_COUNT (5)
48 #define LOCK_RETRY_COUNT (5)
49 #define LOCK_RETRY_DELAY (200)
53 module_param(debug, bool, 0644);
54 MODULE_PARM_DESC(debug, "Debug level (0-1)");
56 MODULE_AUTHOR("Texas Instruments");
57 MODULE_DESCRIPTION("TVP514X linux decoder driver");
58 MODULE_LICENSE("GPL");
60 /* enum tvp514x_std - enum for supported standards */
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
74 struct tvp514x_std_info {
78 struct v4l2_standard standard;
81 static struct tvp514x_reg tvp514x_reg_list_default[0x40];
83 static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable);
85 * struct tvp514x_decoder - TVP5146/47 decoder object
86 * @sd: Subdevice Slave handle
87 * @tvp514x_regs: copy of hw's regs with preset values.
88 * @pdata: Board specific
90 * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
91 * @pix: Current pixel format
92 * @current_std: Current standard
93 * @num_stds: Number of standards
94 * @std_list: Standards list
95 * @input: Input routing at chip level
96 * @output: Output routing at chip level
98 struct tvp514x_decoder {
99 struct v4l2_subdev sd;
100 struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
101 const struct tvp514x_platform_data *pdata;
106 struct v4l2_pix_format pix;
108 enum tvp514x_std current_std;
110 const struct tvp514x_std_info *std_list;
111 /* Input and Output Routing parameters */
116 /* TVP514x default register values */
117 static struct tvp514x_reg tvp514x_reg_list_default[] = {
118 /* Composite selected */
119 {TOK_WRITE, REG_INPUT_SEL, 0x05},
120 {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
122 {TOK_WRITE, REG_VIDEO_STD, 0x00},
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},
136 {TOK_SKIP, 0x0F, 0x00},
137 {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
138 {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
139 {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
141 {TOK_SKIP, 0x13, 0x00},
142 {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
144 {TOK_SKIP, 0x15, 0x00},
146 {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
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},
151 {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
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},
156 {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
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},
161 {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
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},
166 {TOK_SKIP, 0x26, 0x00},
168 {TOK_SKIP, 0x27, 0x00},
169 {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
171 {TOK_SKIP, 0x29, 0x00},
172 {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
174 {TOK_SKIP, 0x2B, 0x00},
175 {TOK_SKIP, REG_SCART_DELAY, 0x00},
176 {TOK_SKIP, REG_CTI_DELAY, 0x00},
177 {TOK_SKIP, REG_CTI_CONTROL, 0x00},
179 {TOK_SKIP, 0x2F, 0x00},
181 {TOK_SKIP, 0x30, 0x00},
183 {TOK_SKIP, 0x31, 0x00},
184 /* HS, VS active high */
185 {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
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},
193 {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
194 {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
195 {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
197 {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
202 * List of image formats supported by TVP5146/47 decoder
203 * Currently we are using 8 bit mode only, but can be
204 * extended to 10/20 bit mode.
206 static const struct v4l2_fmtdesc tvp514x_fmt_list[] = {
209 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
211 .description = "8-bit UYVY 4:2:2 Format",
212 .pixelformat = V4L2_PIX_FMT_UYVY,
217 * Supported standards -
219 * Currently supports two standards only, need to add support for rest of the
220 * modes, like SECAM, etc...
222 static const struct tvp514x_std_info tvp514x_std_list[] = {
223 /* Standard: STD_NTSC_MJ */
225 .width = NTSC_NUM_ACTIVE_PIXELS,
226 .height = NTSC_NUM_ACTIVE_LINES,
227 .video_std = VIDEO_STD_NTSC_MJ_BIT,
232 .frameperiod = {1001, 30000},
235 /* Standard: STD_PAL_BDGHIN */
238 .width = PAL_NUM_ACTIVE_PIXELS,
239 .height = PAL_NUM_ACTIVE_LINES,
240 .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
245 .frameperiod = {1, 25},
249 /* Standard: need to add for additional standard */
253 static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
255 return container_of(sd, struct tvp514x_decoder, sd);
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
264 * Returns value read if successful, or non-zero (-1) otherwise.
266 static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
269 struct i2c_client *client = v4l2_get_subdevdata(sd);
273 err = i2c_smbus_read_byte_data(client, reg);
275 if (retry <= I2C_RETRY_COUNT) {
276 v4l2_warn(sd, "Read: retry ... %d\n", retry);
278 msleep_interruptible(10);
287 * dump_reg() - dump the register content of TVP5146/47.
288 * @sd: ptr to v4l2_subdev struct
289 * @reg: TVP5146/47 register address
291 static void dump_reg(struct v4l2_subdev *sd, u8 reg)
295 val = tvp514x_read_reg(sd, reg);
296 v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
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
305 * Write a value to a register in an TVP5146/47 decoder device.
306 * Returns zero if successful, or non-zero otherwise.
308 static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
311 struct i2c_client *client = v4l2_get_subdevdata(sd);
315 err = i2c_smbus_write_byte_data(client, reg, val);
317 if (retry <= I2C_RETRY_COUNT) {
318 v4l2_warn(sd, "Write: retry ... %d\n", retry);
320 msleep_interruptible(10);
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
333 * Initializes a list of TVP5146/47 registers:-
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
338 * Returns zero if successful, or non-zero otherwise.
340 static int tvp514x_write_regs(struct v4l2_subdev *sd,
341 const struct tvp514x_reg reglist[])
344 const struct tvp514x_reg *next = reglist;
346 for (; next->token != TOK_TERM; next++) {
347 if (next->token == TOK_DELAY) {
352 if (next->token == TOK_SKIP)
355 err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
357 v4l2_err(sd, "Write failed. Err[%d]\n", err);
365 * tvp514x_query_current_std() : Query the current standard detected by TVP5146/47
366 * @sd: ptr to v4l2_subdev struct
368 * Returns the current standard detected by TVP5146/47, STD_INVALID if there is no
371 static enum tvp514x_std tvp514x_query_current_std(struct v4l2_subdev *sd)
375 std = tvp514x_read_reg(sd, REG_VIDEO_STD);
376 if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
377 /* use the standard status register */
378 std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
380 /* use the standard register itself */
383 switch (std_status & VIDEO_STD_MASK) {
384 case VIDEO_STD_NTSC_MJ_BIT:
387 case VIDEO_STD_PAL_BDGHIN_BIT:
388 return STD_PAL_BDGHIN;
397 /* TVP5146/47 register dump function */
398 static void tvp514x_reg_dump(struct v4l2_subdev *sd)
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);
445 * tvp514x_configure() - Configure the TVP5146/47 registers
446 * @sd: ptr to v4l2_subdev struct
447 * @decoder: ptr to tvp514x_decoder structure
449 * Returns zero if successful, or non-zero otherwise.
451 static int tvp514x_configure(struct v4l2_subdev *sd,
452 struct tvp514x_decoder *decoder)
456 /* common register initialization */
458 tvp514x_write_regs(sd, decoder->tvp514x_regs);
463 tvp514x_reg_dump(sd);
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
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.
479 static int tvp514x_detect(struct v4l2_subdev *sd,
480 struct tvp514x_decoder *decoder)
482 u8 chip_id_msb, chip_id_lsb, rom_ver;
483 struct i2c_client *client = v4l2_get_subdevdata(sd);
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);
489 v4l2_dbg(1, debug, sd,
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
498 v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
499 chip_id_msb, chip_id_lsb);
503 decoder->ver = rom_ver;
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);
512 * tvp514x_querystd() - V4L2 decoder interface handler for querystd
513 * @sd: pointer to standard V4L2 sub-device structure
514 * @std_id: standard V4L2 std_id ioctl enum
516 * Returns the current standard detected by TVP5146/47. If no active input is
517 * detected then *std_id is set to 0 and the function returns 0.
519 static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
521 struct tvp514x_decoder *decoder = to_decoder(sd);
522 enum tvp514x_std current_std;
523 enum tvp514x_input input_sel;
524 u8 sync_lock_status, lock_mask;
529 *std_id = V4L2_STD_UNKNOWN;
531 /* query the current standard */
532 current_std = tvp514x_query_current_std(sd);
533 if (current_std == STD_INVALID)
536 input_sel = decoder->input;
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;
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;
569 /*Need to add other interfaces*/
573 /* check whether signal is locked */
574 sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
575 if (lock_mask != (sync_lock_status & lock_mask))
576 return 0; /* No input detected */
578 *std_id = decoder->std_list[current_std].standard.id;
580 v4l2_dbg(1, debug, sd, "Current STD: %s\n",
581 decoder->std_list[current_std].standard.name);
586 * tvp514x_s_std() - V4L2 decoder interface handler for s_std
587 * @sd: pointer to standard V4L2 sub-device structure
588 * @std_id: standard V4L2 v4l2_std_id ioctl enum
590 * If std_id is supported, sets the requested standard. Otherwise, returns
593 static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
595 struct tvp514x_decoder *decoder = to_decoder(sd);
598 for (i = 0; i < decoder->num_stds; i++)
599 if (std_id & decoder->std_list[i].standard.id)
602 if ((i == decoder->num_stds) || (i == STD_INVALID))
605 err = tvp514x_write_reg(sd, REG_VIDEO_STD,
606 decoder->std_list[i].video_std);
610 decoder->current_std = i;
611 decoder->tvp514x_regs[REG_VIDEO_STD].val =
612 decoder->std_list[i].video_std;
614 v4l2_dbg(1, debug, sd, "Standard set to: %s",
615 decoder->std_list[i].standard.name);
620 * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
621 * @sd: pointer to standard V4L2 sub-device structure
622 * @input: input selector for routing the signal
623 * @output: output selector for routing the signal
624 * @config: config value. Not used
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
630 static int tvp514x_s_routing(struct v4l2_subdev *sd,
631 u32 input, u32 output, u32 config)
633 struct tvp514x_decoder *decoder = to_decoder(sd);
635 enum tvp514x_input input_sel;
636 enum tvp514x_output output_sel;
637 u8 sync_lock_status, lock_mask;
638 int try_count = LOCK_RETRY_COUNT;
640 if ((input >= INPUT_INVALID) ||
641 (output >= OUTPUT_INVALID))
642 /* Index out of bound */
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.
650 * So power up the TVP514x device here, since it is important to lock
651 * the signal at this stage.
653 if (!decoder->streaming)
654 tvp514x_s_stream(sd, 1);
659 err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
663 output_sel |= tvp514x_read_reg(sd,
664 REG_OUTPUT_FORMATTER1) & 0x7;
665 err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
670 decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
671 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
674 msleep(LOCK_RETRY_DELAY);
676 tvp514x_write_reg(sd, REG_CLEAR_LOST_LOCK, 0x01);
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;
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;
711 /* Need to add other interfaces*/
716 while (try_count-- > 0) {
717 /* Allow decoder to sync up with new input */
718 msleep(LOCK_RETRY_DELAY);
720 sync_lock_status = tvp514x_read_reg(sd,
722 if (lock_mask == (sync_lock_status & lock_mask))
730 decoder->input = input;
731 decoder->output = output;
733 v4l2_dbg(1, debug, sd, "Input set to: %d\n", input_sel);
739 * tvp514x_queryctrl() - V4L2 decoder interface handler for queryctrl
740 * @sd: pointer to standard V4L2 sub-device structure
741 * @qctrl: standard V4L2 v4l2_queryctrl structure
743 * If the requested control is supported, returns the control information.
744 * Otherwise, returns -EINVAL if the control is not supported.
747 tvp514x_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qctrl)
755 case V4L2_CID_BRIGHTNESS:
756 /* Brightness supported is (0-255), */
757 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
759 case V4L2_CID_CONTRAST:
760 case V4L2_CID_SATURATION:
762 * Saturation and Contrast supported is -
763 * Contrast: 0 - 255 (Default - 128)
764 * Saturation: 0 - 255 (Default - 128)
766 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
769 /* Hue Supported is -
770 * Hue - -180 - +180 (Default - 0, Step - +180)
772 err = v4l2_ctrl_query_fill(qctrl, -180, 180, 180, 0);
774 case V4L2_CID_AUTOGAIN:
776 * Auto Gain supported is -
777 * 0 - 1 (Default - 1)
779 err = v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 1);
782 v4l2_err(sd, "invalid control id %d\n", qctrl->id);
786 v4l2_dbg(1, debug, sd, "Query Control:%s: Min - %d, Max - %d, Def - %d",
787 qctrl->name, qctrl->minimum, qctrl->maximum,
788 qctrl->default_value);
794 * tvp514x_g_ctrl() - V4L2 decoder interface handler for g_ctrl
795 * @sd: pointer to standard V4L2 sub-device structure
796 * @ctrl: pointer to v4l2_control structure
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
803 tvp514x_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
805 struct tvp514x_decoder *decoder = to_decoder(sd);
811 case V4L2_CID_BRIGHTNESS:
812 ctrl->value = decoder->tvp514x_regs[REG_BRIGHTNESS].val;
814 case V4L2_CID_CONTRAST:
815 ctrl->value = decoder->tvp514x_regs[REG_CONTRAST].val;
817 case V4L2_CID_SATURATION:
818 ctrl->value = decoder->tvp514x_regs[REG_SATURATION].val;
821 ctrl->value = decoder->tvp514x_regs[REG_HUE].val;
822 if (ctrl->value == 0x7F)
824 else if (ctrl->value == 0x80)
830 case V4L2_CID_AUTOGAIN:
831 ctrl->value = decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val;
832 if ((ctrl->value & 0x3) == 3)
839 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
843 v4l2_dbg(1, debug, sd, "Get Control: ID - %d - %d",
844 ctrl->id, ctrl->value);
849 * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
850 * @sd: pointer to standard V4L2 sub-device structure
851 * @ctrl: pointer to v4l2_control structure
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.
857 tvp514x_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
859 struct tvp514x_decoder *decoder = to_decoder(sd);
860 int err = -EINVAL, value;
868 case V4L2_CID_BRIGHTNESS:
869 if (ctrl->value < 0 || ctrl->value > 255) {
870 v4l2_err(sd, "invalid brightness setting %d\n",
874 err = tvp514x_write_reg(sd, REG_BRIGHTNESS,
879 decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
881 case V4L2_CID_CONTRAST:
882 if (ctrl->value < 0 || ctrl->value > 255) {
883 v4l2_err(sd, "invalid contrast setting %d\n",
887 err = tvp514x_write_reg(sd, REG_CONTRAST, value);
891 decoder->tvp514x_regs[REG_CONTRAST].val = value;
893 case V4L2_CID_SATURATION:
894 if (ctrl->value < 0 || ctrl->value > 255) {
895 v4l2_err(sd, "invalid saturation setting %d\n",
899 err = tvp514x_write_reg(sd, REG_SATURATION, value);
903 decoder->tvp514x_regs[REG_SATURATION].val = value;
908 else if (value == -180)
913 v4l2_err(sd, "invalid hue setting %d\n", ctrl->value);
916 err = tvp514x_write_reg(sd, REG_HUE, value);
920 decoder->tvp514x_regs[REG_HUE].val = value;
922 case V4L2_CID_AUTOGAIN:
928 v4l2_err(sd, "invalid auto gain setting %d\n",
932 err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value);
936 decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
939 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
943 v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d",
944 ctrl->id, ctrl->value);
950 * tvp514x_enum_fmt_cap() - V4L2 decoder interface handler for enum_fmt
951 * @sd: pointer to standard V4L2 sub-device structure
952 * @fmt: standard V4L2 VIDIOC_ENUM_FMT ioctl structure
954 * Implement the VIDIOC_ENUM_FMT ioctl to enumerate supported formats
957 tvp514x_enum_fmt_cap(struct v4l2_subdev *sd, struct v4l2_fmtdesc *fmt)
959 if (fmt == NULL || fmt->index)
962 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
963 /* only capture is supported */
966 /* only one format */
968 strlcpy(fmt->description, "8-bit UYVY 4:2:2 Format",
969 sizeof(fmt->description));
970 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
975 * tvp514x_try_fmt_cap() - V4L2 decoder interface handler for try_fmt
976 * @sd: pointer to standard V4L2 sub-device structure
977 * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
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.
984 tvp514x_try_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
986 struct tvp514x_decoder *decoder = to_decoder(sd);
987 struct v4l2_pix_format *pix;
988 enum tvp514x_std current_std;
993 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
994 /* only capture is supported */
995 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
999 /* Calculate height and width based on current standard */
1000 current_std = decoder->current_std;
1002 pix->pixelformat = V4L2_PIX_FMT_UYVY;
1003 pix->width = decoder->std_list[current_std].width;
1004 pix->height = decoder->std_list[current_std].height;
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;
1011 v4l2_dbg(1, debug, sd, "Try FMT: bytesperline - %d"
1012 "Width - %d, Height - %d",
1014 pix->width, pix->height);
1019 * tvp514x_s_fmt_cap() - V4L2 decoder interface handler for s_fmt
1020 * @sd: pointer to standard V4L2 sub-device structure
1021 * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
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.
1028 tvp514x_s_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
1030 struct tvp514x_decoder *decoder = to_decoder(sd);
1031 struct v4l2_pix_format *pix;
1037 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1038 /* only capture is supported */
1042 rval = tvp514x_try_fmt_cap(sd, f);
1046 decoder->pix = *pix;
1052 * tvp514x_g_fmt_cap() - V4L2 decoder interface handler for tvp514x_g_fmt_cap
1053 * @sd: pointer to standard V4L2 sub-device structure
1054 * @f: pointer to standard V4L2 v4l2_format structure
1056 * Returns the decoder's current pixel format in the v4l2_format
1060 tvp514x_g_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
1062 struct tvp514x_decoder *decoder = to_decoder(sd);
1067 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1068 /* only capture is supported */
1071 f->fmt.pix = decoder->pix;
1073 v4l2_dbg(1, debug, sd, "Current FMT: bytesperline - %d"
1074 "Width - %d, Height - %d",
1075 decoder->pix.bytesperline,
1076 decoder->pix.width, decoder->pix.height);
1081 * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
1082 * @sd: pointer to standard V4L2 sub-device structure
1083 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
1085 * Returns the decoder's video CAPTURE parameters.
1088 tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
1090 struct tvp514x_decoder *decoder = to_decoder(sd);
1091 struct v4l2_captureparm *cparm;
1092 enum tvp514x_std current_std;
1097 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1098 /* only capture is supported */
1101 /* get the current standard */
1102 current_std = decoder->current_std;
1104 cparm = &a->parm.capture;
1105 cparm->capability = V4L2_CAP_TIMEPERFRAME;
1106 cparm->timeperframe =
1107 decoder->std_list[current_std].standard.frameperiod;
1113 * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
1114 * @sd: pointer to standard V4L2 sub-device structure
1115 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
1117 * Configures the decoder to use the input parameters, if possible. If
1118 * not possible, returns the appropriate error code.
1121 tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
1123 struct tvp514x_decoder *decoder = to_decoder(sd);
1124 struct v4l2_fract *timeperframe;
1125 enum tvp514x_std current_std;
1130 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1131 /* only capture is supported */
1134 timeperframe = &a->parm.capture.timeperframe;
1136 /* get the current standard */
1137 current_std = decoder->current_std;
1140 decoder->std_list[current_std].standard.frameperiod;
1146 * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
1147 * @sd: pointer to standard V4L2 sub-device structure
1148 * @enable: streaming enable or disable
1150 * Sets streaming to enable or disable, if possible.
1152 static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
1155 struct i2c_client *client = v4l2_get_subdevdata(sd);
1156 struct tvp514x_decoder *decoder = to_decoder(sd);
1158 if (decoder->streaming == enable)
1164 /* Power Down Sequence */
1165 err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
1167 v4l2_err(sd, "Unable to turn off decoder\n");
1170 decoder->streaming = enable;
1175 struct tvp514x_reg *int_seq = (struct tvp514x_reg *)
1176 client->driver->id_table->driver_data;
1178 /* Power Up Sequence */
1179 err = tvp514x_write_regs(sd, int_seq);
1181 v4l2_err(sd, "Unable to turn on decoder\n");
1184 /* Detect if not already detected */
1185 err = tvp514x_detect(sd, decoder);
1187 v4l2_err(sd, "Unable to detect decoder\n");
1190 err = tvp514x_configure(sd, decoder);
1192 v4l2_err(sd, "Unable to configure decoder\n");
1195 decoder->streaming = enable;
1206 static 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,
1213 static 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,
1225 static const struct v4l2_subdev_ops tvp514x_ops = {
1226 .core = &tvp514x_core_ops,
1227 .video = &tvp514x_video_ops,
1230 static struct tvp514x_decoder tvp514x_dev = {
1234 /* Default to NTSC 8-bit YUV 422 */
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,
1241 NTSC_NUM_ACTIVE_PIXELS * 2 * NTSC_NUM_ACTIVE_LINES,
1242 .colorspace = V4L2_COLORSPACE_SMPTE170M,
1245 .current_std = STD_NTSC_MJ,
1246 .std_list = tvp514x_std_list,
1247 .num_stds = ARRAY_SIZE(tvp514x_std_list),
1252 * tvp514x_probe() - decoder driver i2c probe handler
1253 * @client: i2c driver client device structure
1254 * @id: i2c driver id table
1256 * Register decoder as an i2c client device and V4L2
1260 tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1262 struct tvp514x_decoder *decoder;
1263 struct v4l2_subdev *sd;
1265 /* Check if the adapter supports the needed features */
1266 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1269 if (!client->dev.platform_data) {
1270 v4l2_err(client, "No platform data!!\n");
1274 decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
1278 /* Initialize the tvp514x_decoder with default configuration */
1279 *decoder = tvp514x_dev;
1280 /* Copy default register configuration */
1281 memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
1282 sizeof(tvp514x_reg_list_default));
1284 /* Copy board specific information here */
1285 decoder->pdata = client->dev.platform_data;
1288 * Fetch platform specific data, and configure the
1289 * tvp514x_reg_list[] accordingly. Since this is one
1290 * time configuration, no need to preserve.
1292 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
1293 (decoder->pdata->clk_polarity << 1);
1294 decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
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;
1301 /* Register with V4L2 layer as slave device */
1303 v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
1305 v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
1312 * tvp514x_remove() - decoder driver i2c remove handler
1313 * @client: i2c driver client device structure
1315 * Unregister decoder as an i2c client device and V4L2
1316 * device. Complement of tvp514x_probe().
1318 static int tvp514x_remove(struct i2c_client *client)
1320 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1321 struct tvp514x_decoder *decoder = to_decoder(sd);
1323 v4l2_device_unregister_subdev(sd);
1327 /* TVP5146 Init/Power on Sequence */
1328 static 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},
1343 /* TVP5147 Init/Power on Sequence */
1344 static 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},
1366 /* TVP5146M2/TVP5147M1 Init/Power on Sequence */
1367 static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1368 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1369 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1374 * I2C Device Table -
1376 * name - Name of the actual device/chip.
1377 * driver_data - Driver data
1379 static const struct i2c_device_id tvp514x_id[] = {
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},
1387 MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1389 static struct i2c_driver tvp514x_driver = {
1391 .owner = THIS_MODULE,
1392 .name = TVP514X_MODULE_NAME,
1394 .probe = tvp514x_probe,
1395 .remove = tvp514x_remove,
1396 .id_table = tvp514x_id,
1399 static int __init tvp514x_init(void)
1401 return i2c_add_driver(&tvp514x_driver);
1404 static void __exit tvp514x_exit(void)
1406 i2c_del_driver(&tvp514x_driver);
1409 module_init(tvp514x_init);
1410 module_exit(tvp514x_exit);