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.
28 #include <linux/i2c.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/videodev2.h>
32 #include <linux/module.h>
33 #include <linux/v4l2-mediabus.h>
35 #include <linux/of_graph.h>
37 #include <media/v4l2-async.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-common.h>
40 #include <media/v4l2-mediabus.h>
41 #include <media/v4l2-fwnode.h>
42 #include <media/v4l2-ctrls.h>
43 #include <media/i2c/tvp514x.h>
44 #include <media/media-entity.h>
46 #include "tvp514x_regs.h"
48 /* Private macros for TVP */
49 #define I2C_RETRY_COUNT (5)
50 #define LOCK_RETRY_COUNT (5)
51 #define LOCK_RETRY_DELAY (200)
55 module_param(debug, bool, 0644);
56 MODULE_PARM_DESC(debug, "Debug level (0-1)");
58 MODULE_AUTHOR("Texas Instruments");
59 MODULE_DESCRIPTION("TVP514X linux decoder driver");
60 MODULE_LICENSE("GPL");
62 /* enum tvp514x_std - enum for supported standards */
70 * struct tvp514x_std_info - Structure to store standard informations
71 * @width: Line width in pixels
72 * @height:Number of active lines
73 * @video_std: Value to write in REG_VIDEO_STD register
74 * @standard: v4l2 standard structure information
76 struct tvp514x_std_info {
80 struct v4l2_standard standard;
83 static struct tvp514x_reg tvp514x_reg_list_default[0x40];
85 static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable);
87 * struct tvp514x_decoder - TVP5146/47 decoder object
88 * @sd: Subdevice Slave handle
89 * @tvp514x_regs: copy of hw's regs with preset values.
90 * @pdata: Board specific
92 * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
93 * @pix: Current pixel format
94 * @num_fmts: Number of formats
95 * @fmt_list: Format list
96 * @current_std: Current standard
97 * @num_stds: Number of standards
98 * @std_list: Standards list
99 * @input: Input routing at chip level
100 * @output: Output routing at chip level
102 struct tvp514x_decoder {
103 struct v4l2_subdev sd;
104 struct v4l2_ctrl_handler hdl;
105 struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
106 const struct tvp514x_platform_data *pdata;
111 struct v4l2_pix_format pix;
113 const struct v4l2_fmtdesc *fmt_list;
115 enum tvp514x_std current_std;
117 const struct tvp514x_std_info *std_list;
118 /* Input and Output Routing parameters */
122 /* mc related members */
123 struct media_pad pad;
124 struct v4l2_mbus_framefmt format;
126 struct tvp514x_reg *int_seq;
129 /* TVP514x default register values */
130 static struct tvp514x_reg tvp514x_reg_list_default[] = {
131 /* Composite selected */
132 {TOK_WRITE, REG_INPUT_SEL, 0x05},
133 {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
135 {TOK_WRITE, REG_VIDEO_STD, 0x00},
136 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
137 {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
138 {TOK_WRITE, REG_COLOR_KILLER, 0x10},
139 {TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
140 {TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
141 {TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
142 {TOK_WRITE, REG_BRIGHTNESS, 0x80},
143 {TOK_WRITE, REG_CONTRAST, 0x80},
144 {TOK_WRITE, REG_SATURATION, 0x80},
145 {TOK_WRITE, REG_HUE, 0x00},
146 {TOK_WRITE, REG_CHROMA_CONTROL1, 0x00},
147 {TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E},
149 {TOK_SKIP, 0x0F, 0x00},
150 {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
151 {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
152 {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
154 {TOK_SKIP, 0x13, 0x00},
155 {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
157 {TOK_SKIP, 0x15, 0x00},
159 {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
160 {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
161 {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
162 {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
164 {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
165 {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
166 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
167 {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
169 {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
170 {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
171 {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
172 {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
174 {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
175 {TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
176 {TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
177 {TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
179 {TOK_SKIP, 0x26, 0x00},
181 {TOK_SKIP, 0x27, 0x00},
182 {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
184 {TOK_SKIP, 0x29, 0x00},
185 {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
187 {TOK_SKIP, 0x2B, 0x00},
188 {TOK_SKIP, REG_SCART_DELAY, 0x00},
189 {TOK_SKIP, REG_CTI_DELAY, 0x00},
190 {TOK_SKIP, REG_CTI_CONTROL, 0x00},
192 {TOK_SKIP, 0x2F, 0x00},
194 {TOK_SKIP, 0x30, 0x00},
196 {TOK_SKIP, 0x31, 0x00},
197 /* HS, VS active high */
198 {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
200 {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00},
201 /* Enable clk & data */
202 {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11},
203 /* Enable AVID & FLD */
204 {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE},
206 {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
207 {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
208 {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
210 {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
215 * List of image formats supported by TVP5146/47 decoder
216 * Currently we are using 8 bit mode only, but can be
217 * extended to 10/20 bit mode.
219 static const struct v4l2_fmtdesc tvp514x_fmt_list[] = {
222 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
224 .description = "8-bit UYVY 4:2:2 Format",
225 .pixelformat = V4L2_PIX_FMT_UYVY,
230 * Supported standards -
232 * Currently supports two standards only, need to add support for rest of the
233 * modes, like SECAM, etc...
235 static const struct tvp514x_std_info tvp514x_std_list[] = {
236 /* Standard: STD_NTSC_MJ */
238 .width = NTSC_NUM_ACTIVE_PIXELS,
239 .height = NTSC_NUM_ACTIVE_LINES,
240 .video_std = VIDEO_STD_NTSC_MJ_BIT,
245 .frameperiod = {1001, 30000},
248 /* Standard: STD_PAL_BDGHIN */
251 .width = PAL_NUM_ACTIVE_PIXELS,
252 .height = PAL_NUM_ACTIVE_LINES,
253 .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
258 .frameperiod = {1, 25},
262 /* Standard: need to add for additional standard */
266 static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
268 return container_of(sd, struct tvp514x_decoder, sd);
271 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
273 return &container_of(ctrl->handler, struct tvp514x_decoder, hdl)->sd;
278 * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
279 * @sd: ptr to v4l2_subdev struct
280 * @reg: TVP5146/47 register address
282 * Returns value read if successful, or non-zero (-1) otherwise.
284 static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
287 struct i2c_client *client = v4l2_get_subdevdata(sd);
291 err = i2c_smbus_read_byte_data(client, reg);
293 if (retry <= I2C_RETRY_COUNT) {
294 v4l2_warn(sd, "Read: retry ... %d\n", retry);
296 msleep_interruptible(10);
305 * dump_reg() - dump the register content of TVP5146/47.
306 * @sd: ptr to v4l2_subdev struct
307 * @reg: TVP5146/47 register address
309 static void dump_reg(struct v4l2_subdev *sd, u8 reg)
313 val = tvp514x_read_reg(sd, reg);
314 v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
318 * tvp514x_write_reg() - Write a value to a register in TVP5146/47
319 * @sd: ptr to v4l2_subdev struct
320 * @reg: TVP5146/47 register address
321 * @val: value to be written to the register
323 * Write a value to a register in an TVP5146/47 decoder device.
324 * Returns zero if successful, or non-zero otherwise.
326 static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
329 struct i2c_client *client = v4l2_get_subdevdata(sd);
333 err = i2c_smbus_write_byte_data(client, reg, val);
335 if (retry <= I2C_RETRY_COUNT) {
336 v4l2_warn(sd, "Write: retry ... %d\n", retry);
338 msleep_interruptible(10);
347 * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
348 * @sd: ptr to v4l2_subdev struct
349 * @reglist: list of TVP5146/47 registers and values
351 * Initializes a list of TVP5146/47 registers:-
352 * if token is TOK_TERM, then entire write operation terminates
353 * if token is TOK_DELAY, then a delay of 'val' msec is introduced
354 * if token is TOK_SKIP, then the register write is skipped
355 * if token is TOK_WRITE, then the register write is performed
356 * Returns zero if successful, or non-zero otherwise.
358 static int tvp514x_write_regs(struct v4l2_subdev *sd,
359 const struct tvp514x_reg reglist[])
362 const struct tvp514x_reg *next = reglist;
364 for (; next->token != TOK_TERM; next++) {
365 if (next->token == TOK_DELAY) {
370 if (next->token == TOK_SKIP)
373 err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
375 v4l2_err(sd, "Write failed. Err[%d]\n", err);
383 * tvp514x_query_current_std() : Query the current standard detected by TVP5146/47
384 * @sd: ptr to v4l2_subdev struct
386 * Returns the current standard detected by TVP5146/47, STD_INVALID if there is no
389 static enum tvp514x_std tvp514x_query_current_std(struct v4l2_subdev *sd)
393 std = tvp514x_read_reg(sd, REG_VIDEO_STD);
394 if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
395 /* use the standard status register */
396 std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
398 /* use the standard register itself */
401 switch (std_status & VIDEO_STD_MASK) {
402 case VIDEO_STD_NTSC_MJ_BIT:
405 case VIDEO_STD_PAL_BDGHIN_BIT:
406 return STD_PAL_BDGHIN;
415 /* TVP5146/47 register dump function */
416 static void tvp514x_reg_dump(struct v4l2_subdev *sd)
418 dump_reg(sd, REG_INPUT_SEL);
419 dump_reg(sd, REG_AFE_GAIN_CTRL);
420 dump_reg(sd, REG_VIDEO_STD);
421 dump_reg(sd, REG_OPERATION_MODE);
422 dump_reg(sd, REG_COLOR_KILLER);
423 dump_reg(sd, REG_LUMA_CONTROL1);
424 dump_reg(sd, REG_LUMA_CONTROL2);
425 dump_reg(sd, REG_LUMA_CONTROL3);
426 dump_reg(sd, REG_BRIGHTNESS);
427 dump_reg(sd, REG_CONTRAST);
428 dump_reg(sd, REG_SATURATION);
429 dump_reg(sd, REG_HUE);
430 dump_reg(sd, REG_CHROMA_CONTROL1);
431 dump_reg(sd, REG_CHROMA_CONTROL2);
432 dump_reg(sd, REG_COMP_PR_SATURATION);
433 dump_reg(sd, REG_COMP_Y_CONTRAST);
434 dump_reg(sd, REG_COMP_PB_SATURATION);
435 dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
436 dump_reg(sd, REG_AVID_START_PIXEL_LSB);
437 dump_reg(sd, REG_AVID_START_PIXEL_MSB);
438 dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
439 dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
440 dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
441 dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
442 dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
443 dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
444 dump_reg(sd, REG_VSYNC_START_LINE_LSB);
445 dump_reg(sd, REG_VSYNC_START_LINE_MSB);
446 dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
447 dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
448 dump_reg(sd, REG_VBLK_START_LINE_LSB);
449 dump_reg(sd, REG_VBLK_START_LINE_MSB);
450 dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
451 dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
452 dump_reg(sd, REG_SYNC_CONTROL);
453 dump_reg(sd, REG_OUTPUT_FORMATTER1);
454 dump_reg(sd, REG_OUTPUT_FORMATTER2);
455 dump_reg(sd, REG_OUTPUT_FORMATTER3);
456 dump_reg(sd, REG_OUTPUT_FORMATTER4);
457 dump_reg(sd, REG_OUTPUT_FORMATTER5);
458 dump_reg(sd, REG_OUTPUT_FORMATTER6);
459 dump_reg(sd, REG_CLEAR_LOST_LOCK);
463 * tvp514x_configure() - Configure the TVP5146/47 registers
464 * @sd: ptr to v4l2_subdev struct
465 * @decoder: ptr to tvp514x_decoder structure
467 * Returns zero if successful, or non-zero otherwise.
469 static int tvp514x_configure(struct v4l2_subdev *sd,
470 struct tvp514x_decoder *decoder)
474 /* common register initialization */
476 tvp514x_write_regs(sd, decoder->tvp514x_regs);
481 tvp514x_reg_dump(sd);
487 * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
488 * @sd: pointer to standard V4L2 sub-device structure
489 * @decoder: pointer to tvp514x_decoder structure
491 * A device is considered to be detected if the chip ID (LSB and MSB)
492 * registers match the expected values.
493 * Any value of the rom version register is accepted.
494 * Returns ENODEV error number if no device is detected, or zero
495 * if a device is detected.
497 static int tvp514x_detect(struct v4l2_subdev *sd,
498 struct tvp514x_decoder *decoder)
500 u8 chip_id_msb, chip_id_lsb, rom_ver;
501 struct i2c_client *client = v4l2_get_subdevdata(sd);
503 chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
504 chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
505 rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
507 v4l2_dbg(1, debug, sd,
508 "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
509 chip_id_msb, chip_id_lsb, rom_ver);
510 if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
511 || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
512 && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
513 /* We didn't read the values we expected, so this must not be
516 v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
517 chip_id_msb, chip_id_lsb);
521 decoder->ver = rom_ver;
523 v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
524 client->name, decoder->ver,
525 client->addr << 1, client->adapter->name);
530 * tvp514x_querystd() - V4L2 decoder interface handler for querystd
531 * @sd: pointer to standard V4L2 sub-device structure
532 * @std_id: standard V4L2 std_id ioctl enum
534 * Returns the current standard detected by TVP5146/47. If no active input is
535 * detected then *std_id is set to 0 and the function returns 0.
537 static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
539 struct tvp514x_decoder *decoder = to_decoder(sd);
540 enum tvp514x_std current_std;
541 enum tvp514x_input input_sel;
542 u8 sync_lock_status, lock_mask;
547 /* To query the standard the TVP514x must power on the ADCs. */
548 if (!decoder->streaming) {
549 tvp514x_s_stream(sd, 1);
550 msleep(LOCK_RETRY_DELAY);
553 /* query the current standard */
554 current_std = tvp514x_query_current_std(sd);
555 if (current_std == STD_INVALID) {
556 *std_id = V4L2_STD_UNKNOWN;
560 input_sel = decoder->input;
563 case INPUT_CVBS_VI1A:
564 case INPUT_CVBS_VI1B:
565 case INPUT_CVBS_VI1C:
566 case INPUT_CVBS_VI2A:
567 case INPUT_CVBS_VI2B:
568 case INPUT_CVBS_VI2C:
569 case INPUT_CVBS_VI3A:
570 case INPUT_CVBS_VI3B:
571 case INPUT_CVBS_VI3C:
572 case INPUT_CVBS_VI4A:
573 lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
574 STATUS_HORZ_SYNC_LOCK_BIT |
575 STATUS_VIRT_SYNC_LOCK_BIT;
578 case INPUT_SVIDEO_VI2A_VI1A:
579 case INPUT_SVIDEO_VI2B_VI1B:
580 case INPUT_SVIDEO_VI2C_VI1C:
581 case INPUT_SVIDEO_VI2A_VI3A:
582 case INPUT_SVIDEO_VI2B_VI3B:
583 case INPUT_SVIDEO_VI2C_VI3C:
584 case INPUT_SVIDEO_VI4A_VI1A:
585 case INPUT_SVIDEO_VI4A_VI1B:
586 case INPUT_SVIDEO_VI4A_VI1C:
587 case INPUT_SVIDEO_VI4A_VI3A:
588 case INPUT_SVIDEO_VI4A_VI3B:
589 case INPUT_SVIDEO_VI4A_VI3C:
590 lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
591 STATUS_VIRT_SYNC_LOCK_BIT;
593 /*Need to add other interfaces*/
597 /* check whether signal is locked */
598 sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
599 if (lock_mask != (sync_lock_status & lock_mask)) {
600 *std_id = V4L2_STD_UNKNOWN;
601 return 0; /* No input detected */
604 *std_id &= decoder->std_list[current_std].standard.id;
606 v4l2_dbg(1, debug, sd, "Current STD: %s\n",
607 decoder->std_list[current_std].standard.name);
612 * tvp514x_s_std() - V4L2 decoder interface handler for s_std
613 * @sd: pointer to standard V4L2 sub-device structure
614 * @std_id: standard V4L2 v4l2_std_id ioctl enum
616 * If std_id is supported, sets the requested standard. Otherwise, returns
619 static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
621 struct tvp514x_decoder *decoder = to_decoder(sd);
624 for (i = 0; i < decoder->num_stds; i++)
625 if (std_id & decoder->std_list[i].standard.id)
628 if ((i == decoder->num_stds) || (i == STD_INVALID))
631 err = tvp514x_write_reg(sd, REG_VIDEO_STD,
632 decoder->std_list[i].video_std);
636 decoder->current_std = i;
637 decoder->tvp514x_regs[REG_VIDEO_STD].val =
638 decoder->std_list[i].video_std;
640 v4l2_dbg(1, debug, sd, "Standard set to: %s\n",
641 decoder->std_list[i].standard.name);
646 * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
647 * @sd: pointer to standard V4L2 sub-device structure
648 * @input: input selector for routing the signal
649 * @output: output selector for routing the signal
650 * @config: config value. Not used
652 * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
653 * the input is not supported or there is no active signal present in the
656 static int tvp514x_s_routing(struct v4l2_subdev *sd,
657 u32 input, u32 output, u32 config)
659 struct tvp514x_decoder *decoder = to_decoder(sd);
661 enum tvp514x_input input_sel;
662 enum tvp514x_output output_sel;
664 if ((input >= INPUT_INVALID) ||
665 (output >= OUTPUT_INVALID))
666 /* Index out of bound */
672 err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
676 output_sel |= tvp514x_read_reg(sd,
677 REG_OUTPUT_FORMATTER1) & 0x7;
678 err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
683 decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
684 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
685 decoder->input = input;
686 decoder->output = output;
688 v4l2_dbg(1, debug, sd, "Input set to: %d\n", input_sel);
694 * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
695 * @ctrl: pointer to v4l2_ctrl structure
697 * If the requested control is supported, sets the control's current
698 * value in HW. Otherwise, returns -EINVAL if the control is not supported.
700 static int tvp514x_s_ctrl(struct v4l2_ctrl *ctrl)
702 struct v4l2_subdev *sd = to_sd(ctrl);
703 struct tvp514x_decoder *decoder = to_decoder(sd);
704 int err = -EINVAL, value;
709 case V4L2_CID_BRIGHTNESS:
710 err = tvp514x_write_reg(sd, REG_BRIGHTNESS, value);
712 decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
714 case V4L2_CID_CONTRAST:
715 err = tvp514x_write_reg(sd, REG_CONTRAST, value);
717 decoder->tvp514x_regs[REG_CONTRAST].val = value;
719 case V4L2_CID_SATURATION:
720 err = tvp514x_write_reg(sd, REG_SATURATION, value);
722 decoder->tvp514x_regs[REG_SATURATION].val = value;
727 else if (value == -180)
729 err = tvp514x_write_reg(sd, REG_HUE, value);
731 decoder->tvp514x_regs[REG_HUE].val = value;
733 case V4L2_CID_AUTOGAIN:
734 err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value ? 0x0f : 0x0c);
736 decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
740 v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d\n",
741 ctrl->id, ctrl->val);
746 * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
747 * @sd: pointer to standard V4L2 sub-device structure
748 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
750 * Returns the decoder's video CAPTURE parameters.
753 tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
755 struct tvp514x_decoder *decoder = to_decoder(sd);
756 struct v4l2_captureparm *cparm;
757 enum tvp514x_std current_std;
762 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
763 /* only capture is supported */
766 /* get the current standard */
767 current_std = decoder->current_std;
769 cparm = &a->parm.capture;
770 cparm->capability = V4L2_CAP_TIMEPERFRAME;
771 cparm->timeperframe =
772 decoder->std_list[current_std].standard.frameperiod;
778 * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
779 * @sd: pointer to standard V4L2 sub-device structure
780 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
782 * Configures the decoder to use the input parameters, if possible. If
783 * not possible, returns the appropriate error code.
786 tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
788 struct tvp514x_decoder *decoder = to_decoder(sd);
789 struct v4l2_fract *timeperframe;
790 enum tvp514x_std current_std;
795 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
796 /* only capture is supported */
799 timeperframe = &a->parm.capture.timeperframe;
801 /* get the current standard */
802 current_std = decoder->current_std;
805 decoder->std_list[current_std].standard.frameperiod;
811 * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
812 * @sd: pointer to standard V4L2 sub-device structure
813 * @enable: streaming enable or disable
815 * Sets streaming to enable or disable, if possible.
817 static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
820 struct tvp514x_decoder *decoder = to_decoder(sd);
822 if (decoder->streaming == enable)
828 /* Power Down Sequence */
829 err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
831 v4l2_err(sd, "Unable to turn off decoder\n");
834 decoder->streaming = enable;
839 /* Power Up Sequence */
840 err = tvp514x_write_regs(sd, decoder->int_seq);
842 v4l2_err(sd, "Unable to turn on decoder\n");
845 /* Detect if not already detected */
846 err = tvp514x_detect(sd, decoder);
848 v4l2_err(sd, "Unable to detect decoder\n");
851 err = tvp514x_configure(sd, decoder);
853 v4l2_err(sd, "Unable to configure decoder\n");
856 decoder->streaming = enable;
867 static const struct v4l2_ctrl_ops tvp514x_ctrl_ops = {
868 .s_ctrl = tvp514x_s_ctrl,
872 * tvp514x_enum_mbus_code() - V4L2 decoder interface handler for enum_mbus_code
873 * @sd: pointer to standard V4L2 sub-device structure
874 * @cfg: pad configuration
875 * @code: pointer to v4l2_subdev_mbus_code_enum structure
877 * Enumertaes mbus codes supported
879 static int tvp514x_enum_mbus_code(struct v4l2_subdev *sd,
880 struct v4l2_subdev_pad_config *cfg,
881 struct v4l2_subdev_mbus_code_enum *code)
884 u32 index = code->index;
886 memset(code, 0, sizeof(*code));
893 code->code = MEDIA_BUS_FMT_UYVY8_2X8;
899 * tvp514x_get_pad_format() - V4L2 decoder interface handler for get pad format
900 * @sd: pointer to standard V4L2 sub-device structure
901 * @cfg: pad configuration
902 * @format: pointer to v4l2_subdev_format structure
904 * Retrieves pad format which is active or tried based on requirement
906 static int tvp514x_get_pad_format(struct v4l2_subdev *sd,
907 struct v4l2_subdev_pad_config *cfg,
908 struct v4l2_subdev_format *format)
910 struct tvp514x_decoder *decoder = to_decoder(sd);
911 __u32 which = format->which;
916 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
917 format->format = decoder->format;
921 format->format.code = MEDIA_BUS_FMT_UYVY8_2X8;
922 format->format.width = tvp514x_std_list[decoder->current_std].width;
923 format->format.height = tvp514x_std_list[decoder->current_std].height;
924 format->format.colorspace = V4L2_COLORSPACE_SMPTE170M;
925 format->format.field = V4L2_FIELD_INTERLACED;
931 * tvp514x_set_pad_format() - V4L2 decoder interface handler for set pad format
932 * @sd: pointer to standard V4L2 sub-device structure
933 * @cfg: pad configuration
934 * @format: pointer to v4l2_subdev_format structure
936 * Set pad format for the output pad
938 static int tvp514x_set_pad_format(struct v4l2_subdev *sd,
939 struct v4l2_subdev_pad_config *cfg,
940 struct v4l2_subdev_format *fmt)
942 struct tvp514x_decoder *decoder = to_decoder(sd);
944 if (fmt->format.field != V4L2_FIELD_INTERLACED ||
945 fmt->format.code != MEDIA_BUS_FMT_UYVY8_2X8 ||
946 fmt->format.colorspace != V4L2_COLORSPACE_SMPTE170M ||
947 fmt->format.width != tvp514x_std_list[decoder->current_std].width ||
948 fmt->format.height != tvp514x_std_list[decoder->current_std].height)
951 decoder->format = fmt->format;
956 static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
957 .s_std = tvp514x_s_std,
958 .s_routing = tvp514x_s_routing,
959 .querystd = tvp514x_querystd,
960 .g_parm = tvp514x_g_parm,
961 .s_parm = tvp514x_s_parm,
962 .s_stream = tvp514x_s_stream,
965 static const struct v4l2_subdev_pad_ops tvp514x_pad_ops = {
966 .enum_mbus_code = tvp514x_enum_mbus_code,
967 .get_fmt = tvp514x_get_pad_format,
968 .set_fmt = tvp514x_set_pad_format,
971 static const struct v4l2_subdev_ops tvp514x_ops = {
972 .video = &tvp514x_video_ops,
973 .pad = &tvp514x_pad_ops,
976 static const struct tvp514x_decoder tvp514x_dev = {
978 .fmt_list = tvp514x_fmt_list,
979 .num_fmts = ARRAY_SIZE(tvp514x_fmt_list),
981 /* Default to NTSC 8-bit YUV 422 */
982 .width = NTSC_NUM_ACTIVE_PIXELS,
983 .height = NTSC_NUM_ACTIVE_LINES,
984 .pixelformat = V4L2_PIX_FMT_UYVY,
985 .field = V4L2_FIELD_INTERLACED,
986 .bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2,
987 .sizeimage = NTSC_NUM_ACTIVE_PIXELS * 2 *
988 NTSC_NUM_ACTIVE_LINES,
989 .colorspace = V4L2_COLORSPACE_SMPTE170M,
991 .current_std = STD_NTSC_MJ,
992 .std_list = tvp514x_std_list,
993 .num_stds = ARRAY_SIZE(tvp514x_std_list),
997 static struct tvp514x_platform_data *
998 tvp514x_get_pdata(struct i2c_client *client)
1000 struct tvp514x_platform_data *pdata = NULL;
1001 struct v4l2_fwnode_endpoint bus_cfg;
1002 struct device_node *endpoint;
1005 if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
1006 return client->dev.platform_data;
1008 endpoint = of_graph_get_next_endpoint(client->dev.of_node, NULL);
1012 if (v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint), &bus_cfg))
1015 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
1019 flags = bus_cfg.bus.parallel.flags;
1021 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1022 pdata->hs_polarity = 1;
1024 if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
1025 pdata->vs_polarity = 1;
1027 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
1028 pdata->clk_polarity = 1;
1031 of_node_put(endpoint);
1036 * tvp514x_probe() - decoder driver i2c probe handler
1037 * @client: i2c driver client device structure
1038 * @id: i2c driver id table
1040 * Register decoder as an i2c client device and V4L2
1044 tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1046 struct tvp514x_platform_data *pdata = tvp514x_get_pdata(client);
1047 struct tvp514x_decoder *decoder;
1048 struct v4l2_subdev *sd;
1051 if (pdata == NULL) {
1052 dev_err(&client->dev, "No platform data\n");
1056 /* Check if the adapter supports the needed features */
1057 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1060 decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL);
1064 /* Initialize the tvp514x_decoder with default configuration */
1065 *decoder = tvp514x_dev;
1066 /* Copy default register configuration */
1067 memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
1068 sizeof(tvp514x_reg_list_default));
1070 decoder->int_seq = (struct tvp514x_reg *)id->driver_data;
1072 /* Copy board specific information here */
1073 decoder->pdata = pdata;
1076 * Fetch platform specific data, and configure the
1077 * tvp514x_reg_list[] accordingly. Since this is one
1078 * time configuration, no need to preserve.
1080 decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
1081 (decoder->pdata->clk_polarity << 1);
1082 decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
1083 ((decoder->pdata->hs_polarity << 2) |
1084 (decoder->pdata->vs_polarity << 3));
1085 /* Set default standard to auto */
1086 decoder->tvp514x_regs[REG_VIDEO_STD].val =
1087 VIDEO_STD_AUTO_SWITCH_BIT;
1089 /* Register with V4L2 layer as slave device */
1091 v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
1093 #if defined(CONFIG_MEDIA_CONTROLLER)
1094 decoder->pad.flags = MEDIA_PAD_FL_SOURCE;
1095 decoder->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1096 decoder->sd.entity.flags |= MEDIA_ENT_F_ATV_DECODER;
1098 ret = media_entity_pads_init(&decoder->sd.entity, 1, &decoder->pad);
1100 v4l2_err(sd, "%s decoder driver failed to register !!\n",
1105 v4l2_ctrl_handler_init(&decoder->hdl, 5);
1106 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1107 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1108 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1109 V4L2_CID_CONTRAST, 0, 255, 1, 128);
1110 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1111 V4L2_CID_SATURATION, 0, 255, 1, 128);
1112 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1113 V4L2_CID_HUE, -180, 180, 180, 0);
1114 v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
1115 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1116 sd->ctrl_handler = &decoder->hdl;
1117 if (decoder->hdl.error) {
1118 ret = decoder->hdl.error;
1121 v4l2_ctrl_handler_setup(&decoder->hdl);
1123 ret = v4l2_async_register_subdev(&decoder->sd);
1125 v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
1129 v4l2_ctrl_handler_free(&decoder->hdl);
1130 #if defined(CONFIG_MEDIA_CONTROLLER)
1131 media_entity_cleanup(&decoder->sd.entity);
1138 * tvp514x_remove() - decoder driver i2c remove handler
1139 * @client: i2c driver client device structure
1141 * Unregister decoder as an i2c client device and V4L2
1142 * device. Complement of tvp514x_probe().
1144 static int tvp514x_remove(struct i2c_client *client)
1146 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1147 struct tvp514x_decoder *decoder = to_decoder(sd);
1149 v4l2_async_unregister_subdev(&decoder->sd);
1150 #if defined(CONFIG_MEDIA_CONTROLLER)
1151 media_entity_cleanup(&decoder->sd.entity);
1153 v4l2_ctrl_handler_free(&decoder->hdl);
1156 /* TVP5146 Init/Power on Sequence */
1157 static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
1158 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1159 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1160 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1161 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1162 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1163 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1164 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1165 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1166 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1167 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1168 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1172 /* TVP5147 Init/Power on Sequence */
1173 static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
1174 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1175 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1176 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1177 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1178 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1179 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1180 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1181 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1182 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
1183 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1184 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
1185 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
1186 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1187 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1188 {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1189 {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1190 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1191 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1195 /* TVP5146M2/TVP5147M1 Init/Power on Sequence */
1196 static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1197 {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1198 {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1203 * I2C Device Table -
1205 * name - Name of the actual device/chip.
1206 * driver_data - Driver data
1208 static const struct i2c_device_id tvp514x_id[] = {
1209 {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
1210 {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
1211 {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
1212 {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
1216 MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1218 #if IS_ENABLED(CONFIG_OF)
1219 static const struct of_device_id tvp514x_of_match[] = {
1220 { .compatible = "ti,tvp5146", },
1221 { .compatible = "ti,tvp5146m2", },
1222 { .compatible = "ti,tvp5147", },
1223 { .compatible = "ti,tvp5147m1", },
1226 MODULE_DEVICE_TABLE(of, tvp514x_of_match);
1229 static struct i2c_driver tvp514x_driver = {
1231 .of_match_table = of_match_ptr(tvp514x_of_match),
1232 .name = TVP514X_MODULE_NAME,
1234 .probe = tvp514x_probe,
1235 .remove = tvp514x_remove,
1236 .id_table = tvp514x_id,
1239 module_i2c_driver(tvp514x_driver);