2 * drivers/media/i2c/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>
15 * Prabhakar Lad <prabhakar.lad@ti.com>
17 * This package is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License version 2 as
19 * published by the Free Software Foundation.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include <linux/i2c.h>
33 #include <linux/slab.h>
34 #include <linux/delay.h>
35 #include <linux/videodev2.h>
36 #include <linux/module.h>
37 #include <linux/v4l2-mediabus.h>
40 #include <media/v4l2-async.h>
41 #include <media/v4l2-device.h>
42 #include <media/v4l2-common.h>
43 #include <media/v4l2-mediabus.h>
44 #include <media/v4l2-of.h>
45 #include <media/v4l2-ctrls.h>
46 #include <media/tvp514x.h>
47 #include <media/media-entity.h>
49 #include "tvp514x_regs.h"
51 /* Private macros for TVP */
52 #define I2C_RETRY_COUNT (5)
53 #define LOCK_RETRY_COUNT (5)
54 #define LOCK_RETRY_DELAY (200)
58 module_param(debug, bool, 0644);
59 MODULE_PARM_DESC(debug, "Debug level (0-1)");
61 MODULE_AUTHOR("Texas Instruments");
62 MODULE_DESCRIPTION("TVP514X linux decoder driver");
63 MODULE_LICENSE("GPL");
65 /* enum tvp514x_std - enum for supported standards */
73 * struct tvp514x_std_info - Structure to store standard informations
74 * @width: Line width in pixels
75 * @height:Number of active lines
76 * @video_std: Value to write in REG_VIDEO_STD register
77 * @standard: v4l2 standard structure information
79 struct tvp514x_std_info {
83 struct v4l2_standard standard;
86 static struct tvp514x_reg tvp514x_reg_list_default[0x40];
88 static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable);
90 * struct tvp514x_decoder - TVP5146/47 decoder object
91 * @sd: Subdevice Slave handle
92 * @tvp514x_regs: copy of hw's regs with preset values.
93 * @pdata: Board specific
95 * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
96 * @pix: Current pixel format
97 * @num_fmts: Number of formats
98 * @fmt_list: Format list
99 * @current_std: Current standard
100 * @num_stds: Number of standards
101 * @std_list: Standards list
102 * @input: Input routing at chip level
103 * @output: Output routing at chip level
105 struct tvp514x_decoder {
106 struct v4l2_subdev sd;
107 struct v4l2_ctrl_handler hdl;
108 struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
109 const struct tvp514x_platform_data *pdata;
114 struct v4l2_pix_format pix;
116 const struct v4l2_fmtdesc *fmt_list;
118 enum tvp514x_std current_std;
120 const struct tvp514x_std_info *std_list;
121 /* Input and Output Routing parameters */
125 /* mc related members */
126 struct media_pad pad;
127 struct v4l2_mbus_framefmt format;
129 struct tvp514x_reg *int_seq;
132 /* TVP514x default register values */
133 static struct tvp514x_reg tvp514x_reg_list_default[] = {
134 /* Composite selected */
135 {TOK_WRITE, REG_INPUT_SEL, 0x05},
136 {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
138 {TOK_WRITE, REG_VIDEO_STD, 0x00},
139 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
140 {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
141 {TOK_WRITE, REG_COLOR_KILLER, 0x10},
142 {TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
143 {TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
144 {TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
145 {TOK_WRITE, REG_BRIGHTNESS, 0x80},
146 {TOK_WRITE, REG_CONTRAST, 0x80},
147 {TOK_WRITE, REG_SATURATION, 0x80},
148 {TOK_WRITE, REG_HUE, 0x00},
149 {TOK_WRITE, REG_CHROMA_CONTROL1, 0x00},
150 {TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E},
152 {TOK_SKIP, 0x0F, 0x00},
153 {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
154 {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
155 {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
157 {TOK_SKIP, 0x13, 0x00},
158 {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
160 {TOK_SKIP, 0x15, 0x00},
162 {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
163 {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
164 {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
165 {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
167 {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
168 {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
169 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
170 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
172 {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
173 {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
174 {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
175 {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
177 {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
178 {TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
179 {TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
180 {TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
182 {TOK_SKIP, 0x26, 0x00},
184 {TOK_SKIP, 0x27, 0x00},
185 {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
187 {TOK_SKIP, 0x29, 0x00},
188 {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
190 {TOK_SKIP, 0x2B, 0x00},
191 {TOK_SKIP, REG_SCART_DELAY, 0x00},
192 {TOK_SKIP, REG_CTI_DELAY, 0x00},
193 {TOK_SKIP, REG_CTI_CONTROL, 0x00},
195 {TOK_SKIP, 0x2F, 0x00},
197 {TOK_SKIP, 0x30, 0x00},
199 {TOK_SKIP, 0x31, 0x00},
200 /* HS, VS active high */
201 {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
203 {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00},
204 /* Enable clk & data */
205 {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11},
206 /* Enable AVID & FLD */
207 {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE},
209 {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
210 {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
211 {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
213 {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
218 * List of image formats supported by TVP5146/47 decoder
219 * Currently we are using 8 bit mode only, but can be
220 * extended to 10/20 bit mode.
222 static const struct v4l2_fmtdesc tvp514x_fmt_list[] = {
225 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
227 .description = "8-bit UYVY 4:2:2 Format",
228 .pixelformat = V4L2_PIX_FMT_UYVY,
233 * Supported standards -
235 * Currently supports two standards only, need to add support for rest of the
236 * modes, like SECAM, etc...
238 static const struct tvp514x_std_info tvp514x_std_list[] = {
239 /* Standard: STD_NTSC_MJ */
241 .width = NTSC_NUM_ACTIVE_PIXELS,
242 .height = NTSC_NUM_ACTIVE_LINES,
243 .video_std = VIDEO_STD_NTSC_MJ_BIT,
248 .frameperiod = {1001, 30000},
251 /* Standard: STD_PAL_BDGHIN */
254 .width = PAL_NUM_ACTIVE_PIXELS,
255 .height = PAL_NUM_ACTIVE_LINES,
256 .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
261 .frameperiod = {1, 25},
265 /* Standard: need to add for additional standard */
269 static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
271 return container_of(sd, struct tvp514x_decoder, sd);
274 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
276 return &container_of(ctrl->handler, struct tvp514x_decoder, hdl)->sd;
281 * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
282 * @sd: ptr to v4l2_subdev struct
283 * @reg: TVP5146/47 register address
285 * Returns value read if successful, or non-zero (-1) otherwise.
287 static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
290 struct i2c_client *client = v4l2_get_subdevdata(sd);
294 err = i2c_smbus_read_byte_data(client, reg);
296 if (retry <= I2C_RETRY_COUNT) {
297 v4l2_warn(sd, "Read: retry ... %d\n", retry);
299 msleep_interruptible(10);
308 * dump_reg() - dump the register content of TVP5146/47.
309 * @sd: ptr to v4l2_subdev struct
310 * @reg: TVP5146/47 register address
312 static void dump_reg(struct v4l2_subdev *sd, u8 reg)
316 val = tvp514x_read_reg(sd, reg);
317 v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
321 * tvp514x_write_reg() - Write a value to a register in TVP5146/47
322 * @sd: ptr to v4l2_subdev struct
323 * @reg: TVP5146/47 register address
324 * @val: value to be written to the register
326 * Write a value to a register in an TVP5146/47 decoder device.
327 * Returns zero if successful, or non-zero otherwise.
329 static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
332 struct i2c_client *client = v4l2_get_subdevdata(sd);
336 err = i2c_smbus_write_byte_data(client, reg, val);
338 if (retry <= I2C_RETRY_COUNT) {
339 v4l2_warn(sd, "Write: retry ... %d\n", retry);
341 msleep_interruptible(10);
350 * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
351 * @sd: ptr to v4l2_subdev struct
352 * @reglist: list of TVP5146/47 registers and values
354 * Initializes a list of TVP5146/47 registers:-
355 * if token is TOK_TERM, then entire write operation terminates
356 * if token is TOK_DELAY, then a delay of 'val' msec is introduced
357 * if token is TOK_SKIP, then the register write is skipped
358 * if token is TOK_WRITE, then the register write is performed
359 * Returns zero if successful, or non-zero otherwise.
361 static int tvp514x_write_regs(struct v4l2_subdev *sd,
362 const struct tvp514x_reg reglist[])
365 const struct tvp514x_reg *next = reglist;
367 for (; next->token != TOK_TERM; next++) {
368 if (next->token == TOK_DELAY) {
373 if (next->token == TOK_SKIP)
376 err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
378 v4l2_err(sd, "Write failed. Err[%d]\n", err);
386 * tvp514x_query_current_std() : Query the current standard detected by TVP5146/47
387 * @sd: ptr to v4l2_subdev struct
389 * Returns the current standard detected by TVP5146/47, STD_INVALID if there is no
392 static enum tvp514x_std tvp514x_query_current_std(struct v4l2_subdev *sd)
396 std = tvp514x_read_reg(sd, REG_VIDEO_STD);
397 if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
398 /* use the standard status register */
399 std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
401 /* use the standard register itself */
404 switch (std_status & VIDEO_STD_MASK) {
405 case VIDEO_STD_NTSC_MJ_BIT:
408 case VIDEO_STD_PAL_BDGHIN_BIT:
409 return STD_PAL_BDGHIN;
418 /* TVP5146/47 register dump function */
419 static void tvp514x_reg_dump(struct v4l2_subdev *sd)
421 dump_reg(sd, REG_INPUT_SEL);
422 dump_reg(sd, REG_AFE_GAIN_CTRL);
423 dump_reg(sd, REG_VIDEO_STD);
424 dump_reg(sd, REG_OPERATION_MODE);
425 dump_reg(sd, REG_COLOR_KILLER);
426 dump_reg(sd, REG_LUMA_CONTROL1);
427 dump_reg(sd, REG_LUMA_CONTROL2);
428 dump_reg(sd, REG_LUMA_CONTROL3);
429 dump_reg(sd, REG_BRIGHTNESS);
430 dump_reg(sd, REG_CONTRAST);
431 dump_reg(sd, REG_SATURATION);
432 dump_reg(sd, REG_HUE);
433 dump_reg(sd, REG_CHROMA_CONTROL1);
434 dump_reg(sd, REG_CHROMA_CONTROL2);
435 dump_reg(sd, REG_COMP_PR_SATURATION);
436 dump_reg(sd, REG_COMP_Y_CONTRAST);
437 dump_reg(sd, REG_COMP_PB_SATURATION);
438 dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
439 dump_reg(sd, REG_AVID_START_PIXEL_LSB);
440 dump_reg(sd, REG_AVID_START_PIXEL_MSB);
441 dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
442 dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
443 dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
444 dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
445 dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
446 dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
447 dump_reg(sd, REG_VSYNC_START_LINE_LSB);
448 dump_reg(sd, REG_VSYNC_START_LINE_MSB);
449 dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
450 dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
451 dump_reg(sd, REG_VBLK_START_LINE_LSB);
452 dump_reg(sd, REG_VBLK_START_LINE_MSB);
453 dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
454 dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
455 dump_reg(sd, REG_SYNC_CONTROL);
456 dump_reg(sd, REG_OUTPUT_FORMATTER1);
457 dump_reg(sd, REG_OUTPUT_FORMATTER2);
458 dump_reg(sd, REG_OUTPUT_FORMATTER3);
459 dump_reg(sd, REG_OUTPUT_FORMATTER4);
460 dump_reg(sd, REG_OUTPUT_FORMATTER5);
461 dump_reg(sd, REG_OUTPUT_FORMATTER6);
462 dump_reg(sd, REG_CLEAR_LOST_LOCK);
466 * tvp514x_configure() - Configure the TVP5146/47 registers
467 * @sd: ptr to v4l2_subdev struct
468 * @decoder: ptr to tvp514x_decoder structure
470 * Returns zero if successful, or non-zero otherwise.
472 static int tvp514x_configure(struct v4l2_subdev *sd,
473 struct tvp514x_decoder *decoder)
477 /* common register initialization */
479 tvp514x_write_regs(sd, decoder->tvp514x_regs);
484 tvp514x_reg_dump(sd);
490 * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
491 * @sd: pointer to standard V4L2 sub-device structure
492 * @decoder: pointer to tvp514x_decoder structure
494 * A device is considered to be detected if the chip ID (LSB and MSB)
495 * registers match the expected values.
496 * Any value of the rom version register is accepted.
497 * Returns ENODEV error number if no device is detected, or zero
498 * if a device is detected.
500 static int tvp514x_detect(struct v4l2_subdev *sd,
501 struct tvp514x_decoder *decoder)
503 u8 chip_id_msb, chip_id_lsb, rom_ver;
504 struct i2c_client *client = v4l2_get_subdevdata(sd);
506 chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
507 chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
508 rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
510 v4l2_dbg(1, debug, sd,
511 "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
512 chip_id_msb, chip_id_lsb, rom_ver);
513 if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
514 || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
515 && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
516 /* We didn't read the values we expected, so this must not be
519 v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
520 chip_id_msb, chip_id_lsb);
524 decoder->ver = rom_ver;
526 v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
527 client->name, decoder->ver,
528 client->addr << 1, client->adapter->name);
533 * tvp514x_querystd() - V4L2 decoder interface handler for querystd
534 * @sd: pointer to standard V4L2 sub-device structure
535 * @std_id: standard V4L2 std_id ioctl enum
537 * Returns the current standard detected by TVP5146/47. If no active input is
538 * detected then *std_id is set to 0 and the function returns 0.
540 static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
542 struct tvp514x_decoder *decoder = to_decoder(sd);
543 enum tvp514x_std current_std;
544 enum tvp514x_input input_sel;
545 u8 sync_lock_status, lock_mask;
550 /* To query the standard the TVP514x must power on the ADCs. */
551 if (!decoder->streaming) {
552 tvp514x_s_stream(sd, 1);
553 msleep(LOCK_RETRY_DELAY);
556 /* query the current standard */
557 current_std = tvp514x_query_current_std(sd);
558 if (current_std == STD_INVALID) {
559 *std_id = V4L2_STD_UNKNOWN;
563 input_sel = decoder->input;
566 case INPUT_CVBS_VI1A:
567 case INPUT_CVBS_VI1B:
568 case INPUT_CVBS_VI1C:
569 case INPUT_CVBS_VI2A:
570 case INPUT_CVBS_VI2B:
571 case INPUT_CVBS_VI2C:
572 case INPUT_CVBS_VI3A:
573 case INPUT_CVBS_VI3B:
574 case INPUT_CVBS_VI3C:
575 case INPUT_CVBS_VI4A:
576 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
577 STATUS_HORZ_SYNC_LOCK_BIT |
578 STATUS_VIRT_SYNC_LOCK_BIT;
581 case INPUT_SVIDEO_VI2A_VI1A:
582 case INPUT_SVIDEO_VI2B_VI1B:
583 case INPUT_SVIDEO_VI2C_VI1C:
584 case INPUT_SVIDEO_VI2A_VI3A:
585 case INPUT_SVIDEO_VI2B_VI3B:
586 case INPUT_SVIDEO_VI2C_VI3C:
587 case INPUT_SVIDEO_VI4A_VI1A:
588 case INPUT_SVIDEO_VI4A_VI1B:
589 case INPUT_SVIDEO_VI4A_VI1C:
590 case INPUT_SVIDEO_VI4A_VI3A:
591 case INPUT_SVIDEO_VI4A_VI3B:
592 case INPUT_SVIDEO_VI4A_VI3C:
593 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
594 STATUS_VIRT_SYNC_LOCK_BIT;
596 /*Need to add other interfaces*/
600 /* check whether signal is locked */
601 sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
602 if (lock_mask != (sync_lock_status & lock_mask)) {
603 *std_id = V4L2_STD_UNKNOWN;
604 return 0; /* No input detected */
607 *std_id &= decoder->std_list[current_std].standard.id;
609 v4l2_dbg(1, debug, sd, "Current STD: %s\n",
610 decoder->std_list[current_std].standard.name);
615 * tvp514x_s_std() - V4L2 decoder interface handler for s_std
616 * @sd: pointer to standard V4L2 sub-device structure
617 * @std_id: standard V4L2 v4l2_std_id ioctl enum
619 * If std_id is supported, sets the requested standard. Otherwise, returns
622 static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
624 struct tvp514x_decoder *decoder = to_decoder(sd);
627 for (i = 0; i < decoder->num_stds; i++)
628 if (std_id & decoder->std_list[i].standard.id)
631 if ((i == decoder->num_stds) || (i == STD_INVALID))
634 err = tvp514x_write_reg(sd, REG_VIDEO_STD,
635 decoder->std_list[i].video_std);
639 decoder->current_std = i;
640 decoder->tvp514x_regs[REG_VIDEO_STD].val =
641 decoder->std_list[i].video_std;
643 v4l2_dbg(1, debug, sd, "Standard set to: %s\n",
644 decoder->std_list[i].standard.name);
649 * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
650 * @sd: pointer to standard V4L2 sub-device structure
651 * @input: input selector for routing the signal
652 * @output: output selector for routing the signal
653 * @config: config value. Not used
655 * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
656 * the input is not supported or there is no active signal present in the
659 static int tvp514x_s_routing(struct v4l2_subdev *sd,
660 u32 input, u32 output, u32 config)
662 struct tvp514x_decoder *decoder = to_decoder(sd);
664 enum tvp514x_input input_sel;
665 enum tvp514x_output output_sel;
667 if ((input >= INPUT_INVALID) ||
668 (output >= OUTPUT_INVALID))
669 /* Index out of bound */
675 err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
679 output_sel |= tvp514x_read_reg(sd,
680 REG_OUTPUT_FORMATTER1) & 0x7;
681 err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
686 decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
687 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
688 decoder->input = input;
689 decoder->output = output;
691 v4l2_dbg(1, debug, sd, "Input set to: %d\n", input_sel);
697 * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
698 * @ctrl: pointer to v4l2_ctrl structure
700 * If the requested control is supported, sets the control's current
701 * value in HW. Otherwise, returns -EINVAL if the control is not supported.
703 static int tvp514x_s_ctrl(struct v4l2_ctrl *ctrl)
705 struct v4l2_subdev *sd = to_sd(ctrl);
706 struct tvp514x_decoder *decoder = to_decoder(sd);
707 int err = -EINVAL, value;
712 case V4L2_CID_BRIGHTNESS:
713 err = tvp514x_write_reg(sd, REG_BRIGHTNESS, value);
715 decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
717 case V4L2_CID_CONTRAST:
718 err = tvp514x_write_reg(sd, REG_CONTRAST, value);
720 decoder->tvp514x_regs[REG_CONTRAST].val = value;
722 case V4L2_CID_SATURATION:
723 err = tvp514x_write_reg(sd, REG_SATURATION, value);
725 decoder->tvp514x_regs[REG_SATURATION].val = value;
730 else if (value == -180)
732 err = tvp514x_write_reg(sd, REG_HUE, value);
734 decoder->tvp514x_regs[REG_HUE].val = value;
736 case V4L2_CID_AUTOGAIN:
737 err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value ? 0x0f : 0x0c);
739 decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
743 v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d\n",
744 ctrl->id, ctrl->val);
749 * tvp514x_enum_mbus_fmt() - V4L2 decoder interface handler for enum_mbus_fmt
750 * @sd: pointer to standard V4L2 sub-device structure
751 * @index: index of pixelcode to retrieve
752 * @code: receives the pixelcode
754 * Enumerates supported mediabus formats
757 tvp514x_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index,
758 enum v4l2_mbus_pixelcode *code)
763 *code = V4L2_MBUS_FMT_YUYV10_2X10;
768 * tvp514x_mbus_fmt() - V4L2 decoder interface handler for try/s/g_mbus_fmt
769 * @sd: pointer to standard V4L2 sub-device structure
770 * @f: pointer to the mediabus format structure
772 * Negotiates the image capture size and mediabus format.
775 tvp514x_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f)
777 struct tvp514x_decoder *decoder = to_decoder(sd);
778 enum tvp514x_std current_std;
783 /* Calculate height and width based on current standard */
784 current_std = decoder->current_std;
786 f->code = V4L2_MBUS_FMT_YUYV8_2X8;
787 f->width = decoder->std_list[current_std].width;
788 f->height = decoder->std_list[current_std].height;
789 f->field = V4L2_FIELD_INTERLACED;
790 f->colorspace = V4L2_COLORSPACE_SMPTE170M;
791 v4l2_dbg(1, debug, sd, "MBUS_FMT: Width - %d, Height - %d\n",
792 f->width, f->height);
797 * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
798 * @sd: pointer to standard V4L2 sub-device structure
799 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
801 * Returns the decoder's video CAPTURE parameters.
804 tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
806 struct tvp514x_decoder *decoder = to_decoder(sd);
807 struct v4l2_captureparm *cparm;
808 enum tvp514x_std current_std;
813 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
814 /* only capture is supported */
817 /* get the current standard */
818 current_std = decoder->current_std;
820 cparm = &a->parm.capture;
821 cparm->capability = V4L2_CAP_TIMEPERFRAME;
822 cparm->timeperframe =
823 decoder->std_list[current_std].standard.frameperiod;
829 * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
830 * @sd: pointer to standard V4L2 sub-device structure
831 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
833 * Configures the decoder to use the input parameters, if possible. If
834 * not possible, returns the appropriate error code.
837 tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
839 struct tvp514x_decoder *decoder = to_decoder(sd);
840 struct v4l2_fract *timeperframe;
841 enum tvp514x_std current_std;
846 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
847 /* only capture is supported */
850 timeperframe = &a->parm.capture.timeperframe;
852 /* get the current standard */
853 current_std = decoder->current_std;
856 decoder->std_list[current_std].standard.frameperiod;
862 * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
863 * @sd: pointer to standard V4L2 sub-device structure
864 * @enable: streaming enable or disable
866 * Sets streaming to enable or disable, if possible.
868 static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
871 struct tvp514x_decoder *decoder = to_decoder(sd);
873 if (decoder->streaming == enable)
879 /* Power Down Sequence */
880 err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
882 v4l2_err(sd, "Unable to turn off decoder\n");
885 decoder->streaming = enable;
890 /* Power Up Sequence */
891 err = tvp514x_write_regs(sd, decoder->int_seq);
893 v4l2_err(sd, "Unable to turn on decoder\n");
896 /* Detect if not already detected */
897 err = tvp514x_detect(sd, decoder);
899 v4l2_err(sd, "Unable to detect decoder\n");
902 err = tvp514x_configure(sd, decoder);
904 v4l2_err(sd, "Unable to configure decoder\n");
907 decoder->streaming = enable;
918 static const struct v4l2_ctrl_ops tvp514x_ctrl_ops = {
919 .s_ctrl = tvp514x_s_ctrl,
923 * tvp514x_enum_mbus_code() - V4L2 decoder interface handler for enum_mbus_code
924 * @sd: pointer to standard V4L2 sub-device structure
926 * @code: pointer to v4l2_subdev_mbus_code_enum structure
928 * Enumertaes mbus codes supported
930 static int tvp514x_enum_mbus_code(struct v4l2_subdev *sd,
931 struct v4l2_subdev_fh *fh,
932 struct v4l2_subdev_mbus_code_enum *code)
935 u32 index = code->index;
937 memset(code, 0, sizeof(*code));
944 code->code = V4L2_MBUS_FMT_YUYV8_2X8;
950 * tvp514x_get_pad_format() - V4L2 decoder interface handler for get pad format
951 * @sd: pointer to standard V4L2 sub-device structure
953 * @format: pointer to v4l2_subdev_format structure
955 * Retrieves pad format which is active or tried based on requirement
957 static int tvp514x_get_pad_format(struct v4l2_subdev *sd,
958 struct v4l2_subdev_fh *fh,
959 struct v4l2_subdev_format *format)
961 struct tvp514x_decoder *decoder = to_decoder(sd);
962 __u32 which = format->which;
964 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
965 format->format = decoder->format;
969 format->format.code = V4L2_MBUS_FMT_YUYV8_2X8;
970 format->format.width = tvp514x_std_list[decoder->current_std].width;
971 format->format.height = tvp514x_std_list[decoder->current_std].height;
972 format->format.colorspace = V4L2_COLORSPACE_SMPTE170M;
973 format->format.field = V4L2_FIELD_INTERLACED;
979 * tvp514x_set_pad_format() - V4L2 decoder interface handler for set pad format
980 * @sd: pointer to standard V4L2 sub-device structure
982 * @format: pointer to v4l2_subdev_format structure
984 * Set pad format for the output pad
986 static int tvp514x_set_pad_format(struct v4l2_subdev *sd,
987 struct v4l2_subdev_fh *fh,
988 struct v4l2_subdev_format *fmt)
990 struct tvp514x_decoder *decoder = to_decoder(sd);
992 if (fmt->format.field != V4L2_FIELD_INTERLACED ||
993 fmt->format.code != V4L2_MBUS_FMT_YUYV8_2X8 ||
994 fmt->format.colorspace != V4L2_COLORSPACE_SMPTE170M ||
995 fmt->format.width != tvp514x_std_list[decoder->current_std].width ||
996 fmt->format.height != tvp514x_std_list[decoder->current_std].height)
999 decoder->format = fmt->format;
1004 static const struct v4l2_subdev_core_ops tvp514x_core_ops = {
1005 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
1006 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
1007 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
1008 .g_ctrl = v4l2_subdev_g_ctrl,
1009 .s_ctrl = v4l2_subdev_s_ctrl,
1010 .queryctrl = v4l2_subdev_queryctrl,
1011 .querymenu = v4l2_subdev_querymenu,
1012 .s_std = tvp514x_s_std,
1015 static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
1016 .s_routing = tvp514x_s_routing,
1017 .querystd = tvp514x_querystd,
1018 .enum_mbus_fmt = tvp514x_enum_mbus_fmt,
1019 .g_mbus_fmt = tvp514x_mbus_fmt,
1020 .try_mbus_fmt = tvp514x_mbus_fmt,
1021 .s_mbus_fmt = tvp514x_mbus_fmt,
1022 .g_parm = tvp514x_g_parm,
1023 .s_parm = tvp514x_s_parm,
1024 .s_stream = tvp514x_s_stream,
1027 static const struct v4l2_subdev_pad_ops tvp514x_pad_ops = {
1028 .enum_mbus_code = tvp514x_enum_mbus_code,
1029 .get_fmt = tvp514x_get_pad_format,
1030 .set_fmt = tvp514x_set_pad_format,
1033 static const struct v4l2_subdev_ops tvp514x_ops = {
1034 .core = &tvp514x_core_ops,
1035 .video = &tvp514x_video_ops,
1036 .pad = &tvp514x_pad_ops,
1039 static struct tvp514x_decoder tvp514x_dev = {
1041 .fmt_list = tvp514x_fmt_list,
1042 .num_fmts = ARRAY_SIZE(tvp514x_fmt_list),
1044 /* Default to NTSC 8-bit YUV 422 */
1045 .width = NTSC_NUM_ACTIVE_PIXELS,
1046 .height = NTSC_NUM_ACTIVE_LINES,
1047 .pixelformat = V4L2_PIX_FMT_UYVY,
1048 .field = V4L2_FIELD_INTERLACED,
1049 .bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2,
1050 .sizeimage = NTSC_NUM_ACTIVE_PIXELS * 2 *
1051 NTSC_NUM_ACTIVE_LINES,
1052 .colorspace = V4L2_COLORSPACE_SMPTE170M,
1054 .current_std = STD_NTSC_MJ,
1055 .std_list = tvp514x_std_list,
1056 .num_stds = ARRAY_SIZE(tvp514x_std_list),
1060 static struct tvp514x_platform_data *
1061 tvp514x_get_pdata(struct i2c_client *client)
1063 struct tvp514x_platform_data *pdata;
1064 struct v4l2_of_endpoint bus_cfg;
1065 struct device_node *endpoint;
1068 if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
1069 return client->dev.platform_data;
1071 endpoint = v4l2_of_get_next_endpoint(client->dev.of_node, NULL);
1075 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
1079 v4l2_of_parse_endpoint(endpoint, &bus_cfg);
1080 flags = bus_cfg.bus.parallel.flags;
1082 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1083 pdata->hs_polarity = 1;
1085 if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
1086 pdata->vs_polarity = 1;
1088 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
1089 pdata->clk_polarity = 1;
1092 of_node_put(endpoint);
1097 * tvp514x_probe() - decoder driver i2c probe handler
1098 * @client: i2c driver client device structure
1099 * @id: i2c driver id table
1101 * Register decoder as an i2c client device and V4L2
1105 tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1107 struct tvp514x_platform_data *pdata = tvp514x_get_pdata(client);
1108 struct tvp514x_decoder *decoder;
1109 struct v4l2_subdev *sd;
1112 if (pdata == NULL) {
1113 dev_err(&client->dev, "No platform data\n");
1117 /* Check if the adapter supports the needed features */
1118 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1121 decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL);
1125 /* Initialize the tvp514x_decoder with default configuration */
1126 *decoder = tvp514x_dev;
1127 /* Copy default register configuration */
1128 memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
1129 sizeof(tvp514x_reg_list_default));
1131 decoder->int_seq = (struct tvp514x_reg *)id->driver_data;
1133 /* Copy board specific information here */
1134 decoder->pdata = pdata;
1137 * Fetch platform specific data, and configure the
1138 * tvp514x_reg_list[] accordingly. Since this is one
1139 * time configuration, no need to preserve.
1141 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
1142 (decoder->pdata->clk_polarity << 1);
1143 decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
1144 ((decoder->pdata->hs_polarity << 2) |
1145 (decoder->pdata->vs_polarity << 3));
1146 /* Set default standard to auto */
1147 decoder->tvp514x_regs[REG_VIDEO_STD].val =
1148 VIDEO_STD_AUTO_SWITCH_BIT;
1150 /* Register with V4L2 layer as slave device */
1152 v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
1154 #if defined(CONFIG_MEDIA_CONTROLLER)
1155 decoder->pad.flags = MEDIA_PAD_FL_SOURCE;
1156 decoder->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1157 decoder->sd.entity.flags |= MEDIA_ENT_T_V4L2_SUBDEV_DECODER;
1159 ret = media_entity_init(&decoder->sd.entity, 1, &decoder->pad, 0);
1161 v4l2_err(sd, "%s decoder driver failed to register !!\n",
1166 v4l2_ctrl_handler_init(&decoder->hdl, 5);
1167 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1168 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1169 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1170 V4L2_CID_CONTRAST, 0, 255, 1, 128);
1171 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1172 V4L2_CID_SATURATION, 0, 255, 1, 128);
1173 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1174 V4L2_CID_HUE, -180, 180, 180, 0);
1175 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1176 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1177 sd->ctrl_handler = &decoder->hdl;
1178 if (decoder->hdl.error) {
1179 ret = decoder->hdl.error;
1182 v4l2_ctrl_handler_setup(&decoder->hdl);
1184 ret = v4l2_async_register_subdev(&decoder->sd);
1186 v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
1190 v4l2_ctrl_handler_free(&decoder->hdl);
1191 #if defined(CONFIG_MEDIA_CONTROLLER)
1192 media_entity_cleanup(&decoder->sd.entity);
1199 * tvp514x_remove() - decoder driver i2c remove handler
1200 * @client: i2c driver client device structure
1202 * Unregister decoder as an i2c client device and V4L2
1203 * device. Complement of tvp514x_probe().
1205 static int tvp514x_remove(struct i2c_client *client)
1207 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1208 struct tvp514x_decoder *decoder = to_decoder(sd);
1210 v4l2_async_unregister_subdev(&decoder->sd);
1211 v4l2_device_unregister_subdev(sd);
1212 #if defined(CONFIG_MEDIA_CONTROLLER)
1213 media_entity_cleanup(&decoder->sd.entity);
1215 v4l2_ctrl_handler_free(&decoder->hdl);
1218 /* TVP5146 Init/Power on Sequence */
1219 static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
1220 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1221 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1222 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1223 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1224 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1225 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1226 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1227 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1228 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1229 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1230 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1234 /* TVP5147 Init/Power on Sequence */
1235 static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
1236 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1237 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1238 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1239 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1240 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1241 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1242 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1243 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1244 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
1245 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1246 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
1247 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
1248 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1249 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1250 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1251 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1252 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1253 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1257 /* TVP5146M2/TVP5147M1 Init/Power on Sequence */
1258 static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1259 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1260 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1265 * I2C Device Table -
1267 * name - Name of the actual device/chip.
1268 * driver_data - Driver data
1270 static const struct i2c_device_id tvp514x_id[] = {
1271 {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
1272 {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
1273 {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
1274 {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
1278 MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1280 #if IS_ENABLED(CONFIG_OF)
1281 static const struct of_device_id tvp514x_of_match[] = {
1282 { .compatible = "ti,tvp5146", },
1283 { .compatible = "ti,tvp5146m2", },
1284 { .compatible = "ti,tvp5147", },
1285 { .compatible = "ti,tvp5147m1", },
1288 MODULE_DEVICE_TABLE(of, tvp514x_of_match);
1291 static struct i2c_driver tvp514x_driver = {
1293 .of_match_table = of_match_ptr(tvp514x_of_match),
1294 .owner = THIS_MODULE,
1295 .name = TVP514X_MODULE_NAME,
1297 .probe = tvp514x_probe,
1298 .remove = tvp514x_remove,
1299 .id_table = tvp514x_id,
1302 module_i2c_driver(tvp514x_driver);