1 // SPDX-License-Identifier: GPL-2.0
3 // tvp5150 - Texas Instruments TVP5150A/AM1 and TVP5151 video decoder driver
5 // Copyright (c) 2005,2006 Mauro Carvalho Chehab <mchehab@kernel.org>
7 #include <dt-bindings/media/tvp5150.h>
9 #include <linux/slab.h>
10 #include <linux/videodev2.h>
11 #include <linux/delay.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/of_graph.h>
16 #include <linux/regmap.h>
17 #include <media/v4l2-async.h>
18 #include <media/v4l2-device.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-fwnode.h>
21 #include <media/v4l2-mc.h>
23 #include "tvp5150_reg.h"
25 #define TVP5150_H_MAX 720U
26 #define TVP5150_V_MAX_525_60 480U
27 #define TVP5150_V_MAX_OTHERS 576U
28 #define TVP5150_MAX_CROP_LEFT 511
29 #define TVP5150_MAX_CROP_TOP 127
30 #define TVP5150_CROP_SHIFT 2
31 #define TVP5150_MBUS_FMT MEDIA_BUS_FMT_UYVY8_2X8
32 #define TVP5150_FIELD V4L2_FIELD_ALTERNATE
33 #define TVP5150_COLORSPACE V4L2_COLORSPACE_SMPTE170M
35 MODULE_DESCRIPTION("Texas Instruments TVP5150A/TVP5150AM1/TVP5151 video decoder driver");
36 MODULE_AUTHOR("Mauro Carvalho Chehab");
37 MODULE_LICENSE("GPL v2");
41 module_param(debug
, int, 0644);
42 MODULE_PARM_DESC(debug
, "Debug level (0-2)");
44 #define dprintk0(__dev, __arg...) dev_dbg_lvl(__dev, 0, 0, __arg)
53 struct v4l2_subdev sd
;
54 #ifdef CONFIG_MEDIA_CONTROLLER
55 struct media_pad pads
[TVP5150_NUM_PADS
];
56 struct media_entity input_ent
[TVP5150_INPUT_NUM
];
57 struct media_pad input_pad
[TVP5150_INPUT_NUM
];
59 struct v4l2_ctrl_handler hdl
;
60 struct v4l2_rect rect
;
61 struct regmap
*regmap
;
64 v4l2_std_id norm
; /* Current set standard */
65 v4l2_std_id detected_norm
;
75 enum v4l2_mbus_type mbus_type
;
78 static inline struct tvp5150
*to_tvp5150(struct v4l2_subdev
*sd
)
80 return container_of(sd
, struct tvp5150
, sd
);
83 static inline struct v4l2_subdev
*to_sd(struct v4l2_ctrl
*ctrl
)
85 return &container_of(ctrl
->handler
, struct tvp5150
, hdl
)->sd
;
88 static int tvp5150_read(struct v4l2_subdev
*sd
, unsigned char addr
)
90 struct tvp5150
*decoder
= to_tvp5150(sd
);
93 ret
= regmap_read(decoder
->regmap
, addr
, &val
);
100 static void dump_reg_range(struct v4l2_subdev
*sd
, char *s
, u8 init
,
101 const u8 end
, int max_line
)
107 dprintk0(sd
->dev
, "too much data to dump\n");
111 for (i
= init
; i
< end
; i
+= max_line
) {
112 len
= (end
- i
> max_line
) ? max_line
: end
- i
;
114 for (j
= 0; j
< len
; j
++)
115 buf
[j
] = tvp5150_read(sd
, i
+ j
);
117 dprintk0(sd
->dev
, "%s reg %02x = %*ph\n", s
, i
, len
, buf
);
121 static int tvp5150_log_status(struct v4l2_subdev
*sd
)
123 dprintk0(sd
->dev
, "tvp5150: Video input source selection #1 = 0x%02x\n",
124 tvp5150_read(sd
, TVP5150_VD_IN_SRC_SEL_1
));
125 dprintk0(sd
->dev
, "tvp5150: Analog channel controls = 0x%02x\n",
126 tvp5150_read(sd
, TVP5150_ANAL_CHL_CTL
));
127 dprintk0(sd
->dev
, "tvp5150: Operation mode controls = 0x%02x\n",
128 tvp5150_read(sd
, TVP5150_OP_MODE_CTL
));
129 dprintk0(sd
->dev
, "tvp5150: Miscellaneous controls = 0x%02x\n",
130 tvp5150_read(sd
, TVP5150_MISC_CTL
));
131 dprintk0(sd
->dev
, "tvp5150: Autoswitch mask= 0x%02x\n",
132 tvp5150_read(sd
, TVP5150_AUTOSW_MSK
));
133 dprintk0(sd
->dev
, "tvp5150: Color killer threshold control = 0x%02x\n",
134 tvp5150_read(sd
, TVP5150_COLOR_KIL_THSH_CTL
));
135 dprintk0(sd
->dev
, "tvp5150: Luminance processing controls #1 #2 and #3 = %02x %02x %02x\n",
136 tvp5150_read(sd
, TVP5150_LUMA_PROC_CTL_1
),
137 tvp5150_read(sd
, TVP5150_LUMA_PROC_CTL_2
),
138 tvp5150_read(sd
, TVP5150_LUMA_PROC_CTL_3
));
139 dprintk0(sd
->dev
, "tvp5150: Brightness control = 0x%02x\n",
140 tvp5150_read(sd
, TVP5150_BRIGHT_CTL
));
141 dprintk0(sd
->dev
, "tvp5150: Color saturation control = 0x%02x\n",
142 tvp5150_read(sd
, TVP5150_SATURATION_CTL
));
143 dprintk0(sd
->dev
, "tvp5150: Hue control = 0x%02x\n",
144 tvp5150_read(sd
, TVP5150_HUE_CTL
));
145 dprintk0(sd
->dev
, "tvp5150: Contrast control = 0x%02x\n",
146 tvp5150_read(sd
, TVP5150_CONTRAST_CTL
));
147 dprintk0(sd
->dev
, "tvp5150: Outputs and data rates select = 0x%02x\n",
148 tvp5150_read(sd
, TVP5150_DATA_RATE_SEL
));
149 dprintk0(sd
->dev
, "tvp5150: Configuration shared pins = 0x%02x\n",
150 tvp5150_read(sd
, TVP5150_CONF_SHARED_PIN
));
151 dprintk0(sd
->dev
, "tvp5150: Active video cropping start = 0x%02x%02x\n",
152 tvp5150_read(sd
, TVP5150_ACT_VD_CROP_ST_MSB
),
153 tvp5150_read(sd
, TVP5150_ACT_VD_CROP_ST_LSB
));
154 dprintk0(sd
->dev
, "tvp5150: Active video cropping stop = 0x%02x%02x\n",
155 tvp5150_read(sd
, TVP5150_ACT_VD_CROP_STP_MSB
),
156 tvp5150_read(sd
, TVP5150_ACT_VD_CROP_STP_LSB
));
157 dprintk0(sd
->dev
, "tvp5150: Genlock/RTC = 0x%02x\n",
158 tvp5150_read(sd
, TVP5150_GENLOCK
));
159 dprintk0(sd
->dev
, "tvp5150: Horizontal sync start = 0x%02x\n",
160 tvp5150_read(sd
, TVP5150_HORIZ_SYNC_START
));
161 dprintk0(sd
->dev
, "tvp5150: Vertical blanking start = 0x%02x\n",
162 tvp5150_read(sd
, TVP5150_VERT_BLANKING_START
));
163 dprintk0(sd
->dev
, "tvp5150: Vertical blanking stop = 0x%02x\n",
164 tvp5150_read(sd
, TVP5150_VERT_BLANKING_STOP
));
165 dprintk0(sd
->dev
, "tvp5150: Chrominance processing control #1 and #2 = %02x %02x\n",
166 tvp5150_read(sd
, TVP5150_CHROMA_PROC_CTL_1
),
167 tvp5150_read(sd
, TVP5150_CHROMA_PROC_CTL_2
));
168 dprintk0(sd
->dev
, "tvp5150: Interrupt reset register B = 0x%02x\n",
169 tvp5150_read(sd
, TVP5150_INT_RESET_REG_B
));
170 dprintk0(sd
->dev
, "tvp5150: Interrupt enable register B = 0x%02x\n",
171 tvp5150_read(sd
, TVP5150_INT_ENABLE_REG_B
));
172 dprintk0(sd
->dev
, "tvp5150: Interrupt configuration register B = 0x%02x\n",
173 tvp5150_read(sd
, TVP5150_INTT_CONFIG_REG_B
));
174 dprintk0(sd
->dev
, "tvp5150: Video standard = 0x%02x\n",
175 tvp5150_read(sd
, TVP5150_VIDEO_STD
));
176 dprintk0(sd
->dev
, "tvp5150: Chroma gain factor: Cb=0x%02x Cr=0x%02x\n",
177 tvp5150_read(sd
, TVP5150_CB_GAIN_FACT
),
178 tvp5150_read(sd
, TVP5150_CR_GAIN_FACTOR
));
179 dprintk0(sd
->dev
, "tvp5150: Macrovision on counter = 0x%02x\n",
180 tvp5150_read(sd
, TVP5150_MACROVISION_ON_CTR
));
181 dprintk0(sd
->dev
, "tvp5150: Macrovision off counter = 0x%02x\n",
182 tvp5150_read(sd
, TVP5150_MACROVISION_OFF_CTR
));
183 dprintk0(sd
->dev
, "tvp5150: ITU-R BT.656.%d timing(TVP5150AM1 only)\n",
184 (tvp5150_read(sd
, TVP5150_REV_SELECT
) & 1) ? 3 : 4);
185 dprintk0(sd
->dev
, "tvp5150: Device ID = %02x%02x\n",
186 tvp5150_read(sd
, TVP5150_MSB_DEV_ID
),
187 tvp5150_read(sd
, TVP5150_LSB_DEV_ID
));
188 dprintk0(sd
->dev
, "tvp5150: ROM version = (hex) %02x.%02x\n",
189 tvp5150_read(sd
, TVP5150_ROM_MAJOR_VER
),
190 tvp5150_read(sd
, TVP5150_ROM_MINOR_VER
));
191 dprintk0(sd
->dev
, "tvp5150: Vertical line count = 0x%02x%02x\n",
192 tvp5150_read(sd
, TVP5150_VERT_LN_COUNT_MSB
),
193 tvp5150_read(sd
, TVP5150_VERT_LN_COUNT_LSB
));
194 dprintk0(sd
->dev
, "tvp5150: Interrupt status register B = 0x%02x\n",
195 tvp5150_read(sd
, TVP5150_INT_STATUS_REG_B
));
196 dprintk0(sd
->dev
, "tvp5150: Interrupt active register B = 0x%02x\n",
197 tvp5150_read(sd
, TVP5150_INT_ACTIVE_REG_B
));
198 dprintk0(sd
->dev
, "tvp5150: Status regs #1 to #5 = %02x %02x %02x %02x %02x\n",
199 tvp5150_read(sd
, TVP5150_STATUS_REG_1
),
200 tvp5150_read(sd
, TVP5150_STATUS_REG_2
),
201 tvp5150_read(sd
, TVP5150_STATUS_REG_3
),
202 tvp5150_read(sd
, TVP5150_STATUS_REG_4
),
203 tvp5150_read(sd
, TVP5150_STATUS_REG_5
));
205 dump_reg_range(sd
, "Teletext filter 1", TVP5150_TELETEXT_FIL1_INI
,
206 TVP5150_TELETEXT_FIL1_END
, 8);
207 dump_reg_range(sd
, "Teletext filter 2", TVP5150_TELETEXT_FIL2_INI
,
208 TVP5150_TELETEXT_FIL2_END
, 8);
210 dprintk0(sd
->dev
, "tvp5150: Teletext filter enable = 0x%02x\n",
211 tvp5150_read(sd
, TVP5150_TELETEXT_FIL_ENA
));
212 dprintk0(sd
->dev
, "tvp5150: Interrupt status register A = 0x%02x\n",
213 tvp5150_read(sd
, TVP5150_INT_STATUS_REG_A
));
214 dprintk0(sd
->dev
, "tvp5150: Interrupt enable register A = 0x%02x\n",
215 tvp5150_read(sd
, TVP5150_INT_ENABLE_REG_A
));
216 dprintk0(sd
->dev
, "tvp5150: Interrupt configuration = 0x%02x\n",
217 tvp5150_read(sd
, TVP5150_INT_CONF
));
218 dprintk0(sd
->dev
, "tvp5150: VDP status register = 0x%02x\n",
219 tvp5150_read(sd
, TVP5150_VDP_STATUS_REG
));
220 dprintk0(sd
->dev
, "tvp5150: FIFO word count = 0x%02x\n",
221 tvp5150_read(sd
, TVP5150_FIFO_WORD_COUNT
));
222 dprintk0(sd
->dev
, "tvp5150: FIFO interrupt threshold = 0x%02x\n",
223 tvp5150_read(sd
, TVP5150_FIFO_INT_THRESHOLD
));
224 dprintk0(sd
->dev
, "tvp5150: FIFO reset = 0x%02x\n",
225 tvp5150_read(sd
, TVP5150_FIFO_RESET
));
226 dprintk0(sd
->dev
, "tvp5150: Line number interrupt = 0x%02x\n",
227 tvp5150_read(sd
, TVP5150_LINE_NUMBER_INT
));
228 dprintk0(sd
->dev
, "tvp5150: Pixel alignment register = 0x%02x%02x\n",
229 tvp5150_read(sd
, TVP5150_PIX_ALIGN_REG_HIGH
),
230 tvp5150_read(sd
, TVP5150_PIX_ALIGN_REG_LOW
));
231 dprintk0(sd
->dev
, "tvp5150: FIFO output control = 0x%02x\n",
232 tvp5150_read(sd
, TVP5150_FIFO_OUT_CTRL
));
233 dprintk0(sd
->dev
, "tvp5150: Full field enable = 0x%02x\n",
234 tvp5150_read(sd
, TVP5150_FULL_FIELD_ENA
));
235 dprintk0(sd
->dev
, "tvp5150: Full field mode register = 0x%02x\n",
236 tvp5150_read(sd
, TVP5150_FULL_FIELD_MODE_REG
));
238 dump_reg_range(sd
, "CC data", TVP5150_CC_DATA_INI
,
239 TVP5150_CC_DATA_END
, 8);
241 dump_reg_range(sd
, "WSS data", TVP5150_WSS_DATA_INI
,
242 TVP5150_WSS_DATA_END
, 8);
244 dump_reg_range(sd
, "VPS data", TVP5150_VPS_DATA_INI
,
245 TVP5150_VPS_DATA_END
, 8);
247 dump_reg_range(sd
, "VITC data", TVP5150_VITC_DATA_INI
,
248 TVP5150_VITC_DATA_END
, 10);
250 dump_reg_range(sd
, "Line mode", TVP5150_LINE_MODE_INI
,
251 TVP5150_LINE_MODE_END
, 8);
255 /****************************************************************************
257 ****************************************************************************/
259 static void tvp5150_selmux(struct v4l2_subdev
*sd
)
262 struct tvp5150
*decoder
= to_tvp5150(sd
);
263 unsigned int mask
, val
;
266 /* Only tvp5150am1 and tvp5151 have signal generator support */
267 if ((decoder
->dev_id
== 0x5150 && decoder
->rom_ver
== 0x0400) ||
268 (decoder
->dev_id
== 0x5151 && decoder
->rom_ver
== 0x0100)) {
269 if (!decoder
->enable
)
273 switch (decoder
->input
) {
274 case TVP5150_COMPOSITE1
:
277 case TVP5150_COMPOSITE0
:
285 dev_dbg_lvl(sd
->dev
, 1, debug
, "Selecting video route: route input=%i, output=%i => tvp5150 input=%i, opmode=%i\n",
286 decoder
->input
, decoder
->output
,
289 regmap_write(decoder
->regmap
, TVP5150_OP_MODE_CTL
, opmode
);
290 regmap_write(decoder
->regmap
, TVP5150_VD_IN_SRC_SEL_1
, input
);
293 * Setup the FID/GLCO/VLK/HVLK and INTREQ/GPCL/VBLK output signals. For
294 * S-Video we output the vertical lock (VLK) signal on FID/GLCO/VLK/HVLK
295 * and set INTREQ/GPCL/VBLK to logic 0. For composite we output the
296 * field indicator (FID) signal on FID/GLCO/VLK/HVLK and set
297 * INTREQ/GPCL/VBLK to logic 1.
299 mask
= TVP5150_MISC_CTL_GPCL
| TVP5150_MISC_CTL_HVLK
;
300 if (decoder
->input
== TVP5150_SVIDEO
)
301 val
= TVP5150_MISC_CTL_HVLK
;
303 val
= TVP5150_MISC_CTL_GPCL
;
304 regmap_update_bits(decoder
->regmap
, TVP5150_MISC_CTL
, mask
, val
);
307 struct i2c_reg_value
{
312 /* Default values as sugested at TVP5150AM1 datasheet */
313 static const struct i2c_reg_value tvp5150_init_default
[] = {
315 TVP5150_VD_IN_SRC_SEL_1
, 0x00
318 TVP5150_ANAL_CHL_CTL
, 0x15
321 TVP5150_OP_MODE_CTL
, 0x00
324 TVP5150_MISC_CTL
, 0x01
327 TVP5150_COLOR_KIL_THSH_CTL
, 0x10
330 TVP5150_LUMA_PROC_CTL_1
, 0x60
333 TVP5150_LUMA_PROC_CTL_2
, 0x00
336 TVP5150_BRIGHT_CTL
, 0x80
339 TVP5150_SATURATION_CTL
, 0x80
342 TVP5150_HUE_CTL
, 0x00
345 TVP5150_CONTRAST_CTL
, 0x80
348 TVP5150_DATA_RATE_SEL
, 0x47
351 TVP5150_LUMA_PROC_CTL_3
, 0x00
354 TVP5150_CONF_SHARED_PIN
, 0x08
357 TVP5150_ACT_VD_CROP_ST_MSB
, 0x00
360 TVP5150_ACT_VD_CROP_ST_LSB
, 0x00
363 TVP5150_ACT_VD_CROP_STP_MSB
, 0x00
366 TVP5150_ACT_VD_CROP_STP_LSB
, 0x00
369 TVP5150_GENLOCK
, 0x01
372 TVP5150_HORIZ_SYNC_START
, 0x80
375 TVP5150_VERT_BLANKING_START
, 0x00
378 TVP5150_VERT_BLANKING_STOP
, 0x00
381 TVP5150_CHROMA_PROC_CTL_1
, 0x0c
384 TVP5150_CHROMA_PROC_CTL_2
, 0x14
387 TVP5150_INT_RESET_REG_B
, 0x00
390 TVP5150_INT_ENABLE_REG_B
, 0x00
393 TVP5150_INTT_CONFIG_REG_B
, 0x00
396 TVP5150_VIDEO_STD
, 0x00
399 TVP5150_MACROVISION_ON_CTR
, 0x0f
402 TVP5150_MACROVISION_OFF_CTR
, 0x01
405 TVP5150_TELETEXT_FIL_ENA
, 0x00
408 TVP5150_INT_STATUS_REG_A
, 0x00
411 TVP5150_INT_ENABLE_REG_A
, 0x00
414 TVP5150_INT_CONF
, 0x04
417 TVP5150_FIFO_INT_THRESHOLD
, 0x80
420 TVP5150_FIFO_RESET
, 0x00
423 TVP5150_LINE_NUMBER_INT
, 0x00
426 TVP5150_PIX_ALIGN_REG_LOW
, 0x4e
429 TVP5150_PIX_ALIGN_REG_HIGH
, 0x00
432 TVP5150_FIFO_OUT_CTRL
, 0x01
435 TVP5150_FULL_FIELD_ENA
, 0x00
438 TVP5150_LINE_MODE_INI
, 0x00
441 TVP5150_FULL_FIELD_MODE_REG
, 0x7f
448 /* Default values as sugested at TVP5150AM1 datasheet */
449 static const struct i2c_reg_value tvp5150_init_enable
[] = {
450 { /* Automatic offset and AGC enabled */
451 TVP5150_ANAL_CHL_CTL
, 0x15
452 }, { /* Activate YCrCb output 0x9 or 0xd ? */
453 TVP5150_MISC_CTL
, TVP5150_MISC_CTL_GPCL
|
454 TVP5150_MISC_CTL_INTREQ_OE
|
455 TVP5150_MISC_CTL_YCBCR_OE
|
456 TVP5150_MISC_CTL_SYNC_OE
|
457 TVP5150_MISC_CTL_VBLANK
|
458 TVP5150_MISC_CTL_CLOCK_OE
,
459 }, { /* Activates video std autodetection for all standards */
460 TVP5150_AUTOSW_MSK
, 0x0
461 }, { /* Default format: 0x47. For 4:2:2: 0x40 */
462 TVP5150_DATA_RATE_SEL
, 0x47
464 TVP5150_CHROMA_PROC_CTL_1
, 0x0c
466 TVP5150_CHROMA_PROC_CTL_2
, 0x54
467 }, { /* Non documented, but initialized on WinTV USB2 */
474 struct tvp5150_vbi_type
{
475 unsigned int vbi_type
;
476 unsigned int ini_line
;
477 unsigned int end_line
;
478 unsigned int by_field
:1;
481 struct i2c_vbi_ram_value
{
483 struct tvp5150_vbi_type type
;
484 unsigned char values
[16];
487 /* This struct have the values for each supported VBI Standard
489 tvp5150_vbi_types should follow the same order as vbi_ram_default
490 * value 0 means rom position 0x10, value 1 means rom position 0x30
491 * and so on. There are 16 possible locations from 0 to 15.
494 static struct i2c_vbi_ram_value vbi_ram_default
[] = {
497 * FIXME: Current api doesn't handle all VBI types, those not
498 * yet supported are placed under #if 0
501 [0] = {0x010, /* Teletext, SECAM, WST System A */
502 {V4L2_SLICED_TELETEXT_SECAM
, 6, 23, 1},
503 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x26,
504 0xe6, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00 }
507 [1] = {0x030, /* Teletext, PAL, WST System B */
508 {V4L2_SLICED_TELETEXT_B
, 6, 22, 1},
509 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x2b,
510 0xa6, 0x72, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00 }
513 [2] = {0x050, /* Teletext, PAL, WST System C */
514 {V4L2_SLICED_TELETEXT_PAL_C
, 6, 22, 1},
515 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
516 0xa6, 0x98, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
518 [3] = {0x070, /* Teletext, NTSC, WST System B */
519 {V4L2_SLICED_TELETEXT_NTSC_B
, 10, 21, 1},
520 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x23,
521 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
523 [4] = {0x090, /* Tetetext, NTSC NABTS System C */
524 {V4L2_SLICED_TELETEXT_NTSC_C
, 10, 21, 1},
525 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
526 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x15, 0x00 }
528 [5] = {0x0b0, /* Teletext, NTSC-J, NABTS System D */
529 {V4L2_SLICED_TELETEXT_NTSC_D
, 10, 21, 1},
530 { 0xaa, 0xaa, 0xff, 0xff, 0xa7, 0x2e, 0x20, 0x23,
531 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
533 [6] = {0x0d0, /* Closed Caption, PAL/SECAM */
534 {V4L2_SLICED_CAPTION_625
, 22, 22, 1},
535 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
536 0xa6, 0x7b, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
539 [7] = {0x0f0, /* Closed Caption, NTSC */
540 {V4L2_SLICED_CAPTION_525
, 21, 21, 1},
541 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
542 0x69, 0x8c, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
544 [8] = {0x110, /* Wide Screen Signal, PAL/SECAM */
545 {V4L2_SLICED_WSS_625
, 23, 23, 1},
546 { 0x5b, 0x55, 0xc5, 0xff, 0x00, 0x71, 0x6e, 0x42,
547 0xa6, 0xcd, 0x0f, 0x00, 0x00, 0x00, 0x3a, 0x00 }
550 [9] = {0x130, /* Wide Screen Signal, NTSC C */
551 {V4L2_SLICED_WSS_525
, 20, 20, 1},
552 { 0x38, 0x00, 0x3f, 0x00, 0x00, 0x71, 0x6e, 0x43,
553 0x69, 0x7c, 0x08, 0x00, 0x00, 0x00, 0x39, 0x00 }
555 [10] = {0x150, /* Vertical Interval Timecode (VITC), PAL/SECAM */
556 {V4l2_SLICED_VITC_625
, 6, 22, 0},
557 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
558 0xa6, 0x85, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
560 [11] = {0x170, /* Vertical Interval Timecode (VITC), NTSC */
561 {V4l2_SLICED_VITC_525
, 10, 20, 0},
562 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
563 0x69, 0x94, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
566 [12] = {0x190, /* Video Program System (VPS), PAL */
567 {V4L2_SLICED_VPS
, 16, 16, 0},
568 { 0xaa, 0xaa, 0xff, 0xff, 0xba, 0xce, 0x2b, 0x0d,
569 0xa6, 0xda, 0x0b, 0x00, 0x00, 0x00, 0x60, 0x00 }
571 /* 0x1d0 User programmable */
574 static int tvp5150_write_inittab(struct v4l2_subdev
*sd
,
575 const struct i2c_reg_value
*regs
)
577 struct tvp5150
*decoder
= to_tvp5150(sd
);
579 while (regs
->reg
!= 0xff) {
580 regmap_write(decoder
->regmap
, regs
->reg
, regs
->value
);
586 static int tvp5150_vdp_init(struct v4l2_subdev
*sd
)
588 struct tvp5150
*decoder
= to_tvp5150(sd
);
589 struct regmap
*map
= decoder
->regmap
;
593 /* Disable Full Field */
594 regmap_write(map
, TVP5150_FULL_FIELD_ENA
, 0);
596 /* Before programming, Line mode should be at 0xff */
597 for (i
= TVP5150_LINE_MODE_INI
; i
<= TVP5150_LINE_MODE_END
; i
++)
598 regmap_write(map
, i
, 0xff);
601 for (j
= 0; j
< ARRAY_SIZE(vbi_ram_default
); j
++) {
602 const struct i2c_vbi_ram_value
*regs
= &vbi_ram_default
[j
];
604 if (!regs
->type
.vbi_type
)
607 regmap_write(map
, TVP5150_CONF_RAM_ADDR_HIGH
, regs
->reg
>> 8);
608 regmap_write(map
, TVP5150_CONF_RAM_ADDR_LOW
, regs
->reg
);
610 for (i
= 0; i
< 16; i
++)
611 regmap_write(map
, TVP5150_VDP_CONF_RAM_DATA
,
617 /* Fills VBI capabilities based on i2c_vbi_ram_value struct */
618 static int tvp5150_g_sliced_vbi_cap(struct v4l2_subdev
*sd
,
619 struct v4l2_sliced_vbi_cap
*cap
)
623 dev_dbg_lvl(sd
->dev
, 1, debug
, "g_sliced_vbi_cap\n");
624 memset(cap
, 0, sizeof(*cap
));
626 for (i
= 0; i
< ARRAY_SIZE(vbi_ram_default
); i
++) {
627 const struct i2c_vbi_ram_value
*regs
= &vbi_ram_default
[i
];
629 if (!regs
->type
.vbi_type
)
632 for (line
= regs
->type
.ini_line
;
633 line
<= regs
->type
.end_line
;
635 cap
->service_lines
[0][line
] |= regs
->type
.vbi_type
;
637 cap
->service_set
|= regs
->type
.vbi_type
;
642 /* Set vbi processing
643 * type - one of tvp5150_vbi_types
644 * line - line to gather data
645 * fields: bit 0 field1, bit 1, field2
646 * flags (default=0xf0) is a bitmask, were set means:
647 * bit 7: enable filtering null bytes on CC
648 * bit 6: send data also to FIFO
649 * bit 5: don't allow data with errors on FIFO
650 * bit 4: enable ECC when possible
651 * pix_align = pix alignment:
655 static int tvp5150_set_vbi(struct v4l2_subdev
*sd
,
656 unsigned int type
, u8 flags
, int line
,
659 struct tvp5150
*decoder
= to_tvp5150(sd
);
660 v4l2_std_id std
= decoder
->norm
;
664 if (std
== V4L2_STD_ALL
) {
665 dev_err(sd
->dev
, "VBI can't be configured without knowing number of lines\n");
667 } else if (std
& V4L2_STD_625_50
) {
668 /* Don't follow NTSC Line number convension */
672 if (line
< 6 || line
> 27)
675 for (i
= 0; i
< ARRAY_SIZE(vbi_ram_default
); i
++) {
676 const struct i2c_vbi_ram_value
*regs
= &vbi_ram_default
[i
];
678 if (!regs
->type
.vbi_type
)
681 if ((type
& regs
->type
.vbi_type
) &&
682 (line
>= regs
->type
.ini_line
) &&
683 (line
<= regs
->type
.end_line
))
688 type
= pos
| (flags
& 0xf0);
689 reg
= ((line
- 6) << 1) + TVP5150_LINE_MODE_INI
;
692 regmap_write(decoder
->regmap
, reg
, type
);
695 regmap_write(decoder
->regmap
, reg
+ 1, type
);
700 static int tvp5150_get_vbi(struct v4l2_subdev
*sd
, int line
)
702 struct tvp5150
*decoder
= to_tvp5150(sd
);
703 v4l2_std_id std
= decoder
->norm
;
708 if (std
== V4L2_STD_ALL
) {
709 dev_err(sd
->dev
, "VBI can't be configured without knowing number of lines\n");
711 } else if (std
& V4L2_STD_625_50
) {
712 /* Don't follow NTSC Line number convension */
716 if (line
< 6 || line
> 27)
719 reg
= ((line
- 6) << 1) + TVP5150_LINE_MODE_INI
;
721 for (i
= 0; i
<= 1; i
++) {
722 ret
= tvp5150_read(sd
, reg
+ i
);
724 dev_err(sd
->dev
, "%s: failed with error = %d\n",
729 if (pos
< ARRAY_SIZE(vbi_ram_default
))
730 type
|= vbi_ram_default
[pos
].type
.vbi_type
;
736 static int tvp5150_set_std(struct v4l2_subdev
*sd
, v4l2_std_id std
)
738 struct tvp5150
*decoder
= to_tvp5150(sd
);
741 /* First tests should be against specific std */
743 if (std
== V4L2_STD_NTSC_443
) {
744 fmt
= VIDEO_STD_NTSC_4_43_BIT
;
745 } else if (std
== V4L2_STD_PAL_M
) {
746 fmt
= VIDEO_STD_PAL_M_BIT
;
747 } else if (std
== V4L2_STD_PAL_N
|| std
== V4L2_STD_PAL_Nc
) {
748 fmt
= VIDEO_STD_PAL_COMBINATION_N_BIT
;
750 /* Then, test against generic ones */
751 if (std
& V4L2_STD_NTSC
)
752 fmt
= VIDEO_STD_NTSC_MJ_BIT
;
753 else if (std
& V4L2_STD_PAL
)
754 fmt
= VIDEO_STD_PAL_BDGHIN_BIT
;
755 else if (std
& V4L2_STD_SECAM
)
756 fmt
= VIDEO_STD_SECAM_BIT
;
759 dev_dbg_lvl(sd
->dev
, 1, debug
, "Set video std register to %d.\n", fmt
);
760 regmap_write(decoder
->regmap
, TVP5150_VIDEO_STD
, fmt
);
764 static int tvp5150_g_std(struct v4l2_subdev
*sd
, v4l2_std_id
*std
)
766 struct tvp5150
*decoder
= to_tvp5150(sd
);
768 *std
= decoder
->norm
;
773 static int tvp5150_s_std(struct v4l2_subdev
*sd
, v4l2_std_id std
)
775 struct tvp5150
*decoder
= to_tvp5150(sd
);
777 if (decoder
->norm
== std
)
780 /* Change cropping height limits */
781 if (std
& V4L2_STD_525_60
)
782 decoder
->rect
.height
= TVP5150_V_MAX_525_60
;
784 decoder
->rect
.height
= TVP5150_V_MAX_OTHERS
;
788 return tvp5150_set_std(sd
, std
);
791 static v4l2_std_id
tvp5150_read_std(struct v4l2_subdev
*sd
)
793 int val
= tvp5150_read(sd
, TVP5150_STATUS_REG_5
);
795 switch (val
& 0x0F) {
797 return V4L2_STD_NTSC
;
801 return V4L2_STD_PAL_M
;
803 return V4L2_STD_PAL_N
| V4L2_STD_PAL_Nc
;
805 return V4L2_STD_NTSC_443
;
807 return V4L2_STD_SECAM
;
809 return V4L2_STD_UNKNOWN
;
813 static int query_lock(struct v4l2_subdev
*sd
)
815 struct tvp5150
*decoder
= to_tvp5150(sd
);
819 return decoder
->lock
;
821 regmap_read(decoder
->regmap
, TVP5150_STATUS_REG_1
, &status
);
823 /* For standard detection, we need the 3 locks */
824 return (status
& 0x0e) == 0x0e;
827 static int tvp5150_querystd(struct v4l2_subdev
*sd
, v4l2_std_id
*std_id
)
829 *std_id
= query_lock(sd
) ? tvp5150_read_std(sd
) : V4L2_STD_UNKNOWN
;
834 static const struct v4l2_event tvp5150_ev_fmt
= {
835 .type
= V4L2_EVENT_SOURCE_CHANGE
,
836 .u
.src_change
.changes
= V4L2_EVENT_SRC_CH_RESOLUTION
,
839 static irqreturn_t
tvp5150_isr(int irq
, void *dev_id
)
841 struct tvp5150
*decoder
= dev_id
;
842 struct regmap
*map
= decoder
->regmap
;
843 unsigned int mask
, active
= 0, status
= 0;
845 mask
= TVP5150_MISC_CTL_YCBCR_OE
| TVP5150_MISC_CTL_SYNC_OE
|
846 TVP5150_MISC_CTL_CLOCK_OE
;
848 regmap_read(map
, TVP5150_INT_STATUS_REG_A
, &status
);
850 regmap_write(map
, TVP5150_INT_STATUS_REG_A
, status
);
852 if (status
& TVP5150_INT_A_LOCK
) {
853 decoder
->lock
= !!(status
& TVP5150_INT_A_LOCK_STATUS
);
854 dev_dbg_lvl(decoder
->sd
.dev
, 1, debug
,
855 "sync lo%s signal\n",
856 decoder
->lock
? "ck" : "ss");
857 v4l2_subdev_notify_event(&decoder
->sd
, &tvp5150_ev_fmt
);
858 regmap_update_bits(map
, TVP5150_MISC_CTL
, mask
,
859 decoder
->lock
? decoder
->oe
: 0);
865 regmap_read(map
, TVP5150_INT_ACTIVE_REG_B
, &active
);
868 regmap_read(map
, TVP5150_INT_STATUS_REG_B
, &status
);
870 regmap_write(map
, TVP5150_INT_RESET_REG_B
, status
);
876 static int tvp5150_reset(struct v4l2_subdev
*sd
, u32 val
)
878 struct tvp5150
*decoder
= to_tvp5150(sd
);
879 struct regmap
*map
= decoder
->regmap
;
881 /* Initializes TVP5150 to its default values */
882 tvp5150_write_inittab(sd
, tvp5150_init_default
);
885 /* Configure pins: FID, VSYNC, INTREQ, SCLK */
886 regmap_write(map
, TVP5150_CONF_SHARED_PIN
, 0x0);
887 /* Set interrupt polarity to active high */
888 regmap_write(map
, TVP5150_INT_CONF
, TVP5150_VDPOE
| 0x1);
889 regmap_write(map
, TVP5150_INTT_CONFIG_REG_B
, 0x1);
891 /* Configure pins: FID, VSYNC, GPCL/VBLK, SCLK */
892 regmap_write(map
, TVP5150_CONF_SHARED_PIN
, 0x2);
893 /* Keep interrupt polarity active low */
894 regmap_write(map
, TVP5150_INT_CONF
, TVP5150_VDPOE
);
895 regmap_write(map
, TVP5150_INTT_CONFIG_REG_B
, 0x0);
898 /* Initializes VDP registers */
899 tvp5150_vdp_init(sd
);
901 /* Selects decoder input */
904 /* Initialize image preferences */
905 v4l2_ctrl_handler_setup(&decoder
->hdl
);
910 static int tvp5150_enable(struct v4l2_subdev
*sd
)
912 struct tvp5150
*decoder
= to_tvp5150(sd
);
915 /* Initializes TVP5150 to stream enabled values */
916 tvp5150_write_inittab(sd
, tvp5150_init_enable
);
918 if (decoder
->norm
== V4L2_STD_ALL
)
919 std
= tvp5150_read_std(sd
);
923 /* Disable autoswitch mode */
924 tvp5150_set_std(sd
, std
);
927 * Enable the YCbCr and clock outputs. In discrete sync mode
928 * (non-BT.656) additionally enable the the sync outputs.
930 switch (decoder
->mbus_type
) {
931 case V4L2_MBUS_PARALLEL
:
932 /* 8-bit 4:2:2 YUV with discrete sync output */
933 regmap_update_bits(decoder
->regmap
, TVP5150_DATA_RATE_SEL
,
935 decoder
->oe
= TVP5150_MISC_CTL_YCBCR_OE
|
936 TVP5150_MISC_CTL_CLOCK_OE
|
937 TVP5150_MISC_CTL_SYNC_OE
;
939 case V4L2_MBUS_BT656
:
940 decoder
->oe
= TVP5150_MISC_CTL_YCBCR_OE
|
941 TVP5150_MISC_CTL_CLOCK_OE
;
950 static int tvp5150_s_ctrl(struct v4l2_ctrl
*ctrl
)
952 struct v4l2_subdev
*sd
= to_sd(ctrl
);
953 struct tvp5150
*decoder
= to_tvp5150(sd
);
956 case V4L2_CID_BRIGHTNESS
:
957 regmap_write(decoder
->regmap
, TVP5150_BRIGHT_CTL
, ctrl
->val
);
959 case V4L2_CID_CONTRAST
:
960 regmap_write(decoder
->regmap
, TVP5150_CONTRAST_CTL
, ctrl
->val
);
962 case V4L2_CID_SATURATION
:
963 regmap_write(decoder
->regmap
, TVP5150_SATURATION_CTL
,
967 regmap_write(decoder
->regmap
, TVP5150_HUE_CTL
, ctrl
->val
);
969 case V4L2_CID_TEST_PATTERN
:
970 decoder
->enable
= ctrl
->val
? false : true;
977 static void tvp5150_set_default(v4l2_std_id std
, struct v4l2_rect
*crop
)
979 /* Default is no cropping */
982 crop
->width
= TVP5150_H_MAX
;
983 if (std
& V4L2_STD_525_60
)
984 crop
->height
= TVP5150_V_MAX_525_60
;
986 crop
->height
= TVP5150_V_MAX_OTHERS
;
989 static int tvp5150_fill_fmt(struct v4l2_subdev
*sd
,
990 struct v4l2_subdev_pad_config
*cfg
,
991 struct v4l2_subdev_format
*format
)
993 struct v4l2_mbus_framefmt
*f
;
994 struct tvp5150
*decoder
= to_tvp5150(sd
);
996 if (!format
|| (format
->pad
!= TVP5150_PAD_VID_OUT
))
1001 f
->width
= decoder
->rect
.width
;
1002 f
->height
= decoder
->rect
.height
/ 2;
1004 f
->code
= TVP5150_MBUS_FMT
;
1005 f
->field
= TVP5150_FIELD
;
1006 f
->colorspace
= TVP5150_COLORSPACE
;
1008 dev_dbg_lvl(sd
->dev
, 1, debug
, "width = %d, height = %d\n", f
->width
,
1013 static int tvp5150_set_selection(struct v4l2_subdev
*sd
,
1014 struct v4l2_subdev_pad_config
*cfg
,
1015 struct v4l2_subdev_selection
*sel
)
1017 struct tvp5150
*decoder
= to_tvp5150(sd
);
1018 struct v4l2_rect rect
= sel
->r
;
1022 if (sel
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
||
1023 sel
->target
!= V4L2_SEL_TGT_CROP
)
1026 dev_dbg_lvl(sd
->dev
, 1, debug
, "%s left=%d, top=%d, width=%d, height=%d\n",
1027 __func__
, rect
.left
, rect
.top
, rect
.width
, rect
.height
);
1029 /* tvp5150 has some special limits */
1030 rect
.left
= clamp(rect
.left
, 0, TVP5150_MAX_CROP_LEFT
);
1031 rect
.top
= clamp(rect
.top
, 0, TVP5150_MAX_CROP_TOP
);
1033 /* Calculate height based on current standard */
1034 if (decoder
->norm
== V4L2_STD_ALL
)
1035 std
= tvp5150_read_std(sd
);
1037 std
= decoder
->norm
;
1039 if (std
& V4L2_STD_525_60
)
1040 hmax
= TVP5150_V_MAX_525_60
;
1042 hmax
= TVP5150_V_MAX_OTHERS
;
1046 * - width = 2 due to UYVY colorspace
1047 * - height, image = no special alignment
1049 v4l_bound_align_image(&rect
.width
,
1050 TVP5150_H_MAX
- TVP5150_MAX_CROP_LEFT
- rect
.left
,
1051 TVP5150_H_MAX
- rect
.left
, 1, &rect
.height
,
1052 hmax
- TVP5150_MAX_CROP_TOP
- rect
.top
,
1053 hmax
- rect
.top
, 0, 0);
1055 regmap_write(decoder
->regmap
, TVP5150_VERT_BLANKING_START
, rect
.top
);
1056 regmap_write(decoder
->regmap
, TVP5150_VERT_BLANKING_STOP
,
1057 rect
.top
+ rect
.height
- hmax
);
1058 regmap_write(decoder
->regmap
, TVP5150_ACT_VD_CROP_ST_MSB
,
1059 rect
.left
>> TVP5150_CROP_SHIFT
);
1060 regmap_write(decoder
->regmap
, TVP5150_ACT_VD_CROP_ST_LSB
,
1061 rect
.left
| (1 << TVP5150_CROP_SHIFT
));
1062 regmap_write(decoder
->regmap
, TVP5150_ACT_VD_CROP_STP_MSB
,
1063 (rect
.left
+ rect
.width
- TVP5150_MAX_CROP_LEFT
) >>
1064 TVP5150_CROP_SHIFT
);
1065 regmap_write(decoder
->regmap
, TVP5150_ACT_VD_CROP_STP_LSB
,
1066 rect
.left
+ rect
.width
- TVP5150_MAX_CROP_LEFT
);
1068 decoder
->rect
= rect
;
1073 static int tvp5150_get_selection(struct v4l2_subdev
*sd
,
1074 struct v4l2_subdev_pad_config
*cfg
,
1075 struct v4l2_subdev_selection
*sel
)
1077 struct tvp5150
*decoder
= container_of(sd
, struct tvp5150
, sd
);
1080 if (sel
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
1083 switch (sel
->target
) {
1084 case V4L2_SEL_TGT_CROP_BOUNDS
:
1087 sel
->r
.width
= TVP5150_H_MAX
;
1089 /* Calculate height based on current standard */
1090 if (decoder
->norm
== V4L2_STD_ALL
)
1091 std
= tvp5150_read_std(sd
);
1093 std
= decoder
->norm
;
1094 if (std
& V4L2_STD_525_60
)
1095 sel
->r
.height
= TVP5150_V_MAX_525_60
;
1097 sel
->r
.height
= TVP5150_V_MAX_OTHERS
;
1099 case V4L2_SEL_TGT_CROP
:
1100 sel
->r
= decoder
->rect
;
1107 static int tvp5150_g_mbus_config(struct v4l2_subdev
*sd
,
1108 struct v4l2_mbus_config
*cfg
)
1110 struct tvp5150
*decoder
= to_tvp5150(sd
);
1112 cfg
->type
= decoder
->mbus_type
;
1113 cfg
->flags
= V4L2_MBUS_MASTER
| V4L2_MBUS_PCLK_SAMPLE_RISING
1114 | V4L2_MBUS_FIELD_EVEN_LOW
| V4L2_MBUS_DATA_ACTIVE_HIGH
;
1119 /****************************************************************************
1121 ****************************************************************************/
1122 static int tvp5150_init_cfg(struct v4l2_subdev
*sd
,
1123 struct v4l2_subdev_pad_config
*cfg
)
1125 struct tvp5150
*decoder
= to_tvp5150(sd
);
1129 * Reset selection to maximum on subdev_open() if autodetection is on
1130 * and a standard change is detected.
1132 if (decoder
->norm
== V4L2_STD_ALL
) {
1133 std
= tvp5150_read_std(sd
);
1134 if (std
!= decoder
->detected_norm
) {
1135 decoder
->detected_norm
= std
;
1136 tvp5150_set_default(std
, &decoder
->rect
);
1143 static int tvp5150_enum_mbus_code(struct v4l2_subdev
*sd
,
1144 struct v4l2_subdev_pad_config
*cfg
,
1145 struct v4l2_subdev_mbus_code_enum
*code
)
1147 if (code
->pad
|| code
->index
)
1150 code
->code
= TVP5150_MBUS_FMT
;
1154 static int tvp5150_enum_frame_size(struct v4l2_subdev
*sd
,
1155 struct v4l2_subdev_pad_config
*cfg
,
1156 struct v4l2_subdev_frame_size_enum
*fse
)
1158 struct tvp5150
*decoder
= to_tvp5150(sd
);
1160 if (fse
->index
>= 8 || fse
->code
!= TVP5150_MBUS_FMT
)
1163 fse
->code
= TVP5150_MBUS_FMT
;
1164 fse
->min_width
= decoder
->rect
.width
;
1165 fse
->max_width
= decoder
->rect
.width
;
1166 fse
->min_height
= decoder
->rect
.height
/ 2;
1167 fse
->max_height
= decoder
->rect
.height
/ 2;
1172 /****************************************************************************
1174 ****************************************************************************/
1176 #ifdef CONFIG_MEDIA_CONTROLLER
1177 static int tvp5150_link_setup(struct media_entity
*entity
,
1178 const struct media_pad
*local
,
1179 const struct media_pad
*remote
, u32 flags
)
1181 struct v4l2_subdev
*sd
= media_entity_to_v4l2_subdev(entity
);
1182 struct tvp5150
*decoder
= to_tvp5150(sd
);
1185 for (i
= 0; i
< TVP5150_INPUT_NUM
; i
++) {
1186 if (remote
->entity
== &decoder
->input_ent
[i
])
1190 /* Do nothing for entities that are not input connectors */
1191 if (i
== TVP5150_INPUT_NUM
)
1201 static const struct media_entity_operations tvp5150_sd_media_ops
= {
1202 .link_setup
= tvp5150_link_setup
,
1206 /****************************************************************************
1208 ****************************************************************************/
1210 static int tvp5150_s_stream(struct v4l2_subdev
*sd
, int enable
)
1212 struct tvp5150
*decoder
= to_tvp5150(sd
);
1213 unsigned int mask
, val
= 0, int_val
= 0;
1215 mask
= TVP5150_MISC_CTL_YCBCR_OE
| TVP5150_MISC_CTL_SYNC_OE
|
1216 TVP5150_MISC_CTL_CLOCK_OE
;
1221 /* Enable outputs if decoder is locked */
1223 val
= decoder
->lock
? decoder
->oe
: 0;
1226 int_val
= TVP5150_INT_A_LOCK
;
1227 v4l2_subdev_notify_event(&decoder
->sd
, &tvp5150_ev_fmt
);
1230 regmap_update_bits(decoder
->regmap
, TVP5150_MISC_CTL
, mask
, val
);
1232 /* Enable / Disable lock interrupt */
1233 regmap_update_bits(decoder
->regmap
, TVP5150_INT_ENABLE_REG_A
,
1234 TVP5150_INT_A_LOCK
, int_val
);
1239 static int tvp5150_s_routing(struct v4l2_subdev
*sd
,
1240 u32 input
, u32 output
, u32 config
)
1242 struct tvp5150
*decoder
= to_tvp5150(sd
);
1244 decoder
->input
= input
;
1245 decoder
->output
= output
;
1247 if (output
== TVP5150_BLACK_SCREEN
)
1248 decoder
->enable
= false;
1250 decoder
->enable
= true;
1256 static int tvp5150_s_raw_fmt(struct v4l2_subdev
*sd
, struct v4l2_vbi_format
*fmt
)
1258 struct tvp5150
*decoder
= to_tvp5150(sd
);
1261 * this is for capturing 36 raw vbi lines
1262 * if there's a way to cut off the beginning 2 vbi lines
1263 * with the tvp5150 then the vbi line count could be lowered
1264 * to 17 lines/field again, although I couldn't find a register
1265 * which could do that cropping
1268 if (fmt
->sample_format
== V4L2_PIX_FMT_GREY
)
1269 regmap_write(decoder
->regmap
, TVP5150_LUMA_PROC_CTL_1
, 0x70);
1270 if (fmt
->count
[0] == 18 && fmt
->count
[1] == 18) {
1271 regmap_write(decoder
->regmap
, TVP5150_VERT_BLANKING_START
,
1273 regmap_write(decoder
->regmap
, TVP5150_VERT_BLANKING_STOP
, 0x01);
1278 static int tvp5150_s_sliced_fmt(struct v4l2_subdev
*sd
, struct v4l2_sliced_vbi_format
*svbi
)
1280 struct tvp5150
*decoder
= to_tvp5150(sd
);
1283 if (svbi
->service_set
!= 0) {
1284 for (i
= 0; i
<= 23; i
++) {
1285 svbi
->service_lines
[1][i
] = 0;
1286 svbi
->service_lines
[0][i
] =
1287 tvp5150_set_vbi(sd
, svbi
->service_lines
[0][i
],
1291 regmap_write(decoder
->regmap
, TVP5150_FIFO_OUT_CTRL
, 1);
1294 regmap_write(decoder
->regmap
, TVP5150_FIFO_OUT_CTRL
, 0);
1296 /* Disable Full Field */
1297 regmap_write(decoder
->regmap
, TVP5150_FULL_FIELD_ENA
, 0);
1299 /* Disable Line modes */
1300 for (i
= TVP5150_LINE_MODE_INI
; i
<= TVP5150_LINE_MODE_END
; i
++)
1301 regmap_write(decoder
->regmap
, i
, 0xff);
1306 static int tvp5150_g_sliced_fmt(struct v4l2_subdev
*sd
, struct v4l2_sliced_vbi_format
*svbi
)
1310 memset(svbi
->service_lines
, 0, sizeof(svbi
->service_lines
));
1312 for (i
= 0; i
<= 23; i
++) {
1313 svbi
->service_lines
[0][i
] =
1314 tvp5150_get_vbi(sd
, i
);
1315 mask
|= svbi
->service_lines
[0][i
];
1317 svbi
->service_set
= mask
;
1321 #ifdef CONFIG_VIDEO_ADV_DEBUG
1322 static int tvp5150_g_register(struct v4l2_subdev
*sd
, struct v4l2_dbg_register
*reg
)
1326 res
= tvp5150_read(sd
, reg
->reg
& 0xff);
1328 dev_err(sd
->dev
, "%s: failed with error = %d\n", __func__
, res
);
1337 static int tvp5150_s_register(struct v4l2_subdev
*sd
, const struct v4l2_dbg_register
*reg
)
1339 struct tvp5150
*decoder
= to_tvp5150(sd
);
1341 return regmap_write(decoder
->regmap
, reg
->reg
& 0xff, reg
->val
& 0xff);
1345 static int tvp5150_g_tuner(struct v4l2_subdev
*sd
, struct v4l2_tuner
*vt
)
1347 int status
= tvp5150_read(sd
, 0x88);
1349 vt
->signal
= ((status
& 0x04) && (status
& 0x02)) ? 0xffff : 0x0;
1353 static int tvp5150_registered(struct v4l2_subdev
*sd
)
1355 #ifdef CONFIG_MEDIA_CONTROLLER
1356 struct tvp5150
*decoder
= to_tvp5150(sd
);
1360 for (i
= 0; i
< TVP5150_INPUT_NUM
; i
++) {
1361 struct media_entity
*input
= &decoder
->input_ent
[i
];
1362 struct media_pad
*pad
= &decoder
->input_pad
[i
];
1367 decoder
->input_pad
[i
].flags
= MEDIA_PAD_FL_SOURCE
;
1369 ret
= media_entity_pads_init(input
, 1, pad
);
1373 ret
= media_device_register_entity(sd
->v4l2_dev
->mdev
, input
);
1377 ret
= media_create_pad_link(input
, 0, &sd
->entity
,
1378 TVP5150_PAD_IF_INPUT
, 0);
1380 media_device_unregister_entity(input
);
1389 /* ----------------------------------------------------------------------- */
1391 static const struct v4l2_ctrl_ops tvp5150_ctrl_ops
= {
1392 .s_ctrl
= tvp5150_s_ctrl
,
1395 static const struct v4l2_subdev_core_ops tvp5150_core_ops
= {
1396 .log_status
= tvp5150_log_status
,
1397 .reset
= tvp5150_reset
,
1398 #ifdef CONFIG_VIDEO_ADV_DEBUG
1399 .g_register
= tvp5150_g_register
,
1400 .s_register
= tvp5150_s_register
,
1404 static const struct v4l2_subdev_tuner_ops tvp5150_tuner_ops
= {
1405 .g_tuner
= tvp5150_g_tuner
,
1408 static const struct v4l2_subdev_video_ops tvp5150_video_ops
= {
1409 .s_std
= tvp5150_s_std
,
1410 .g_std
= tvp5150_g_std
,
1411 .querystd
= tvp5150_querystd
,
1412 .s_stream
= tvp5150_s_stream
,
1413 .s_routing
= tvp5150_s_routing
,
1414 .g_mbus_config
= tvp5150_g_mbus_config
,
1417 static const struct v4l2_subdev_vbi_ops tvp5150_vbi_ops
= {
1418 .g_sliced_vbi_cap
= tvp5150_g_sliced_vbi_cap
,
1419 .g_sliced_fmt
= tvp5150_g_sliced_fmt
,
1420 .s_sliced_fmt
= tvp5150_s_sliced_fmt
,
1421 .s_raw_fmt
= tvp5150_s_raw_fmt
,
1424 static const struct v4l2_subdev_pad_ops tvp5150_pad_ops
= {
1425 .init_cfg
= tvp5150_init_cfg
,
1426 .enum_mbus_code
= tvp5150_enum_mbus_code
,
1427 .enum_frame_size
= tvp5150_enum_frame_size
,
1428 .set_fmt
= tvp5150_fill_fmt
,
1429 .get_fmt
= tvp5150_fill_fmt
,
1430 .get_selection
= tvp5150_get_selection
,
1431 .set_selection
= tvp5150_set_selection
,
1434 static const struct v4l2_subdev_ops tvp5150_ops
= {
1435 .core
= &tvp5150_core_ops
,
1436 .tuner
= &tvp5150_tuner_ops
,
1437 .video
= &tvp5150_video_ops
,
1438 .vbi
= &tvp5150_vbi_ops
,
1439 .pad
= &tvp5150_pad_ops
,
1442 static const struct v4l2_subdev_internal_ops tvp5150_internal_ops
= {
1443 .registered
= tvp5150_registered
,
1446 /****************************************************************************
1448 ****************************************************************************/
1450 static const struct regmap_range tvp5150_readable_ranges
[] = {
1452 .range_min
= TVP5150_VD_IN_SRC_SEL_1
,
1453 .range_max
= TVP5150_AUTOSW_MSK
,
1455 .range_min
= TVP5150_COLOR_KIL_THSH_CTL
,
1456 .range_max
= TVP5150_CONF_SHARED_PIN
,
1458 .range_min
= TVP5150_ACT_VD_CROP_ST_MSB
,
1459 .range_max
= TVP5150_HORIZ_SYNC_START
,
1461 .range_min
= TVP5150_VERT_BLANKING_START
,
1462 .range_max
= TVP5150_INTT_CONFIG_REG_B
,
1464 .range_min
= TVP5150_VIDEO_STD
,
1465 .range_max
= TVP5150_VIDEO_STD
,
1467 .range_min
= TVP5150_CB_GAIN_FACT
,
1468 .range_max
= TVP5150_REV_SELECT
,
1470 .range_min
= TVP5150_MSB_DEV_ID
,
1471 .range_max
= TVP5150_STATUS_REG_5
,
1473 .range_min
= TVP5150_CC_DATA_INI
,
1474 .range_max
= TVP5150_TELETEXT_FIL_ENA
,
1476 .range_min
= TVP5150_INT_STATUS_REG_A
,
1477 .range_max
= TVP5150_FIFO_OUT_CTRL
,
1479 .range_min
= TVP5150_FULL_FIELD_ENA
,
1480 .range_max
= TVP5150_FULL_FIELD_MODE_REG
,
1484 static bool tvp5150_volatile_reg(struct device
*dev
, unsigned int reg
)
1487 case TVP5150_VERT_LN_COUNT_MSB
:
1488 case TVP5150_VERT_LN_COUNT_LSB
:
1489 case TVP5150_INT_STATUS_REG_A
:
1490 case TVP5150_INT_STATUS_REG_B
:
1491 case TVP5150_INT_ACTIVE_REG_B
:
1492 case TVP5150_STATUS_REG_1
:
1493 case TVP5150_STATUS_REG_2
:
1494 case TVP5150_STATUS_REG_3
:
1495 case TVP5150_STATUS_REG_4
:
1496 case TVP5150_STATUS_REG_5
:
1497 /* CC, WSS, VPS, VITC data? */
1498 case TVP5150_VBI_FIFO_READ_DATA
:
1499 case TVP5150_VDP_STATUS_REG
:
1500 case TVP5150_FIFO_WORD_COUNT
:
1507 static const struct regmap_access_table tvp5150_readable_table
= {
1508 .yes_ranges
= tvp5150_readable_ranges
,
1509 .n_yes_ranges
= ARRAY_SIZE(tvp5150_readable_ranges
),
1512 static struct regmap_config tvp5150_config
= {
1515 .max_register
= 0xff,
1517 .cache_type
= REGCACHE_RBTREE
,
1519 .rd_table
= &tvp5150_readable_table
,
1520 .volatile_reg
= tvp5150_volatile_reg
,
1523 static int tvp5150_detect_version(struct tvp5150
*core
)
1525 struct v4l2_subdev
*sd
= &core
->sd
;
1526 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
1531 * Read consequent registers - TVP5150_MSB_DEV_ID, TVP5150_LSB_DEV_ID,
1532 * TVP5150_ROM_MAJOR_VER, TVP5150_ROM_MINOR_VER
1534 res
= regmap_bulk_read(core
->regmap
, TVP5150_MSB_DEV_ID
, regs
, 4);
1536 dev_err(&c
->dev
, "reading ID registers failed: %d\n", res
);
1540 core
->dev_id
= (regs
[0] << 8) | regs
[1];
1541 core
->rom_ver
= (regs
[2] << 8) | regs
[3];
1543 dev_info(sd
->dev
, "tvp%04x (%u.%u) chip found @ 0x%02x (%s)\n",
1544 core
->dev_id
, regs
[2], regs
[3], c
->addr
<< 1,
1547 if (core
->dev_id
== 0x5150 && core
->rom_ver
== 0x0321) {
1548 dev_info(sd
->dev
, "tvp5150a detected.\n");
1549 } else if (core
->dev_id
== 0x5150 && core
->rom_ver
== 0x0400) {
1550 dev_info(sd
->dev
, "tvp5150am1 detected.\n");
1552 /* ITU-T BT.656.4 timing */
1553 regmap_write(core
->regmap
, TVP5150_REV_SELECT
, 0);
1554 } else if (core
->dev_id
== 0x5151 && core
->rom_ver
== 0x0100) {
1555 dev_info(sd
->dev
, "tvp5151 detected.\n");
1557 dev_info(sd
->dev
, "*** unknown tvp%04x chip detected.\n",
1564 static int tvp5150_init(struct i2c_client
*c
)
1566 struct gpio_desc
*pdn_gpio
;
1567 struct gpio_desc
*reset_gpio
;
1569 pdn_gpio
= devm_gpiod_get_optional(&c
->dev
, "pdn", GPIOD_OUT_HIGH
);
1570 if (IS_ERR(pdn_gpio
))
1571 return PTR_ERR(pdn_gpio
);
1574 gpiod_set_value_cansleep(pdn_gpio
, 0);
1575 /* Delay time between power supplies active and reset */
1579 reset_gpio
= devm_gpiod_get_optional(&c
->dev
, "reset", GPIOD_OUT_HIGH
);
1580 if (IS_ERR(reset_gpio
))
1581 return PTR_ERR(reset_gpio
);
1584 /* RESETB pulse duration */
1586 gpiod_set_value_cansleep(reset_gpio
, 0);
1587 /* Delay time between end of reset to I2C active */
1588 usleep_range(200, 250);
1594 static int tvp5150_parse_dt(struct tvp5150
*decoder
, struct device_node
*np
)
1596 struct v4l2_fwnode_endpoint bus_cfg
= { .bus_type
= 0 };
1597 struct device_node
*ep
;
1598 #ifdef CONFIG_MEDIA_CONTROLLER
1599 struct device_node
*connectors
, *child
;
1600 struct media_entity
*input
;
1607 ep
= of_graph_get_next_endpoint(np
, NULL
);
1611 ret
= v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep
), &bus_cfg
);
1615 flags
= bus_cfg
.bus
.parallel
.flags
;
1617 if (bus_cfg
.bus_type
== V4L2_MBUS_PARALLEL
&&
1618 !(flags
& V4L2_MBUS_HSYNC_ACTIVE_HIGH
&&
1619 flags
& V4L2_MBUS_VSYNC_ACTIVE_HIGH
&&
1620 flags
& V4L2_MBUS_FIELD_EVEN_LOW
)) {
1625 decoder
->mbus_type
= bus_cfg
.bus_type
;
1627 #ifdef CONFIG_MEDIA_CONTROLLER
1628 connectors
= of_get_child_by_name(np
, "connectors");
1633 for_each_available_child_of_node(connectors
, child
) {
1634 ret
= of_property_read_u32(child
, "input", &input_type
);
1636 dev_err(decoder
->sd
.dev
,
1637 "missing type property in node %pOFn\n",
1642 if (input_type
>= TVP5150_INPUT_NUM
) {
1647 input
= &decoder
->input_ent
[input_type
];
1649 /* Each input connector can only be defined once */
1651 dev_err(decoder
->sd
.dev
,
1652 "input %s with same type already exists\n",
1658 switch (input_type
) {
1659 case TVP5150_COMPOSITE0
:
1660 case TVP5150_COMPOSITE1
:
1661 input
->function
= MEDIA_ENT_F_CONN_COMPOSITE
;
1663 case TVP5150_SVIDEO
:
1664 input
->function
= MEDIA_ENT_F_CONN_SVIDEO
;
1668 input
->flags
= MEDIA_ENT_FL_CONNECTOR
;
1670 ret
= of_property_read_string(child
, "label", &name
);
1672 dev_err(decoder
->sd
.dev
,
1673 "missing label property in node %pOFn\n",
1682 of_node_put(connectors
);
1689 static const char * const tvp5150_test_patterns
[2] = {
1694 static int tvp5150_probe(struct i2c_client
*c
,
1695 const struct i2c_device_id
*id
)
1697 struct tvp5150
*core
;
1698 struct v4l2_subdev
*sd
;
1699 struct device_node
*np
= c
->dev
.of_node
;
1703 /* Check if the adapter supports the needed features */
1704 if (!i2c_check_functionality(c
->adapter
,
1705 I2C_FUNC_SMBUS_READ_BYTE
| I2C_FUNC_SMBUS_WRITE_BYTE_DATA
))
1708 res
= tvp5150_init(c
);
1712 core
= devm_kzalloc(&c
->dev
, sizeof(*core
), GFP_KERNEL
);
1716 map
= devm_regmap_init_i2c(c
, &tvp5150_config
);
1718 return PTR_ERR(map
);
1723 if (IS_ENABLED(CONFIG_OF
) && np
) {
1724 res
= tvp5150_parse_dt(core
, np
);
1726 dev_err(sd
->dev
, "DT parsing error: %d\n", res
);
1730 /* Default to BT.656 embedded sync */
1731 core
->mbus_type
= V4L2_MBUS_BT656
;
1734 v4l2_i2c_subdev_init(sd
, c
, &tvp5150_ops
);
1735 sd
->internal_ops
= &tvp5150_internal_ops
;
1736 sd
->flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1738 #if defined(CONFIG_MEDIA_CONTROLLER)
1739 core
->pads
[TVP5150_PAD_IF_INPUT
].flags
= MEDIA_PAD_FL_SINK
;
1740 core
->pads
[TVP5150_PAD_IF_INPUT
].sig_type
= PAD_SIGNAL_ANALOG
;
1741 core
->pads
[TVP5150_PAD_VID_OUT
].flags
= MEDIA_PAD_FL_SOURCE
;
1742 core
->pads
[TVP5150_PAD_VID_OUT
].sig_type
= PAD_SIGNAL_DV
;
1744 sd
->entity
.function
= MEDIA_ENT_F_ATV_DECODER
;
1746 res
= media_entity_pads_init(&sd
->entity
, TVP5150_NUM_PADS
, core
->pads
);
1750 sd
->entity
.ops
= &tvp5150_sd_media_ops
;
1753 res
= tvp5150_detect_version(core
);
1757 core
->norm
= V4L2_STD_ALL
; /* Default is autodetect */
1758 core
->detected_norm
= V4L2_STD_UNKNOWN
;
1759 core
->input
= TVP5150_COMPOSITE1
;
1760 core
->enable
= true;
1762 v4l2_ctrl_handler_init(&core
->hdl
, 5);
1763 v4l2_ctrl_new_std(&core
->hdl
, &tvp5150_ctrl_ops
,
1764 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 128);
1765 v4l2_ctrl_new_std(&core
->hdl
, &tvp5150_ctrl_ops
,
1766 V4L2_CID_CONTRAST
, 0, 255, 1, 128);
1767 v4l2_ctrl_new_std(&core
->hdl
, &tvp5150_ctrl_ops
,
1768 V4L2_CID_SATURATION
, 0, 255, 1, 128);
1769 v4l2_ctrl_new_std(&core
->hdl
, &tvp5150_ctrl_ops
,
1770 V4L2_CID_HUE
, -128, 127, 1, 0);
1771 v4l2_ctrl_new_std(&core
->hdl
, &tvp5150_ctrl_ops
,
1772 V4L2_CID_PIXEL_RATE
, 27000000,
1773 27000000, 1, 27000000);
1774 v4l2_ctrl_new_std_menu_items(&core
->hdl
, &tvp5150_ctrl_ops
,
1775 V4L2_CID_TEST_PATTERN
,
1776 ARRAY_SIZE(tvp5150_test_patterns
) - 1,
1777 0, 0, tvp5150_test_patterns
);
1778 sd
->ctrl_handler
= &core
->hdl
;
1779 if (core
->hdl
.error
) {
1780 res
= core
->hdl
.error
;
1784 tvp5150_set_default(tvp5150_read_std(sd
), &core
->rect
);
1787 tvp5150_reset(sd
, 0); /* Calls v4l2_ctrl_handler_setup() */
1789 res
= devm_request_threaded_irq(&c
->dev
, c
->irq
, NULL
,
1790 tvp5150_isr
, IRQF_TRIGGER_HIGH
|
1791 IRQF_ONESHOT
, "tvp5150", core
);
1796 res
= v4l2_async_register_subdev(sd
);
1801 tvp5150_log_status(sd
);
1805 v4l2_ctrl_handler_free(&core
->hdl
);
1809 static int tvp5150_remove(struct i2c_client
*c
)
1811 struct v4l2_subdev
*sd
= i2c_get_clientdata(c
);
1812 struct tvp5150
*decoder
= to_tvp5150(sd
);
1814 dev_dbg_lvl(sd
->dev
, 1, debug
,
1815 "tvp5150.c: removing tvp5150 adapter on address 0x%x\n",
1818 v4l2_async_unregister_subdev(sd
);
1819 v4l2_ctrl_handler_free(&decoder
->hdl
);
1823 /* ----------------------------------------------------------------------- */
1825 static const struct i2c_device_id tvp5150_id
[] = {
1829 MODULE_DEVICE_TABLE(i2c
, tvp5150_id
);
1831 #if IS_ENABLED(CONFIG_OF)
1832 static const struct of_device_id tvp5150_of_match
[] = {
1833 { .compatible
= "ti,tvp5150", },
1836 MODULE_DEVICE_TABLE(of
, tvp5150_of_match
);
1839 static struct i2c_driver tvp5150_driver
= {
1841 .of_match_table
= of_match_ptr(tvp5150_of_match
),
1844 .probe
= tvp5150_probe
,
1845 .remove
= tvp5150_remove
,
1846 .id_table
= tvp5150_id
,
1849 module_i2c_driver(tvp5150_driver
);