1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // tvp5150 - Texas Instruments TVP5150A/AM1 and TVP5151 video decoder driver 4 // 5 // Copyright (c) 2005,2006 Mauro Carvalho Chehab <mchehab@kernel.org> 6 7 #include <dt-bindings/media/tvp5150.h> 8 #include <linux/i2c.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> 22 #include <media/v4l2-rect.h> 23 24 #include "tvp5150_reg.h" 25 26 #define TVP5150_H_MAX 720U 27 #define TVP5150_V_MAX_525_60 480U 28 #define TVP5150_V_MAX_OTHERS 576U 29 #define TVP5150_MAX_CROP_LEFT 511 30 #define TVP5150_MAX_CROP_TOP 127 31 #define TVP5150_CROP_SHIFT 2 32 #define TVP5150_MBUS_FMT MEDIA_BUS_FMT_UYVY8_2X8 33 #define TVP5150_FIELD V4L2_FIELD_ALTERNATE 34 #define TVP5150_COLORSPACE V4L2_COLORSPACE_SMPTE170M 35 36 #define TVP5150_MAX_CONNECTORS 3 /* Check dt-bindings for more information */ 37 38 MODULE_DESCRIPTION("Texas Instruments TVP5150A/TVP5150AM1/TVP5151 video decoder driver"); 39 MODULE_AUTHOR("Mauro Carvalho Chehab"); 40 MODULE_LICENSE("GPL v2"); 41 42 43 static int debug; 44 module_param(debug, int, 0644); 45 MODULE_PARM_DESC(debug, "Debug level (0-2)"); 46 47 #define dprintk0(__dev, __arg...) dev_dbg_lvl(__dev, 0, 0, __arg) 48 49 enum tvp5150_pads { 50 TVP5150_PAD_AIP1A, 51 TVP5150_PAD_AIP1B, 52 TVP5150_PAD_VID_OUT, 53 TVP5150_NUM_PADS 54 }; 55 56 struct tvp5150_connector { 57 struct v4l2_fwnode_connector base; 58 struct media_entity ent; 59 struct media_pad pad; 60 }; 61 62 struct tvp5150 { 63 struct v4l2_subdev sd; 64 65 struct media_pad pads[TVP5150_NUM_PADS]; 66 struct tvp5150_connector connectors[TVP5150_MAX_CONNECTORS]; 67 unsigned int connectors_num; 68 69 struct v4l2_ctrl_handler hdl; 70 struct v4l2_rect rect; 71 struct regmap *regmap; 72 int irq; 73 74 v4l2_std_id norm; /* Current set standard */ 75 v4l2_std_id detected_norm; 76 u32 input; 77 u32 output; 78 u32 oe; 79 int enable; 80 bool lock; 81 82 u16 dev_id; 83 u16 rom_ver; 84 85 enum v4l2_mbus_type mbus_type; 86 }; 87 88 static inline struct tvp5150 *to_tvp5150(struct v4l2_subdev *sd) 89 { 90 return container_of(sd, struct tvp5150, sd); 91 } 92 93 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl) 94 { 95 return &container_of(ctrl->handler, struct tvp5150, hdl)->sd; 96 } 97 98 static int tvp5150_read(struct v4l2_subdev *sd, unsigned char addr) 99 { 100 struct tvp5150 *decoder = to_tvp5150(sd); 101 int ret, val; 102 103 ret = regmap_read(decoder->regmap, addr, &val); 104 if (ret < 0) 105 return ret; 106 107 return val; 108 } 109 110 static void dump_reg_range(struct v4l2_subdev *sd, char *s, u8 init, 111 const u8 end, int max_line) 112 { 113 u8 buf[16]; 114 int i = 0, j, len; 115 116 if (max_line > 16) { 117 dprintk0(sd->dev, "too much data to dump\n"); 118 return; 119 } 120 121 for (i = init; i < end; i += max_line) { 122 len = (end - i > max_line) ? max_line : end - i; 123 124 for (j = 0; j < len; j++) 125 buf[j] = tvp5150_read(sd, i + j); 126 127 dprintk0(sd->dev, "%s reg %02x = %*ph\n", s, i, len, buf); 128 } 129 } 130 131 static int tvp5150_log_status(struct v4l2_subdev *sd) 132 { 133 dprintk0(sd->dev, "tvp5150: Video input source selection #1 = 0x%02x\n", 134 tvp5150_read(sd, TVP5150_VD_IN_SRC_SEL_1)); 135 dprintk0(sd->dev, "tvp5150: Analog channel controls = 0x%02x\n", 136 tvp5150_read(sd, TVP5150_ANAL_CHL_CTL)); 137 dprintk0(sd->dev, "tvp5150: Operation mode controls = 0x%02x\n", 138 tvp5150_read(sd, TVP5150_OP_MODE_CTL)); 139 dprintk0(sd->dev, "tvp5150: Miscellaneous controls = 0x%02x\n", 140 tvp5150_read(sd, TVP5150_MISC_CTL)); 141 dprintk0(sd->dev, "tvp5150: Autoswitch mask= 0x%02x\n", 142 tvp5150_read(sd, TVP5150_AUTOSW_MSK)); 143 dprintk0(sd->dev, "tvp5150: Color killer threshold control = 0x%02x\n", 144 tvp5150_read(sd, TVP5150_COLOR_KIL_THSH_CTL)); 145 dprintk0(sd->dev, "tvp5150: Luminance processing controls #1 #2 and #3 = %02x %02x %02x\n", 146 tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_1), 147 tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_2), 148 tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_3)); 149 dprintk0(sd->dev, "tvp5150: Brightness control = 0x%02x\n", 150 tvp5150_read(sd, TVP5150_BRIGHT_CTL)); 151 dprintk0(sd->dev, "tvp5150: Color saturation control = 0x%02x\n", 152 tvp5150_read(sd, TVP5150_SATURATION_CTL)); 153 dprintk0(sd->dev, "tvp5150: Hue control = 0x%02x\n", 154 tvp5150_read(sd, TVP5150_HUE_CTL)); 155 dprintk0(sd->dev, "tvp5150: Contrast control = 0x%02x\n", 156 tvp5150_read(sd, TVP5150_CONTRAST_CTL)); 157 dprintk0(sd->dev, "tvp5150: Outputs and data rates select = 0x%02x\n", 158 tvp5150_read(sd, TVP5150_DATA_RATE_SEL)); 159 dprintk0(sd->dev, "tvp5150: Configuration shared pins = 0x%02x\n", 160 tvp5150_read(sd, TVP5150_CONF_SHARED_PIN)); 161 dprintk0(sd->dev, "tvp5150: Active video cropping start = 0x%02x%02x\n", 162 tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_MSB), 163 tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_LSB)); 164 dprintk0(sd->dev, "tvp5150: Active video cropping stop = 0x%02x%02x\n", 165 tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_MSB), 166 tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_LSB)); 167 dprintk0(sd->dev, "tvp5150: Genlock/RTC = 0x%02x\n", 168 tvp5150_read(sd, TVP5150_GENLOCK)); 169 dprintk0(sd->dev, "tvp5150: Horizontal sync start = 0x%02x\n", 170 tvp5150_read(sd, TVP5150_HORIZ_SYNC_START)); 171 dprintk0(sd->dev, "tvp5150: Vertical blanking start = 0x%02x\n", 172 tvp5150_read(sd, TVP5150_VERT_BLANKING_START)); 173 dprintk0(sd->dev, "tvp5150: Vertical blanking stop = 0x%02x\n", 174 tvp5150_read(sd, TVP5150_VERT_BLANKING_STOP)); 175 dprintk0(sd->dev, "tvp5150: Chrominance processing control #1 and #2 = %02x %02x\n", 176 tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_1), 177 tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_2)); 178 dprintk0(sd->dev, "tvp5150: Interrupt reset register B = 0x%02x\n", 179 tvp5150_read(sd, TVP5150_INT_RESET_REG_B)); 180 dprintk0(sd->dev, "tvp5150: Interrupt enable register B = 0x%02x\n", 181 tvp5150_read(sd, TVP5150_INT_ENABLE_REG_B)); 182 dprintk0(sd->dev, "tvp5150: Interrupt configuration register B = 0x%02x\n", 183 tvp5150_read(sd, TVP5150_INTT_CONFIG_REG_B)); 184 dprintk0(sd->dev, "tvp5150: Video standard = 0x%02x\n", 185 tvp5150_read(sd, TVP5150_VIDEO_STD)); 186 dprintk0(sd->dev, "tvp5150: Chroma gain factor: Cb=0x%02x Cr=0x%02x\n", 187 tvp5150_read(sd, TVP5150_CB_GAIN_FACT), 188 tvp5150_read(sd, TVP5150_CR_GAIN_FACTOR)); 189 dprintk0(sd->dev, "tvp5150: Macrovision on counter = 0x%02x\n", 190 tvp5150_read(sd, TVP5150_MACROVISION_ON_CTR)); 191 dprintk0(sd->dev, "tvp5150: Macrovision off counter = 0x%02x\n", 192 tvp5150_read(sd, TVP5150_MACROVISION_OFF_CTR)); 193 dprintk0(sd->dev, "tvp5150: ITU-R BT.656.%d timing(TVP5150AM1 only)\n", 194 (tvp5150_read(sd, TVP5150_REV_SELECT) & 1) ? 3 : 4); 195 dprintk0(sd->dev, "tvp5150: Device ID = %02x%02x\n", 196 tvp5150_read(sd, TVP5150_MSB_DEV_ID), 197 tvp5150_read(sd, TVP5150_LSB_DEV_ID)); 198 dprintk0(sd->dev, "tvp5150: ROM version = (hex) %02x.%02x\n", 199 tvp5150_read(sd, TVP5150_ROM_MAJOR_VER), 200 tvp5150_read(sd, TVP5150_ROM_MINOR_VER)); 201 dprintk0(sd->dev, "tvp5150: Vertical line count = 0x%02x%02x\n", 202 tvp5150_read(sd, TVP5150_VERT_LN_COUNT_MSB), 203 tvp5150_read(sd, TVP5150_VERT_LN_COUNT_LSB)); 204 dprintk0(sd->dev, "tvp5150: Interrupt status register B = 0x%02x\n", 205 tvp5150_read(sd, TVP5150_INT_STATUS_REG_B)); 206 dprintk0(sd->dev, "tvp5150: Interrupt active register B = 0x%02x\n", 207 tvp5150_read(sd, TVP5150_INT_ACTIVE_REG_B)); 208 dprintk0(sd->dev, "tvp5150: Status regs #1 to #5 = %02x %02x %02x %02x %02x\n", 209 tvp5150_read(sd, TVP5150_STATUS_REG_1), 210 tvp5150_read(sd, TVP5150_STATUS_REG_2), 211 tvp5150_read(sd, TVP5150_STATUS_REG_3), 212 tvp5150_read(sd, TVP5150_STATUS_REG_4), 213 tvp5150_read(sd, TVP5150_STATUS_REG_5)); 214 215 dump_reg_range(sd, "Teletext filter 1", TVP5150_TELETEXT_FIL1_INI, 216 TVP5150_TELETEXT_FIL1_END, 8); 217 dump_reg_range(sd, "Teletext filter 2", TVP5150_TELETEXT_FIL2_INI, 218 TVP5150_TELETEXT_FIL2_END, 8); 219 220 dprintk0(sd->dev, "tvp5150: Teletext filter enable = 0x%02x\n", 221 tvp5150_read(sd, TVP5150_TELETEXT_FIL_ENA)); 222 dprintk0(sd->dev, "tvp5150: Interrupt status register A = 0x%02x\n", 223 tvp5150_read(sd, TVP5150_INT_STATUS_REG_A)); 224 dprintk0(sd->dev, "tvp5150: Interrupt enable register A = 0x%02x\n", 225 tvp5150_read(sd, TVP5150_INT_ENABLE_REG_A)); 226 dprintk0(sd->dev, "tvp5150: Interrupt configuration = 0x%02x\n", 227 tvp5150_read(sd, TVP5150_INT_CONF)); 228 dprintk0(sd->dev, "tvp5150: VDP status register = 0x%02x\n", 229 tvp5150_read(sd, TVP5150_VDP_STATUS_REG)); 230 dprintk0(sd->dev, "tvp5150: FIFO word count = 0x%02x\n", 231 tvp5150_read(sd, TVP5150_FIFO_WORD_COUNT)); 232 dprintk0(sd->dev, "tvp5150: FIFO interrupt threshold = 0x%02x\n", 233 tvp5150_read(sd, TVP5150_FIFO_INT_THRESHOLD)); 234 dprintk0(sd->dev, "tvp5150: FIFO reset = 0x%02x\n", 235 tvp5150_read(sd, TVP5150_FIFO_RESET)); 236 dprintk0(sd->dev, "tvp5150: Line number interrupt = 0x%02x\n", 237 tvp5150_read(sd, TVP5150_LINE_NUMBER_INT)); 238 dprintk0(sd->dev, "tvp5150: Pixel alignment register = 0x%02x%02x\n", 239 tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_HIGH), 240 tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_LOW)); 241 dprintk0(sd->dev, "tvp5150: FIFO output control = 0x%02x\n", 242 tvp5150_read(sd, TVP5150_FIFO_OUT_CTRL)); 243 dprintk0(sd->dev, "tvp5150: Full field enable = 0x%02x\n", 244 tvp5150_read(sd, TVP5150_FULL_FIELD_ENA)); 245 dprintk0(sd->dev, "tvp5150: Full field mode register = 0x%02x\n", 246 tvp5150_read(sd, TVP5150_FULL_FIELD_MODE_REG)); 247 248 dump_reg_range(sd, "CC data", TVP5150_CC_DATA_INI, 249 TVP5150_CC_DATA_END, 8); 250 251 dump_reg_range(sd, "WSS data", TVP5150_WSS_DATA_INI, 252 TVP5150_WSS_DATA_END, 8); 253 254 dump_reg_range(sd, "VPS data", TVP5150_VPS_DATA_INI, 255 TVP5150_VPS_DATA_END, 8); 256 257 dump_reg_range(sd, "VITC data", TVP5150_VITC_DATA_INI, 258 TVP5150_VITC_DATA_END, 10); 259 260 dump_reg_range(sd, "Line mode", TVP5150_LINE_MODE_INI, 261 TVP5150_LINE_MODE_END, 8); 262 return 0; 263 } 264 265 /**************************************************************************** 266 Basic functions 267 ****************************************************************************/ 268 269 static void tvp5150_selmux(struct v4l2_subdev *sd) 270 { 271 int opmode = 0; 272 struct tvp5150 *decoder = to_tvp5150(sd); 273 unsigned int mask, val; 274 int input = 0; 275 276 /* Only tvp5150am1 and tvp5151 have signal generator support */ 277 if ((decoder->dev_id == 0x5150 && decoder->rom_ver == 0x0400) || 278 (decoder->dev_id == 0x5151 && decoder->rom_ver == 0x0100)) { 279 if (!decoder->enable) 280 input = 8; 281 } 282 283 switch (decoder->input) { 284 case TVP5150_COMPOSITE1: 285 input |= 2; 286 /* fall through */ 287 case TVP5150_COMPOSITE0: 288 break; 289 case TVP5150_SVIDEO: 290 default: 291 input |= 1; 292 break; 293 } 294 295 dev_dbg_lvl(sd->dev, 1, debug, "Selecting video route: route input=%i, output=%i => tvp5150 input=%i, opmode=%i\n", 296 decoder->input, decoder->output, 297 input, opmode); 298 299 regmap_write(decoder->regmap, TVP5150_OP_MODE_CTL, opmode); 300 regmap_write(decoder->regmap, TVP5150_VD_IN_SRC_SEL_1, input); 301 302 /* 303 * Setup the FID/GLCO/VLK/HVLK and INTREQ/GPCL/VBLK output signals. For 304 * S-Video we output the vertical lock (VLK) signal on FID/GLCO/VLK/HVLK 305 * and set INTREQ/GPCL/VBLK to logic 0. For composite we output the 306 * field indicator (FID) signal on FID/GLCO/VLK/HVLK and set 307 * INTREQ/GPCL/VBLK to logic 1. 308 */ 309 mask = TVP5150_MISC_CTL_GPCL | TVP5150_MISC_CTL_HVLK; 310 if (decoder->input == TVP5150_SVIDEO) 311 val = TVP5150_MISC_CTL_HVLK; 312 else 313 val = TVP5150_MISC_CTL_GPCL; 314 regmap_update_bits(decoder->regmap, TVP5150_MISC_CTL, mask, val); 315 }; 316 317 struct i2c_reg_value { 318 unsigned char reg; 319 unsigned char value; 320 }; 321 322 /* Default values as sugested at TVP5150AM1 datasheet */ 323 static const struct i2c_reg_value tvp5150_init_default[] = { 324 { /* 0x00 */ 325 TVP5150_VD_IN_SRC_SEL_1, 0x00 326 }, 327 { /* 0x01 */ 328 TVP5150_ANAL_CHL_CTL, 0x15 329 }, 330 { /* 0x02 */ 331 TVP5150_OP_MODE_CTL, 0x00 332 }, 333 { /* 0x03 */ 334 TVP5150_MISC_CTL, 0x01 335 }, 336 { /* 0x06 */ 337 TVP5150_COLOR_KIL_THSH_CTL, 0x10 338 }, 339 { /* 0x07 */ 340 TVP5150_LUMA_PROC_CTL_1, 0x60 341 }, 342 { /* 0x08 */ 343 TVP5150_LUMA_PROC_CTL_2, 0x00 344 }, 345 { /* 0x09 */ 346 TVP5150_BRIGHT_CTL, 0x80 347 }, 348 { /* 0x0a */ 349 TVP5150_SATURATION_CTL, 0x80 350 }, 351 { /* 0x0b */ 352 TVP5150_HUE_CTL, 0x00 353 }, 354 { /* 0x0c */ 355 TVP5150_CONTRAST_CTL, 0x80 356 }, 357 { /* 0x0d */ 358 TVP5150_DATA_RATE_SEL, 0x47 359 }, 360 { /* 0x0e */ 361 TVP5150_LUMA_PROC_CTL_3, 0x00 362 }, 363 { /* 0x0f */ 364 TVP5150_CONF_SHARED_PIN, 0x08 365 }, 366 { /* 0x11 */ 367 TVP5150_ACT_VD_CROP_ST_MSB, 0x00 368 }, 369 { /* 0x12 */ 370 TVP5150_ACT_VD_CROP_ST_LSB, 0x00 371 }, 372 { /* 0x13 */ 373 TVP5150_ACT_VD_CROP_STP_MSB, 0x00 374 }, 375 { /* 0x14 */ 376 TVP5150_ACT_VD_CROP_STP_LSB, 0x00 377 }, 378 { /* 0x15 */ 379 TVP5150_GENLOCK, 0x01 380 }, 381 { /* 0x16 */ 382 TVP5150_HORIZ_SYNC_START, 0x80 383 }, 384 { /* 0x18 */ 385 TVP5150_VERT_BLANKING_START, 0x00 386 }, 387 { /* 0x19 */ 388 TVP5150_VERT_BLANKING_STOP, 0x00 389 }, 390 { /* 0x1a */ 391 TVP5150_CHROMA_PROC_CTL_1, 0x0c 392 }, 393 { /* 0x1b */ 394 TVP5150_CHROMA_PROC_CTL_2, 0x14 395 }, 396 { /* 0x1c */ 397 TVP5150_INT_RESET_REG_B, 0x00 398 }, 399 { /* 0x1d */ 400 TVP5150_INT_ENABLE_REG_B, 0x00 401 }, 402 { /* 0x1e */ 403 TVP5150_INTT_CONFIG_REG_B, 0x00 404 }, 405 { /* 0x28 */ 406 TVP5150_VIDEO_STD, 0x00 407 }, 408 { /* 0x2e */ 409 TVP5150_MACROVISION_ON_CTR, 0x0f 410 }, 411 { /* 0x2f */ 412 TVP5150_MACROVISION_OFF_CTR, 0x01 413 }, 414 { /* 0xbb */ 415 TVP5150_TELETEXT_FIL_ENA, 0x00 416 }, 417 { /* 0xc0 */ 418 TVP5150_INT_STATUS_REG_A, 0x00 419 }, 420 { /* 0xc1 */ 421 TVP5150_INT_ENABLE_REG_A, 0x00 422 }, 423 { /* 0xc2 */ 424 TVP5150_INT_CONF, 0x04 425 }, 426 { /* 0xc8 */ 427 TVP5150_FIFO_INT_THRESHOLD, 0x80 428 }, 429 { /* 0xc9 */ 430 TVP5150_FIFO_RESET, 0x00 431 }, 432 { /* 0xca */ 433 TVP5150_LINE_NUMBER_INT, 0x00 434 }, 435 { /* 0xcb */ 436 TVP5150_PIX_ALIGN_REG_LOW, 0x4e 437 }, 438 { /* 0xcc */ 439 TVP5150_PIX_ALIGN_REG_HIGH, 0x00 440 }, 441 { /* 0xcd */ 442 TVP5150_FIFO_OUT_CTRL, 0x01 443 }, 444 { /* 0xcf */ 445 TVP5150_FULL_FIELD_ENA, 0x00 446 }, 447 { /* 0xd0 */ 448 TVP5150_LINE_MODE_INI, 0x00 449 }, 450 { /* 0xfc */ 451 TVP5150_FULL_FIELD_MODE_REG, 0x7f 452 }, 453 { /* end of data */ 454 0xff, 0xff 455 } 456 }; 457 458 /* Default values as sugested at TVP5150AM1 datasheet */ 459 static const struct i2c_reg_value tvp5150_init_enable[] = { 460 { /* Automatic offset and AGC enabled */ 461 TVP5150_ANAL_CHL_CTL, 0x15 462 }, { /* Activate YCrCb output 0x9 or 0xd ? */ 463 TVP5150_MISC_CTL, TVP5150_MISC_CTL_GPCL | 464 TVP5150_MISC_CTL_INTREQ_OE | 465 TVP5150_MISC_CTL_YCBCR_OE | 466 TVP5150_MISC_CTL_SYNC_OE | 467 TVP5150_MISC_CTL_VBLANK | 468 TVP5150_MISC_CTL_CLOCK_OE, 469 }, { /* Activates video std autodetection for all standards */ 470 TVP5150_AUTOSW_MSK, 0x0 471 }, { /* Default format: 0x47. For 4:2:2: 0x40 */ 472 TVP5150_DATA_RATE_SEL, 0x47 473 }, { 474 TVP5150_CHROMA_PROC_CTL_1, 0x0c 475 }, { 476 TVP5150_CHROMA_PROC_CTL_2, 0x54 477 }, { /* Non documented, but initialized on WinTV USB2 */ 478 0x27, 0x20 479 }, { 480 0xff, 0xff 481 } 482 }; 483 484 struct tvp5150_vbi_type { 485 unsigned int vbi_type; 486 unsigned int ini_line; 487 unsigned int end_line; 488 unsigned int by_field :1; 489 }; 490 491 struct i2c_vbi_ram_value { 492 u16 reg; 493 struct tvp5150_vbi_type type; 494 unsigned char values[16]; 495 }; 496 497 /* This struct have the values for each supported VBI Standard 498 * by 499 tvp5150_vbi_types should follow the same order as vbi_ram_default 500 * value 0 means rom position 0x10, value 1 means rom position 0x30 501 * and so on. There are 16 possible locations from 0 to 15. 502 */ 503 504 static struct i2c_vbi_ram_value vbi_ram_default[] = { 505 506 /* 507 * FIXME: Current api doesn't handle all VBI types, those not 508 * yet supported are placed under #if 0 509 */ 510 #if 0 511 [0] = {0x010, /* Teletext, SECAM, WST System A */ 512 {V4L2_SLICED_TELETEXT_SECAM, 6, 23, 1}, 513 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x26, 514 0xe6, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00 } 515 }, 516 #endif 517 [1] = {0x030, /* Teletext, PAL, WST System B */ 518 {V4L2_SLICED_TELETEXT_B, 6, 22, 1}, 519 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x2b, 520 0xa6, 0x72, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00 } 521 }, 522 #if 0 523 [2] = {0x050, /* Teletext, PAL, WST System C */ 524 {V4L2_SLICED_TELETEXT_PAL_C, 6, 22, 1}, 525 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22, 526 0xa6, 0x98, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 } 527 }, 528 [3] = {0x070, /* Teletext, NTSC, WST System B */ 529 {V4L2_SLICED_TELETEXT_NTSC_B, 10, 21, 1}, 530 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x23, 531 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 } 532 }, 533 [4] = {0x090, /* Tetetext, NTSC NABTS System C */ 534 {V4L2_SLICED_TELETEXT_NTSC_C, 10, 21, 1}, 535 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22, 536 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x15, 0x00 } 537 }, 538 [5] = {0x0b0, /* Teletext, NTSC-J, NABTS System D */ 539 {V4L2_SLICED_TELETEXT_NTSC_D, 10, 21, 1}, 540 { 0xaa, 0xaa, 0xff, 0xff, 0xa7, 0x2e, 0x20, 0x23, 541 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 } 542 }, 543 [6] = {0x0d0, /* Closed Caption, PAL/SECAM */ 544 {V4L2_SLICED_CAPTION_625, 22, 22, 1}, 545 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02, 546 0xa6, 0x7b, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 } 547 }, 548 #endif 549 [7] = {0x0f0, /* Closed Caption, NTSC */ 550 {V4L2_SLICED_CAPTION_525, 21, 21, 1}, 551 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02, 552 0x69, 0x8c, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 } 553 }, 554 [8] = {0x110, /* Wide Screen Signal, PAL/SECAM */ 555 {V4L2_SLICED_WSS_625, 23, 23, 1}, 556 { 0x5b, 0x55, 0xc5, 0xff, 0x00, 0x71, 0x6e, 0x42, 557 0xa6, 0xcd, 0x0f, 0x00, 0x00, 0x00, 0x3a, 0x00 } 558 }, 559 #if 0 560 [9] = {0x130, /* Wide Screen Signal, NTSC C */ 561 {V4L2_SLICED_WSS_525, 20, 20, 1}, 562 { 0x38, 0x00, 0x3f, 0x00, 0x00, 0x71, 0x6e, 0x43, 563 0x69, 0x7c, 0x08, 0x00, 0x00, 0x00, 0x39, 0x00 } 564 }, 565 [10] = {0x150, /* Vertical Interval Timecode (VITC), PAL/SECAM */ 566 {V4l2_SLICED_VITC_625, 6, 22, 0}, 567 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49, 568 0xa6, 0x85, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 } 569 }, 570 [11] = {0x170, /* Vertical Interval Timecode (VITC), NTSC */ 571 {V4l2_SLICED_VITC_525, 10, 20, 0}, 572 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49, 573 0x69, 0x94, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 } 574 }, 575 #endif 576 [12] = {0x190, /* Video Program System (VPS), PAL */ 577 {V4L2_SLICED_VPS, 16, 16, 0}, 578 { 0xaa, 0xaa, 0xff, 0xff, 0xba, 0xce, 0x2b, 0x0d, 579 0xa6, 0xda, 0x0b, 0x00, 0x00, 0x00, 0x60, 0x00 } 580 }, 581 /* 0x1d0 User programmable */ 582 }; 583 584 static int tvp5150_write_inittab(struct v4l2_subdev *sd, 585 const struct i2c_reg_value *regs) 586 { 587 struct tvp5150 *decoder = to_tvp5150(sd); 588 589 while (regs->reg != 0xff) { 590 regmap_write(decoder->regmap, regs->reg, regs->value); 591 regs++; 592 } 593 return 0; 594 } 595 596 static int tvp5150_vdp_init(struct v4l2_subdev *sd) 597 { 598 struct tvp5150 *decoder = to_tvp5150(sd); 599 struct regmap *map = decoder->regmap; 600 unsigned int i; 601 int j; 602 603 /* Disable Full Field */ 604 regmap_write(map, TVP5150_FULL_FIELD_ENA, 0); 605 606 /* Before programming, Line mode should be at 0xff */ 607 for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++) 608 regmap_write(map, i, 0xff); 609 610 /* Load Ram Table */ 611 for (j = 0; j < ARRAY_SIZE(vbi_ram_default); j++) { 612 const struct i2c_vbi_ram_value *regs = &vbi_ram_default[j]; 613 614 if (!regs->type.vbi_type) 615 continue; 616 617 regmap_write(map, TVP5150_CONF_RAM_ADDR_HIGH, regs->reg >> 8); 618 regmap_write(map, TVP5150_CONF_RAM_ADDR_LOW, regs->reg); 619 620 for (i = 0; i < 16; i++) 621 regmap_write(map, TVP5150_VDP_CONF_RAM_DATA, 622 regs->values[i]); 623 } 624 return 0; 625 } 626 627 /* Fills VBI capabilities based on i2c_vbi_ram_value struct */ 628 static int tvp5150_g_sliced_vbi_cap(struct v4l2_subdev *sd, 629 struct v4l2_sliced_vbi_cap *cap) 630 { 631 int line, i; 632 633 dev_dbg_lvl(sd->dev, 1, debug, "g_sliced_vbi_cap\n"); 634 memset(cap, 0, sizeof(*cap)); 635 636 for (i = 0; i < ARRAY_SIZE(vbi_ram_default); i++) { 637 const struct i2c_vbi_ram_value *regs = &vbi_ram_default[i]; 638 639 if (!regs->type.vbi_type) 640 continue; 641 642 for (line = regs->type.ini_line; 643 line <= regs->type.end_line; 644 line++) { 645 cap->service_lines[0][line] |= regs->type.vbi_type; 646 } 647 cap->service_set |= regs->type.vbi_type; 648 } 649 return 0; 650 } 651 652 /* Set vbi processing 653 * type - one of tvp5150_vbi_types 654 * line - line to gather data 655 * fields: bit 0 field1, bit 1, field2 656 * flags (default=0xf0) is a bitmask, were set means: 657 * bit 7: enable filtering null bytes on CC 658 * bit 6: send data also to FIFO 659 * bit 5: don't allow data with errors on FIFO 660 * bit 4: enable ECC when possible 661 * pix_align = pix alignment: 662 * LSB = field1 663 * MSB = field2 664 */ 665 static int tvp5150_set_vbi(struct v4l2_subdev *sd, 666 unsigned int type, u8 flags, int line, 667 const int fields) 668 { 669 struct tvp5150 *decoder = to_tvp5150(sd); 670 v4l2_std_id std = decoder->norm; 671 u8 reg; 672 int i, pos = 0; 673 674 if (std == V4L2_STD_ALL) { 675 dev_err(sd->dev, "VBI can't be configured without knowing number of lines\n"); 676 return 0; 677 } else if (std & V4L2_STD_625_50) { 678 /* Don't follow NTSC Line number convension */ 679 line += 3; 680 } 681 682 if (line < 6 || line > 27) 683 return 0; 684 685 for (i = 0; i < ARRAY_SIZE(vbi_ram_default); i++) { 686 const struct i2c_vbi_ram_value *regs = &vbi_ram_default[i]; 687 688 if (!regs->type.vbi_type) 689 continue; 690 691 if ((type & regs->type.vbi_type) && 692 (line >= regs->type.ini_line) && 693 (line <= regs->type.end_line)) 694 break; 695 pos++; 696 } 697 698 type = pos | (flags & 0xf0); 699 reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI; 700 701 if (fields & 1) 702 regmap_write(decoder->regmap, reg, type); 703 704 if (fields & 2) 705 regmap_write(decoder->regmap, reg + 1, type); 706 707 return type; 708 } 709 710 static int tvp5150_get_vbi(struct v4l2_subdev *sd, int line) 711 { 712 struct tvp5150 *decoder = to_tvp5150(sd); 713 v4l2_std_id std = decoder->norm; 714 u8 reg; 715 int pos, type = 0; 716 int i, ret = 0; 717 718 if (std == V4L2_STD_ALL) { 719 dev_err(sd->dev, "VBI can't be configured without knowing number of lines\n"); 720 return 0; 721 } else if (std & V4L2_STD_625_50) { 722 /* Don't follow NTSC Line number convension */ 723 line += 3; 724 } 725 726 if (line < 6 || line > 27) 727 return 0; 728 729 reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI; 730 731 for (i = 0; i <= 1; i++) { 732 ret = tvp5150_read(sd, reg + i); 733 if (ret < 0) { 734 dev_err(sd->dev, "%s: failed with error = %d\n", 735 __func__, ret); 736 return 0; 737 } 738 pos = ret & 0x0f; 739 if (pos < ARRAY_SIZE(vbi_ram_default)) 740 type |= vbi_ram_default[pos].type.vbi_type; 741 } 742 743 return type; 744 } 745 746 static int tvp5150_set_std(struct v4l2_subdev *sd, v4l2_std_id std) 747 { 748 struct tvp5150 *decoder = to_tvp5150(sd); 749 int fmt = 0; 750 751 /* First tests should be against specific std */ 752 753 if (std == V4L2_STD_NTSC_443) { 754 fmt = VIDEO_STD_NTSC_4_43_BIT; 755 } else if (std == V4L2_STD_PAL_M) { 756 fmt = VIDEO_STD_PAL_M_BIT; 757 } else if (std == V4L2_STD_PAL_N || std == V4L2_STD_PAL_Nc) { 758 fmt = VIDEO_STD_PAL_COMBINATION_N_BIT; 759 } else { 760 /* Then, test against generic ones */ 761 if (std & V4L2_STD_NTSC) 762 fmt = VIDEO_STD_NTSC_MJ_BIT; 763 else if (std & V4L2_STD_PAL) 764 fmt = VIDEO_STD_PAL_BDGHIN_BIT; 765 else if (std & V4L2_STD_SECAM) 766 fmt = VIDEO_STD_SECAM_BIT; 767 } 768 769 dev_dbg_lvl(sd->dev, 1, debug, "Set video std register to %d.\n", fmt); 770 regmap_write(decoder->regmap, TVP5150_VIDEO_STD, fmt); 771 return 0; 772 } 773 774 static int tvp5150_g_std(struct v4l2_subdev *sd, v4l2_std_id *std) 775 { 776 struct tvp5150 *decoder = to_tvp5150(sd); 777 778 *std = decoder->norm; 779 780 return 0; 781 } 782 783 static int tvp5150_s_std(struct v4l2_subdev *sd, v4l2_std_id std) 784 { 785 struct tvp5150 *decoder = to_tvp5150(sd); 786 787 if (decoder->norm == std) 788 return 0; 789 790 /* Change cropping height limits */ 791 if (std & V4L2_STD_525_60) 792 decoder->rect.height = TVP5150_V_MAX_525_60; 793 else 794 decoder->rect.height = TVP5150_V_MAX_OTHERS; 795 796 decoder->norm = std; 797 798 return tvp5150_set_std(sd, std); 799 } 800 801 static v4l2_std_id tvp5150_read_std(struct v4l2_subdev *sd) 802 { 803 int val = tvp5150_read(sd, TVP5150_STATUS_REG_5); 804 805 switch (val & 0x0F) { 806 case 0x01: 807 return V4L2_STD_NTSC; 808 case 0x03: 809 return V4L2_STD_PAL; 810 case 0x05: 811 return V4L2_STD_PAL_M; 812 case 0x07: 813 return V4L2_STD_PAL_N | V4L2_STD_PAL_Nc; 814 case 0x09: 815 return V4L2_STD_NTSC_443; 816 case 0xb: 817 return V4L2_STD_SECAM; 818 default: 819 return V4L2_STD_UNKNOWN; 820 } 821 } 822 823 static int query_lock(struct v4l2_subdev *sd) 824 { 825 struct tvp5150 *decoder = to_tvp5150(sd); 826 int status; 827 828 if (decoder->irq) 829 return decoder->lock; 830 831 regmap_read(decoder->regmap, TVP5150_STATUS_REG_1, &status); 832 833 /* For standard detection, we need the 3 locks */ 834 return (status & 0x0e) == 0x0e; 835 } 836 837 static int tvp5150_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id) 838 { 839 *std_id = query_lock(sd) ? tvp5150_read_std(sd) : V4L2_STD_UNKNOWN; 840 841 return 0; 842 } 843 844 static const struct v4l2_event tvp5150_ev_fmt = { 845 .type = V4L2_EVENT_SOURCE_CHANGE, 846 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION, 847 }; 848 849 static irqreturn_t tvp5150_isr(int irq, void *dev_id) 850 { 851 struct tvp5150 *decoder = dev_id; 852 struct regmap *map = decoder->regmap; 853 unsigned int mask, active = 0, status = 0; 854 855 mask = TVP5150_MISC_CTL_YCBCR_OE | TVP5150_MISC_CTL_SYNC_OE | 856 TVP5150_MISC_CTL_CLOCK_OE; 857 858 regmap_read(map, TVP5150_INT_STATUS_REG_A, &status); 859 if (status) { 860 regmap_write(map, TVP5150_INT_STATUS_REG_A, status); 861 862 if (status & TVP5150_INT_A_LOCK) { 863 decoder->lock = !!(status & TVP5150_INT_A_LOCK_STATUS); 864 dev_dbg_lvl(decoder->sd.dev, 1, debug, 865 "sync lo%s signal\n", 866 decoder->lock ? "ck" : "ss"); 867 v4l2_subdev_notify_event(&decoder->sd, &tvp5150_ev_fmt); 868 regmap_update_bits(map, TVP5150_MISC_CTL, mask, 869 decoder->lock ? decoder->oe : 0); 870 } 871 872 return IRQ_HANDLED; 873 } 874 875 regmap_read(map, TVP5150_INT_ACTIVE_REG_B, &active); 876 if (active) { 877 status = 0; 878 regmap_read(map, TVP5150_INT_STATUS_REG_B, &status); 879 if (status) 880 regmap_write(map, TVP5150_INT_RESET_REG_B, status); 881 } 882 883 return IRQ_HANDLED; 884 } 885 886 static int tvp5150_reset(struct v4l2_subdev *sd, u32 val) 887 { 888 struct tvp5150 *decoder = to_tvp5150(sd); 889 struct regmap *map = decoder->regmap; 890 891 /* Initializes TVP5150 to its default values */ 892 tvp5150_write_inittab(sd, tvp5150_init_default); 893 894 if (decoder->irq) { 895 /* Configure pins: FID, VSYNC, INTREQ, SCLK */ 896 regmap_write(map, TVP5150_CONF_SHARED_PIN, 0x0); 897 /* Set interrupt polarity to active high */ 898 regmap_write(map, TVP5150_INT_CONF, TVP5150_VDPOE | 0x1); 899 regmap_write(map, TVP5150_INTT_CONFIG_REG_B, 0x1); 900 } else { 901 /* Configure pins: FID, VSYNC, GPCL/VBLK, SCLK */ 902 regmap_write(map, TVP5150_CONF_SHARED_PIN, 0x2); 903 /* Keep interrupt polarity active low */ 904 regmap_write(map, TVP5150_INT_CONF, TVP5150_VDPOE); 905 regmap_write(map, TVP5150_INTT_CONFIG_REG_B, 0x0); 906 } 907 908 /* Initializes VDP registers */ 909 tvp5150_vdp_init(sd); 910 911 /* Selects decoder input */ 912 tvp5150_selmux(sd); 913 914 /* Initialize image preferences */ 915 v4l2_ctrl_handler_setup(&decoder->hdl); 916 917 return 0; 918 } 919 920 static int tvp5150_enable(struct v4l2_subdev *sd) 921 { 922 struct tvp5150 *decoder = to_tvp5150(sd); 923 v4l2_std_id std; 924 925 /* Initializes TVP5150 to stream enabled values */ 926 tvp5150_write_inittab(sd, tvp5150_init_enable); 927 928 if (decoder->norm == V4L2_STD_ALL) 929 std = tvp5150_read_std(sd); 930 else 931 std = decoder->norm; 932 933 /* Disable autoswitch mode */ 934 tvp5150_set_std(sd, std); 935 936 /* 937 * Enable the YCbCr and clock outputs. In discrete sync mode 938 * (non-BT.656) additionally enable the the sync outputs. 939 */ 940 switch (decoder->mbus_type) { 941 case V4L2_MBUS_PARALLEL: 942 /* 8-bit 4:2:2 YUV with discrete sync output */ 943 regmap_update_bits(decoder->regmap, TVP5150_DATA_RATE_SEL, 944 0x7, 0x0); 945 decoder->oe = TVP5150_MISC_CTL_YCBCR_OE | 946 TVP5150_MISC_CTL_CLOCK_OE | 947 TVP5150_MISC_CTL_SYNC_OE; 948 break; 949 case V4L2_MBUS_BT656: 950 decoder->oe = TVP5150_MISC_CTL_YCBCR_OE | 951 TVP5150_MISC_CTL_CLOCK_OE; 952 break; 953 default: 954 return -EINVAL; 955 } 956 957 return 0; 958 }; 959 960 static int tvp5150_s_ctrl(struct v4l2_ctrl *ctrl) 961 { 962 struct v4l2_subdev *sd = to_sd(ctrl); 963 struct tvp5150 *decoder = to_tvp5150(sd); 964 965 switch (ctrl->id) { 966 case V4L2_CID_BRIGHTNESS: 967 regmap_write(decoder->regmap, TVP5150_BRIGHT_CTL, ctrl->val); 968 return 0; 969 case V4L2_CID_CONTRAST: 970 regmap_write(decoder->regmap, TVP5150_CONTRAST_CTL, ctrl->val); 971 return 0; 972 case V4L2_CID_SATURATION: 973 regmap_write(decoder->regmap, TVP5150_SATURATION_CTL, 974 ctrl->val); 975 return 0; 976 case V4L2_CID_HUE: 977 regmap_write(decoder->regmap, TVP5150_HUE_CTL, ctrl->val); 978 return 0; 979 case V4L2_CID_TEST_PATTERN: 980 decoder->enable = ctrl->val ? false : true; 981 tvp5150_selmux(sd); 982 return 0; 983 } 984 return -EINVAL; 985 } 986 987 static void tvp5150_set_default(v4l2_std_id std, struct v4l2_rect *crop) 988 { 989 /* Default is no cropping */ 990 crop->top = 0; 991 crop->left = 0; 992 crop->width = TVP5150_H_MAX; 993 if (std & V4L2_STD_525_60) 994 crop->height = TVP5150_V_MAX_525_60; 995 else 996 crop->height = TVP5150_V_MAX_OTHERS; 997 } 998 999 static struct v4l2_rect * 1000 tvp5150_get_pad_crop(struct tvp5150 *decoder, 1001 struct v4l2_subdev_pad_config *cfg, unsigned int pad, 1002 enum v4l2_subdev_format_whence which) 1003 { 1004 switch (which) { 1005 case V4L2_SUBDEV_FORMAT_ACTIVE: 1006 return &decoder->rect; 1007 case V4L2_SUBDEV_FORMAT_TRY: 1008 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 1009 return v4l2_subdev_get_try_crop(&decoder->sd, cfg, pad); 1010 #else 1011 return ERR_PTR(-EINVAL); 1012 #endif 1013 default: 1014 return ERR_PTR(-EINVAL); 1015 } 1016 } 1017 1018 static int tvp5150_fill_fmt(struct v4l2_subdev *sd, 1019 struct v4l2_subdev_pad_config *cfg, 1020 struct v4l2_subdev_format *format) 1021 { 1022 struct v4l2_mbus_framefmt *f; 1023 struct tvp5150 *decoder = to_tvp5150(sd); 1024 1025 if (!format || (format->pad != TVP5150_PAD_VID_OUT)) 1026 return -EINVAL; 1027 1028 f = &format->format; 1029 1030 f->width = decoder->rect.width; 1031 f->height = decoder->rect.height / 2; 1032 1033 f->code = TVP5150_MBUS_FMT; 1034 f->field = TVP5150_FIELD; 1035 f->colorspace = TVP5150_COLORSPACE; 1036 1037 dev_dbg_lvl(sd->dev, 1, debug, "width = %d, height = %d\n", f->width, 1038 f->height); 1039 return 0; 1040 } 1041 1042 static unsigned int tvp5150_get_hmax(struct v4l2_subdev *sd) 1043 { 1044 struct tvp5150 *decoder = to_tvp5150(sd); 1045 v4l2_std_id std; 1046 1047 /* Calculate height based on current standard */ 1048 if (decoder->norm == V4L2_STD_ALL) 1049 std = tvp5150_read_std(sd); 1050 else 1051 std = decoder->norm; 1052 1053 return (std & V4L2_STD_525_60) ? 1054 TVP5150_V_MAX_525_60 : TVP5150_V_MAX_OTHERS; 1055 } 1056 1057 static void tvp5150_set_hw_selection(struct v4l2_subdev *sd, 1058 struct v4l2_rect *rect) 1059 { 1060 struct tvp5150 *decoder = to_tvp5150(sd); 1061 unsigned int hmax = tvp5150_get_hmax(sd); 1062 1063 regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_START, rect->top); 1064 regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_STOP, 1065 rect->top + rect->height - hmax); 1066 regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_ST_MSB, 1067 rect->left >> TVP5150_CROP_SHIFT); 1068 regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_ST_LSB, 1069 rect->left | (1 << TVP5150_CROP_SHIFT)); 1070 regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_STP_MSB, 1071 (rect->left + rect->width - TVP5150_MAX_CROP_LEFT) >> 1072 TVP5150_CROP_SHIFT); 1073 regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_STP_LSB, 1074 rect->left + rect->width - TVP5150_MAX_CROP_LEFT); 1075 } 1076 1077 static int tvp5150_set_selection(struct v4l2_subdev *sd, 1078 struct v4l2_subdev_pad_config *cfg, 1079 struct v4l2_subdev_selection *sel) 1080 { 1081 struct tvp5150 *decoder = to_tvp5150(sd); 1082 struct v4l2_rect *rect = &sel->r; 1083 struct v4l2_rect *crop; 1084 unsigned int hmax; 1085 1086 if (sel->target != V4L2_SEL_TGT_CROP) 1087 return -EINVAL; 1088 1089 dev_dbg_lvl(sd->dev, 1, debug, "%s left=%d, top=%d, width=%d, height=%d\n", 1090 __func__, rect->left, rect->top, rect->width, rect->height); 1091 1092 /* tvp5150 has some special limits */ 1093 rect->left = clamp(rect->left, 0, TVP5150_MAX_CROP_LEFT); 1094 rect->top = clamp(rect->top, 0, TVP5150_MAX_CROP_TOP); 1095 hmax = tvp5150_get_hmax(sd); 1096 1097 /* 1098 * alignments: 1099 * - width = 2 due to UYVY colorspace 1100 * - height, image = no special alignment 1101 */ 1102 v4l_bound_align_image(&rect->width, 1103 TVP5150_H_MAX - TVP5150_MAX_CROP_LEFT - rect->left, 1104 TVP5150_H_MAX - rect->left, 1, &rect->height, 1105 hmax - TVP5150_MAX_CROP_TOP - rect->top, 1106 hmax - rect->top, 0, 0); 1107 1108 if (!IS_ENABLED(CONFIG_VIDEO_V4L2_SUBDEV_API) && 1109 sel->which == V4L2_SUBDEV_FORMAT_TRY) 1110 return 0; 1111 1112 crop = tvp5150_get_pad_crop(decoder, cfg, sel->pad, sel->which); 1113 if (IS_ERR(crop)) 1114 return PTR_ERR(crop); 1115 1116 /* 1117 * Update output image size if the selection (crop) rectangle size or 1118 * position has been modified. 1119 */ 1120 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE && 1121 !v4l2_rect_equal(rect, crop)) 1122 tvp5150_set_hw_selection(sd, rect); 1123 1124 *crop = *rect; 1125 1126 return 0; 1127 } 1128 1129 static int tvp5150_get_selection(struct v4l2_subdev *sd, 1130 struct v4l2_subdev_pad_config *cfg, 1131 struct v4l2_subdev_selection *sel) 1132 { 1133 struct tvp5150 *decoder = container_of(sd, struct tvp5150, sd); 1134 struct v4l2_rect *crop; 1135 v4l2_std_id std; 1136 1137 switch (sel->target) { 1138 case V4L2_SEL_TGT_CROP_BOUNDS: 1139 sel->r.left = 0; 1140 sel->r.top = 0; 1141 sel->r.width = TVP5150_H_MAX; 1142 1143 /* Calculate height based on current standard */ 1144 if (decoder->norm == V4L2_STD_ALL) 1145 std = tvp5150_read_std(sd); 1146 else 1147 std = decoder->norm; 1148 if (std & V4L2_STD_525_60) 1149 sel->r.height = TVP5150_V_MAX_525_60; 1150 else 1151 sel->r.height = TVP5150_V_MAX_OTHERS; 1152 return 0; 1153 case V4L2_SEL_TGT_CROP: 1154 crop = tvp5150_get_pad_crop(decoder, cfg, sel->pad, 1155 sel->which); 1156 if (IS_ERR(crop)) 1157 return PTR_ERR(crop); 1158 sel->r = *crop; 1159 return 0; 1160 default: 1161 return -EINVAL; 1162 } 1163 } 1164 1165 static int tvp5150_g_mbus_config(struct v4l2_subdev *sd, 1166 struct v4l2_mbus_config *cfg) 1167 { 1168 struct tvp5150 *decoder = to_tvp5150(sd); 1169 1170 cfg->type = decoder->mbus_type; 1171 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING 1172 | V4L2_MBUS_FIELD_EVEN_LOW | V4L2_MBUS_DATA_ACTIVE_HIGH; 1173 1174 return 0; 1175 } 1176 1177 /**************************************************************************** 1178 V4L2 subdev pad ops 1179 ****************************************************************************/ 1180 static int tvp5150_init_cfg(struct v4l2_subdev *sd, 1181 struct v4l2_subdev_pad_config *cfg) 1182 { 1183 struct tvp5150 *decoder = to_tvp5150(sd); 1184 v4l2_std_id std; 1185 1186 /* 1187 * Reset selection to maximum on subdev_open() if autodetection is on 1188 * and a standard change is detected. 1189 */ 1190 if (decoder->norm == V4L2_STD_ALL) { 1191 std = tvp5150_read_std(sd); 1192 if (std != decoder->detected_norm) { 1193 decoder->detected_norm = std; 1194 tvp5150_set_default(std, &decoder->rect); 1195 } 1196 } 1197 1198 return 0; 1199 } 1200 1201 static int tvp5150_enum_mbus_code(struct v4l2_subdev *sd, 1202 struct v4l2_subdev_pad_config *cfg, 1203 struct v4l2_subdev_mbus_code_enum *code) 1204 { 1205 if (code->pad || code->index) 1206 return -EINVAL; 1207 1208 code->code = TVP5150_MBUS_FMT; 1209 return 0; 1210 } 1211 1212 static int tvp5150_enum_frame_size(struct v4l2_subdev *sd, 1213 struct v4l2_subdev_pad_config *cfg, 1214 struct v4l2_subdev_frame_size_enum *fse) 1215 { 1216 struct tvp5150 *decoder = to_tvp5150(sd); 1217 1218 if (fse->index >= 8 || fse->code != TVP5150_MBUS_FMT) 1219 return -EINVAL; 1220 1221 fse->code = TVP5150_MBUS_FMT; 1222 fse->min_width = decoder->rect.width; 1223 fse->max_width = decoder->rect.width; 1224 fse->min_height = decoder->rect.height / 2; 1225 fse->max_height = decoder->rect.height / 2; 1226 1227 return 0; 1228 } 1229 1230 /**************************************************************************** 1231 * Media entity ops 1232 ****************************************************************************/ 1233 #if defined(CONFIG_MEDIA_CONTROLLER) 1234 static int tvp5150_set_link(struct media_pad *connector_pad, 1235 struct media_pad *tvp5150_pad, u32 flags) 1236 { 1237 struct media_link *link; 1238 1239 link = media_entity_find_link(connector_pad, tvp5150_pad); 1240 if (!link) 1241 return -EINVAL; 1242 1243 link->flags = flags; 1244 link->reverse->flags = link->flags; 1245 1246 return 0; 1247 } 1248 1249 static int tvp5150_disable_all_input_links(struct tvp5150 *decoder) 1250 { 1251 struct media_pad *connector_pad; 1252 unsigned int i; 1253 int err; 1254 1255 for (i = 0; i < TVP5150_NUM_PADS - 1; i++) { 1256 connector_pad = media_entity_remote_pad(&decoder->pads[i]); 1257 if (!connector_pad) 1258 continue; 1259 1260 err = tvp5150_set_link(connector_pad, &decoder->pads[i], 0); 1261 if (err) 1262 return err; 1263 } 1264 1265 return 0; 1266 } 1267 1268 static int tvp5150_s_routing(struct v4l2_subdev *sd, u32 input, u32 output, 1269 u32 config); 1270 1271 static int tvp5150_link_setup(struct media_entity *entity, 1272 const struct media_pad *tvp5150_pad, 1273 const struct media_pad *remote, u32 flags) 1274 { 1275 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity); 1276 struct tvp5150 *decoder = to_tvp5150(sd); 1277 struct media_pad *other_tvp5150_pad = 1278 &decoder->pads[tvp5150_pad->index ^ 1]; 1279 struct v4l2_fwnode_connector *v4l2c; 1280 bool is_svideo = false; 1281 unsigned int i; 1282 int err; 1283 1284 /* 1285 * The TVP5150 state is determined by the enabled sink pad link(s). 1286 * Enabling or disabling the source pad link has no effect. 1287 */ 1288 if (tvp5150_pad->flags & MEDIA_PAD_FL_SOURCE) 1289 return 0; 1290 1291 /* Check if the svideo connector should be enabled */ 1292 for (i = 0; i < decoder->connectors_num; i++) { 1293 if (remote->entity == &decoder->connectors[i].ent) { 1294 v4l2c = &decoder->connectors[i].base; 1295 is_svideo = v4l2c->type == V4L2_CONN_SVIDEO; 1296 break; 1297 } 1298 } 1299 1300 dev_dbg_lvl(sd->dev, 1, debug, "link setup '%s':%d->'%s':%d[%d]", 1301 remote->entity->name, remote->index, 1302 tvp5150_pad->entity->name, tvp5150_pad->index, 1303 flags & MEDIA_LNK_FL_ENABLED); 1304 if (is_svideo) 1305 dev_dbg_lvl(sd->dev, 1, debug, 1306 "link setup '%s':%d->'%s':%d[%d]", 1307 remote->entity->name, remote->index, 1308 other_tvp5150_pad->entity->name, 1309 other_tvp5150_pad->index, 1310 flags & MEDIA_LNK_FL_ENABLED); 1311 1312 /* 1313 * The TVP5150 has an internal mux which allows the following setup: 1314 * 1315 * comp-connector1 --\ 1316 * |---> AIP1A 1317 * / 1318 * svideo-connector -| 1319 * \ 1320 * |---> AIP1B 1321 * comp-connector2 --/ 1322 * 1323 * We can't rely on user space that the current connector gets disabled 1324 * first before enabling the new connector. Disable all active 1325 * connector links to be on the safe side. 1326 */ 1327 err = tvp5150_disable_all_input_links(decoder); 1328 if (err) 1329 return err; 1330 1331 tvp5150_s_routing(sd, is_svideo ? TVP5150_SVIDEO : tvp5150_pad->index, 1332 flags & MEDIA_LNK_FL_ENABLED ? TVP5150_NORMAL : 1333 TVP5150_BLACK_SCREEN, 0); 1334 1335 if (flags & MEDIA_LNK_FL_ENABLED) { 1336 /* 1337 * S-Video connector is conneted to both ports AIP1A and AIP1B. 1338 * Both links must be enabled in one-shot regardless which link 1339 * the user requests. 1340 */ 1341 if (is_svideo) { 1342 err = tvp5150_set_link((struct media_pad *)remote, 1343 other_tvp5150_pad, flags); 1344 if (err) 1345 return err; 1346 } 1347 } 1348 1349 return 0; 1350 } 1351 1352 static const struct media_entity_operations tvp5150_sd_media_ops = { 1353 .link_setup = tvp5150_link_setup, 1354 }; 1355 #endif 1356 /**************************************************************************** 1357 I2C Command 1358 ****************************************************************************/ 1359 1360 static int tvp5150_s_stream(struct v4l2_subdev *sd, int enable) 1361 { 1362 struct tvp5150 *decoder = to_tvp5150(sd); 1363 unsigned int mask, val = 0, int_val = 0; 1364 1365 mask = TVP5150_MISC_CTL_YCBCR_OE | TVP5150_MISC_CTL_SYNC_OE | 1366 TVP5150_MISC_CTL_CLOCK_OE; 1367 1368 if (enable) { 1369 tvp5150_enable(sd); 1370 1371 /* Enable outputs if decoder is locked */ 1372 if (decoder->irq) 1373 val = decoder->lock ? decoder->oe : 0; 1374 else 1375 val = decoder->oe; 1376 int_val = TVP5150_INT_A_LOCK; 1377 v4l2_subdev_notify_event(&decoder->sd, &tvp5150_ev_fmt); 1378 } 1379 1380 regmap_update_bits(decoder->regmap, TVP5150_MISC_CTL, mask, val); 1381 if (decoder->irq) 1382 /* Enable / Disable lock interrupt */ 1383 regmap_update_bits(decoder->regmap, TVP5150_INT_ENABLE_REG_A, 1384 TVP5150_INT_A_LOCK, int_val); 1385 1386 return 0; 1387 } 1388 1389 static int tvp5150_s_routing(struct v4l2_subdev *sd, 1390 u32 input, u32 output, u32 config) 1391 { 1392 struct tvp5150 *decoder = to_tvp5150(sd); 1393 1394 decoder->input = input; 1395 decoder->output = output; 1396 1397 if (output == TVP5150_BLACK_SCREEN) 1398 decoder->enable = false; 1399 else 1400 decoder->enable = true; 1401 1402 tvp5150_selmux(sd); 1403 return 0; 1404 } 1405 1406 static int tvp5150_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt) 1407 { 1408 struct tvp5150 *decoder = to_tvp5150(sd); 1409 1410 /* 1411 * this is for capturing 36 raw vbi lines 1412 * if there's a way to cut off the beginning 2 vbi lines 1413 * with the tvp5150 then the vbi line count could be lowered 1414 * to 17 lines/field again, although I couldn't find a register 1415 * which could do that cropping 1416 */ 1417 1418 if (fmt->sample_format == V4L2_PIX_FMT_GREY) 1419 regmap_write(decoder->regmap, TVP5150_LUMA_PROC_CTL_1, 0x70); 1420 if (fmt->count[0] == 18 && fmt->count[1] == 18) { 1421 regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_START, 1422 0x00); 1423 regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_STOP, 0x01); 1424 } 1425 return 0; 1426 } 1427 1428 static int tvp5150_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi) 1429 { 1430 struct tvp5150 *decoder = to_tvp5150(sd); 1431 int i; 1432 1433 if (svbi->service_set != 0) { 1434 for (i = 0; i <= 23; i++) { 1435 svbi->service_lines[1][i] = 0; 1436 svbi->service_lines[0][i] = 1437 tvp5150_set_vbi(sd, svbi->service_lines[0][i], 1438 0xf0, i, 3); 1439 } 1440 /* Enables FIFO */ 1441 regmap_write(decoder->regmap, TVP5150_FIFO_OUT_CTRL, 1); 1442 } else { 1443 /* Disables FIFO*/ 1444 regmap_write(decoder->regmap, TVP5150_FIFO_OUT_CTRL, 0); 1445 1446 /* Disable Full Field */ 1447 regmap_write(decoder->regmap, TVP5150_FULL_FIELD_ENA, 0); 1448 1449 /* Disable Line modes */ 1450 for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++) 1451 regmap_write(decoder->regmap, i, 0xff); 1452 } 1453 return 0; 1454 } 1455 1456 static int tvp5150_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi) 1457 { 1458 int i, mask = 0; 1459 1460 memset(svbi->service_lines, 0, sizeof(svbi->service_lines)); 1461 1462 for (i = 0; i <= 23; i++) { 1463 svbi->service_lines[0][i] = 1464 tvp5150_get_vbi(sd, i); 1465 mask |= svbi->service_lines[0][i]; 1466 } 1467 svbi->service_set = mask; 1468 return 0; 1469 } 1470 1471 #ifdef CONFIG_VIDEO_ADV_DEBUG 1472 static int tvp5150_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg) 1473 { 1474 int res; 1475 1476 res = tvp5150_read(sd, reg->reg & 0xff); 1477 if (res < 0) { 1478 dev_err(sd->dev, "%s: failed with error = %d\n", __func__, res); 1479 return res; 1480 } 1481 1482 reg->val = res; 1483 reg->size = 1; 1484 return 0; 1485 } 1486 1487 static int tvp5150_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg) 1488 { 1489 struct tvp5150 *decoder = to_tvp5150(sd); 1490 1491 return regmap_write(decoder->regmap, reg->reg & 0xff, reg->val & 0xff); 1492 } 1493 #endif 1494 1495 static int tvp5150_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt) 1496 { 1497 int status = tvp5150_read(sd, 0x88); 1498 1499 vt->signal = ((status & 0x04) && (status & 0x02)) ? 0xffff : 0x0; 1500 return 0; 1501 } 1502 1503 static int tvp5150_registered(struct v4l2_subdev *sd) 1504 { 1505 #if defined(CONFIG_MEDIA_CONTROLLER) 1506 struct tvp5150 *decoder = to_tvp5150(sd); 1507 unsigned int i; 1508 int ret; 1509 1510 /* 1511 * Setup connector pads and links. Enable the link to the first 1512 * available connector per default. 1513 */ 1514 for (i = 0; i < decoder->connectors_num; i++) { 1515 struct media_entity *con = &decoder->connectors[i].ent; 1516 struct media_pad *pad = &decoder->connectors[i].pad; 1517 struct v4l2_fwnode_connector *v4l2c = 1518 &decoder->connectors[i].base; 1519 struct v4l2_connector_link *link = 1520 v4l2_connector_first_link(v4l2c); 1521 unsigned int port = link->fwnode_link.remote_port; 1522 unsigned int flags = i ? 0 : MEDIA_LNK_FL_ENABLED; 1523 bool is_svideo = v4l2c->type == V4L2_CONN_SVIDEO; 1524 1525 pad->flags = MEDIA_PAD_FL_SOURCE; 1526 ret = media_entity_pads_init(con, 1, pad); 1527 if (ret < 0) 1528 goto err; 1529 1530 ret = media_device_register_entity(sd->v4l2_dev->mdev, con); 1531 if (ret < 0) 1532 goto err; 1533 1534 ret = media_create_pad_link(con, 0, &sd->entity, port, flags); 1535 if (ret < 0) 1536 goto err; 1537 1538 if (is_svideo) { 1539 /* 1540 * Check tvp5150_link_setup() comments for more 1541 * information. 1542 */ 1543 link = v4l2_connector_last_link(v4l2c); 1544 port = link->fwnode_link.remote_port; 1545 ret = media_create_pad_link(con, 0, &sd->entity, port, 1546 flags); 1547 if (ret < 0) 1548 goto err; 1549 } 1550 1551 /* Enable default input. */ 1552 if (flags == MEDIA_LNK_FL_ENABLED) { 1553 decoder->input = 1554 is_svideo ? TVP5150_SVIDEO : 1555 port == 0 ? TVP5150_COMPOSITE0 : 1556 TVP5150_COMPOSITE1; 1557 1558 tvp5150_selmux(sd); 1559 } 1560 } 1561 1562 return 0; 1563 1564 err: 1565 for (i = 0; i < decoder->connectors_num; i++) 1566 media_device_unregister_entity(&decoder->connectors[i].ent); 1567 return ret; 1568 #endif 1569 1570 return 0; 1571 } 1572 1573 /* ----------------------------------------------------------------------- */ 1574 1575 static const struct v4l2_ctrl_ops tvp5150_ctrl_ops = { 1576 .s_ctrl = tvp5150_s_ctrl, 1577 }; 1578 1579 static const struct v4l2_subdev_core_ops tvp5150_core_ops = { 1580 .log_status = tvp5150_log_status, 1581 .reset = tvp5150_reset, 1582 #ifdef CONFIG_VIDEO_ADV_DEBUG 1583 .g_register = tvp5150_g_register, 1584 .s_register = tvp5150_s_register, 1585 #endif 1586 }; 1587 1588 static const struct v4l2_subdev_tuner_ops tvp5150_tuner_ops = { 1589 .g_tuner = tvp5150_g_tuner, 1590 }; 1591 1592 static const struct v4l2_subdev_video_ops tvp5150_video_ops = { 1593 .s_std = tvp5150_s_std, 1594 .g_std = tvp5150_g_std, 1595 .querystd = tvp5150_querystd, 1596 .s_stream = tvp5150_s_stream, 1597 .s_routing = tvp5150_s_routing, 1598 .g_mbus_config = tvp5150_g_mbus_config, 1599 }; 1600 1601 static const struct v4l2_subdev_vbi_ops tvp5150_vbi_ops = { 1602 .g_sliced_vbi_cap = tvp5150_g_sliced_vbi_cap, 1603 .g_sliced_fmt = tvp5150_g_sliced_fmt, 1604 .s_sliced_fmt = tvp5150_s_sliced_fmt, 1605 .s_raw_fmt = tvp5150_s_raw_fmt, 1606 }; 1607 1608 static const struct v4l2_subdev_pad_ops tvp5150_pad_ops = { 1609 .init_cfg = tvp5150_init_cfg, 1610 .enum_mbus_code = tvp5150_enum_mbus_code, 1611 .enum_frame_size = tvp5150_enum_frame_size, 1612 .set_fmt = tvp5150_fill_fmt, 1613 .get_fmt = tvp5150_fill_fmt, 1614 .get_selection = tvp5150_get_selection, 1615 .set_selection = tvp5150_set_selection, 1616 }; 1617 1618 static const struct v4l2_subdev_ops tvp5150_ops = { 1619 .core = &tvp5150_core_ops, 1620 .tuner = &tvp5150_tuner_ops, 1621 .video = &tvp5150_video_ops, 1622 .vbi = &tvp5150_vbi_ops, 1623 .pad = &tvp5150_pad_ops, 1624 }; 1625 1626 static const struct v4l2_subdev_internal_ops tvp5150_internal_ops = { 1627 .registered = tvp5150_registered, 1628 }; 1629 1630 /**************************************************************************** 1631 I2C Client & Driver 1632 ****************************************************************************/ 1633 1634 static const struct regmap_range tvp5150_readable_ranges[] = { 1635 { 1636 .range_min = TVP5150_VD_IN_SRC_SEL_1, 1637 .range_max = TVP5150_AUTOSW_MSK, 1638 }, { 1639 .range_min = TVP5150_COLOR_KIL_THSH_CTL, 1640 .range_max = TVP5150_CONF_SHARED_PIN, 1641 }, { 1642 .range_min = TVP5150_ACT_VD_CROP_ST_MSB, 1643 .range_max = TVP5150_HORIZ_SYNC_START, 1644 }, { 1645 .range_min = TVP5150_VERT_BLANKING_START, 1646 .range_max = TVP5150_INTT_CONFIG_REG_B, 1647 }, { 1648 .range_min = TVP5150_VIDEO_STD, 1649 .range_max = TVP5150_VIDEO_STD, 1650 }, { 1651 .range_min = TVP5150_CB_GAIN_FACT, 1652 .range_max = TVP5150_REV_SELECT, 1653 }, { 1654 .range_min = TVP5150_MSB_DEV_ID, 1655 .range_max = TVP5150_STATUS_REG_5, 1656 }, { 1657 .range_min = TVP5150_CC_DATA_INI, 1658 .range_max = TVP5150_TELETEXT_FIL_ENA, 1659 }, { 1660 .range_min = TVP5150_INT_STATUS_REG_A, 1661 .range_max = TVP5150_FIFO_OUT_CTRL, 1662 }, { 1663 .range_min = TVP5150_FULL_FIELD_ENA, 1664 .range_max = TVP5150_FULL_FIELD_MODE_REG, 1665 }, 1666 }; 1667 1668 static bool tvp5150_volatile_reg(struct device *dev, unsigned int reg) 1669 { 1670 switch (reg) { 1671 case TVP5150_VERT_LN_COUNT_MSB: 1672 case TVP5150_VERT_LN_COUNT_LSB: 1673 case TVP5150_INT_STATUS_REG_A: 1674 case TVP5150_INT_STATUS_REG_B: 1675 case TVP5150_INT_ACTIVE_REG_B: 1676 case TVP5150_STATUS_REG_1: 1677 case TVP5150_STATUS_REG_2: 1678 case TVP5150_STATUS_REG_3: 1679 case TVP5150_STATUS_REG_4: 1680 case TVP5150_STATUS_REG_5: 1681 /* CC, WSS, VPS, VITC data? */ 1682 case TVP5150_VBI_FIFO_READ_DATA: 1683 case TVP5150_VDP_STATUS_REG: 1684 case TVP5150_FIFO_WORD_COUNT: 1685 return true; 1686 default: 1687 return false; 1688 } 1689 } 1690 1691 static const struct regmap_access_table tvp5150_readable_table = { 1692 .yes_ranges = tvp5150_readable_ranges, 1693 .n_yes_ranges = ARRAY_SIZE(tvp5150_readable_ranges), 1694 }; 1695 1696 static struct regmap_config tvp5150_config = { 1697 .reg_bits = 8, 1698 .val_bits = 8, 1699 .max_register = 0xff, 1700 1701 .cache_type = REGCACHE_RBTREE, 1702 1703 .rd_table = &tvp5150_readable_table, 1704 .volatile_reg = tvp5150_volatile_reg, 1705 }; 1706 1707 static int tvp5150_detect_version(struct tvp5150 *core) 1708 { 1709 struct v4l2_subdev *sd = &core->sd; 1710 struct i2c_client *c = v4l2_get_subdevdata(sd); 1711 u8 regs[4]; 1712 int res; 1713 1714 /* 1715 * Read consequent registers - TVP5150_MSB_DEV_ID, TVP5150_LSB_DEV_ID, 1716 * TVP5150_ROM_MAJOR_VER, TVP5150_ROM_MINOR_VER 1717 */ 1718 res = regmap_bulk_read(core->regmap, TVP5150_MSB_DEV_ID, regs, 4); 1719 if (res < 0) { 1720 dev_err(&c->dev, "reading ID registers failed: %d\n", res); 1721 return res; 1722 } 1723 1724 core->dev_id = (regs[0] << 8) | regs[1]; 1725 core->rom_ver = (regs[2] << 8) | regs[3]; 1726 1727 dev_info(sd->dev, "tvp%04x (%u.%u) chip found @ 0x%02x (%s)\n", 1728 core->dev_id, regs[2], regs[3], c->addr << 1, 1729 c->adapter->name); 1730 1731 if (core->dev_id == 0x5150 && core->rom_ver == 0x0321) { 1732 dev_info(sd->dev, "tvp5150a detected.\n"); 1733 } else if (core->dev_id == 0x5150 && core->rom_ver == 0x0400) { 1734 dev_info(sd->dev, "tvp5150am1 detected.\n"); 1735 1736 /* ITU-T BT.656.4 timing */ 1737 regmap_write(core->regmap, TVP5150_REV_SELECT, 0); 1738 } else if (core->dev_id == 0x5151 && core->rom_ver == 0x0100) { 1739 dev_info(sd->dev, "tvp5151 detected.\n"); 1740 } else { 1741 dev_info(sd->dev, "*** unknown tvp%04x chip detected.\n", 1742 core->dev_id); 1743 } 1744 1745 return 0; 1746 } 1747 1748 static int tvp5150_init(struct i2c_client *c) 1749 { 1750 struct gpio_desc *pdn_gpio; 1751 struct gpio_desc *reset_gpio; 1752 1753 pdn_gpio = devm_gpiod_get_optional(&c->dev, "pdn", GPIOD_OUT_HIGH); 1754 if (IS_ERR(pdn_gpio)) 1755 return PTR_ERR(pdn_gpio); 1756 1757 if (pdn_gpio) { 1758 gpiod_set_value_cansleep(pdn_gpio, 0); 1759 /* Delay time between power supplies active and reset */ 1760 msleep(20); 1761 } 1762 1763 reset_gpio = devm_gpiod_get_optional(&c->dev, "reset", GPIOD_OUT_HIGH); 1764 if (IS_ERR(reset_gpio)) 1765 return PTR_ERR(reset_gpio); 1766 1767 if (reset_gpio) { 1768 /* RESETB pulse duration */ 1769 ndelay(500); 1770 gpiod_set_value_cansleep(reset_gpio, 0); 1771 /* Delay time between end of reset to I2C active */ 1772 usleep_range(200, 250); 1773 } 1774 1775 return 0; 1776 } 1777 1778 #if defined(CONFIG_MEDIA_CONTROLLER) 1779 static int tvp5150_mc_init(struct tvp5150 *decoder) 1780 { 1781 struct v4l2_subdev *sd = &decoder->sd; 1782 unsigned int i; 1783 1784 sd->entity.ops = &tvp5150_sd_media_ops; 1785 sd->entity.function = MEDIA_ENT_F_ATV_DECODER; 1786 1787 for (i = 0; i < TVP5150_NUM_PADS - 1; i++) { 1788 decoder->pads[i].flags = MEDIA_PAD_FL_SINK; 1789 decoder->pads[i].sig_type = PAD_SIGNAL_ANALOG; 1790 } 1791 1792 decoder->pads[i].flags = MEDIA_PAD_FL_SOURCE; 1793 decoder->pads[i].sig_type = PAD_SIGNAL_DV; 1794 1795 return media_entity_pads_init(&sd->entity, TVP5150_NUM_PADS, 1796 decoder->pads); 1797 } 1798 1799 #else /* !defined(CONFIG_MEDIA_CONTROLLER) */ 1800 1801 static inline int tvp5150_mc_init(struct tvp5150 *decoder) 1802 { 1803 return 0; 1804 } 1805 #endif /* defined(CONFIG_MEDIA_CONTROLLER) */ 1806 1807 static int tvp5150_validate_connectors(struct tvp5150 *decoder) 1808 { 1809 struct device *dev = decoder->sd.dev; 1810 struct tvp5150_connector *tvpc; 1811 struct v4l2_fwnode_connector *v4l2c; 1812 unsigned int i; 1813 1814 if (!decoder->connectors_num) { 1815 dev_err(dev, "No valid connector found\n"); 1816 return -ENODEV; 1817 } 1818 1819 for (i = 0; i < decoder->connectors_num; i++) { 1820 struct v4l2_connector_link *link0 = NULL; 1821 struct v4l2_connector_link *link1; 1822 1823 tvpc = &decoder->connectors[i]; 1824 v4l2c = &tvpc->base; 1825 1826 if (v4l2c->type == V4L2_CONN_COMPOSITE) { 1827 if (v4l2c->nr_of_links != 1) { 1828 dev_err(dev, "Composite: connector needs 1 link\n"); 1829 return -EINVAL; 1830 } 1831 link0 = v4l2_connector_first_link(v4l2c); 1832 if (!link0) { 1833 dev_err(dev, "Composite: invalid first link\n"); 1834 return -EINVAL; 1835 } 1836 if (link0->fwnode_link.remote_id == 1) { 1837 dev_err(dev, "Composite: invalid endpoint id\n"); 1838 return -EINVAL; 1839 } 1840 } 1841 1842 if (v4l2c->type == V4L2_CONN_SVIDEO) { 1843 if (v4l2c->nr_of_links != 2) { 1844 dev_err(dev, "SVideo: connector needs 2 links\n"); 1845 return -EINVAL; 1846 } 1847 link0 = v4l2_connector_first_link(v4l2c); 1848 if (!link0) { 1849 dev_err(dev, "SVideo: invalid first link\n"); 1850 return -EINVAL; 1851 } 1852 link1 = v4l2_connector_last_link(v4l2c); 1853 if (link0->fwnode_link.remote_port == 1854 link1->fwnode_link.remote_port) { 1855 dev_err(dev, "SVideo: invalid link setup\n"); 1856 return -EINVAL; 1857 } 1858 } 1859 } 1860 1861 return 0; 1862 } 1863 1864 static int tvp5150_parse_dt(struct tvp5150 *decoder, struct device_node *np) 1865 { 1866 struct device *dev = decoder->sd.dev; 1867 struct v4l2_fwnode_endpoint bus_cfg = { 1868 .bus_type = V4L2_MBUS_UNKNOWN 1869 }; 1870 struct device_node *ep_np; 1871 struct tvp5150_connector *tvpc; 1872 struct v4l2_fwnode_connector *v4l2c; 1873 unsigned int flags, ep_num; 1874 unsigned int i; 1875 int ret; 1876 1877 /* At least 1 output and 1 input */ 1878 ep_num = of_graph_get_endpoint_count(np); 1879 if (ep_num < 2 || ep_num > 5) { 1880 dev_err(dev, "At least 1 input and 1 output must be connected to the device.\n"); 1881 return -EINVAL; 1882 } 1883 1884 /* Layout if all connectors are used: 1885 * 1886 * tvp-5150 port@0 (AIP1A) 1887 * endpoint@0 -----------> Comp0-Con port 1888 * endpoint@1 --------+--> Svideo-Con port 1889 * tvp-5150 port@1 (AIP1B) | 1890 * endpoint@1 --------+ 1891 * endpoint@0 -----------> Comp1-Con port 1892 * tvp-5150 port@2 1893 * endpoint (video bitstream output at YOUT[0-7] parallel bus) 1894 */ 1895 for_each_endpoint_of_node(np, ep_np) { 1896 struct fwnode_handle *ep_fwnode = of_fwnode_handle(ep_np); 1897 unsigned int next_connector = decoder->connectors_num; 1898 struct of_endpoint ep; 1899 1900 of_graph_parse_endpoint(ep_np, &ep); 1901 if (ep.port > 1 || ep.id > 1) { 1902 dev_dbg(dev, "Ignore connector on port@%u/ep@%u\n", 1903 ep.port, ep.id); 1904 continue; 1905 } 1906 1907 tvpc = &decoder->connectors[next_connector]; 1908 v4l2c = &tvpc->base; 1909 1910 if (ep.port == 0 || (ep.port == 1 && ep.id == 0)) { 1911 ret = v4l2_fwnode_connector_parse(ep_fwnode, v4l2c); 1912 if (ret) 1913 goto err_put; 1914 ret = v4l2_fwnode_connector_add_link(ep_fwnode, v4l2c); 1915 if (ret) 1916 goto err_put; 1917 decoder->connectors_num++; 1918 } else { 1919 /* Adding the 2nd svideo link */ 1920 for (i = 0; i < TVP5150_MAX_CONNECTORS; i++) { 1921 tvpc = &decoder->connectors[i]; 1922 v4l2c = &tvpc->base; 1923 if (v4l2c->type == V4L2_CONN_SVIDEO) 1924 break; 1925 } 1926 1927 ret = v4l2_fwnode_connector_add_link(ep_fwnode, v4l2c); 1928 if (ret) 1929 goto err_put; 1930 } 1931 } 1932 1933 ret = tvp5150_validate_connectors(decoder); 1934 if (ret) 1935 goto err_free; 1936 1937 for (i = 0; i < decoder->connectors_num; i++) { 1938 tvpc = &decoder->connectors[i]; 1939 v4l2c = &tvpc->base; 1940 tvpc->ent.flags = MEDIA_ENT_FL_CONNECTOR; 1941 tvpc->ent.function = v4l2c->type == V4L2_CONN_SVIDEO ? 1942 MEDIA_ENT_F_CONN_SVIDEO : MEDIA_ENT_F_CONN_COMPOSITE; 1943 tvpc->ent.name = devm_kasprintf(dev, GFP_KERNEL, "%s %s", 1944 v4l2c->name, v4l2c->label ? 1945 v4l2c->label : ""); 1946 } 1947 1948 ep_np = of_graph_get_endpoint_by_regs(np, TVP5150_PAD_VID_OUT, 0); 1949 if (!ep_np) { 1950 dev_err(dev, "Error no output endpoint available\n"); 1951 goto err_free; 1952 } 1953 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_np), &bus_cfg); 1954 of_node_put(ep_np); 1955 if (ret) 1956 goto err_free; 1957 1958 flags = bus_cfg.bus.parallel.flags; 1959 if (bus_cfg.bus_type == V4L2_MBUS_PARALLEL && 1960 !(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH && 1961 flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH && 1962 flags & V4L2_MBUS_FIELD_EVEN_LOW)) { 1963 ret = -EINVAL; 1964 goto err_free; 1965 } 1966 1967 decoder->mbus_type = bus_cfg.bus_type; 1968 1969 return 0; 1970 1971 err_put: 1972 of_node_put(ep_np); 1973 err_free: 1974 for (i = 0; i < TVP5150_MAX_CONNECTORS; i++) 1975 v4l2_fwnode_connector_free(&decoder->connectors[i].base); 1976 1977 return ret; 1978 } 1979 1980 static const char * const tvp5150_test_patterns[2] = { 1981 "Disabled", 1982 "Black screen" 1983 }; 1984 1985 static int tvp5150_probe(struct i2c_client *c) 1986 { 1987 struct tvp5150 *core; 1988 struct v4l2_subdev *sd; 1989 struct device_node *np = c->dev.of_node; 1990 struct regmap *map; 1991 int res; 1992 1993 /* Check if the adapter supports the needed features */ 1994 if (!i2c_check_functionality(c->adapter, 1995 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) 1996 return -EIO; 1997 1998 res = tvp5150_init(c); 1999 if (res) 2000 return res; 2001 2002 core = devm_kzalloc(&c->dev, sizeof(*core), GFP_KERNEL); 2003 if (!core) 2004 return -ENOMEM; 2005 2006 map = devm_regmap_init_i2c(c, &tvp5150_config); 2007 if (IS_ERR(map)) 2008 return PTR_ERR(map); 2009 2010 core->regmap = map; 2011 sd = &core->sd; 2012 v4l2_i2c_subdev_init(sd, c, &tvp5150_ops); 2013 sd->internal_ops = &tvp5150_internal_ops; 2014 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 2015 2016 if (IS_ENABLED(CONFIG_OF) && np) { 2017 res = tvp5150_parse_dt(core, np); 2018 if (res) { 2019 dev_err(sd->dev, "DT parsing error: %d\n", res); 2020 return res; 2021 } 2022 } else { 2023 /* Default to BT.656 embedded sync */ 2024 core->mbus_type = V4L2_MBUS_BT656; 2025 } 2026 2027 res = tvp5150_mc_init(core); 2028 if (res) 2029 return res; 2030 2031 res = tvp5150_detect_version(core); 2032 if (res < 0) 2033 return res; 2034 2035 core->norm = V4L2_STD_ALL; /* Default is autodetect */ 2036 core->detected_norm = V4L2_STD_UNKNOWN; 2037 core->input = TVP5150_COMPOSITE1; 2038 core->enable = true; 2039 2040 v4l2_ctrl_handler_init(&core->hdl, 5); 2041 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops, 2042 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128); 2043 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops, 2044 V4L2_CID_CONTRAST, 0, 255, 1, 128); 2045 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops, 2046 V4L2_CID_SATURATION, 0, 255, 1, 128); 2047 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops, 2048 V4L2_CID_HUE, -128, 127, 1, 0); 2049 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops, 2050 V4L2_CID_PIXEL_RATE, 27000000, 2051 27000000, 1, 27000000); 2052 v4l2_ctrl_new_std_menu_items(&core->hdl, &tvp5150_ctrl_ops, 2053 V4L2_CID_TEST_PATTERN, 2054 ARRAY_SIZE(tvp5150_test_patterns) - 1, 2055 0, 0, tvp5150_test_patterns); 2056 sd->ctrl_handler = &core->hdl; 2057 if (core->hdl.error) { 2058 res = core->hdl.error; 2059 goto err; 2060 } 2061 2062 tvp5150_set_default(tvp5150_read_std(sd), &core->rect); 2063 2064 core->irq = c->irq; 2065 tvp5150_reset(sd, 0); /* Calls v4l2_ctrl_handler_setup() */ 2066 if (c->irq) { 2067 res = devm_request_threaded_irq(&c->dev, c->irq, NULL, 2068 tvp5150_isr, IRQF_TRIGGER_HIGH | 2069 IRQF_ONESHOT, "tvp5150", core); 2070 if (res) 2071 goto err; 2072 } 2073 2074 res = v4l2_async_register_subdev(sd); 2075 if (res < 0) 2076 goto err; 2077 2078 if (debug > 1) 2079 tvp5150_log_status(sd); 2080 return 0; 2081 2082 err: 2083 v4l2_ctrl_handler_free(&core->hdl); 2084 return res; 2085 } 2086 2087 static int tvp5150_remove(struct i2c_client *c) 2088 { 2089 struct v4l2_subdev *sd = i2c_get_clientdata(c); 2090 struct tvp5150 *decoder = to_tvp5150(sd); 2091 unsigned int i; 2092 2093 dev_dbg_lvl(sd->dev, 1, debug, 2094 "tvp5150.c: removing tvp5150 adapter on address 0x%x\n", 2095 c->addr << 1); 2096 2097 for (i = 0; i < decoder->connectors_num; i++) 2098 v4l2_fwnode_connector_free(&decoder->connectors[i].base); 2099 for (i = 0; i < decoder->connectors_num; i++) 2100 media_device_unregister_entity(&decoder->connectors[i].ent); 2101 v4l2_async_unregister_subdev(sd); 2102 v4l2_ctrl_handler_free(&decoder->hdl); 2103 return 0; 2104 } 2105 2106 /* ----------------------------------------------------------------------- */ 2107 2108 static const struct i2c_device_id tvp5150_id[] = { 2109 { "tvp5150", 0 }, 2110 { } 2111 }; 2112 MODULE_DEVICE_TABLE(i2c, tvp5150_id); 2113 2114 #if IS_ENABLED(CONFIG_OF) 2115 static const struct of_device_id tvp5150_of_match[] = { 2116 { .compatible = "ti,tvp5150", }, 2117 { /* sentinel */ }, 2118 }; 2119 MODULE_DEVICE_TABLE(of, tvp5150_of_match); 2120 #endif 2121 2122 static struct i2c_driver tvp5150_driver = { 2123 .driver = { 2124 .of_match_table = of_match_ptr(tvp5150_of_match), 2125 .name = "tvp5150", 2126 }, 2127 .probe_new = tvp5150_probe, 2128 .remove = tvp5150_remove, 2129 .id_table = tvp5150_id, 2130 }; 2131 2132 module_i2c_driver(tvp5150_driver); 2133