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>
39 #include <linux/of_graph.h>
41 #include <media/v4l2-async.h>
42 #include <media/v4l2-device.h>
43 #include <media/v4l2-common.h>
44 #include <media/v4l2-mediabus.h>
45 #include <media/v4l2-of.h>
46 #include <media/v4l2-ctrls.h>
47 #include <media/i2c/tvp514x.h>
48 #include <media/media-entity.h>
50 #include "tvp514x_regs.h"
52 /* Private macros for TVP */
53 #define I2C_RETRY_COUNT (5)
54 #define LOCK_RETRY_COUNT (5)
55 #define LOCK_RETRY_DELAY (200)
59 module_param(debug
, bool, 0644);
60 MODULE_PARM_DESC(debug
, "Debug level (0-1)");
62 MODULE_AUTHOR("Texas Instruments");
63 MODULE_DESCRIPTION("TVP514X linux decoder driver");
64 MODULE_LICENSE("GPL");
66 /* enum tvp514x_std - enum for supported standards */
74 * struct tvp514x_std_info - Structure to store standard informations
75 * @width: Line width in pixels
76 * @height:Number of active lines
77 * @video_std: Value to write in REG_VIDEO_STD register
78 * @standard: v4l2 standard structure information
80 struct tvp514x_std_info
{
84 struct v4l2_standard standard
;
87 static struct tvp514x_reg tvp514x_reg_list_default
[0x40];
89 static int tvp514x_s_stream(struct v4l2_subdev
*sd
, int enable
);
91 * struct tvp514x_decoder - TVP5146/47 decoder object
92 * @sd: Subdevice Slave handle
93 * @tvp514x_regs: copy of hw's regs with preset values.
94 * @pdata: Board specific
96 * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
97 * @pix: Current pixel format
98 * @num_fmts: Number of formats
99 * @fmt_list: Format list
100 * @current_std: Current standard
101 * @num_stds: Number of standards
102 * @std_list: Standards list
103 * @input: Input routing at chip level
104 * @output: Output routing at chip level
106 struct tvp514x_decoder
{
107 struct v4l2_subdev sd
;
108 struct v4l2_ctrl_handler hdl
;
109 struct tvp514x_reg tvp514x_regs
[ARRAY_SIZE(tvp514x_reg_list_default
)];
110 const struct tvp514x_platform_data
*pdata
;
115 struct v4l2_pix_format pix
;
117 const struct v4l2_fmtdesc
*fmt_list
;
119 enum tvp514x_std current_std
;
121 const struct tvp514x_std_info
*std_list
;
122 /* Input and Output Routing parameters */
126 /* mc related members */
127 struct media_pad pad
;
128 struct v4l2_mbus_framefmt format
;
130 struct tvp514x_reg
*int_seq
;
133 /* TVP514x default register values */
134 static struct tvp514x_reg tvp514x_reg_list_default
[] = {
135 /* Composite selected */
136 {TOK_WRITE
, REG_INPUT_SEL
, 0x05},
137 {TOK_WRITE
, REG_AFE_GAIN_CTRL
, 0x0F},
139 {TOK_WRITE
, REG_VIDEO_STD
, 0x00},
140 {TOK_WRITE
, REG_OPERATION_MODE
, 0x00},
141 {TOK_SKIP
, REG_AUTOSWITCH_MASK
, 0x3F},
142 {TOK_WRITE
, REG_COLOR_KILLER
, 0x10},
143 {TOK_WRITE
, REG_LUMA_CONTROL1
, 0x00},
144 {TOK_WRITE
, REG_LUMA_CONTROL2
, 0x00},
145 {TOK_WRITE
, REG_LUMA_CONTROL3
, 0x02},
146 {TOK_WRITE
, REG_BRIGHTNESS
, 0x80},
147 {TOK_WRITE
, REG_CONTRAST
, 0x80},
148 {TOK_WRITE
, REG_SATURATION
, 0x80},
149 {TOK_WRITE
, REG_HUE
, 0x00},
150 {TOK_WRITE
, REG_CHROMA_CONTROL1
, 0x00},
151 {TOK_WRITE
, REG_CHROMA_CONTROL2
, 0x0E},
153 {TOK_SKIP
, 0x0F, 0x00},
154 {TOK_WRITE
, REG_COMP_PR_SATURATION
, 0x80},
155 {TOK_WRITE
, REG_COMP_Y_CONTRAST
, 0x80},
156 {TOK_WRITE
, REG_COMP_PB_SATURATION
, 0x80},
158 {TOK_SKIP
, 0x13, 0x00},
159 {TOK_WRITE
, REG_COMP_Y_BRIGHTNESS
, 0x80},
161 {TOK_SKIP
, 0x15, 0x00},
163 {TOK_SKIP
, REG_AVID_START_PIXEL_LSB
, 0x55},
164 {TOK_SKIP
, REG_AVID_START_PIXEL_MSB
, 0x00},
165 {TOK_SKIP
, REG_AVID_STOP_PIXEL_LSB
, 0x25},
166 {TOK_SKIP
, REG_AVID_STOP_PIXEL_MSB
, 0x03},
168 {TOK_SKIP
, REG_HSYNC_START_PIXEL_LSB
, 0x00},
169 {TOK_SKIP
, REG_HSYNC_START_PIXEL_MSB
, 0x00},
170 {TOK_SKIP
, REG_HSYNC_STOP_PIXEL_LSB
, 0x40},
171 {TOK_SKIP
, REG_HSYNC_STOP_PIXEL_MSB
, 0x00},
173 {TOK_SKIP
, REG_VSYNC_START_LINE_LSB
, 0x04},
174 {TOK_SKIP
, REG_VSYNC_START_LINE_MSB
, 0x00},
175 {TOK_SKIP
, REG_VSYNC_STOP_LINE_LSB
, 0x07},
176 {TOK_SKIP
, REG_VSYNC_STOP_LINE_MSB
, 0x00},
178 {TOK_SKIP
, REG_VBLK_START_LINE_LSB
, 0x01},
179 {TOK_SKIP
, REG_VBLK_START_LINE_MSB
, 0x00},
180 {TOK_SKIP
, REG_VBLK_STOP_LINE_LSB
, 0x15},
181 {TOK_SKIP
, REG_VBLK_STOP_LINE_MSB
, 0x00},
183 {TOK_SKIP
, 0x26, 0x00},
185 {TOK_SKIP
, 0x27, 0x00},
186 {TOK_SKIP
, REG_FAST_SWTICH_CONTROL
, 0xCC},
188 {TOK_SKIP
, 0x29, 0x00},
189 {TOK_SKIP
, REG_FAST_SWTICH_SCART_DELAY
, 0x00},
191 {TOK_SKIP
, 0x2B, 0x00},
192 {TOK_SKIP
, REG_SCART_DELAY
, 0x00},
193 {TOK_SKIP
, REG_CTI_DELAY
, 0x00},
194 {TOK_SKIP
, REG_CTI_CONTROL
, 0x00},
196 {TOK_SKIP
, 0x2F, 0x00},
198 {TOK_SKIP
, 0x30, 0x00},
200 {TOK_SKIP
, 0x31, 0x00},
201 /* HS, VS active high */
202 {TOK_WRITE
, REG_SYNC_CONTROL
, 0x00},
204 {TOK_WRITE
, REG_OUTPUT_FORMATTER1
, 0x00},
205 /* Enable clk & data */
206 {TOK_WRITE
, REG_OUTPUT_FORMATTER2
, 0x11},
207 /* Enable AVID & FLD */
208 {TOK_WRITE
, REG_OUTPUT_FORMATTER3
, 0xEE},
210 {TOK_WRITE
, REG_OUTPUT_FORMATTER4
, 0xAF},
211 {TOK_WRITE
, REG_OUTPUT_FORMATTER5
, 0xFF},
212 {TOK_WRITE
, REG_OUTPUT_FORMATTER6
, 0xFF},
214 {TOK_WRITE
, REG_CLEAR_LOST_LOCK
, 0x01},
219 * List of image formats supported by TVP5146/47 decoder
220 * Currently we are using 8 bit mode only, but can be
221 * extended to 10/20 bit mode.
223 static const struct v4l2_fmtdesc tvp514x_fmt_list
[] = {
226 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
228 .description
= "8-bit UYVY 4:2:2 Format",
229 .pixelformat
= V4L2_PIX_FMT_UYVY
,
234 * Supported standards -
236 * Currently supports two standards only, need to add support for rest of the
237 * modes, like SECAM, etc...
239 static const struct tvp514x_std_info tvp514x_std_list
[] = {
240 /* Standard: STD_NTSC_MJ */
242 .width
= NTSC_NUM_ACTIVE_PIXELS
,
243 .height
= NTSC_NUM_ACTIVE_LINES
,
244 .video_std
= VIDEO_STD_NTSC_MJ_BIT
,
249 .frameperiod
= {1001, 30000},
252 /* Standard: STD_PAL_BDGHIN */
255 .width
= PAL_NUM_ACTIVE_PIXELS
,
256 .height
= PAL_NUM_ACTIVE_LINES
,
257 .video_std
= VIDEO_STD_PAL_BDGHIN_BIT
,
262 .frameperiod
= {1, 25},
266 /* Standard: need to add for additional standard */
270 static inline struct tvp514x_decoder
*to_decoder(struct v4l2_subdev
*sd
)
272 return container_of(sd
, struct tvp514x_decoder
, sd
);
275 static inline struct v4l2_subdev
*to_sd(struct v4l2_ctrl
*ctrl
)
277 return &container_of(ctrl
->handler
, struct tvp514x_decoder
, hdl
)->sd
;
282 * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
283 * @sd: ptr to v4l2_subdev struct
284 * @reg: TVP5146/47 register address
286 * Returns value read if successful, or non-zero (-1) otherwise.
288 static int tvp514x_read_reg(struct v4l2_subdev
*sd
, u8 reg
)
291 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
295 err
= i2c_smbus_read_byte_data(client
, reg
);
297 if (retry
<= I2C_RETRY_COUNT
) {
298 v4l2_warn(sd
, "Read: retry ... %d\n", retry
);
300 msleep_interruptible(10);
309 * dump_reg() - dump the register content of TVP5146/47.
310 * @sd: ptr to v4l2_subdev struct
311 * @reg: TVP5146/47 register address
313 static void dump_reg(struct v4l2_subdev
*sd
, u8 reg
)
317 val
= tvp514x_read_reg(sd
, reg
);
318 v4l2_info(sd
, "Reg(0x%.2X): 0x%.2X\n", reg
, val
);
322 * tvp514x_write_reg() - Write a value to a register in TVP5146/47
323 * @sd: ptr to v4l2_subdev struct
324 * @reg: TVP5146/47 register address
325 * @val: value to be written to the register
327 * Write a value to a register in an TVP5146/47 decoder device.
328 * Returns zero if successful, or non-zero otherwise.
330 static int tvp514x_write_reg(struct v4l2_subdev
*sd
, u8 reg
, u8 val
)
333 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
337 err
= i2c_smbus_write_byte_data(client
, reg
, val
);
339 if (retry
<= I2C_RETRY_COUNT
) {
340 v4l2_warn(sd
, "Write: retry ... %d\n", retry
);
342 msleep_interruptible(10);
351 * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
352 * @sd: ptr to v4l2_subdev struct
353 * @reglist: list of TVP5146/47 registers and values
355 * Initializes a list of TVP5146/47 registers:-
356 * if token is TOK_TERM, then entire write operation terminates
357 * if token is TOK_DELAY, then a delay of 'val' msec is introduced
358 * if token is TOK_SKIP, then the register write is skipped
359 * if token is TOK_WRITE, then the register write is performed
360 * Returns zero if successful, or non-zero otherwise.
362 static int tvp514x_write_regs(struct v4l2_subdev
*sd
,
363 const struct tvp514x_reg reglist
[])
366 const struct tvp514x_reg
*next
= reglist
;
368 for (; next
->token
!= TOK_TERM
; next
++) {
369 if (next
->token
== TOK_DELAY
) {
374 if (next
->token
== TOK_SKIP
)
377 err
= tvp514x_write_reg(sd
, next
->reg
, (u8
) next
->val
);
379 v4l2_err(sd
, "Write failed. Err[%d]\n", err
);
387 * tvp514x_query_current_std() : Query the current standard detected by TVP5146/47
388 * @sd: ptr to v4l2_subdev struct
390 * Returns the current standard detected by TVP5146/47, STD_INVALID if there is no
393 static enum tvp514x_std
tvp514x_query_current_std(struct v4l2_subdev
*sd
)
397 std
= tvp514x_read_reg(sd
, REG_VIDEO_STD
);
398 if ((std
& VIDEO_STD_MASK
) == VIDEO_STD_AUTO_SWITCH_BIT
)
399 /* use the standard status register */
400 std_status
= tvp514x_read_reg(sd
, REG_VIDEO_STD_STATUS
);
402 /* use the standard register itself */
405 switch (std_status
& VIDEO_STD_MASK
) {
406 case VIDEO_STD_NTSC_MJ_BIT
:
409 case VIDEO_STD_PAL_BDGHIN_BIT
:
410 return STD_PAL_BDGHIN
;
419 /* TVP5146/47 register dump function */
420 static void tvp514x_reg_dump(struct v4l2_subdev
*sd
)
422 dump_reg(sd
, REG_INPUT_SEL
);
423 dump_reg(sd
, REG_AFE_GAIN_CTRL
);
424 dump_reg(sd
, REG_VIDEO_STD
);
425 dump_reg(sd
, REG_OPERATION_MODE
);
426 dump_reg(sd
, REG_COLOR_KILLER
);
427 dump_reg(sd
, REG_LUMA_CONTROL1
);
428 dump_reg(sd
, REG_LUMA_CONTROL2
);
429 dump_reg(sd
, REG_LUMA_CONTROL3
);
430 dump_reg(sd
, REG_BRIGHTNESS
);
431 dump_reg(sd
, REG_CONTRAST
);
432 dump_reg(sd
, REG_SATURATION
);
433 dump_reg(sd
, REG_HUE
);
434 dump_reg(sd
, REG_CHROMA_CONTROL1
);
435 dump_reg(sd
, REG_CHROMA_CONTROL2
);
436 dump_reg(sd
, REG_COMP_PR_SATURATION
);
437 dump_reg(sd
, REG_COMP_Y_CONTRAST
);
438 dump_reg(sd
, REG_COMP_PB_SATURATION
);
439 dump_reg(sd
, REG_COMP_Y_BRIGHTNESS
);
440 dump_reg(sd
, REG_AVID_START_PIXEL_LSB
);
441 dump_reg(sd
, REG_AVID_START_PIXEL_MSB
);
442 dump_reg(sd
, REG_AVID_STOP_PIXEL_LSB
);
443 dump_reg(sd
, REG_AVID_STOP_PIXEL_MSB
);
444 dump_reg(sd
, REG_HSYNC_START_PIXEL_LSB
);
445 dump_reg(sd
, REG_HSYNC_START_PIXEL_MSB
);
446 dump_reg(sd
, REG_HSYNC_STOP_PIXEL_LSB
);
447 dump_reg(sd
, REG_HSYNC_STOP_PIXEL_MSB
);
448 dump_reg(sd
, REG_VSYNC_START_LINE_LSB
);
449 dump_reg(sd
, REG_VSYNC_START_LINE_MSB
);
450 dump_reg(sd
, REG_VSYNC_STOP_LINE_LSB
);
451 dump_reg(sd
, REG_VSYNC_STOP_LINE_MSB
);
452 dump_reg(sd
, REG_VBLK_START_LINE_LSB
);
453 dump_reg(sd
, REG_VBLK_START_LINE_MSB
);
454 dump_reg(sd
, REG_VBLK_STOP_LINE_LSB
);
455 dump_reg(sd
, REG_VBLK_STOP_LINE_MSB
);
456 dump_reg(sd
, REG_SYNC_CONTROL
);
457 dump_reg(sd
, REG_OUTPUT_FORMATTER1
);
458 dump_reg(sd
, REG_OUTPUT_FORMATTER2
);
459 dump_reg(sd
, REG_OUTPUT_FORMATTER3
);
460 dump_reg(sd
, REG_OUTPUT_FORMATTER4
);
461 dump_reg(sd
, REG_OUTPUT_FORMATTER5
);
462 dump_reg(sd
, REG_OUTPUT_FORMATTER6
);
463 dump_reg(sd
, REG_CLEAR_LOST_LOCK
);
467 * tvp514x_configure() - Configure the TVP5146/47 registers
468 * @sd: ptr to v4l2_subdev struct
469 * @decoder: ptr to tvp514x_decoder structure
471 * Returns zero if successful, or non-zero otherwise.
473 static int tvp514x_configure(struct v4l2_subdev
*sd
,
474 struct tvp514x_decoder
*decoder
)
478 /* common register initialization */
480 tvp514x_write_regs(sd
, decoder
->tvp514x_regs
);
485 tvp514x_reg_dump(sd
);
491 * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
492 * @sd: pointer to standard V4L2 sub-device structure
493 * @decoder: pointer to tvp514x_decoder structure
495 * A device is considered to be detected if the chip ID (LSB and MSB)
496 * registers match the expected values.
497 * Any value of the rom version register is accepted.
498 * Returns ENODEV error number if no device is detected, or zero
499 * if a device is detected.
501 static int tvp514x_detect(struct v4l2_subdev
*sd
,
502 struct tvp514x_decoder
*decoder
)
504 u8 chip_id_msb
, chip_id_lsb
, rom_ver
;
505 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
507 chip_id_msb
= tvp514x_read_reg(sd
, REG_CHIP_ID_MSB
);
508 chip_id_lsb
= tvp514x_read_reg(sd
, REG_CHIP_ID_LSB
);
509 rom_ver
= tvp514x_read_reg(sd
, REG_ROM_VERSION
);
511 v4l2_dbg(1, debug
, sd
,
512 "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
513 chip_id_msb
, chip_id_lsb
, rom_ver
);
514 if ((chip_id_msb
!= TVP514X_CHIP_ID_MSB
)
515 || ((chip_id_lsb
!= TVP5146_CHIP_ID_LSB
)
516 && (chip_id_lsb
!= TVP5147_CHIP_ID_LSB
))) {
517 /* We didn't read the values we expected, so this must not be
520 v4l2_err(sd
, "chip id mismatch msb:0x%x lsb:0x%x\n",
521 chip_id_msb
, chip_id_lsb
);
525 decoder
->ver
= rom_ver
;
527 v4l2_info(sd
, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
528 client
->name
, decoder
->ver
,
529 client
->addr
<< 1, client
->adapter
->name
);
534 * tvp514x_querystd() - V4L2 decoder interface handler for querystd
535 * @sd: pointer to standard V4L2 sub-device structure
536 * @std_id: standard V4L2 std_id ioctl enum
538 * Returns the current standard detected by TVP5146/47. If no active input is
539 * detected then *std_id is set to 0 and the function returns 0.
541 static int tvp514x_querystd(struct v4l2_subdev
*sd
, v4l2_std_id
*std_id
)
543 struct tvp514x_decoder
*decoder
= to_decoder(sd
);
544 enum tvp514x_std current_std
;
545 enum tvp514x_input input_sel
;
546 u8 sync_lock_status
, lock_mask
;
551 /* To query the standard the TVP514x must power on the ADCs. */
552 if (!decoder
->streaming
) {
553 tvp514x_s_stream(sd
, 1);
554 msleep(LOCK_RETRY_DELAY
);
557 /* query the current standard */
558 current_std
= tvp514x_query_current_std(sd
);
559 if (current_std
== STD_INVALID
) {
560 *std_id
= V4L2_STD_UNKNOWN
;
564 input_sel
= decoder
->input
;
567 case INPUT_CVBS_VI1A
:
568 case INPUT_CVBS_VI1B
:
569 case INPUT_CVBS_VI1C
:
570 case INPUT_CVBS_VI2A
:
571 case INPUT_CVBS_VI2B
:
572 case INPUT_CVBS_VI2C
:
573 case INPUT_CVBS_VI3A
:
574 case INPUT_CVBS_VI3B
:
575 case INPUT_CVBS_VI3C
:
576 case INPUT_CVBS_VI4A
:
577 lock_mask
= STATUS_CLR_SUBCAR_LOCK_BIT
|
578 STATUS_HORZ_SYNC_LOCK_BIT
|
579 STATUS_VIRT_SYNC_LOCK_BIT
;
582 case INPUT_SVIDEO_VI2A_VI1A
:
583 case INPUT_SVIDEO_VI2B_VI1B
:
584 case INPUT_SVIDEO_VI2C_VI1C
:
585 case INPUT_SVIDEO_VI2A_VI3A
:
586 case INPUT_SVIDEO_VI2B_VI3B
:
587 case INPUT_SVIDEO_VI2C_VI3C
:
588 case INPUT_SVIDEO_VI4A_VI1A
:
589 case INPUT_SVIDEO_VI4A_VI1B
:
590 case INPUT_SVIDEO_VI4A_VI1C
:
591 case INPUT_SVIDEO_VI4A_VI3A
:
592 case INPUT_SVIDEO_VI4A_VI3B
:
593 case INPUT_SVIDEO_VI4A_VI3C
:
594 lock_mask
= STATUS_HORZ_SYNC_LOCK_BIT
|
595 STATUS_VIRT_SYNC_LOCK_BIT
;
597 /*Need to add other interfaces*/
601 /* check whether signal is locked */
602 sync_lock_status
= tvp514x_read_reg(sd
, REG_STATUS1
);
603 if (lock_mask
!= (sync_lock_status
& lock_mask
)) {
604 *std_id
= V4L2_STD_UNKNOWN
;
605 return 0; /* No input detected */
608 *std_id
&= decoder
->std_list
[current_std
].standard
.id
;
610 v4l2_dbg(1, debug
, sd
, "Current STD: %s\n",
611 decoder
->std_list
[current_std
].standard
.name
);
616 * tvp514x_s_std() - V4L2 decoder interface handler for s_std
617 * @sd: pointer to standard V4L2 sub-device structure
618 * @std_id: standard V4L2 v4l2_std_id ioctl enum
620 * If std_id is supported, sets the requested standard. Otherwise, returns
623 static int tvp514x_s_std(struct v4l2_subdev
*sd
, v4l2_std_id std_id
)
625 struct tvp514x_decoder
*decoder
= to_decoder(sd
);
628 for (i
= 0; i
< decoder
->num_stds
; i
++)
629 if (std_id
& decoder
->std_list
[i
].standard
.id
)
632 if ((i
== decoder
->num_stds
) || (i
== STD_INVALID
))
635 err
= tvp514x_write_reg(sd
, REG_VIDEO_STD
,
636 decoder
->std_list
[i
].video_std
);
640 decoder
->current_std
= i
;
641 decoder
->tvp514x_regs
[REG_VIDEO_STD
].val
=
642 decoder
->std_list
[i
].video_std
;
644 v4l2_dbg(1, debug
, sd
, "Standard set to: %s\n",
645 decoder
->std_list
[i
].standard
.name
);
650 * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
651 * @sd: pointer to standard V4L2 sub-device structure
652 * @input: input selector for routing the signal
653 * @output: output selector for routing the signal
654 * @config: config value. Not used
656 * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
657 * the input is not supported or there is no active signal present in the
660 static int tvp514x_s_routing(struct v4l2_subdev
*sd
,
661 u32 input
, u32 output
, u32 config
)
663 struct tvp514x_decoder
*decoder
= to_decoder(sd
);
665 enum tvp514x_input input_sel
;
666 enum tvp514x_output output_sel
;
668 if ((input
>= INPUT_INVALID
) ||
669 (output
>= OUTPUT_INVALID
))
670 /* Index out of bound */
676 err
= tvp514x_write_reg(sd
, REG_INPUT_SEL
, input_sel
);
680 output_sel
|= tvp514x_read_reg(sd
,
681 REG_OUTPUT_FORMATTER1
) & 0x7;
682 err
= tvp514x_write_reg(sd
, REG_OUTPUT_FORMATTER1
,
687 decoder
->tvp514x_regs
[REG_INPUT_SEL
].val
= input_sel
;
688 decoder
->tvp514x_regs
[REG_OUTPUT_FORMATTER1
].val
= output_sel
;
689 decoder
->input
= input
;
690 decoder
->output
= output
;
692 v4l2_dbg(1, debug
, sd
, "Input set to: %d\n", input_sel
);
698 * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
699 * @ctrl: pointer to v4l2_ctrl structure
701 * If the requested control is supported, sets the control's current
702 * value in HW. Otherwise, returns -EINVAL if the control is not supported.
704 static int tvp514x_s_ctrl(struct v4l2_ctrl
*ctrl
)
706 struct v4l2_subdev
*sd
= to_sd(ctrl
);
707 struct tvp514x_decoder
*decoder
= to_decoder(sd
);
708 int err
= -EINVAL
, value
;
713 case V4L2_CID_BRIGHTNESS
:
714 err
= tvp514x_write_reg(sd
, REG_BRIGHTNESS
, value
);
716 decoder
->tvp514x_regs
[REG_BRIGHTNESS
].val
= value
;
718 case V4L2_CID_CONTRAST
:
719 err
= tvp514x_write_reg(sd
, REG_CONTRAST
, value
);
721 decoder
->tvp514x_regs
[REG_CONTRAST
].val
= value
;
723 case V4L2_CID_SATURATION
:
724 err
= tvp514x_write_reg(sd
, REG_SATURATION
, value
);
726 decoder
->tvp514x_regs
[REG_SATURATION
].val
= value
;
731 else if (value
== -180)
733 err
= tvp514x_write_reg(sd
, REG_HUE
, value
);
735 decoder
->tvp514x_regs
[REG_HUE
].val
= value
;
737 case V4L2_CID_AUTOGAIN
:
738 err
= tvp514x_write_reg(sd
, REG_AFE_GAIN_CTRL
, value
? 0x0f : 0x0c);
740 decoder
->tvp514x_regs
[REG_AFE_GAIN_CTRL
].val
= value
;
744 v4l2_dbg(1, debug
, sd
, "Set Control: ID - %d - %d\n",
745 ctrl
->id
, ctrl
->val
);
750 * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
751 * @sd: pointer to standard V4L2 sub-device structure
752 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
754 * Returns the decoder's video CAPTURE parameters.
757 tvp514x_g_parm(struct v4l2_subdev
*sd
, struct v4l2_streamparm
*a
)
759 struct tvp514x_decoder
*decoder
= to_decoder(sd
);
760 struct v4l2_captureparm
*cparm
;
761 enum tvp514x_std current_std
;
766 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
767 /* only capture is supported */
770 /* get the current standard */
771 current_std
= decoder
->current_std
;
773 cparm
= &a
->parm
.capture
;
774 cparm
->capability
= V4L2_CAP_TIMEPERFRAME
;
775 cparm
->timeperframe
=
776 decoder
->std_list
[current_std
].standard
.frameperiod
;
782 * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
783 * @sd: pointer to standard V4L2 sub-device structure
784 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
786 * Configures the decoder to use the input parameters, if possible. If
787 * not possible, returns the appropriate error code.
790 tvp514x_s_parm(struct v4l2_subdev
*sd
, struct v4l2_streamparm
*a
)
792 struct tvp514x_decoder
*decoder
= to_decoder(sd
);
793 struct v4l2_fract
*timeperframe
;
794 enum tvp514x_std current_std
;
799 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
800 /* only capture is supported */
803 timeperframe
= &a
->parm
.capture
.timeperframe
;
805 /* get the current standard */
806 current_std
= decoder
->current_std
;
809 decoder
->std_list
[current_std
].standard
.frameperiod
;
815 * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
816 * @sd: pointer to standard V4L2 sub-device structure
817 * @enable: streaming enable or disable
819 * Sets streaming to enable or disable, if possible.
821 static int tvp514x_s_stream(struct v4l2_subdev
*sd
, int enable
)
824 struct tvp514x_decoder
*decoder
= to_decoder(sd
);
826 if (decoder
->streaming
== enable
)
832 /* Power Down Sequence */
833 err
= tvp514x_write_reg(sd
, REG_OPERATION_MODE
, 0x01);
835 v4l2_err(sd
, "Unable to turn off decoder\n");
838 decoder
->streaming
= enable
;
843 /* Power Up Sequence */
844 err
= tvp514x_write_regs(sd
, decoder
->int_seq
);
846 v4l2_err(sd
, "Unable to turn on decoder\n");
849 /* Detect if not already detected */
850 err
= tvp514x_detect(sd
, decoder
);
852 v4l2_err(sd
, "Unable to detect decoder\n");
855 err
= tvp514x_configure(sd
, decoder
);
857 v4l2_err(sd
, "Unable to configure decoder\n");
860 decoder
->streaming
= enable
;
871 static const struct v4l2_ctrl_ops tvp514x_ctrl_ops
= {
872 .s_ctrl
= tvp514x_s_ctrl
,
876 * tvp514x_enum_mbus_code() - V4L2 decoder interface handler for enum_mbus_code
877 * @sd: pointer to standard V4L2 sub-device structure
878 * @cfg: pad configuration
879 * @code: pointer to v4l2_subdev_mbus_code_enum structure
881 * Enumertaes mbus codes supported
883 static int tvp514x_enum_mbus_code(struct v4l2_subdev
*sd
,
884 struct v4l2_subdev_pad_config
*cfg
,
885 struct v4l2_subdev_mbus_code_enum
*code
)
888 u32 index
= code
->index
;
890 memset(code
, 0, sizeof(*code
));
897 code
->code
= MEDIA_BUS_FMT_UYVY8_2X8
;
903 * tvp514x_get_pad_format() - V4L2 decoder interface handler for get pad format
904 * @sd: pointer to standard V4L2 sub-device structure
905 * @cfg: pad configuration
906 * @format: pointer to v4l2_subdev_format structure
908 * Retrieves pad format which is active or tried based on requirement
910 static int tvp514x_get_pad_format(struct v4l2_subdev
*sd
,
911 struct v4l2_subdev_pad_config
*cfg
,
912 struct v4l2_subdev_format
*format
)
914 struct tvp514x_decoder
*decoder
= to_decoder(sd
);
915 __u32 which
= format
->which
;
920 if (which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
921 format
->format
= decoder
->format
;
925 format
->format
.code
= MEDIA_BUS_FMT_UYVY8_2X8
;
926 format
->format
.width
= tvp514x_std_list
[decoder
->current_std
].width
;
927 format
->format
.height
= tvp514x_std_list
[decoder
->current_std
].height
;
928 format
->format
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
929 format
->format
.field
= V4L2_FIELD_INTERLACED
;
935 * tvp514x_set_pad_format() - V4L2 decoder interface handler for set pad format
936 * @sd: pointer to standard V4L2 sub-device structure
937 * @cfg: pad configuration
938 * @format: pointer to v4l2_subdev_format structure
940 * Set pad format for the output pad
942 static int tvp514x_set_pad_format(struct v4l2_subdev
*sd
,
943 struct v4l2_subdev_pad_config
*cfg
,
944 struct v4l2_subdev_format
*fmt
)
946 struct tvp514x_decoder
*decoder
= to_decoder(sd
);
948 if (fmt
->format
.field
!= V4L2_FIELD_INTERLACED
||
949 fmt
->format
.code
!= MEDIA_BUS_FMT_UYVY8_2X8
||
950 fmt
->format
.colorspace
!= V4L2_COLORSPACE_SMPTE170M
||
951 fmt
->format
.width
!= tvp514x_std_list
[decoder
->current_std
].width
||
952 fmt
->format
.height
!= tvp514x_std_list
[decoder
->current_std
].height
)
955 decoder
->format
= fmt
->format
;
960 static const struct v4l2_subdev_video_ops tvp514x_video_ops
= {
961 .s_std
= tvp514x_s_std
,
962 .s_routing
= tvp514x_s_routing
,
963 .querystd
= tvp514x_querystd
,
964 .g_parm
= tvp514x_g_parm
,
965 .s_parm
= tvp514x_s_parm
,
966 .s_stream
= tvp514x_s_stream
,
969 static const struct v4l2_subdev_pad_ops tvp514x_pad_ops
= {
970 .enum_mbus_code
= tvp514x_enum_mbus_code
,
971 .get_fmt
= tvp514x_get_pad_format
,
972 .set_fmt
= tvp514x_set_pad_format
,
975 static const struct v4l2_subdev_ops tvp514x_ops
= {
976 .video
= &tvp514x_video_ops
,
977 .pad
= &tvp514x_pad_ops
,
980 static const struct tvp514x_decoder tvp514x_dev
= {
982 .fmt_list
= tvp514x_fmt_list
,
983 .num_fmts
= ARRAY_SIZE(tvp514x_fmt_list
),
985 /* Default to NTSC 8-bit YUV 422 */
986 .width
= NTSC_NUM_ACTIVE_PIXELS
,
987 .height
= NTSC_NUM_ACTIVE_LINES
,
988 .pixelformat
= V4L2_PIX_FMT_UYVY
,
989 .field
= V4L2_FIELD_INTERLACED
,
990 .bytesperline
= NTSC_NUM_ACTIVE_PIXELS
* 2,
991 .sizeimage
= NTSC_NUM_ACTIVE_PIXELS
* 2 *
992 NTSC_NUM_ACTIVE_LINES
,
993 .colorspace
= V4L2_COLORSPACE_SMPTE170M
,
995 .current_std
= STD_NTSC_MJ
,
996 .std_list
= tvp514x_std_list
,
997 .num_stds
= ARRAY_SIZE(tvp514x_std_list
),
1001 static struct tvp514x_platform_data
*
1002 tvp514x_get_pdata(struct i2c_client
*client
)
1004 struct tvp514x_platform_data
*pdata
= NULL
;
1005 struct v4l2_of_endpoint bus_cfg
;
1006 struct device_node
*endpoint
;
1009 if (!IS_ENABLED(CONFIG_OF
) || !client
->dev
.of_node
)
1010 return client
->dev
.platform_data
;
1012 endpoint
= of_graph_get_next_endpoint(client
->dev
.of_node
, NULL
);
1016 if (v4l2_of_parse_endpoint(endpoint
, &bus_cfg
))
1019 pdata
= devm_kzalloc(&client
->dev
, sizeof(*pdata
), GFP_KERNEL
);
1023 flags
= bus_cfg
.bus
.parallel
.flags
;
1025 if (flags
& V4L2_MBUS_HSYNC_ACTIVE_HIGH
)
1026 pdata
->hs_polarity
= 1;
1028 if (flags
& V4L2_MBUS_VSYNC_ACTIVE_HIGH
)
1029 pdata
->vs_polarity
= 1;
1031 if (flags
& V4L2_MBUS_PCLK_SAMPLE_RISING
)
1032 pdata
->clk_polarity
= 1;
1035 of_node_put(endpoint
);
1040 * tvp514x_probe() - decoder driver i2c probe handler
1041 * @client: i2c driver client device structure
1042 * @id: i2c driver id table
1044 * Register decoder as an i2c client device and V4L2
1048 tvp514x_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
1050 struct tvp514x_platform_data
*pdata
= tvp514x_get_pdata(client
);
1051 struct tvp514x_decoder
*decoder
;
1052 struct v4l2_subdev
*sd
;
1055 if (pdata
== NULL
) {
1056 dev_err(&client
->dev
, "No platform data\n");
1060 /* Check if the adapter supports the needed features */
1061 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
1064 decoder
= devm_kzalloc(&client
->dev
, sizeof(*decoder
), GFP_KERNEL
);
1068 /* Initialize the tvp514x_decoder with default configuration */
1069 *decoder
= tvp514x_dev
;
1070 /* Copy default register configuration */
1071 memcpy(decoder
->tvp514x_regs
, tvp514x_reg_list_default
,
1072 sizeof(tvp514x_reg_list_default
));
1074 decoder
->int_seq
= (struct tvp514x_reg
*)id
->driver_data
;
1076 /* Copy board specific information here */
1077 decoder
->pdata
= pdata
;
1080 * Fetch platform specific data, and configure the
1081 * tvp514x_reg_list[] accordingly. Since this is one
1082 * time configuration, no need to preserve.
1084 decoder
->tvp514x_regs
[REG_OUTPUT_FORMATTER2
].val
|=
1085 (decoder
->pdata
->clk_polarity
<< 1);
1086 decoder
->tvp514x_regs
[REG_SYNC_CONTROL
].val
|=
1087 ((decoder
->pdata
->hs_polarity
<< 2) |
1088 (decoder
->pdata
->vs_polarity
<< 3));
1089 /* Set default standard to auto */
1090 decoder
->tvp514x_regs
[REG_VIDEO_STD
].val
=
1091 VIDEO_STD_AUTO_SWITCH_BIT
;
1093 /* Register with V4L2 layer as slave device */
1095 v4l2_i2c_subdev_init(sd
, client
, &tvp514x_ops
);
1097 #if defined(CONFIG_MEDIA_CONTROLLER)
1098 decoder
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1099 decoder
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1100 decoder
->sd
.entity
.flags
|= MEDIA_ENT_F_ATV_DECODER
;
1102 ret
= media_entity_pads_init(&decoder
->sd
.entity
, 1, &decoder
->pad
);
1104 v4l2_err(sd
, "%s decoder driver failed to register !!\n",
1109 v4l2_ctrl_handler_init(&decoder
->hdl
, 5);
1110 v4l2_ctrl_new_std(&decoder
->hdl
, &tvp514x_ctrl_ops
,
1111 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 128);
1112 v4l2_ctrl_new_std(&decoder
->hdl
, &tvp514x_ctrl_ops
,
1113 V4L2_CID_CONTRAST
, 0, 255, 1, 128);
1114 v4l2_ctrl_new_std(&decoder
->hdl
, &tvp514x_ctrl_ops
,
1115 V4L2_CID_SATURATION
, 0, 255, 1, 128);
1116 v4l2_ctrl_new_std(&decoder
->hdl
, &tvp514x_ctrl_ops
,
1117 V4L2_CID_HUE
, -180, 180, 180, 0);
1118 v4l2_ctrl_new_std(&decoder
->hdl
, &tvp514x_ctrl_ops
,
1119 V4L2_CID_AUTOGAIN
, 0, 1, 1, 1);
1120 sd
->ctrl_handler
= &decoder
->hdl
;
1121 if (decoder
->hdl
.error
) {
1122 ret
= decoder
->hdl
.error
;
1125 v4l2_ctrl_handler_setup(&decoder
->hdl
);
1127 ret
= v4l2_async_register_subdev(&decoder
->sd
);
1129 v4l2_info(sd
, "%s decoder driver registered !!\n", sd
->name
);
1133 v4l2_ctrl_handler_free(&decoder
->hdl
);
1134 #if defined(CONFIG_MEDIA_CONTROLLER)
1135 media_entity_cleanup(&decoder
->sd
.entity
);
1142 * tvp514x_remove() - decoder driver i2c remove handler
1143 * @client: i2c driver client device structure
1145 * Unregister decoder as an i2c client device and V4L2
1146 * device. Complement of tvp514x_probe().
1148 static int tvp514x_remove(struct i2c_client
*client
)
1150 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1151 struct tvp514x_decoder
*decoder
= to_decoder(sd
);
1153 v4l2_async_unregister_subdev(&decoder
->sd
);
1154 #if defined(CONFIG_MEDIA_CONTROLLER)
1155 media_entity_cleanup(&decoder
->sd
.entity
);
1157 v4l2_ctrl_handler_free(&decoder
->hdl
);
1160 /* TVP5146 Init/Power on Sequence */
1161 static const struct tvp514x_reg tvp5146_init_reg_seq
[] = {
1162 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS1
, 0x02},
1163 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS2
, 0x00},
1164 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS3
, 0x80},
1165 {TOK_WRITE
, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR
, 0x01},
1166 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS1
, 0x60},
1167 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS2
, 0x00},
1168 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS3
, 0xB0},
1169 {TOK_WRITE
, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR
, 0x01},
1170 {TOK_WRITE
, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR
, 0x00},
1171 {TOK_WRITE
, REG_OPERATION_MODE
, 0x01},
1172 {TOK_WRITE
, REG_OPERATION_MODE
, 0x00},
1176 /* TVP5147 Init/Power on Sequence */
1177 static const struct tvp514x_reg tvp5147_init_reg_seq
[] = {
1178 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS1
, 0x02},
1179 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS2
, 0x00},
1180 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS3
, 0x80},
1181 {TOK_WRITE
, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR
, 0x01},
1182 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS1
, 0x60},
1183 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS2
, 0x00},
1184 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS3
, 0xB0},
1185 {TOK_WRITE
, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR
, 0x01},
1186 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS1
, 0x16},
1187 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS2
, 0x00},
1188 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS3
, 0xA0},
1189 {TOK_WRITE
, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR
, 0x16},
1190 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS1
, 0x60},
1191 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS2
, 0x00},
1192 {TOK_WRITE
, REG_VBUS_ADDRESS_ACCESS3
, 0xB0},
1193 {TOK_WRITE
, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR
, 0x00},
1194 {TOK_WRITE
, REG_OPERATION_MODE
, 0x01},
1195 {TOK_WRITE
, REG_OPERATION_MODE
, 0x00},
1199 /* TVP5146M2/TVP5147M1 Init/Power on Sequence */
1200 static const struct tvp514x_reg tvp514xm_init_reg_seq
[] = {
1201 {TOK_WRITE
, REG_OPERATION_MODE
, 0x01},
1202 {TOK_WRITE
, REG_OPERATION_MODE
, 0x00},
1207 * I2C Device Table -
1209 * name - Name of the actual device/chip.
1210 * driver_data - Driver data
1212 static const struct i2c_device_id tvp514x_id
[] = {
1213 {"tvp5146", (unsigned long)tvp5146_init_reg_seq
},
1214 {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq
},
1215 {"tvp5147", (unsigned long)tvp5147_init_reg_seq
},
1216 {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq
},
1220 MODULE_DEVICE_TABLE(i2c
, tvp514x_id
);
1222 #if IS_ENABLED(CONFIG_OF)
1223 static const struct of_device_id tvp514x_of_match
[] = {
1224 { .compatible
= "ti,tvp5146", },
1225 { .compatible
= "ti,tvp5146m2", },
1226 { .compatible
= "ti,tvp5147", },
1227 { .compatible
= "ti,tvp5147m1", },
1230 MODULE_DEVICE_TABLE(of
, tvp514x_of_match
);
1233 static struct i2c_driver tvp514x_driver
= {
1235 .of_match_table
= of_match_ptr(tvp514x_of_match
),
1236 .name
= TVP514X_MODULE_NAME
,
1238 .probe
= tvp514x_probe
,
1239 .remove
= tvp514x_remove
,
1240 .id_table
= tvp514x_id
,
1243 module_i2c_driver(tvp514x_driver
);