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 * @current_std: Current standard
92 * @num_stds: Number of standards
93 * @std_list: Standards list
94 * @input: Input routing at chip level
95 * @output: Output routing at chip level
97 struct tvp514x_decoder {
98 struct v4l2_subdev sd;
99 struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
100 const struct tvp514x_platform_data *pdata;
105 enum tvp514x_std current_std;
107 const struct tvp514x_std_info *std_list;
108 /* Input and Output Routing parameters */
113 /* TVP514x default register values */
114 static struct tvp514x_reg tvp514x_reg_list_default[] = {
115 /* Composite selected */
116 {TOK_WRITE, REG_INPUT_SEL, 0x05},
117 {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
119 {TOK_WRITE, REG_VIDEO_STD, 0x00},
120 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
121 {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
122 {TOK_WRITE, REG_COLOR_KILLER, 0x10},
123 {TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
124 {TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
125 {TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
126 {TOK_WRITE, REG_BRIGHTNESS, 0x80},
127 {TOK_WRITE, REG_CONTRAST, 0x80},
128 {TOK_WRITE, REG_SATURATION, 0x80},
129 {TOK_WRITE, REG_HUE, 0x00},
130 {TOK_WRITE, REG_CHROMA_CONTROL1, 0x00},
131 {TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E},
133 {TOK_SKIP, 0x0F, 0x00},
134 {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
135 {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
136 {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
138 {TOK_SKIP, 0x13, 0x00},
139 {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
141 {TOK_SKIP, 0x15, 0x00},
143 {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
144 {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
145 {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
146 {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
148 {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
149 {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
150 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
151 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
153 {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
154 {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
155 {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
156 {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
158 {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
159 {TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
160 {TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
161 {TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
163 {TOK_SKIP, 0x26, 0x00},
165 {TOK_SKIP, 0x27, 0x00},
166 {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
168 {TOK_SKIP, 0x29, 0x00},
169 {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
171 {TOK_SKIP, 0x2B, 0x00},
172 {TOK_SKIP, REG_SCART_DELAY, 0x00},
173 {TOK_SKIP, REG_CTI_DELAY, 0x00},
174 {TOK_SKIP, REG_CTI_CONTROL, 0x00},
176 {TOK_SKIP, 0x2F, 0x00},
178 {TOK_SKIP, 0x30, 0x00},
180 {TOK_SKIP, 0x31, 0x00},
181 /* HS, VS active high */
182 {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
184 {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00},
185 /* Enable clk & data */
186 {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11},
187 /* Enable AVID & FLD */
188 {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE},
190 {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
191 {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
192 {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
194 {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
199 * Supported standards -
201 * Currently supports two standards only, need to add support for rest of the
202 * modes, like SECAM, etc...
204 static const struct tvp514x_std_info tvp514x_std_list[] = {
205 /* Standard: STD_NTSC_MJ */
207 .width = NTSC_NUM_ACTIVE_PIXELS,
208 .height = NTSC_NUM_ACTIVE_LINES,
209 .video_std = VIDEO_STD_NTSC_MJ_BIT,
214 .frameperiod = {1001, 30000},
217 /* Standard: STD_PAL_BDGHIN */
220 .width = PAL_NUM_ACTIVE_PIXELS,
221 .height = PAL_NUM_ACTIVE_LINES,
222 .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
227 .frameperiod = {1, 25},
231 /* Standard: need to add for additional standard */
235 static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
237 return container_of(sd, struct tvp514x_decoder, sd);
242 * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
243 * @sd: ptr to v4l2_subdev struct
244 * @reg: TVP5146/47 register address
246 * Returns value read if successful, or non-zero (-1) otherwise.
248 static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
251 struct i2c_client *client = v4l2_get_subdevdata(sd);
255 err = i2c_smbus_read_byte_data(client, reg);
257 if (retry <= I2C_RETRY_COUNT) {
258 v4l2_warn(sd, "Read: retry ... %d\n", retry);
260 msleep_interruptible(10);
269 * dump_reg() - dump the register content of TVP5146/47.
270 * @sd: ptr to v4l2_subdev struct
271 * @reg: TVP5146/47 register address
273 static void dump_reg(struct v4l2_subdev *sd, u8 reg)
277 val = tvp514x_read_reg(sd, reg);
278 v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
282 * tvp514x_write_reg() - Write a value to a register in TVP5146/47
283 * @sd: ptr to v4l2_subdev struct
284 * @reg: TVP5146/47 register address
285 * @val: value to be written to the register
287 * Write a value to a register in an TVP5146/47 decoder device.
288 * Returns zero if successful, or non-zero otherwise.
290 static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
293 struct i2c_client *client = v4l2_get_subdevdata(sd);
297 err = i2c_smbus_write_byte_data(client, reg, val);
299 if (retry <= I2C_RETRY_COUNT) {
300 v4l2_warn(sd, "Write: retry ... %d\n", retry);
302 msleep_interruptible(10);
311 * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
312 * @sd: ptr to v4l2_subdev struct
313 * @reglist: list of TVP5146/47 registers and values
315 * Initializes a list of TVP5146/47 registers:-
316 * if token is TOK_TERM, then entire write operation terminates
317 * if token is TOK_DELAY, then a delay of 'val' msec is introduced
318 * if token is TOK_SKIP, then the register write is skipped
319 * if token is TOK_WRITE, then the register write is performed
320 * Returns zero if successful, or non-zero otherwise.
322 static int tvp514x_write_regs(struct v4l2_subdev *sd,
323 const struct tvp514x_reg reglist[])
326 const struct tvp514x_reg *next = reglist;
328 for (; next->token != TOK_TERM; next++) {
329 if (next->token == TOK_DELAY) {
334 if (next->token == TOK_SKIP)
337 err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
339 v4l2_err(sd, "Write failed. Err[%d]\n", err);
347 * tvp514x_query_current_std() : Query the current standard detected by TVP5146/47
348 * @sd: ptr to v4l2_subdev struct
350 * Returns the current standard detected by TVP5146/47, STD_INVALID if there is no
353 static enum tvp514x_std tvp514x_query_current_std(struct v4l2_subdev *sd)
357 std = tvp514x_read_reg(sd, REG_VIDEO_STD);
358 if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
359 /* use the standard status register */
360 std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
362 /* use the standard register itself */
365 switch (std_status & VIDEO_STD_MASK) {
366 case VIDEO_STD_NTSC_MJ_BIT:
369 case VIDEO_STD_PAL_BDGHIN_BIT:
370 return STD_PAL_BDGHIN;
379 /* TVP5146/47 register dump function */
380 static void tvp514x_reg_dump(struct v4l2_subdev *sd)
382 dump_reg(sd, REG_INPUT_SEL);
383 dump_reg(sd, REG_AFE_GAIN_CTRL);
384 dump_reg(sd, REG_VIDEO_STD);
385 dump_reg(sd, REG_OPERATION_MODE);
386 dump_reg(sd, REG_COLOR_KILLER);
387 dump_reg(sd, REG_LUMA_CONTROL1);
388 dump_reg(sd, REG_LUMA_CONTROL2);
389 dump_reg(sd, REG_LUMA_CONTROL3);
390 dump_reg(sd, REG_BRIGHTNESS);
391 dump_reg(sd, REG_CONTRAST);
392 dump_reg(sd, REG_SATURATION);
393 dump_reg(sd, REG_HUE);
394 dump_reg(sd, REG_CHROMA_CONTROL1);
395 dump_reg(sd, REG_CHROMA_CONTROL2);
396 dump_reg(sd, REG_COMP_PR_SATURATION);
397 dump_reg(sd, REG_COMP_Y_CONTRAST);
398 dump_reg(sd, REG_COMP_PB_SATURATION);
399 dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
400 dump_reg(sd, REG_AVID_START_PIXEL_LSB);
401 dump_reg(sd, REG_AVID_START_PIXEL_MSB);
402 dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
403 dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
404 dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
405 dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
406 dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
407 dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
408 dump_reg(sd, REG_VSYNC_START_LINE_LSB);
409 dump_reg(sd, REG_VSYNC_START_LINE_MSB);
410 dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
411 dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
412 dump_reg(sd, REG_VBLK_START_LINE_LSB);
413 dump_reg(sd, REG_VBLK_START_LINE_MSB);
414 dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
415 dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
416 dump_reg(sd, REG_SYNC_CONTROL);
417 dump_reg(sd, REG_OUTPUT_FORMATTER1);
418 dump_reg(sd, REG_OUTPUT_FORMATTER2);
419 dump_reg(sd, REG_OUTPUT_FORMATTER3);
420 dump_reg(sd, REG_OUTPUT_FORMATTER4);
421 dump_reg(sd, REG_OUTPUT_FORMATTER5);
422 dump_reg(sd, REG_OUTPUT_FORMATTER6);
423 dump_reg(sd, REG_CLEAR_LOST_LOCK);
427 * tvp514x_configure() - Configure the TVP5146/47 registers
428 * @sd: ptr to v4l2_subdev struct
429 * @decoder: ptr to tvp514x_decoder structure
431 * Returns zero if successful, or non-zero otherwise.
433 static int tvp514x_configure(struct v4l2_subdev *sd,
434 struct tvp514x_decoder *decoder)
438 /* common register initialization */
440 tvp514x_write_regs(sd, decoder->tvp514x_regs);
445 tvp514x_reg_dump(sd);
451 * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
452 * @sd: pointer to standard V4L2 sub-device structure
453 * @decoder: pointer to tvp514x_decoder structure
455 * A device is considered to be detected if the chip ID (LSB and MSB)
456 * registers match the expected values.
457 * Any value of the rom version register is accepted.
458 * Returns ENODEV error number if no device is detected, or zero
459 * if a device is detected.
461 static int tvp514x_detect(struct v4l2_subdev *sd,
462 struct tvp514x_decoder *decoder)
464 u8 chip_id_msb, chip_id_lsb, rom_ver;
465 struct i2c_client *client = v4l2_get_subdevdata(sd);
467 chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
468 chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
469 rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
471 v4l2_dbg(1, debug, sd,
472 "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
473 chip_id_msb, chip_id_lsb, rom_ver);
474 if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
475 || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
476 && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
477 /* We didn't read the values we expected, so this must not be
480 v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
481 chip_id_msb, chip_id_lsb);
485 decoder->ver = rom_ver;
487 v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
488 client->name, decoder->ver,
489 client->addr << 1, client->adapter->name);
494 * tvp514x_querystd() - V4L2 decoder interface handler for querystd
495 * @sd: pointer to standard V4L2 sub-device structure
496 * @std_id: standard V4L2 std_id ioctl enum
498 * Returns the current standard detected by TVP5146/47. If no active input is
499 * detected then *std_id is set to 0 and the function returns 0.
501 static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
503 struct tvp514x_decoder *decoder = to_decoder(sd);
504 enum tvp514x_std current_std;
505 enum tvp514x_input input_sel;
506 u8 sync_lock_status, lock_mask;
511 *std_id = V4L2_STD_UNKNOWN;
513 /* query the current standard */
514 current_std = tvp514x_query_current_std(sd);
515 if (current_std == STD_INVALID)
518 input_sel = decoder->input;
521 case INPUT_CVBS_VI1A:
522 case INPUT_CVBS_VI1B:
523 case INPUT_CVBS_VI1C:
524 case INPUT_CVBS_VI2A:
525 case INPUT_CVBS_VI2B:
526 case INPUT_CVBS_VI2C:
527 case INPUT_CVBS_VI3A:
528 case INPUT_CVBS_VI3B:
529 case INPUT_CVBS_VI3C:
530 case INPUT_CVBS_VI4A:
531 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
532 STATUS_HORZ_SYNC_LOCK_BIT |
533 STATUS_VIRT_SYNC_LOCK_BIT;
536 case INPUT_SVIDEO_VI2A_VI1A:
537 case INPUT_SVIDEO_VI2B_VI1B:
538 case INPUT_SVIDEO_VI2C_VI1C:
539 case INPUT_SVIDEO_VI2A_VI3A:
540 case INPUT_SVIDEO_VI2B_VI3B:
541 case INPUT_SVIDEO_VI2C_VI3C:
542 case INPUT_SVIDEO_VI4A_VI1A:
543 case INPUT_SVIDEO_VI4A_VI1B:
544 case INPUT_SVIDEO_VI4A_VI1C:
545 case INPUT_SVIDEO_VI4A_VI3A:
546 case INPUT_SVIDEO_VI4A_VI3B:
547 case INPUT_SVIDEO_VI4A_VI3C:
548 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
549 STATUS_VIRT_SYNC_LOCK_BIT;
551 /*Need to add other interfaces*/
555 /* check whether signal is locked */
556 sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
557 if (lock_mask != (sync_lock_status & lock_mask))
558 return 0; /* No input detected */
560 *std_id = decoder->std_list[current_std].standard.id;
562 v4l2_dbg(1, debug, sd, "Current STD: %s\n",
563 decoder->std_list[current_std].standard.name);
568 * tvp514x_s_std() - V4L2 decoder interface handler for s_std
569 * @sd: pointer to standard V4L2 sub-device structure
570 * @std_id: standard V4L2 v4l2_std_id ioctl enum
572 * If std_id is supported, sets the requested standard. Otherwise, returns
575 static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
577 struct tvp514x_decoder *decoder = to_decoder(sd);
580 for (i = 0; i < decoder->num_stds; i++)
581 if (std_id & decoder->std_list[i].standard.id)
584 if ((i == decoder->num_stds) || (i == STD_INVALID))
587 err = tvp514x_write_reg(sd, REG_VIDEO_STD,
588 decoder->std_list[i].video_std);
592 decoder->current_std = i;
593 decoder->tvp514x_regs[REG_VIDEO_STD].val =
594 decoder->std_list[i].video_std;
596 v4l2_dbg(1, debug, sd, "Standard set to: %s\n",
597 decoder->std_list[i].standard.name);
602 * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
603 * @sd: pointer to standard V4L2 sub-device structure
604 * @input: input selector for routing the signal
605 * @output: output selector for routing the signal
606 * @config: config value. Not used
608 * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
609 * the input is not supported or there is no active signal present in the
612 static int tvp514x_s_routing(struct v4l2_subdev *sd,
613 u32 input, u32 output, u32 config)
615 struct tvp514x_decoder *decoder = to_decoder(sd);
617 enum tvp514x_input input_sel;
618 enum tvp514x_output output_sel;
619 u8 sync_lock_status, lock_mask;
620 int try_count = LOCK_RETRY_COUNT;
622 if ((input >= INPUT_INVALID) ||
623 (output >= OUTPUT_INVALID))
624 /* Index out of bound */
628 * For the sequence streamon -> streamoff and again s_input
629 * it fails to lock the signal, since streamoff puts TVP514x
630 * into power off state which leads to failure in sub-sequent s_input.
632 * So power up the TVP514x device here, since it is important to lock
633 * the signal at this stage.
635 if (!decoder->streaming)
636 tvp514x_s_stream(sd, 1);
641 err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
645 output_sel |= tvp514x_read_reg(sd,
646 REG_OUTPUT_FORMATTER1) & 0x7;
647 err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
652 decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
653 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
656 msleep(LOCK_RETRY_DELAY);
658 tvp514x_write_reg(sd, REG_CLEAR_LOST_LOCK, 0x01);
663 case INPUT_CVBS_VI1A:
664 case INPUT_CVBS_VI1B:
665 case INPUT_CVBS_VI1C:
666 case INPUT_CVBS_VI2A:
667 case INPUT_CVBS_VI2B:
668 case INPUT_CVBS_VI2C:
669 case INPUT_CVBS_VI3A:
670 case INPUT_CVBS_VI3B:
671 case INPUT_CVBS_VI3C:
672 case INPUT_CVBS_VI4A:
673 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
674 STATUS_HORZ_SYNC_LOCK_BIT |
675 STATUS_VIRT_SYNC_LOCK_BIT;
678 case INPUT_SVIDEO_VI2A_VI1A:
679 case INPUT_SVIDEO_VI2B_VI1B:
680 case INPUT_SVIDEO_VI2C_VI1C:
681 case INPUT_SVIDEO_VI2A_VI3A:
682 case INPUT_SVIDEO_VI2B_VI3B:
683 case INPUT_SVIDEO_VI2C_VI3C:
684 case INPUT_SVIDEO_VI4A_VI1A:
685 case INPUT_SVIDEO_VI4A_VI1B:
686 case INPUT_SVIDEO_VI4A_VI1C:
687 case INPUT_SVIDEO_VI4A_VI3A:
688 case INPUT_SVIDEO_VI4A_VI3B:
689 case INPUT_SVIDEO_VI4A_VI3C:
690 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
691 STATUS_VIRT_SYNC_LOCK_BIT;
693 /* Need to add other interfaces*/
698 while (try_count-- > 0) {
699 /* Allow decoder to sync up with new input */
700 msleep(LOCK_RETRY_DELAY);
702 sync_lock_status = tvp514x_read_reg(sd,
704 if (lock_mask == (sync_lock_status & lock_mask))
712 decoder->input = input;
713 decoder->output = output;
715 v4l2_dbg(1, debug, sd, "Input set to: %d\n", input_sel);
721 * tvp514x_queryctrl() - V4L2 decoder interface handler for queryctrl
722 * @sd: pointer to standard V4L2 sub-device structure
723 * @qctrl: standard V4L2 v4l2_queryctrl structure
725 * If the requested control is supported, returns the control information.
726 * Otherwise, returns -EINVAL if the control is not supported.
729 tvp514x_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qctrl)
737 case V4L2_CID_BRIGHTNESS:
738 /* Brightness supported is (0-255), */
739 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
741 case V4L2_CID_CONTRAST:
742 case V4L2_CID_SATURATION:
744 * Saturation and Contrast supported is -
745 * Contrast: 0 - 255 (Default - 128)
746 * Saturation: 0 - 255 (Default - 128)
748 err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
751 /* Hue Supported is -
752 * Hue - -180 - +180 (Default - 0, Step - +180)
754 err = v4l2_ctrl_query_fill(qctrl, -180, 180, 180, 0);
756 case V4L2_CID_AUTOGAIN:
758 * Auto Gain supported is -
759 * 0 - 1 (Default - 1)
761 err = v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 1);
764 v4l2_err(sd, "invalid control id %d\n", qctrl->id);
768 v4l2_dbg(1, debug, sd, "Query Control:%s: Min - %d, Max - %d, Def - %d\n",
769 qctrl->name, qctrl->minimum, qctrl->maximum,
770 qctrl->default_value);
776 * tvp514x_g_ctrl() - V4L2 decoder interface handler for g_ctrl
777 * @sd: pointer to standard V4L2 sub-device structure
778 * @ctrl: pointer to v4l2_control structure
780 * If the requested control is supported, returns the control's current
781 * value from the decoder. Otherwise, returns -EINVAL if the control is not
785 tvp514x_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
787 struct tvp514x_decoder *decoder = to_decoder(sd);
793 case V4L2_CID_BRIGHTNESS:
794 ctrl->value = decoder->tvp514x_regs[REG_BRIGHTNESS].val;
796 case V4L2_CID_CONTRAST:
797 ctrl->value = decoder->tvp514x_regs[REG_CONTRAST].val;
799 case V4L2_CID_SATURATION:
800 ctrl->value = decoder->tvp514x_regs[REG_SATURATION].val;
803 ctrl->value = decoder->tvp514x_regs[REG_HUE].val;
804 if (ctrl->value == 0x7F)
806 else if (ctrl->value == 0x80)
812 case V4L2_CID_AUTOGAIN:
813 ctrl->value = decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val;
814 if ((ctrl->value & 0x3) == 3)
821 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
825 v4l2_dbg(1, debug, sd, "Get Control: ID - %d - %d\n",
826 ctrl->id, ctrl->value);
831 * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
832 * @sd: pointer to standard V4L2 sub-device structure
833 * @ctrl: pointer to v4l2_control structure
835 * If the requested control is supported, sets the control's current
836 * value in HW. Otherwise, returns -EINVAL if the control is not supported.
839 tvp514x_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
841 struct tvp514x_decoder *decoder = to_decoder(sd);
842 int err = -EINVAL, value;
850 case V4L2_CID_BRIGHTNESS:
851 if (ctrl->value < 0 || ctrl->value > 255) {
852 v4l2_err(sd, "invalid brightness setting %d\n",
856 err = tvp514x_write_reg(sd, REG_BRIGHTNESS,
861 decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
863 case V4L2_CID_CONTRAST:
864 if (ctrl->value < 0 || ctrl->value > 255) {
865 v4l2_err(sd, "invalid contrast setting %d\n",
869 err = tvp514x_write_reg(sd, REG_CONTRAST, value);
873 decoder->tvp514x_regs[REG_CONTRAST].val = value;
875 case V4L2_CID_SATURATION:
876 if (ctrl->value < 0 || ctrl->value > 255) {
877 v4l2_err(sd, "invalid saturation setting %d\n",
881 err = tvp514x_write_reg(sd, REG_SATURATION, value);
885 decoder->tvp514x_regs[REG_SATURATION].val = value;
890 else if (value == -180)
895 v4l2_err(sd, "invalid hue setting %d\n", ctrl->value);
898 err = tvp514x_write_reg(sd, REG_HUE, value);
902 decoder->tvp514x_regs[REG_HUE].val = value;
904 case V4L2_CID_AUTOGAIN:
910 v4l2_err(sd, "invalid auto gain setting %d\n",
914 err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value);
918 decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
921 v4l2_err(sd, "invalid control id %d\n", ctrl->id);
925 v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d\n",
926 ctrl->id, ctrl->value);
932 * tvp514x_enum_fmt_cap() - V4L2 decoder interface handler for enum_fmt
933 * @sd: pointer to standard V4L2 sub-device structure
934 * @fmt: standard V4L2 VIDIOC_ENUM_FMT ioctl structure
936 * Implement the VIDIOC_ENUM_FMT ioctl to enumerate supported formats
939 tvp514x_enum_fmt_cap(struct v4l2_subdev *sd, struct v4l2_fmtdesc *fmt)
941 if (fmt == NULL || fmt->index)
944 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
945 /* only capture is supported */
948 /* only one format */
950 strlcpy(fmt->description, "8-bit UYVY 4:2:2 Format",
951 sizeof(fmt->description));
952 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
957 * tvp514x_fmt_cap() - V4L2 decoder interface handler for try/s/g_fmt
958 * @sd: pointer to standard V4L2 sub-device structure
959 * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
961 * Implement the VIDIOC_TRY/S/G_FMT ioctl for the CAPTURE buffer type. This
962 * ioctl is used to negotiate the image capture size and pixel format.
965 tvp514x_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
967 struct tvp514x_decoder *decoder = to_decoder(sd);
968 struct v4l2_pix_format *pix;
969 enum tvp514x_std current_std;
974 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
979 /* Calculate height and width based on current standard */
980 current_std = decoder->current_std;
982 pix->pixelformat = V4L2_PIX_FMT_UYVY;
983 pix->width = decoder->std_list[current_std].width;
984 pix->height = decoder->std_list[current_std].height;
985 pix->field = V4L2_FIELD_INTERLACED;
986 pix->bytesperline = pix->width * 2;
987 pix->sizeimage = pix->bytesperline * pix->height;
988 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
991 v4l2_dbg(1, debug, sd, "FMT: bytesperline - %d"
992 "Width - %d, Height - %d\n",
994 pix->width, pix->height);
999 * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
1000 * @sd: pointer to standard V4L2 sub-device structure
1001 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
1003 * Returns the decoder's video CAPTURE parameters.
1006 tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
1008 struct tvp514x_decoder *decoder = to_decoder(sd);
1009 struct v4l2_captureparm *cparm;
1010 enum tvp514x_std current_std;
1015 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1016 /* only capture is supported */
1019 /* get the current standard */
1020 current_std = decoder->current_std;
1022 cparm = &a->parm.capture;
1023 cparm->capability = V4L2_CAP_TIMEPERFRAME;
1024 cparm->timeperframe =
1025 decoder->std_list[current_std].standard.frameperiod;
1031 * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
1032 * @sd: pointer to standard V4L2 sub-device structure
1033 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
1035 * Configures the decoder to use the input parameters, if possible. If
1036 * not possible, returns the appropriate error code.
1039 tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
1041 struct tvp514x_decoder *decoder = to_decoder(sd);
1042 struct v4l2_fract *timeperframe;
1043 enum tvp514x_std current_std;
1048 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1049 /* only capture is supported */
1052 timeperframe = &a->parm.capture.timeperframe;
1054 /* get the current standard */
1055 current_std = decoder->current_std;
1058 decoder->std_list[current_std].standard.frameperiod;
1064 * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
1065 * @sd: pointer to standard V4L2 sub-device structure
1066 * @enable: streaming enable or disable
1068 * Sets streaming to enable or disable, if possible.
1070 static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
1073 struct i2c_client *client = v4l2_get_subdevdata(sd);
1074 struct tvp514x_decoder *decoder = to_decoder(sd);
1076 if (decoder->streaming == enable)
1082 /* Power Down Sequence */
1083 err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
1085 v4l2_err(sd, "Unable to turn off decoder\n");
1088 decoder->streaming = enable;
1093 struct tvp514x_reg *int_seq = (struct tvp514x_reg *)
1094 client->driver->id_table->driver_data;
1096 /* Power Up Sequence */
1097 err = tvp514x_write_regs(sd, int_seq);
1099 v4l2_err(sd, "Unable to turn on decoder\n");
1102 /* Detect if not already detected */
1103 err = tvp514x_detect(sd, decoder);
1105 v4l2_err(sd, "Unable to detect decoder\n");
1108 err = tvp514x_configure(sd, decoder);
1110 v4l2_err(sd, "Unable to configure decoder\n");
1113 decoder->streaming = enable;
1124 static const struct v4l2_subdev_core_ops tvp514x_core_ops = {
1125 .queryctrl = tvp514x_queryctrl,
1126 .g_ctrl = tvp514x_g_ctrl,
1127 .s_ctrl = tvp514x_s_ctrl,
1128 .s_std = tvp514x_s_std,
1131 static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
1132 .s_routing = tvp514x_s_routing,
1133 .querystd = tvp514x_querystd,
1134 .enum_fmt = tvp514x_enum_fmt_cap,
1135 .g_fmt = tvp514x_fmt_cap,
1136 .try_fmt = tvp514x_fmt_cap,
1137 .s_fmt = tvp514x_fmt_cap,
1138 .g_parm = tvp514x_g_parm,
1139 .s_parm = tvp514x_s_parm,
1140 .s_stream = tvp514x_s_stream,
1143 static const struct v4l2_subdev_ops tvp514x_ops = {
1144 .core = &tvp514x_core_ops,
1145 .video = &tvp514x_video_ops,
1148 static struct tvp514x_decoder tvp514x_dev = {
1150 .current_std = STD_NTSC_MJ,
1151 .std_list = tvp514x_std_list,
1152 .num_stds = ARRAY_SIZE(tvp514x_std_list),
1157 * tvp514x_probe() - decoder driver i2c probe handler
1158 * @client: i2c driver client device structure
1159 * @id: i2c driver id table
1161 * Register decoder as an i2c client device and V4L2
1165 tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1167 struct tvp514x_decoder *decoder;
1168 struct v4l2_subdev *sd;
1170 /* Check if the adapter supports the needed features */
1171 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1174 if (!client->dev.platform_data) {
1175 v4l2_err(client, "No platform data!!\n");
1179 decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
1183 /* Initialize the tvp514x_decoder with default configuration */
1184 *decoder = tvp514x_dev;
1185 /* Copy default register configuration */
1186 memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
1187 sizeof(tvp514x_reg_list_default));
1189 /* Copy board specific information here */
1190 decoder->pdata = client->dev.platform_data;
1193 * Fetch platform specific data, and configure the
1194 * tvp514x_reg_list[] accordingly. Since this is one
1195 * time configuration, no need to preserve.
1197 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
1198 (decoder->pdata->clk_polarity << 1);
1199 decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
1200 ((decoder->pdata->hs_polarity << 2) |
1201 (decoder->pdata->vs_polarity << 3));
1202 /* Set default standard to auto */
1203 decoder->tvp514x_regs[REG_VIDEO_STD].val =
1204 VIDEO_STD_AUTO_SWITCH_BIT;
1206 /* Register with V4L2 layer as slave device */
1208 v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
1210 v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
1217 * tvp514x_remove() - decoder driver i2c remove handler
1218 * @client: i2c driver client device structure
1220 * Unregister decoder as an i2c client device and V4L2
1221 * device. Complement of tvp514x_probe().
1223 static int tvp514x_remove(struct i2c_client *client)
1225 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1226 struct tvp514x_decoder *decoder = to_decoder(sd);
1228 v4l2_device_unregister_subdev(sd);
1232 /* TVP5146 Init/Power on Sequence */
1233 static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
1234 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1235 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1236 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1237 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1238 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1239 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1240 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1241 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1242 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1243 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1244 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1248 /* TVP5147 Init/Power on Sequence */
1249 static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
1250 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1251 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1252 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1253 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1254 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1255 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1256 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1257 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1258 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
1259 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1260 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
1261 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
1262 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1263 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1264 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1265 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1266 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1267 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1271 /* TVP5146M2/TVP5147M1 Init/Power on Sequence */
1272 static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1273 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1274 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1279 * I2C Device Table -
1281 * name - Name of the actual device/chip.
1282 * driver_data - Driver data
1284 static const struct i2c_device_id tvp514x_id[] = {
1285 {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
1286 {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
1287 {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
1288 {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
1292 MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1294 static struct i2c_driver tvp514x_driver = {
1296 .owner = THIS_MODULE,
1297 .name = TVP514X_MODULE_NAME,
1299 .probe = tvp514x_probe,
1300 .remove = tvp514x_remove,
1301 .id_table = tvp514x_id,
1304 static int __init tvp514x_init(void)
1306 return i2c_add_driver(&tvp514x_driver);
1309 static void __exit tvp514x_exit(void)
1311 i2c_del_driver(&tvp514x_driver);
1314 module_init(tvp514x_init);
1315 module_exit(tvp514x_exit);