Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // tvp5150 - Texas Instruments TVP5150A/AM1 and TVP5151 video decoder driver
0004 //
0005 // Copyright (c) 2005,2006 Mauro Carvalho Chehab <mchehab@kernel.org>
0006 
0007 #include <dt-bindings/media/tvp5150.h>
0008 #include <linux/i2c.h>
0009 #include <linux/slab.h>
0010 #include <linux/videodev2.h>
0011 #include <linux/delay.h>
0012 #include <linux/gpio/consumer.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/module.h>
0015 #include <linux/of_graph.h>
0016 #include <linux/pm_runtime.h>
0017 #include <linux/regmap.h>
0018 #include <media/v4l2-async.h>
0019 #include <media/v4l2-device.h>
0020 #include <media/v4l2-event.h>
0021 #include <media/v4l2-ctrls.h>
0022 #include <media/v4l2-fwnode.h>
0023 #include <media/v4l2-mc.h>
0024 #include <media/v4l2-rect.h>
0025 
0026 #include "tvp5150_reg.h"
0027 
0028 #define TVP5150_H_MAX       720U
0029 #define TVP5150_V_MAX_525_60    480U
0030 #define TVP5150_V_MAX_OTHERS    576U
0031 #define TVP5150_MAX_CROP_LEFT   511
0032 #define TVP5150_MAX_CROP_TOP    127
0033 #define TVP5150_CROP_SHIFT  2
0034 #define TVP5150_MBUS_FMT    MEDIA_BUS_FMT_UYVY8_2X8
0035 #define TVP5150_FIELD       V4L2_FIELD_ALTERNATE
0036 #define TVP5150_COLORSPACE  V4L2_COLORSPACE_SMPTE170M
0037 #define TVP5150_STD_MASK    (V4L2_STD_NTSC     | \
0038                  V4L2_STD_NTSC_443 | \
0039                  V4L2_STD_PAL      | \
0040                  V4L2_STD_PAL_M    | \
0041                  V4L2_STD_PAL_N    | \
0042                  V4L2_STD_PAL_Nc   | \
0043                  V4L2_STD_SECAM)
0044 
0045 #define TVP5150_MAX_CONNECTORS  3 /* Check dt-bindings for more information */
0046 
0047 MODULE_DESCRIPTION("Texas Instruments TVP5150A/TVP5150AM1/TVP5151 video decoder driver");
0048 MODULE_AUTHOR("Mauro Carvalho Chehab");
0049 MODULE_LICENSE("GPL v2");
0050 
0051 
0052 static int debug;
0053 module_param(debug, int, 0644);
0054 MODULE_PARM_DESC(debug, "Debug level (0-2)");
0055 
0056 #define dprintk0(__dev, __arg...) dev_dbg_lvl(__dev, 0, 0, __arg)
0057 
0058 enum tvp5150_pads {
0059     TVP5150_PAD_AIP1A,
0060     TVP5150_PAD_AIP1B,
0061     TVP5150_PAD_VID_OUT,
0062     TVP5150_NUM_PADS
0063 };
0064 
0065 struct tvp5150_connector {
0066     struct v4l2_fwnode_connector base;
0067     struct media_entity ent;
0068     struct media_pad pad;
0069 };
0070 
0071 struct tvp5150 {
0072     struct v4l2_subdev sd;
0073 
0074     struct media_pad pads[TVP5150_NUM_PADS];
0075     struct tvp5150_connector connectors[TVP5150_MAX_CONNECTORS];
0076     struct tvp5150_connector *cur_connector;
0077     unsigned int connectors_num;
0078 
0079     struct v4l2_ctrl_handler hdl;
0080     struct v4l2_rect rect;
0081     struct regmap *regmap;
0082     int irq;
0083 
0084     v4l2_std_id norm;   /* Current set standard */
0085     v4l2_std_id detected_norm;
0086     u32 input;
0087     u32 output;
0088     u32 oe;
0089     int enable;
0090     bool lock;
0091 
0092     u16 dev_id;
0093     u16 rom_ver;
0094 
0095     enum v4l2_mbus_type mbus_type;
0096 };
0097 
0098 static inline struct tvp5150 *to_tvp5150(struct v4l2_subdev *sd)
0099 {
0100     return container_of(sd, struct tvp5150, sd);
0101 }
0102 
0103 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
0104 {
0105     return &container_of(ctrl->handler, struct tvp5150, hdl)->sd;
0106 }
0107 
0108 static int tvp5150_read(struct v4l2_subdev *sd, unsigned char addr)
0109 {
0110     struct tvp5150 *decoder = to_tvp5150(sd);
0111     int ret, val;
0112 
0113     ret = regmap_read(decoder->regmap, addr, &val);
0114     if (ret < 0)
0115         return ret;
0116 
0117     return val;
0118 }
0119 
0120 static void dump_reg_range(struct v4l2_subdev *sd, char *s, u8 init,
0121                 const u8 end, int max_line)
0122 {
0123     u8 buf[16];
0124     int i = 0, j, len;
0125 
0126     if (max_line > 16) {
0127         dprintk0(sd->dev, "too much data to dump\n");
0128         return;
0129     }
0130 
0131     for (i = init; i < end; i += max_line) {
0132         len = (end - i > max_line) ? max_line : end - i;
0133 
0134         for (j = 0; j < len; j++)
0135             buf[j] = tvp5150_read(sd, i + j);
0136 
0137         dprintk0(sd->dev, "%s reg %02x = %*ph\n", s, i, len, buf);
0138     }
0139 }
0140 
0141 static int tvp5150_log_status(struct v4l2_subdev *sd)
0142 {
0143     dprintk0(sd->dev, "tvp5150: Video input source selection #1 = 0x%02x\n",
0144         tvp5150_read(sd, TVP5150_VD_IN_SRC_SEL_1));
0145     dprintk0(sd->dev, "tvp5150: Analog channel controls = 0x%02x\n",
0146         tvp5150_read(sd, TVP5150_ANAL_CHL_CTL));
0147     dprintk0(sd->dev, "tvp5150: Operation mode controls = 0x%02x\n",
0148         tvp5150_read(sd, TVP5150_OP_MODE_CTL));
0149     dprintk0(sd->dev, "tvp5150: Miscellaneous controls = 0x%02x\n",
0150         tvp5150_read(sd, TVP5150_MISC_CTL));
0151     dprintk0(sd->dev, "tvp5150: Autoswitch mask= 0x%02x\n",
0152         tvp5150_read(sd, TVP5150_AUTOSW_MSK));
0153     dprintk0(sd->dev, "tvp5150: Color killer threshold control = 0x%02x\n",
0154         tvp5150_read(sd, TVP5150_COLOR_KIL_THSH_CTL));
0155     dprintk0(sd->dev, "tvp5150: Luminance processing controls #1 #2 and #3 = %02x %02x %02x\n",
0156         tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_1),
0157         tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_2),
0158         tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_3));
0159     dprintk0(sd->dev, "tvp5150: Brightness control = 0x%02x\n",
0160         tvp5150_read(sd, TVP5150_BRIGHT_CTL));
0161     dprintk0(sd->dev, "tvp5150: Color saturation control = 0x%02x\n",
0162         tvp5150_read(sd, TVP5150_SATURATION_CTL));
0163     dprintk0(sd->dev, "tvp5150: Hue control = 0x%02x\n",
0164         tvp5150_read(sd, TVP5150_HUE_CTL));
0165     dprintk0(sd->dev, "tvp5150: Contrast control = 0x%02x\n",
0166         tvp5150_read(sd, TVP5150_CONTRAST_CTL));
0167     dprintk0(sd->dev, "tvp5150: Outputs and data rates select = 0x%02x\n",
0168         tvp5150_read(sd, TVP5150_DATA_RATE_SEL));
0169     dprintk0(sd->dev, "tvp5150: Configuration shared pins = 0x%02x\n",
0170         tvp5150_read(sd, TVP5150_CONF_SHARED_PIN));
0171     dprintk0(sd->dev, "tvp5150: Active video cropping start = 0x%02x%02x\n",
0172         tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_MSB),
0173         tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_LSB));
0174     dprintk0(sd->dev, "tvp5150: Active video cropping stop  = 0x%02x%02x\n",
0175         tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_MSB),
0176         tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_LSB));
0177     dprintk0(sd->dev, "tvp5150: Genlock/RTC = 0x%02x\n",
0178         tvp5150_read(sd, TVP5150_GENLOCK));
0179     dprintk0(sd->dev, "tvp5150: Horizontal sync start = 0x%02x\n",
0180         tvp5150_read(sd, TVP5150_HORIZ_SYNC_START));
0181     dprintk0(sd->dev, "tvp5150: Vertical blanking start = 0x%02x\n",
0182         tvp5150_read(sd, TVP5150_VERT_BLANKING_START));
0183     dprintk0(sd->dev, "tvp5150: Vertical blanking stop = 0x%02x\n",
0184         tvp5150_read(sd, TVP5150_VERT_BLANKING_STOP));
0185     dprintk0(sd->dev, "tvp5150: Chrominance processing control #1 and #2 = %02x %02x\n",
0186         tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_1),
0187         tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_2));
0188     dprintk0(sd->dev, "tvp5150: Interrupt reset register B = 0x%02x\n",
0189         tvp5150_read(sd, TVP5150_INT_RESET_REG_B));
0190     dprintk0(sd->dev, "tvp5150: Interrupt enable register B = 0x%02x\n",
0191         tvp5150_read(sd, TVP5150_INT_ENABLE_REG_B));
0192     dprintk0(sd->dev, "tvp5150: Interrupt configuration register B = 0x%02x\n",
0193         tvp5150_read(sd, TVP5150_INTT_CONFIG_REG_B));
0194     dprintk0(sd->dev, "tvp5150: Video standard = 0x%02x\n",
0195         tvp5150_read(sd, TVP5150_VIDEO_STD));
0196     dprintk0(sd->dev, "tvp5150: Chroma gain factor: Cb=0x%02x Cr=0x%02x\n",
0197         tvp5150_read(sd, TVP5150_CB_GAIN_FACT),
0198         tvp5150_read(sd, TVP5150_CR_GAIN_FACTOR));
0199     dprintk0(sd->dev, "tvp5150: Macrovision on counter = 0x%02x\n",
0200         tvp5150_read(sd, TVP5150_MACROVISION_ON_CTR));
0201     dprintk0(sd->dev, "tvp5150: Macrovision off counter = 0x%02x\n",
0202         tvp5150_read(sd, TVP5150_MACROVISION_OFF_CTR));
0203     dprintk0(sd->dev, "tvp5150: ITU-R BT.656.%d timing(TVP5150AM1 only)\n",
0204         (tvp5150_read(sd, TVP5150_REV_SELECT) & 1) ? 3 : 4);
0205     dprintk0(sd->dev, "tvp5150: Device ID = %02x%02x\n",
0206         tvp5150_read(sd, TVP5150_MSB_DEV_ID),
0207         tvp5150_read(sd, TVP5150_LSB_DEV_ID));
0208     dprintk0(sd->dev, "tvp5150: ROM version = (hex) %02x.%02x\n",
0209         tvp5150_read(sd, TVP5150_ROM_MAJOR_VER),
0210         tvp5150_read(sd, TVP5150_ROM_MINOR_VER));
0211     dprintk0(sd->dev, "tvp5150: Vertical line count = 0x%02x%02x\n",
0212         tvp5150_read(sd, TVP5150_VERT_LN_COUNT_MSB),
0213         tvp5150_read(sd, TVP5150_VERT_LN_COUNT_LSB));
0214     dprintk0(sd->dev, "tvp5150: Interrupt status register B = 0x%02x\n",
0215         tvp5150_read(sd, TVP5150_INT_STATUS_REG_B));
0216     dprintk0(sd->dev, "tvp5150: Interrupt active register B = 0x%02x\n",
0217         tvp5150_read(sd, TVP5150_INT_ACTIVE_REG_B));
0218     dprintk0(sd->dev, "tvp5150: Status regs #1 to #5 = %02x %02x %02x %02x %02x\n",
0219         tvp5150_read(sd, TVP5150_STATUS_REG_1),
0220         tvp5150_read(sd, TVP5150_STATUS_REG_2),
0221         tvp5150_read(sd, TVP5150_STATUS_REG_3),
0222         tvp5150_read(sd, TVP5150_STATUS_REG_4),
0223         tvp5150_read(sd, TVP5150_STATUS_REG_5));
0224 
0225     dump_reg_range(sd, "Teletext filter 1",   TVP5150_TELETEXT_FIL1_INI,
0226             TVP5150_TELETEXT_FIL1_END, 8);
0227     dump_reg_range(sd, "Teletext filter 2",   TVP5150_TELETEXT_FIL2_INI,
0228             TVP5150_TELETEXT_FIL2_END, 8);
0229 
0230     dprintk0(sd->dev, "tvp5150: Teletext filter enable = 0x%02x\n",
0231         tvp5150_read(sd, TVP5150_TELETEXT_FIL_ENA));
0232     dprintk0(sd->dev, "tvp5150: Interrupt status register A = 0x%02x\n",
0233         tvp5150_read(sd, TVP5150_INT_STATUS_REG_A));
0234     dprintk0(sd->dev, "tvp5150: Interrupt enable register A = 0x%02x\n",
0235         tvp5150_read(sd, TVP5150_INT_ENABLE_REG_A));
0236     dprintk0(sd->dev, "tvp5150: Interrupt configuration = 0x%02x\n",
0237         tvp5150_read(sd, TVP5150_INT_CONF));
0238     dprintk0(sd->dev, "tvp5150: VDP status register = 0x%02x\n",
0239         tvp5150_read(sd, TVP5150_VDP_STATUS_REG));
0240     dprintk0(sd->dev, "tvp5150: FIFO word count = 0x%02x\n",
0241         tvp5150_read(sd, TVP5150_FIFO_WORD_COUNT));
0242     dprintk0(sd->dev, "tvp5150: FIFO interrupt threshold = 0x%02x\n",
0243         tvp5150_read(sd, TVP5150_FIFO_INT_THRESHOLD));
0244     dprintk0(sd->dev, "tvp5150: FIFO reset = 0x%02x\n",
0245         tvp5150_read(sd, TVP5150_FIFO_RESET));
0246     dprintk0(sd->dev, "tvp5150: Line number interrupt = 0x%02x\n",
0247         tvp5150_read(sd, TVP5150_LINE_NUMBER_INT));
0248     dprintk0(sd->dev, "tvp5150: Pixel alignment register = 0x%02x%02x\n",
0249         tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_HIGH),
0250         tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_LOW));
0251     dprintk0(sd->dev, "tvp5150: FIFO output control = 0x%02x\n",
0252         tvp5150_read(sd, TVP5150_FIFO_OUT_CTRL));
0253     dprintk0(sd->dev, "tvp5150: Full field enable = 0x%02x\n",
0254         tvp5150_read(sd, TVP5150_FULL_FIELD_ENA));
0255     dprintk0(sd->dev, "tvp5150: Full field mode register = 0x%02x\n",
0256         tvp5150_read(sd, TVP5150_FULL_FIELD_MODE_REG));
0257 
0258     dump_reg_range(sd, "CC   data",   TVP5150_CC_DATA_INI,
0259             TVP5150_CC_DATA_END, 8);
0260 
0261     dump_reg_range(sd, "WSS  data",   TVP5150_WSS_DATA_INI,
0262             TVP5150_WSS_DATA_END, 8);
0263 
0264     dump_reg_range(sd, "VPS  data",   TVP5150_VPS_DATA_INI,
0265             TVP5150_VPS_DATA_END, 8);
0266 
0267     dump_reg_range(sd, "VITC data",   TVP5150_VITC_DATA_INI,
0268             TVP5150_VITC_DATA_END, 10);
0269 
0270     dump_reg_range(sd, "Line mode",   TVP5150_LINE_MODE_INI,
0271             TVP5150_LINE_MODE_END, 8);
0272     return 0;
0273 }
0274 
0275 /****************************************************************************
0276             Basic functions
0277  ****************************************************************************/
0278 
0279 static void tvp5150_selmux(struct v4l2_subdev *sd)
0280 {
0281     int opmode = 0;
0282     struct tvp5150 *decoder = to_tvp5150(sd);
0283     unsigned int mask, val;
0284     int input = 0;
0285 
0286     /* Only tvp5150am1 and tvp5151 have signal generator support */
0287     if ((decoder->dev_id == 0x5150 && decoder->rom_ver == 0x0400) ||
0288         (decoder->dev_id == 0x5151 && decoder->rom_ver == 0x0100)) {
0289         if (!decoder->enable)
0290             input = 8;
0291     }
0292 
0293     switch (decoder->input) {
0294     case TVP5150_COMPOSITE1:
0295         input |= 2;
0296         fallthrough;
0297     case TVP5150_COMPOSITE0:
0298         break;
0299     case TVP5150_SVIDEO:
0300     default:
0301         input |= 1;
0302         break;
0303     }
0304 
0305     dev_dbg_lvl(sd->dev, 1, debug,
0306             "Selecting video route: route input=%s, output=%s => tvp5150 input=0x%02x, opmode=0x%02x\n",
0307             decoder->input == 0 ? "aip1a" :
0308             decoder->input == 2 ? "aip1b" : "svideo",
0309             decoder->output == 0 ? "normal" : "black-frame-gen",
0310             input, opmode);
0311 
0312     regmap_write(decoder->regmap, TVP5150_OP_MODE_CTL, opmode);
0313     regmap_write(decoder->regmap, TVP5150_VD_IN_SRC_SEL_1, input);
0314 
0315     /*
0316      * Setup the FID/GLCO/VLK/HVLK and INTREQ/GPCL/VBLK output signals. For
0317      * S-Video we output the vertical lock (VLK) signal on FID/GLCO/VLK/HVLK
0318      * and set INTREQ/GPCL/VBLK to logic 0. For composite we output the
0319      * field indicator (FID) signal on FID/GLCO/VLK/HVLK and set
0320      * INTREQ/GPCL/VBLK to logic 1.
0321      */
0322     mask = TVP5150_MISC_CTL_GPCL | TVP5150_MISC_CTL_HVLK;
0323     if (decoder->input == TVP5150_SVIDEO)
0324         val = TVP5150_MISC_CTL_HVLK;
0325     else
0326         val = TVP5150_MISC_CTL_GPCL;
0327     regmap_update_bits(decoder->regmap, TVP5150_MISC_CTL, mask, val);
0328 };
0329 
0330 struct i2c_reg_value {
0331     unsigned char reg;
0332     unsigned char value;
0333 };
0334 
0335 /* Default values as sugested at TVP5150AM1 datasheet */
0336 static const struct i2c_reg_value tvp5150_init_default[] = {
0337     { /* 0x00 */
0338         TVP5150_VD_IN_SRC_SEL_1, 0x00
0339     },
0340     { /* 0x01 */
0341         TVP5150_ANAL_CHL_CTL, 0x15
0342     },
0343     { /* 0x02 */
0344         TVP5150_OP_MODE_CTL, 0x00
0345     },
0346     { /* 0x03 */
0347         TVP5150_MISC_CTL, 0x01
0348     },
0349     { /* 0x06 */
0350         TVP5150_COLOR_KIL_THSH_CTL, 0x10
0351     },
0352     { /* 0x07 */
0353         TVP5150_LUMA_PROC_CTL_1, 0x60
0354     },
0355     { /* 0x08 */
0356         TVP5150_LUMA_PROC_CTL_2, 0x00
0357     },
0358     { /* 0x09 */
0359         TVP5150_BRIGHT_CTL, 0x80
0360     },
0361     { /* 0x0a */
0362         TVP5150_SATURATION_CTL, 0x80
0363     },
0364     { /* 0x0b */
0365         TVP5150_HUE_CTL, 0x00
0366     },
0367     { /* 0x0c */
0368         TVP5150_CONTRAST_CTL, 0x80
0369     },
0370     { /* 0x0d */
0371         TVP5150_DATA_RATE_SEL, 0x47
0372     },
0373     { /* 0x0e */
0374         TVP5150_LUMA_PROC_CTL_3, 0x00
0375     },
0376     { /* 0x0f */
0377         TVP5150_CONF_SHARED_PIN, 0x08
0378     },
0379     { /* 0x11 */
0380         TVP5150_ACT_VD_CROP_ST_MSB, 0x00
0381     },
0382     { /* 0x12 */
0383         TVP5150_ACT_VD_CROP_ST_LSB, 0x00
0384     },
0385     { /* 0x13 */
0386         TVP5150_ACT_VD_CROP_STP_MSB, 0x00
0387     },
0388     { /* 0x14 */
0389         TVP5150_ACT_VD_CROP_STP_LSB, 0x00
0390     },
0391     { /* 0x15 */
0392         TVP5150_GENLOCK, 0x01
0393     },
0394     { /* 0x16 */
0395         TVP5150_HORIZ_SYNC_START, 0x80
0396     },
0397     { /* 0x18 */
0398         TVP5150_VERT_BLANKING_START, 0x00
0399     },
0400     { /* 0x19 */
0401         TVP5150_VERT_BLANKING_STOP, 0x00
0402     },
0403     { /* 0x1a */
0404         TVP5150_CHROMA_PROC_CTL_1, 0x0c
0405     },
0406     { /* 0x1b */
0407         TVP5150_CHROMA_PROC_CTL_2, 0x14
0408     },
0409     { /* 0x1c */
0410         TVP5150_INT_RESET_REG_B, 0x00
0411     },
0412     { /* 0x1d */
0413         TVP5150_INT_ENABLE_REG_B, 0x00
0414     },
0415     { /* 0x1e */
0416         TVP5150_INTT_CONFIG_REG_B, 0x00
0417     },
0418     { /* 0x28 */
0419         TVP5150_VIDEO_STD, 0x00
0420     },
0421     { /* 0x2e */
0422         TVP5150_MACROVISION_ON_CTR, 0x0f
0423     },
0424     { /* 0x2f */
0425         TVP5150_MACROVISION_OFF_CTR, 0x01
0426     },
0427     { /* 0xbb */
0428         TVP5150_TELETEXT_FIL_ENA, 0x00
0429     },
0430     { /* 0xc0 */
0431         TVP5150_INT_STATUS_REG_A, 0x00
0432     },
0433     { /* 0xc1 */
0434         TVP5150_INT_ENABLE_REG_A, 0x00
0435     },
0436     { /* 0xc2 */
0437         TVP5150_INT_CONF, 0x04
0438     },
0439     { /* 0xc8 */
0440         TVP5150_FIFO_INT_THRESHOLD, 0x80
0441     },
0442     { /* 0xc9 */
0443         TVP5150_FIFO_RESET, 0x00
0444     },
0445     { /* 0xca */
0446         TVP5150_LINE_NUMBER_INT, 0x00
0447     },
0448     { /* 0xcb */
0449         TVP5150_PIX_ALIGN_REG_LOW, 0x4e
0450     },
0451     { /* 0xcc */
0452         TVP5150_PIX_ALIGN_REG_HIGH, 0x00
0453     },
0454     { /* 0xcd */
0455         TVP5150_FIFO_OUT_CTRL, 0x01
0456     },
0457     { /* 0xcf */
0458         TVP5150_FULL_FIELD_ENA, 0x00
0459     },
0460     { /* 0xd0 */
0461         TVP5150_LINE_MODE_INI, 0x00
0462     },
0463     { /* 0xfc */
0464         TVP5150_FULL_FIELD_MODE_REG, 0x7f
0465     },
0466     { /* end of data */
0467         0xff, 0xff
0468     }
0469 };
0470 
0471 /* Default values as sugested at TVP5150AM1 datasheet */
0472 static const struct i2c_reg_value tvp5150_init_enable[] = {
0473     {   /* Automatic offset and AGC enabled */
0474         TVP5150_ANAL_CHL_CTL, 0x15
0475     }, {    /* Activate YCrCb output 0x9 or 0xd ? */
0476         TVP5150_MISC_CTL, TVP5150_MISC_CTL_GPCL |
0477                   TVP5150_MISC_CTL_INTREQ_OE |
0478                   TVP5150_MISC_CTL_YCBCR_OE |
0479                   TVP5150_MISC_CTL_SYNC_OE |
0480                   TVP5150_MISC_CTL_VBLANK |
0481                   TVP5150_MISC_CTL_CLOCK_OE,
0482     }, {    /* Activates video std autodetection for all standards */
0483         TVP5150_AUTOSW_MSK, 0x0
0484     }, {    /* Default format: 0x47. For 4:2:2: 0x40 */
0485         TVP5150_DATA_RATE_SEL, 0x47
0486     }, {
0487         TVP5150_CHROMA_PROC_CTL_1, 0x0c
0488     }, {
0489         TVP5150_CHROMA_PROC_CTL_2, 0x54
0490     }, {    /* Non documented, but initialized on WinTV USB2 */
0491         0x27, 0x20
0492     }, {
0493         0xff, 0xff
0494     }
0495 };
0496 
0497 struct tvp5150_vbi_type {
0498     unsigned int vbi_type;
0499     unsigned int ini_line;
0500     unsigned int end_line;
0501     unsigned int by_field :1;
0502 };
0503 
0504 struct i2c_vbi_ram_value {
0505     u16 reg;
0506     struct tvp5150_vbi_type type;
0507     unsigned char values[16];
0508 };
0509 
0510 /* This struct have the values for each supported VBI Standard
0511  * by
0512  tvp5150_vbi_types should follow the same order as vbi_ram_default
0513  * value 0 means rom position 0x10, value 1 means rom position 0x30
0514  * and so on. There are 16 possible locations from 0 to 15.
0515  */
0516 
0517 static struct i2c_vbi_ram_value vbi_ram_default[] = {
0518 
0519     /*
0520      * FIXME: Current api doesn't handle all VBI types, those not
0521      * yet supported are placed under #if 0
0522      */
0523 #if 0
0524     [0] = {0x010, /* Teletext, SECAM, WST System A */
0525         {V4L2_SLICED_TELETEXT_SECAM, 6, 23, 1},
0526         { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x26,
0527           0xe6, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00 }
0528     },
0529 #endif
0530     [1] = {0x030, /* Teletext, PAL, WST System B */
0531         {V4L2_SLICED_TELETEXT_B, 6, 22, 1},
0532         { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x2b,
0533           0xa6, 0x72, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00 }
0534     },
0535 #if 0
0536     [2] = {0x050, /* Teletext, PAL, WST System C */
0537         {V4L2_SLICED_TELETEXT_PAL_C, 6, 22, 1},
0538         { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
0539           0xa6, 0x98, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
0540     },
0541     [3] = {0x070, /* Teletext, NTSC, WST System B */
0542         {V4L2_SLICED_TELETEXT_NTSC_B, 10, 21, 1},
0543         { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x23,
0544           0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
0545     },
0546     [4] = {0x090, /* Tetetext, NTSC NABTS System C */
0547         {V4L2_SLICED_TELETEXT_NTSC_C, 10, 21, 1},
0548         { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
0549           0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x15, 0x00 }
0550     },
0551     [5] = {0x0b0, /* Teletext, NTSC-J, NABTS System D */
0552         {V4L2_SLICED_TELETEXT_NTSC_D, 10, 21, 1},
0553         { 0xaa, 0xaa, 0xff, 0xff, 0xa7, 0x2e, 0x20, 0x23,
0554           0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
0555     },
0556     [6] = {0x0d0, /* Closed Caption, PAL/SECAM */
0557         {V4L2_SLICED_CAPTION_625, 22, 22, 1},
0558         { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
0559           0xa6, 0x7b, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
0560     },
0561 #endif
0562     [7] = {0x0f0, /* Closed Caption, NTSC */
0563         {V4L2_SLICED_CAPTION_525, 21, 21, 1},
0564         { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
0565           0x69, 0x8c, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
0566     },
0567     [8] = {0x110, /* Wide Screen Signal, PAL/SECAM */
0568         {V4L2_SLICED_WSS_625, 23, 23, 1},
0569         { 0x5b, 0x55, 0xc5, 0xff, 0x00, 0x71, 0x6e, 0x42,
0570           0xa6, 0xcd, 0x0f, 0x00, 0x00, 0x00, 0x3a, 0x00 }
0571     },
0572 #if 0
0573     [9] = {0x130, /* Wide Screen Signal, NTSC C */
0574         {V4L2_SLICED_WSS_525, 20, 20, 1},
0575         { 0x38, 0x00, 0x3f, 0x00, 0x00, 0x71, 0x6e, 0x43,
0576           0x69, 0x7c, 0x08, 0x00, 0x00, 0x00, 0x39, 0x00 }
0577     },
0578     [10] = {0x150, /* Vertical Interval Timecode (VITC), PAL/SECAM */
0579         {V4l2_SLICED_VITC_625, 6, 22, 0},
0580         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
0581           0xa6, 0x85, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
0582     },
0583     [11] = {0x170, /* Vertical Interval Timecode (VITC), NTSC */
0584         {V4l2_SLICED_VITC_525, 10, 20, 0},
0585         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
0586           0x69, 0x94, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
0587     },
0588 #endif
0589     [12] = {0x190, /* Video Program System (VPS), PAL */
0590         {V4L2_SLICED_VPS, 16, 16, 0},
0591         { 0xaa, 0xaa, 0xff, 0xff, 0xba, 0xce, 0x2b, 0x0d,
0592           0xa6, 0xda, 0x0b, 0x00, 0x00, 0x00, 0x60, 0x00 }
0593     },
0594     /* 0x1d0 User programmable */
0595 };
0596 
0597 static int tvp5150_write_inittab(struct v4l2_subdev *sd,
0598                 const struct i2c_reg_value *regs)
0599 {
0600     struct tvp5150 *decoder = to_tvp5150(sd);
0601 
0602     while (regs->reg != 0xff) {
0603         regmap_write(decoder->regmap, regs->reg, regs->value);
0604         regs++;
0605     }
0606     return 0;
0607 }
0608 
0609 static int tvp5150_vdp_init(struct v4l2_subdev *sd)
0610 {
0611     struct tvp5150 *decoder = to_tvp5150(sd);
0612     struct regmap *map = decoder->regmap;
0613     unsigned int i;
0614     int j;
0615 
0616     /* Disable Full Field */
0617     regmap_write(map, TVP5150_FULL_FIELD_ENA, 0);
0618 
0619     /* Before programming, Line mode should be at 0xff */
0620     for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
0621         regmap_write(map, i, 0xff);
0622 
0623     /* Load Ram Table */
0624     for (j = 0; j < ARRAY_SIZE(vbi_ram_default); j++) {
0625         const struct i2c_vbi_ram_value *regs = &vbi_ram_default[j];
0626 
0627         if (!regs->type.vbi_type)
0628             continue;
0629 
0630         regmap_write(map, TVP5150_CONF_RAM_ADDR_HIGH, regs->reg >> 8);
0631         regmap_write(map, TVP5150_CONF_RAM_ADDR_LOW, regs->reg);
0632 
0633         for (i = 0; i < 16; i++)
0634             regmap_write(map, TVP5150_VDP_CONF_RAM_DATA,
0635                      regs->values[i]);
0636     }
0637     return 0;
0638 }
0639 
0640 /* Fills VBI capabilities based on i2c_vbi_ram_value struct */
0641 static int tvp5150_g_sliced_vbi_cap(struct v4l2_subdev *sd,
0642                 struct v4l2_sliced_vbi_cap *cap)
0643 {
0644     int line, i;
0645 
0646     dev_dbg_lvl(sd->dev, 1, debug, "g_sliced_vbi_cap\n");
0647     memset(cap, 0, sizeof(*cap));
0648 
0649     for (i = 0; i < ARRAY_SIZE(vbi_ram_default); i++) {
0650         const struct i2c_vbi_ram_value *regs = &vbi_ram_default[i];
0651 
0652         if (!regs->type.vbi_type)
0653             continue;
0654 
0655         for (line = regs->type.ini_line;
0656              line <= regs->type.end_line;
0657              line++) {
0658             cap->service_lines[0][line] |= regs->type.vbi_type;
0659         }
0660         cap->service_set |= regs->type.vbi_type;
0661     }
0662     return 0;
0663 }
0664 
0665 /* Set vbi processing
0666  * type - one of tvp5150_vbi_types
0667  * line - line to gather data
0668  * fields: bit 0 field1, bit 1, field2
0669  * flags (default=0xf0) is a bitmask, were set means:
0670  *  bit 7: enable filtering null bytes on CC
0671  *  bit 6: send data also to FIFO
0672  *  bit 5: don't allow data with errors on FIFO
0673  *  bit 4: enable ECC when possible
0674  * pix_align = pix alignment:
0675  *  LSB = field1
0676  *  MSB = field2
0677  */
0678 static int tvp5150_set_vbi(struct v4l2_subdev *sd,
0679             unsigned int type, u8 flags, int line,
0680             const int fields)
0681 {
0682     struct tvp5150 *decoder = to_tvp5150(sd);
0683     v4l2_std_id std = decoder->norm;
0684     u8 reg;
0685     int i, pos = 0;
0686 
0687     if (std == V4L2_STD_ALL) {
0688         dev_err(sd->dev, "VBI can't be configured without knowing number of lines\n");
0689         return 0;
0690     } else if (std & V4L2_STD_625_50) {
0691         /* Don't follow NTSC Line number convension */
0692         line += 3;
0693     }
0694 
0695     if (line < 6 || line > 27)
0696         return 0;
0697 
0698     for (i = 0; i < ARRAY_SIZE(vbi_ram_default); i++) {
0699         const struct i2c_vbi_ram_value *regs =  &vbi_ram_default[i];
0700 
0701         if (!regs->type.vbi_type)
0702             continue;
0703 
0704         if ((type & regs->type.vbi_type) &&
0705             (line >= regs->type.ini_line) &&
0706             (line <= regs->type.end_line))
0707             break;
0708         pos++;
0709     }
0710 
0711     type = pos | (flags & 0xf0);
0712     reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI;
0713 
0714     if (fields & 1)
0715         regmap_write(decoder->regmap, reg, type);
0716 
0717     if (fields & 2)
0718         regmap_write(decoder->regmap, reg + 1, type);
0719 
0720     return type;
0721 }
0722 
0723 static int tvp5150_get_vbi(struct v4l2_subdev *sd, int line)
0724 {
0725     struct tvp5150 *decoder = to_tvp5150(sd);
0726     v4l2_std_id std = decoder->norm;
0727     u8 reg;
0728     int pos, type = 0;
0729     int i, ret = 0;
0730 
0731     if (std == V4L2_STD_ALL) {
0732         dev_err(sd->dev, "VBI can't be configured without knowing number of lines\n");
0733         return 0;
0734     } else if (std & V4L2_STD_625_50) {
0735         /* Don't follow NTSC Line number convension */
0736         line += 3;
0737     }
0738 
0739     if (line < 6 || line > 27)
0740         return 0;
0741 
0742     reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI;
0743 
0744     for (i = 0; i <= 1; i++) {
0745         ret = tvp5150_read(sd, reg + i);
0746         if (ret < 0) {
0747             dev_err(sd->dev, "%s: failed with error = %d\n",
0748                  __func__, ret);
0749             return 0;
0750         }
0751         pos = ret & 0x0f;
0752         if (pos < ARRAY_SIZE(vbi_ram_default))
0753             type |= vbi_ram_default[pos].type.vbi_type;
0754     }
0755 
0756     return type;
0757 }
0758 
0759 static int tvp5150_set_std(struct v4l2_subdev *sd, v4l2_std_id std)
0760 {
0761     struct tvp5150 *decoder = to_tvp5150(sd);
0762     int fmt = 0;
0763 
0764     /* First tests should be against specific std */
0765 
0766     if (std == V4L2_STD_NTSC_443) {
0767         fmt = VIDEO_STD_NTSC_4_43_BIT;
0768     } else if (std == V4L2_STD_PAL_M) {
0769         fmt = VIDEO_STD_PAL_M_BIT;
0770     } else if (std == V4L2_STD_PAL_N || std == V4L2_STD_PAL_Nc) {
0771         fmt = VIDEO_STD_PAL_COMBINATION_N_BIT;
0772     } else {
0773         /* Then, test against generic ones */
0774         if (std & V4L2_STD_NTSC)
0775             fmt = VIDEO_STD_NTSC_MJ_BIT;
0776         else if (std & V4L2_STD_PAL)
0777             fmt = VIDEO_STD_PAL_BDGHIN_BIT;
0778         else if (std & V4L2_STD_SECAM)
0779             fmt = VIDEO_STD_SECAM_BIT;
0780     }
0781 
0782     dev_dbg_lvl(sd->dev, 1, debug, "Set video std register to %d.\n", fmt);
0783     regmap_write(decoder->regmap, TVP5150_VIDEO_STD, fmt);
0784     return 0;
0785 }
0786 
0787 static int tvp5150_g_std(struct v4l2_subdev *sd, v4l2_std_id *std)
0788 {
0789     struct tvp5150 *decoder = to_tvp5150(sd);
0790 
0791     *std = decoder->norm;
0792 
0793     return 0;
0794 }
0795 
0796 static int tvp5150_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
0797 {
0798     struct tvp5150 *decoder = to_tvp5150(sd);
0799     struct tvp5150_connector *cur_con = decoder->cur_connector;
0800     v4l2_std_id supported_stds;
0801 
0802     if (decoder->norm == std)
0803         return 0;
0804 
0805     /* In case of no of-connectors are available no limitations are made */
0806     if (!decoder->connectors_num)
0807         supported_stds = V4L2_STD_ALL;
0808     else
0809         supported_stds = cur_con->base.connector.analog.sdtv_stds;
0810 
0811     /*
0812      * Check if requested std or group of std's is/are supported by the
0813      * connector.
0814      */
0815     if ((supported_stds & std) == 0)
0816         return -EINVAL;
0817 
0818     /* Change cropping height limits */
0819     if (std & V4L2_STD_525_60)
0820         decoder->rect.height = TVP5150_V_MAX_525_60;
0821     else
0822         decoder->rect.height = TVP5150_V_MAX_OTHERS;
0823 
0824     /* Set only the specific supported std in case of group of std's. */
0825     decoder->norm = supported_stds & std;
0826 
0827     return tvp5150_set_std(sd, std);
0828 }
0829 
0830 static v4l2_std_id tvp5150_read_std(struct v4l2_subdev *sd)
0831 {
0832     int val = tvp5150_read(sd, TVP5150_STATUS_REG_5);
0833 
0834     switch (val & 0x0F) {
0835     case 0x01:
0836         return V4L2_STD_NTSC;
0837     case 0x03:
0838         return V4L2_STD_PAL;
0839     case 0x05:
0840         return V4L2_STD_PAL_M;
0841     case 0x07:
0842         return V4L2_STD_PAL_N | V4L2_STD_PAL_Nc;
0843     case 0x09:
0844         return V4L2_STD_NTSC_443;
0845     case 0xb:
0846         return V4L2_STD_SECAM;
0847     default:
0848         return V4L2_STD_UNKNOWN;
0849     }
0850 }
0851 
0852 static int query_lock(struct v4l2_subdev *sd)
0853 {
0854     struct tvp5150 *decoder = to_tvp5150(sd);
0855     int status;
0856 
0857     if (decoder->irq)
0858         return decoder->lock;
0859 
0860     regmap_read(decoder->regmap, TVP5150_STATUS_REG_1, &status);
0861 
0862     /* For standard detection, we need the 3 locks */
0863     return (status & 0x0e) == 0x0e;
0864 }
0865 
0866 static int tvp5150_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
0867 {
0868     *std_id = query_lock(sd) ? tvp5150_read_std(sd) : V4L2_STD_UNKNOWN;
0869 
0870     return 0;
0871 }
0872 
0873 static const struct v4l2_event tvp5150_ev_fmt = {
0874     .type = V4L2_EVENT_SOURCE_CHANGE,
0875     .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
0876 };
0877 
0878 static irqreturn_t tvp5150_isr(int irq, void *dev_id)
0879 {
0880     struct tvp5150 *decoder = dev_id;
0881     struct regmap *map = decoder->regmap;
0882     unsigned int mask, active = 0, status = 0;
0883 
0884     mask = TVP5150_MISC_CTL_YCBCR_OE | TVP5150_MISC_CTL_SYNC_OE |
0885            TVP5150_MISC_CTL_CLOCK_OE;
0886 
0887     regmap_read(map, TVP5150_INT_STATUS_REG_A, &status);
0888     if (status) {
0889         regmap_write(map, TVP5150_INT_STATUS_REG_A, status);
0890 
0891         if (status & TVP5150_INT_A_LOCK) {
0892             decoder->lock = !!(status & TVP5150_INT_A_LOCK_STATUS);
0893             dev_dbg_lvl(decoder->sd.dev, 1, debug,
0894                     "sync lo%s signal\n",
0895                     decoder->lock ? "ck" : "ss");
0896             v4l2_subdev_notify_event(&decoder->sd, &tvp5150_ev_fmt);
0897             regmap_update_bits(map, TVP5150_MISC_CTL, mask,
0898                        decoder->lock ? decoder->oe : 0);
0899         }
0900 
0901         return IRQ_HANDLED;
0902     }
0903 
0904     regmap_read(map, TVP5150_INT_ACTIVE_REG_B, &active);
0905     if (active) {
0906         status = 0;
0907         regmap_read(map, TVP5150_INT_STATUS_REG_B, &status);
0908         if (status)
0909             regmap_write(map, TVP5150_INT_RESET_REG_B, status);
0910     }
0911 
0912     return IRQ_HANDLED;
0913 }
0914 
0915 static int tvp5150_reset(struct v4l2_subdev *sd, u32 val)
0916 {
0917     struct tvp5150 *decoder = to_tvp5150(sd);
0918     struct regmap *map = decoder->regmap;
0919 
0920     /* Initializes TVP5150 to its default values */
0921     tvp5150_write_inittab(sd, tvp5150_init_default);
0922 
0923     if (decoder->irq) {
0924         /* Configure pins: FID, VSYNC, INTREQ, SCLK */
0925         regmap_write(map, TVP5150_CONF_SHARED_PIN, 0x0);
0926         /* Set interrupt polarity to active high */
0927         regmap_write(map, TVP5150_INT_CONF, TVP5150_VDPOE | 0x1);
0928         regmap_write(map, TVP5150_INTT_CONFIG_REG_B, 0x1);
0929     } else {
0930         /* Configure pins: FID, VSYNC, GPCL/VBLK, SCLK */
0931         regmap_write(map, TVP5150_CONF_SHARED_PIN, 0x2);
0932         /* Keep interrupt polarity active low */
0933         regmap_write(map, TVP5150_INT_CONF, TVP5150_VDPOE);
0934         regmap_write(map, TVP5150_INTT_CONFIG_REG_B, 0x0);
0935     }
0936 
0937     /* Initializes VDP registers */
0938     tvp5150_vdp_init(sd);
0939 
0940     /* Selects decoder input */
0941     tvp5150_selmux(sd);
0942 
0943     /* Initialize image preferences */
0944     v4l2_ctrl_handler_setup(&decoder->hdl);
0945 
0946     return 0;
0947 }
0948 
0949 static int tvp5150_enable(struct v4l2_subdev *sd)
0950 {
0951     struct tvp5150 *decoder = to_tvp5150(sd);
0952     v4l2_std_id std;
0953 
0954     /* Initializes TVP5150 to stream enabled values */
0955     tvp5150_write_inittab(sd, tvp5150_init_enable);
0956 
0957     if (decoder->norm == V4L2_STD_ALL)
0958         std = tvp5150_read_std(sd);
0959     else
0960         std = decoder->norm;
0961 
0962     /* Disable autoswitch mode */
0963     tvp5150_set_std(sd, std);
0964 
0965     /*
0966      * Enable the YCbCr and clock outputs. In discrete sync mode
0967      * (non-BT.656) additionally enable the sync outputs.
0968      */
0969     switch (decoder->mbus_type) {
0970     case V4L2_MBUS_PARALLEL:
0971         /* 8-bit 4:2:2 YUV with discrete sync output */
0972         regmap_update_bits(decoder->regmap, TVP5150_DATA_RATE_SEL,
0973                    0x7, 0x0);
0974         decoder->oe = TVP5150_MISC_CTL_YCBCR_OE |
0975                   TVP5150_MISC_CTL_CLOCK_OE |
0976                   TVP5150_MISC_CTL_SYNC_OE;
0977         break;
0978     case V4L2_MBUS_BT656:
0979         decoder->oe = TVP5150_MISC_CTL_YCBCR_OE |
0980                   TVP5150_MISC_CTL_CLOCK_OE;
0981         break;
0982     default:
0983         return -EINVAL;
0984     }
0985 
0986     return 0;
0987 };
0988 
0989 static int tvp5150_s_ctrl(struct v4l2_ctrl *ctrl)
0990 {
0991     struct v4l2_subdev *sd = to_sd(ctrl);
0992     struct tvp5150 *decoder = to_tvp5150(sd);
0993 
0994     switch (ctrl->id) {
0995     case V4L2_CID_BRIGHTNESS:
0996         regmap_write(decoder->regmap, TVP5150_BRIGHT_CTL, ctrl->val);
0997         return 0;
0998     case V4L2_CID_CONTRAST:
0999         regmap_write(decoder->regmap, TVP5150_CONTRAST_CTL, ctrl->val);
1000         return 0;
1001     case V4L2_CID_SATURATION:
1002         regmap_write(decoder->regmap, TVP5150_SATURATION_CTL,
1003                  ctrl->val);
1004         return 0;
1005     case V4L2_CID_HUE:
1006         regmap_write(decoder->regmap, TVP5150_HUE_CTL, ctrl->val);
1007         return 0;
1008     case V4L2_CID_TEST_PATTERN:
1009         decoder->enable = ctrl->val ? false : true;
1010         tvp5150_selmux(sd);
1011         return 0;
1012     }
1013     return -EINVAL;
1014 }
1015 
1016 static void tvp5150_set_default(v4l2_std_id std, struct v4l2_rect *crop)
1017 {
1018     /* Default is no cropping */
1019     crop->top = 0;
1020     crop->left = 0;
1021     crop->width = TVP5150_H_MAX;
1022     if (std & V4L2_STD_525_60)
1023         crop->height = TVP5150_V_MAX_525_60;
1024     else
1025         crop->height = TVP5150_V_MAX_OTHERS;
1026 }
1027 
1028 static struct v4l2_rect *
1029 tvp5150_get_pad_crop(struct tvp5150 *decoder,
1030              struct v4l2_subdev_state *sd_state, unsigned int pad,
1031              enum v4l2_subdev_format_whence which)
1032 {
1033     switch (which) {
1034     case V4L2_SUBDEV_FORMAT_ACTIVE:
1035         return &decoder->rect;
1036     case V4L2_SUBDEV_FORMAT_TRY:
1037 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1038         return v4l2_subdev_get_try_crop(&decoder->sd, sd_state, pad);
1039 #else
1040         return ERR_PTR(-EINVAL);
1041 #endif
1042     default:
1043         return ERR_PTR(-EINVAL);
1044     }
1045 }
1046 
1047 static int tvp5150_fill_fmt(struct v4l2_subdev *sd,
1048                 struct v4l2_subdev_state *sd_state,
1049                 struct v4l2_subdev_format *format)
1050 {
1051     struct v4l2_mbus_framefmt *f;
1052     struct tvp5150 *decoder = to_tvp5150(sd);
1053 
1054     if (!format || (format->pad != TVP5150_PAD_VID_OUT))
1055         return -EINVAL;
1056 
1057     f = &format->format;
1058 
1059     f->width = decoder->rect.width;
1060     f->height = decoder->rect.height / 2;
1061 
1062     f->code = TVP5150_MBUS_FMT;
1063     f->field = TVP5150_FIELD;
1064     f->colorspace = TVP5150_COLORSPACE;
1065 
1066     dev_dbg_lvl(sd->dev, 1, debug, "width = %d, height = %d\n", f->width,
1067             f->height);
1068     return 0;
1069 }
1070 
1071 static unsigned int tvp5150_get_hmax(struct v4l2_subdev *sd)
1072 {
1073     struct tvp5150 *decoder = to_tvp5150(sd);
1074     v4l2_std_id std;
1075 
1076     /* Calculate height based on current standard */
1077     if (decoder->norm == V4L2_STD_ALL)
1078         std = tvp5150_read_std(sd);
1079     else
1080         std = decoder->norm;
1081 
1082     return (std & V4L2_STD_525_60) ?
1083         TVP5150_V_MAX_525_60 : TVP5150_V_MAX_OTHERS;
1084 }
1085 
1086 static void tvp5150_set_hw_selection(struct v4l2_subdev *sd,
1087                      struct v4l2_rect *rect)
1088 {
1089     struct tvp5150 *decoder = to_tvp5150(sd);
1090     unsigned int hmax = tvp5150_get_hmax(sd);
1091 
1092     regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_START, rect->top);
1093     regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_STOP,
1094              rect->top + rect->height - hmax);
1095     regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_ST_MSB,
1096              rect->left >> TVP5150_CROP_SHIFT);
1097     regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_ST_LSB,
1098              rect->left | (1 << TVP5150_CROP_SHIFT));
1099     regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_STP_MSB,
1100              (rect->left + rect->width - TVP5150_MAX_CROP_LEFT) >>
1101              TVP5150_CROP_SHIFT);
1102     regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_STP_LSB,
1103              rect->left + rect->width - TVP5150_MAX_CROP_LEFT);
1104 }
1105 
1106 static int tvp5150_set_selection(struct v4l2_subdev *sd,
1107                  struct v4l2_subdev_state *sd_state,
1108                  struct v4l2_subdev_selection *sel)
1109 {
1110     struct tvp5150 *decoder = to_tvp5150(sd);
1111     struct v4l2_rect *rect = &sel->r;
1112     struct v4l2_rect *crop;
1113     unsigned int hmax;
1114 
1115     if (sel->target != V4L2_SEL_TGT_CROP)
1116         return -EINVAL;
1117 
1118     dev_dbg_lvl(sd->dev, 1, debug, "%s left=%d, top=%d, width=%d, height=%d\n",
1119         __func__, rect->left, rect->top, rect->width, rect->height);
1120 
1121     /* tvp5150 has some special limits */
1122     rect->left = clamp(rect->left, 0, TVP5150_MAX_CROP_LEFT);
1123     rect->top = clamp(rect->top, 0, TVP5150_MAX_CROP_TOP);
1124     hmax = tvp5150_get_hmax(sd);
1125 
1126     /*
1127      * alignments:
1128      *  - width = 2 due to UYVY colorspace
1129      *  - height, image = no special alignment
1130      */
1131     v4l_bound_align_image(&rect->width,
1132                   TVP5150_H_MAX - TVP5150_MAX_CROP_LEFT - rect->left,
1133                   TVP5150_H_MAX - rect->left, 1, &rect->height,
1134                   hmax - TVP5150_MAX_CROP_TOP - rect->top,
1135                   hmax - rect->top, 0, 0);
1136 
1137     if (!IS_ENABLED(CONFIG_VIDEO_V4L2_SUBDEV_API) &&
1138         sel->which == V4L2_SUBDEV_FORMAT_TRY)
1139         return 0;
1140 
1141     crop = tvp5150_get_pad_crop(decoder, sd_state, sel->pad, sel->which);
1142     if (IS_ERR(crop))
1143         return PTR_ERR(crop);
1144 
1145     /*
1146      * Update output image size if the selection (crop) rectangle size or
1147      * position has been modified.
1148      */
1149     if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE &&
1150         !v4l2_rect_equal(rect, crop))
1151         tvp5150_set_hw_selection(sd, rect);
1152 
1153     *crop = *rect;
1154 
1155     return 0;
1156 }
1157 
1158 static int tvp5150_get_selection(struct v4l2_subdev *sd,
1159                  struct v4l2_subdev_state *sd_state,
1160                  struct v4l2_subdev_selection *sel)
1161 {
1162     struct tvp5150 *decoder = container_of(sd, struct tvp5150, sd);
1163     struct v4l2_rect *crop;
1164     v4l2_std_id std;
1165 
1166     switch (sel->target) {
1167     case V4L2_SEL_TGT_CROP_BOUNDS:
1168         sel->r.left = 0;
1169         sel->r.top = 0;
1170         sel->r.width = TVP5150_H_MAX;
1171 
1172         /* Calculate height based on current standard */
1173         if (decoder->norm == V4L2_STD_ALL)
1174             std = tvp5150_read_std(sd);
1175         else
1176             std = decoder->norm;
1177         if (std & V4L2_STD_525_60)
1178             sel->r.height = TVP5150_V_MAX_525_60;
1179         else
1180             sel->r.height = TVP5150_V_MAX_OTHERS;
1181         return 0;
1182     case V4L2_SEL_TGT_CROP:
1183         crop = tvp5150_get_pad_crop(decoder, sd_state, sel->pad,
1184                         sel->which);
1185         if (IS_ERR(crop))
1186             return PTR_ERR(crop);
1187         sel->r = *crop;
1188         return 0;
1189     default:
1190         return -EINVAL;
1191     }
1192 }
1193 
1194 static int tvp5150_get_mbus_config(struct v4l2_subdev *sd,
1195                    unsigned int pad,
1196                    struct v4l2_mbus_config *cfg)
1197 {
1198     struct tvp5150 *decoder = to_tvp5150(sd);
1199 
1200     cfg->type = decoder->mbus_type;
1201     cfg->bus.parallel.flags = V4L2_MBUS_MASTER
1202                 | V4L2_MBUS_PCLK_SAMPLE_RISING
1203                 | V4L2_MBUS_FIELD_EVEN_LOW
1204                 | V4L2_MBUS_DATA_ACTIVE_HIGH;
1205 
1206     return 0;
1207 }
1208 
1209 /****************************************************************************
1210             V4L2 subdev pad ops
1211  ****************************************************************************/
1212 static int tvp5150_init_cfg(struct v4l2_subdev *sd,
1213                 struct v4l2_subdev_state *sd_state)
1214 {
1215     struct tvp5150 *decoder = to_tvp5150(sd);
1216     v4l2_std_id std;
1217 
1218     /*
1219      * Reset selection to maximum on subdev_open() if autodetection is on
1220      * and a standard change is detected.
1221      */
1222     if (decoder->norm == V4L2_STD_ALL) {
1223         std = tvp5150_read_std(sd);
1224         if (std != decoder->detected_norm) {
1225             decoder->detected_norm = std;
1226             tvp5150_set_default(std, &decoder->rect);
1227         }
1228     }
1229 
1230     return 0;
1231 }
1232 
1233 static int tvp5150_enum_mbus_code(struct v4l2_subdev *sd,
1234         struct v4l2_subdev_state *sd_state,
1235         struct v4l2_subdev_mbus_code_enum *code)
1236 {
1237     if (code->pad || code->index)
1238         return -EINVAL;
1239 
1240     code->code = TVP5150_MBUS_FMT;
1241     return 0;
1242 }
1243 
1244 static int tvp5150_enum_frame_size(struct v4l2_subdev *sd,
1245                    struct v4l2_subdev_state *sd_state,
1246                    struct v4l2_subdev_frame_size_enum *fse)
1247 {
1248     struct tvp5150 *decoder = to_tvp5150(sd);
1249 
1250     if (fse->index >= 8 || fse->code != TVP5150_MBUS_FMT)
1251         return -EINVAL;
1252 
1253     fse->code = TVP5150_MBUS_FMT;
1254     fse->min_width = decoder->rect.width;
1255     fse->max_width = decoder->rect.width;
1256     fse->min_height = decoder->rect.height / 2;
1257     fse->max_height = decoder->rect.height / 2;
1258 
1259     return 0;
1260 }
1261 
1262 /****************************************************************************
1263  *          Media entity ops
1264  ****************************************************************************/
1265 #if defined(CONFIG_MEDIA_CONTROLLER)
1266 static int tvp5150_set_link(struct media_pad *connector_pad,
1267                 struct media_pad *tvp5150_pad, u32 flags)
1268 {
1269     struct media_link *link;
1270 
1271     link = media_entity_find_link(connector_pad, tvp5150_pad);
1272     if (!link)
1273         return -EINVAL;
1274 
1275     link->flags = flags;
1276     link->reverse->flags = link->flags;
1277 
1278     return 0;
1279 }
1280 
1281 static int tvp5150_disable_all_input_links(struct tvp5150 *decoder)
1282 {
1283     struct media_pad *connector_pad;
1284     unsigned int i;
1285     int err;
1286 
1287     for (i = 0; i < TVP5150_NUM_PADS - 1; i++) {
1288         connector_pad = media_pad_remote_pad_first(&decoder->pads[i]);
1289         if (!connector_pad)
1290             continue;
1291 
1292         err = tvp5150_set_link(connector_pad, &decoder->pads[i], 0);
1293         if (err)
1294             return err;
1295     }
1296 
1297     return 0;
1298 }
1299 
1300 static int tvp5150_s_routing(struct v4l2_subdev *sd, u32 input, u32 output,
1301                  u32 config);
1302 
1303 static int tvp5150_link_setup(struct media_entity *entity,
1304                   const struct media_pad *tvp5150_pad,
1305                   const struct media_pad *remote, u32 flags)
1306 {
1307     struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1308     struct tvp5150 *decoder = to_tvp5150(sd);
1309     struct media_pad *other_tvp5150_pad =
1310         &decoder->pads[tvp5150_pad->index ^ 1];
1311     struct v4l2_fwnode_connector *v4l2c;
1312     bool is_svideo = false;
1313     unsigned int i;
1314     int err;
1315 
1316     /*
1317      * The TVP5150 state is determined by the enabled sink pad link(s).
1318      * Enabling or disabling the source pad link has no effect.
1319      */
1320     if (tvp5150_pad->flags & MEDIA_PAD_FL_SOURCE)
1321         return 0;
1322 
1323     /* Check if the svideo connector should be enabled */
1324     for (i = 0; i < decoder->connectors_num; i++) {
1325         if (remote->entity == &decoder->connectors[i].ent) {
1326             v4l2c = &decoder->connectors[i].base;
1327             is_svideo = v4l2c->type == V4L2_CONN_SVIDEO;
1328             break;
1329         }
1330     }
1331 
1332     dev_dbg_lvl(sd->dev, 1, debug, "link setup '%s':%d->'%s':%d[%d]",
1333             remote->entity->name, remote->index,
1334             tvp5150_pad->entity->name, tvp5150_pad->index,
1335             flags & MEDIA_LNK_FL_ENABLED);
1336     if (is_svideo)
1337         dev_dbg_lvl(sd->dev, 1, debug,
1338                 "link setup '%s':%d->'%s':%d[%d]",
1339                 remote->entity->name, remote->index,
1340                 other_tvp5150_pad->entity->name,
1341                 other_tvp5150_pad->index,
1342                 flags & MEDIA_LNK_FL_ENABLED);
1343 
1344     /*
1345      * The TVP5150 has an internal mux which allows the following setup:
1346      *
1347      * comp-connector1  --\
1348      *             |---> AIP1A
1349      *            /
1350      * svideo-connector -|
1351      *            \
1352      *             |---> AIP1B
1353      * comp-connector2  --/
1354      *
1355      * We can't rely on user space that the current connector gets disabled
1356      * first before enabling the new connector. Disable all active
1357      * connector links to be on the safe side.
1358      */
1359     err = tvp5150_disable_all_input_links(decoder);
1360     if (err)
1361         return err;
1362 
1363     tvp5150_s_routing(sd, is_svideo ? TVP5150_SVIDEO : tvp5150_pad->index,
1364               flags & MEDIA_LNK_FL_ENABLED ? TVP5150_NORMAL :
1365               TVP5150_BLACK_SCREEN, 0);
1366 
1367     if (flags & MEDIA_LNK_FL_ENABLED) {
1368         struct v4l2_fwnode_connector_analog *v4l2ca;
1369         u32 new_norm;
1370 
1371         /*
1372          * S-Video connector is conneted to both ports AIP1A and AIP1B.
1373          * Both links must be enabled in one-shot regardless which link
1374          * the user requests.
1375          */
1376         if (is_svideo) {
1377             err = tvp5150_set_link((struct media_pad *)remote,
1378                            other_tvp5150_pad, flags);
1379             if (err)
1380                 return err;
1381         }
1382 
1383         if (!decoder->connectors_num)
1384             return 0;
1385 
1386         /* Update the current connector */
1387         decoder->cur_connector =
1388             container_of(remote, struct tvp5150_connector, pad);
1389 
1390         /*
1391          * Do nothing if the new connector supports the same tv-norms as
1392          * the old one.
1393          */
1394         v4l2ca = &decoder->cur_connector->base.connector.analog;
1395         new_norm = decoder->norm & v4l2ca->sdtv_stds;
1396         if (decoder->norm == new_norm)
1397             return 0;
1398 
1399         /*
1400          * Fallback to the new connector tv-norms if we can't find any
1401          * common between the current tv-norm and the new one.
1402          */
1403         tvp5150_s_std(sd, new_norm ? new_norm : v4l2ca->sdtv_stds);
1404     }
1405 
1406     return 0;
1407 }
1408 
1409 static const struct media_entity_operations tvp5150_sd_media_ops = {
1410     .link_setup = tvp5150_link_setup,
1411 };
1412 #endif
1413 /****************************************************************************
1414             I2C Command
1415  ****************************************************************************/
1416 static int __maybe_unused tvp5150_runtime_suspend(struct device *dev)
1417 {
1418     struct v4l2_subdev *sd = dev_get_drvdata(dev);
1419     struct tvp5150 *decoder = to_tvp5150(sd);
1420 
1421     if (decoder->irq)
1422         /* Disable lock interrupt */
1423         return regmap_update_bits(decoder->regmap,
1424                       TVP5150_INT_ENABLE_REG_A,
1425                       TVP5150_INT_A_LOCK, 0);
1426     return 0;
1427 }
1428 
1429 static int __maybe_unused tvp5150_runtime_resume(struct device *dev)
1430 {
1431     struct v4l2_subdev *sd = dev_get_drvdata(dev);
1432     struct tvp5150 *decoder = to_tvp5150(sd);
1433 
1434     if (decoder->irq)
1435         /* Enable lock interrupt */
1436         return regmap_update_bits(decoder->regmap,
1437                       TVP5150_INT_ENABLE_REG_A,
1438                       TVP5150_INT_A_LOCK,
1439                       TVP5150_INT_A_LOCK);
1440     return 0;
1441 }
1442 
1443 static int tvp5150_s_stream(struct v4l2_subdev *sd, int enable)
1444 {
1445     struct tvp5150 *decoder = to_tvp5150(sd);
1446     unsigned int mask, val = 0;
1447     int ret;
1448 
1449     mask = TVP5150_MISC_CTL_YCBCR_OE | TVP5150_MISC_CTL_SYNC_OE |
1450            TVP5150_MISC_CTL_CLOCK_OE;
1451 
1452     if (enable) {
1453         ret = pm_runtime_resume_and_get(sd->dev);
1454         if (ret < 0)
1455             return ret;
1456 
1457         tvp5150_enable(sd);
1458 
1459         /* Enable outputs if decoder is locked */
1460         if (decoder->irq)
1461             val = decoder->lock ? decoder->oe : 0;
1462         else
1463             val = decoder->oe;
1464 
1465         v4l2_subdev_notify_event(&decoder->sd, &tvp5150_ev_fmt);
1466     } else {
1467         pm_runtime_put(sd->dev);
1468     }
1469 
1470     regmap_update_bits(decoder->regmap, TVP5150_MISC_CTL, mask, val);
1471 
1472     return 0;
1473 }
1474 
1475 static int tvp5150_s_routing(struct v4l2_subdev *sd,
1476                  u32 input, u32 output, u32 config)
1477 {
1478     struct tvp5150 *decoder = to_tvp5150(sd);
1479 
1480     decoder->input = input;
1481     decoder->output = output;
1482 
1483     if (output == TVP5150_BLACK_SCREEN)
1484         decoder->enable = false;
1485     else
1486         decoder->enable = true;
1487 
1488     tvp5150_selmux(sd);
1489     return 0;
1490 }
1491 
1492 static int tvp5150_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt)
1493 {
1494     struct tvp5150 *decoder = to_tvp5150(sd);
1495 
1496     /*
1497      * this is for capturing 36 raw vbi lines
1498      * if there's a way to cut off the beginning 2 vbi lines
1499      * with the tvp5150 then the vbi line count could be lowered
1500      * to 17 lines/field again, although I couldn't find a register
1501      * which could do that cropping
1502      */
1503 
1504     if (fmt->sample_format == V4L2_PIX_FMT_GREY)
1505         regmap_write(decoder->regmap, TVP5150_LUMA_PROC_CTL_1, 0x70);
1506     if (fmt->count[0] == 18 && fmt->count[1] == 18) {
1507         regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_START,
1508                  0x00);
1509         regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_STOP, 0x01);
1510     }
1511     return 0;
1512 }
1513 
1514 static int tvp5150_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
1515 {
1516     struct tvp5150 *decoder = to_tvp5150(sd);
1517     int i;
1518 
1519     if (svbi->service_set != 0) {
1520         for (i = 0; i <= 23; i++) {
1521             svbi->service_lines[1][i] = 0;
1522             svbi->service_lines[0][i] =
1523                 tvp5150_set_vbi(sd, svbi->service_lines[0][i],
1524                         0xf0, i, 3);
1525         }
1526         /* Enables FIFO */
1527         regmap_write(decoder->regmap, TVP5150_FIFO_OUT_CTRL, 1);
1528     } else {
1529         /* Disables FIFO*/
1530         regmap_write(decoder->regmap, TVP5150_FIFO_OUT_CTRL, 0);
1531 
1532         /* Disable Full Field */
1533         regmap_write(decoder->regmap, TVP5150_FULL_FIELD_ENA, 0);
1534 
1535         /* Disable Line modes */
1536         for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
1537             regmap_write(decoder->regmap, i, 0xff);
1538     }
1539     return 0;
1540 }
1541 
1542 static int tvp5150_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
1543 {
1544     int i, mask = 0;
1545 
1546     memset(svbi->service_lines, 0, sizeof(svbi->service_lines));
1547 
1548     for (i = 0; i <= 23; i++) {
1549         svbi->service_lines[0][i] =
1550             tvp5150_get_vbi(sd, i);
1551         mask |= svbi->service_lines[0][i];
1552     }
1553     svbi->service_set = mask;
1554     return 0;
1555 }
1556 
1557 #ifdef CONFIG_VIDEO_ADV_DEBUG
1558 static int tvp5150_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
1559 {
1560     int res;
1561 
1562     res = tvp5150_read(sd, reg->reg & 0xff);
1563     if (res < 0) {
1564         dev_err(sd->dev, "%s: failed with error = %d\n", __func__, res);
1565         return res;
1566     }
1567 
1568     reg->val = res;
1569     reg->size = 1;
1570     return 0;
1571 }
1572 
1573 static int tvp5150_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
1574 {
1575     struct tvp5150 *decoder = to_tvp5150(sd);
1576 
1577     return regmap_write(decoder->regmap, reg->reg & 0xff, reg->val & 0xff);
1578 }
1579 #endif
1580 
1581 static int tvp5150_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1582                    struct v4l2_event_subscription *sub)
1583 {
1584     switch (sub->type) {
1585     case V4L2_EVENT_SOURCE_CHANGE:
1586         return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
1587     case V4L2_EVENT_CTRL:
1588         return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
1589     default:
1590         return -EINVAL;
1591     }
1592 }
1593 
1594 static int tvp5150_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1595 {
1596     int status = tvp5150_read(sd, 0x88);
1597 
1598     vt->signal = ((status & 0x04) && (status & 0x02)) ? 0xffff : 0x0;
1599     return 0;
1600 }
1601 
1602 static int tvp5150_registered(struct v4l2_subdev *sd)
1603 {
1604 #if defined(CONFIG_MEDIA_CONTROLLER)
1605     struct tvp5150 *decoder = to_tvp5150(sd);
1606     unsigned int i;
1607     int ret;
1608 
1609     /*
1610      * Setup connector pads and links. Enable the link to the first
1611      * available connector per default.
1612      */
1613     for (i = 0; i < decoder->connectors_num; i++) {
1614         struct media_entity *con = &decoder->connectors[i].ent;
1615         struct media_pad *pad = &decoder->connectors[i].pad;
1616         struct v4l2_fwnode_connector *v4l2c =
1617             &decoder->connectors[i].base;
1618         struct v4l2_connector_link *link =
1619             v4l2_connector_first_link(v4l2c);
1620         unsigned int port = link->fwnode_link.remote_port;
1621         unsigned int flags = i ? 0 : MEDIA_LNK_FL_ENABLED;
1622         bool is_svideo = v4l2c->type == V4L2_CONN_SVIDEO;
1623 
1624         pad->flags = MEDIA_PAD_FL_SOURCE;
1625         ret = media_entity_pads_init(con, 1, pad);
1626         if (ret < 0)
1627             goto err;
1628 
1629         ret = media_device_register_entity(sd->v4l2_dev->mdev, con);
1630         if (ret < 0)
1631             goto err;
1632 
1633         ret = media_create_pad_link(con, 0, &sd->entity, port, flags);
1634         if (ret < 0)
1635             goto err;
1636 
1637         if (is_svideo) {
1638             /*
1639              * Check tvp5150_link_setup() comments for more
1640              * information.
1641              */
1642             link = v4l2_connector_last_link(v4l2c);
1643             port = link->fwnode_link.remote_port;
1644             ret = media_create_pad_link(con, 0, &sd->entity, port,
1645                             flags);
1646             if (ret < 0)
1647                 goto err;
1648         }
1649 
1650         /* Enable default input. */
1651         if (flags == MEDIA_LNK_FL_ENABLED) {
1652             decoder->input =
1653                 is_svideo ? TVP5150_SVIDEO :
1654                 port == 0 ? TVP5150_COMPOSITE0 :
1655                 TVP5150_COMPOSITE1;
1656 
1657             tvp5150_selmux(sd);
1658             decoder->cur_connector = &decoder->connectors[i];
1659             tvp5150_s_std(sd, v4l2c->connector.analog.sdtv_stds);
1660         }
1661     }
1662 
1663     return 0;
1664 
1665 err:
1666     for (i = 0; i < decoder->connectors_num; i++) {
1667         media_device_unregister_entity(&decoder->connectors[i].ent);
1668         media_entity_cleanup(&decoder->connectors[i].ent);
1669     }
1670     return ret;
1671 #endif
1672 
1673     return 0;
1674 }
1675 
1676 static int tvp5150_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1677 {
1678     return pm_runtime_resume_and_get(sd->dev);
1679 }
1680 
1681 static int tvp5150_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1682 {
1683     pm_runtime_put(sd->dev);
1684 
1685     return 0;
1686 }
1687 
1688 /* ----------------------------------------------------------------------- */
1689 
1690 static const struct v4l2_ctrl_ops tvp5150_ctrl_ops = {
1691     .s_ctrl = tvp5150_s_ctrl,
1692 };
1693 
1694 static const struct v4l2_subdev_core_ops tvp5150_core_ops = {
1695     .log_status = tvp5150_log_status,
1696     .reset = tvp5150_reset,
1697 #ifdef CONFIG_VIDEO_ADV_DEBUG
1698     .g_register = tvp5150_g_register,
1699     .s_register = tvp5150_s_register,
1700 #endif
1701     .subscribe_event = tvp5150_subscribe_event,
1702     .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1703 };
1704 
1705 static const struct v4l2_subdev_tuner_ops tvp5150_tuner_ops = {
1706     .g_tuner = tvp5150_g_tuner,
1707 };
1708 
1709 static const struct v4l2_subdev_video_ops tvp5150_video_ops = {
1710     .s_std = tvp5150_s_std,
1711     .g_std = tvp5150_g_std,
1712     .querystd = tvp5150_querystd,
1713     .s_stream = tvp5150_s_stream,
1714     .s_routing = tvp5150_s_routing,
1715 };
1716 
1717 static const struct v4l2_subdev_vbi_ops tvp5150_vbi_ops = {
1718     .g_sliced_vbi_cap = tvp5150_g_sliced_vbi_cap,
1719     .g_sliced_fmt = tvp5150_g_sliced_fmt,
1720     .s_sliced_fmt = tvp5150_s_sliced_fmt,
1721     .s_raw_fmt = tvp5150_s_raw_fmt,
1722 };
1723 
1724 static const struct v4l2_subdev_pad_ops tvp5150_pad_ops = {
1725     .init_cfg = tvp5150_init_cfg,
1726     .enum_mbus_code = tvp5150_enum_mbus_code,
1727     .enum_frame_size = tvp5150_enum_frame_size,
1728     .set_fmt = tvp5150_fill_fmt,
1729     .get_fmt = tvp5150_fill_fmt,
1730     .get_selection = tvp5150_get_selection,
1731     .set_selection = tvp5150_set_selection,
1732     .get_mbus_config = tvp5150_get_mbus_config,
1733 };
1734 
1735 static const struct v4l2_subdev_ops tvp5150_ops = {
1736     .core = &tvp5150_core_ops,
1737     .tuner = &tvp5150_tuner_ops,
1738     .video = &tvp5150_video_ops,
1739     .vbi = &tvp5150_vbi_ops,
1740     .pad = &tvp5150_pad_ops,
1741 };
1742 
1743 static const struct v4l2_subdev_internal_ops tvp5150_internal_ops = {
1744     .registered = tvp5150_registered,
1745     .open = tvp5150_open,
1746     .close = tvp5150_close,
1747 };
1748 
1749 /****************************************************************************
1750             I2C Client & Driver
1751  ****************************************************************************/
1752 
1753 static const struct regmap_range tvp5150_readable_ranges[] = {
1754     {
1755         .range_min = TVP5150_VD_IN_SRC_SEL_1,
1756         .range_max = TVP5150_AUTOSW_MSK,
1757     }, {
1758         .range_min = TVP5150_COLOR_KIL_THSH_CTL,
1759         .range_max = TVP5150_CONF_SHARED_PIN,
1760     }, {
1761         .range_min = TVP5150_ACT_VD_CROP_ST_MSB,
1762         .range_max = TVP5150_HORIZ_SYNC_START,
1763     }, {
1764         .range_min = TVP5150_VERT_BLANKING_START,
1765         .range_max = TVP5150_INTT_CONFIG_REG_B,
1766     }, {
1767         .range_min = TVP5150_VIDEO_STD,
1768         .range_max = TVP5150_VIDEO_STD,
1769     }, {
1770         .range_min = TVP5150_CB_GAIN_FACT,
1771         .range_max = TVP5150_REV_SELECT,
1772     }, {
1773         .range_min = TVP5150_MSB_DEV_ID,
1774         .range_max = TVP5150_STATUS_REG_5,
1775     }, {
1776         .range_min = TVP5150_CC_DATA_INI,
1777         .range_max = TVP5150_TELETEXT_FIL_ENA,
1778     }, {
1779         .range_min = TVP5150_INT_STATUS_REG_A,
1780         .range_max = TVP5150_FIFO_OUT_CTRL,
1781     }, {
1782         .range_min = TVP5150_FULL_FIELD_ENA,
1783         .range_max = TVP5150_FULL_FIELD_MODE_REG,
1784     },
1785 };
1786 
1787 static bool tvp5150_volatile_reg(struct device *dev, unsigned int reg)
1788 {
1789     switch (reg) {
1790     case TVP5150_VERT_LN_COUNT_MSB:
1791     case TVP5150_VERT_LN_COUNT_LSB:
1792     case TVP5150_INT_STATUS_REG_A:
1793     case TVP5150_INT_STATUS_REG_B:
1794     case TVP5150_INT_ACTIVE_REG_B:
1795     case TVP5150_STATUS_REG_1:
1796     case TVP5150_STATUS_REG_2:
1797     case TVP5150_STATUS_REG_3:
1798     case TVP5150_STATUS_REG_4:
1799     case TVP5150_STATUS_REG_5:
1800     /* CC, WSS, VPS, VITC data? */
1801     case TVP5150_VBI_FIFO_READ_DATA:
1802     case TVP5150_VDP_STATUS_REG:
1803     case TVP5150_FIFO_WORD_COUNT:
1804         return true;
1805     default:
1806         return false;
1807     }
1808 }
1809 
1810 static const struct regmap_access_table tvp5150_readable_table = {
1811     .yes_ranges = tvp5150_readable_ranges,
1812     .n_yes_ranges = ARRAY_SIZE(tvp5150_readable_ranges),
1813 };
1814 
1815 static struct regmap_config tvp5150_config = {
1816     .reg_bits = 8,
1817     .val_bits = 8,
1818     .max_register = 0xff,
1819 
1820     .cache_type = REGCACHE_RBTREE,
1821 
1822     .rd_table = &tvp5150_readable_table,
1823     .volatile_reg = tvp5150_volatile_reg,
1824 };
1825 
1826 static int tvp5150_detect_version(struct tvp5150 *core)
1827 {
1828     struct v4l2_subdev *sd = &core->sd;
1829     struct i2c_client *c = v4l2_get_subdevdata(sd);
1830     u8 regs[4];
1831     int res;
1832 
1833     /*
1834      * Read consequent registers - TVP5150_MSB_DEV_ID, TVP5150_LSB_DEV_ID,
1835      * TVP5150_ROM_MAJOR_VER, TVP5150_ROM_MINOR_VER
1836      */
1837     res = regmap_bulk_read(core->regmap, TVP5150_MSB_DEV_ID, regs, 4);
1838     if (res < 0) {
1839         dev_err(&c->dev, "reading ID registers failed: %d\n", res);
1840         return res;
1841     }
1842 
1843     core->dev_id = (regs[0] << 8) | regs[1];
1844     core->rom_ver = (regs[2] << 8) | regs[3];
1845 
1846     dev_info(sd->dev, "tvp%04x (%u.%u) chip found @ 0x%02x (%s)\n",
1847           core->dev_id, regs[2], regs[3], c->addr << 1,
1848           c->adapter->name);
1849 
1850     if (core->dev_id == 0x5150 && core->rom_ver == 0x0321) {
1851         dev_info(sd->dev, "tvp5150a detected.\n");
1852     } else if (core->dev_id == 0x5150 && core->rom_ver == 0x0400) {
1853         dev_info(sd->dev, "tvp5150am1 detected.\n");
1854 
1855         /* ITU-T BT.656.4 timing */
1856         regmap_write(core->regmap, TVP5150_REV_SELECT, 0);
1857     } else if (core->dev_id == 0x5151 && core->rom_ver == 0x0100) {
1858         dev_info(sd->dev, "tvp5151 detected.\n");
1859     } else {
1860         dev_info(sd->dev, "*** unknown tvp%04x chip detected.\n",
1861               core->dev_id);
1862     }
1863 
1864     return 0;
1865 }
1866 
1867 static int tvp5150_init(struct i2c_client *c)
1868 {
1869     struct gpio_desc *pdn_gpio;
1870     struct gpio_desc *reset_gpio;
1871 
1872     pdn_gpio = devm_gpiod_get_optional(&c->dev, "pdn", GPIOD_OUT_HIGH);
1873     if (IS_ERR(pdn_gpio))
1874         return PTR_ERR(pdn_gpio);
1875 
1876     if (pdn_gpio) {
1877         gpiod_set_value_cansleep(pdn_gpio, 0);
1878         /* Delay time between power supplies active and reset */
1879         msleep(20);
1880     }
1881 
1882     reset_gpio = devm_gpiod_get_optional(&c->dev, "reset", GPIOD_OUT_HIGH);
1883     if (IS_ERR(reset_gpio))
1884         return PTR_ERR(reset_gpio);
1885 
1886     if (reset_gpio) {
1887         /* RESETB pulse duration */
1888         ndelay(500);
1889         gpiod_set_value_cansleep(reset_gpio, 0);
1890         /* Delay time between end of reset to I2C active */
1891         usleep_range(200, 250);
1892     }
1893 
1894     return 0;
1895 }
1896 
1897 #if defined(CONFIG_MEDIA_CONTROLLER)
1898 static int tvp5150_mc_init(struct tvp5150 *decoder)
1899 {
1900     struct v4l2_subdev *sd = &decoder->sd;
1901     unsigned int i;
1902 
1903     sd->entity.ops = &tvp5150_sd_media_ops;
1904     sd->entity.function = MEDIA_ENT_F_ATV_DECODER;
1905 
1906     for (i = 0; i < TVP5150_NUM_PADS - 1; i++) {
1907         decoder->pads[i].flags = MEDIA_PAD_FL_SINK;
1908         decoder->pads[i].sig_type = PAD_SIGNAL_ANALOG;
1909     }
1910 
1911     decoder->pads[i].flags = MEDIA_PAD_FL_SOURCE;
1912     decoder->pads[i].sig_type = PAD_SIGNAL_DV;
1913 
1914     return media_entity_pads_init(&sd->entity, TVP5150_NUM_PADS,
1915                       decoder->pads);
1916 }
1917 
1918 #else /* !defined(CONFIG_MEDIA_CONTROLLER) */
1919 
1920 static inline int tvp5150_mc_init(struct tvp5150 *decoder)
1921 {
1922     return 0;
1923 }
1924 #endif /* defined(CONFIG_MEDIA_CONTROLLER) */
1925 
1926 static int tvp5150_validate_connectors(struct tvp5150 *decoder)
1927 {
1928     struct device *dev = decoder->sd.dev;
1929     struct tvp5150_connector *tvpc;
1930     struct v4l2_fwnode_connector *v4l2c;
1931     unsigned int i;
1932 
1933     if (!decoder->connectors_num) {
1934         dev_err(dev, "No valid connector found\n");
1935         return -ENODEV;
1936     }
1937 
1938     for (i = 0; i < decoder->connectors_num; i++) {
1939         struct v4l2_connector_link *link0 = NULL;
1940         struct v4l2_connector_link *link1;
1941 
1942         tvpc = &decoder->connectors[i];
1943         v4l2c = &tvpc->base;
1944 
1945         if (v4l2c->type == V4L2_CONN_COMPOSITE) {
1946             if (v4l2c->nr_of_links != 1) {
1947                 dev_err(dev, "Composite: connector needs 1 link\n");
1948                 return -EINVAL;
1949             }
1950             link0 = v4l2_connector_first_link(v4l2c);
1951             if (!link0) {
1952                 dev_err(dev, "Composite: invalid first link\n");
1953                 return -EINVAL;
1954             }
1955             if (link0->fwnode_link.remote_id == 1) {
1956                 dev_err(dev, "Composite: invalid endpoint id\n");
1957                 return -EINVAL;
1958             }
1959         }
1960 
1961         if (v4l2c->type == V4L2_CONN_SVIDEO) {
1962             if (v4l2c->nr_of_links != 2) {
1963                 dev_err(dev, "SVideo: connector needs 2 links\n");
1964                 return -EINVAL;
1965             }
1966             link0 = v4l2_connector_first_link(v4l2c);
1967             if (!link0) {
1968                 dev_err(dev, "SVideo: invalid first link\n");
1969                 return -EINVAL;
1970             }
1971             link1 = v4l2_connector_last_link(v4l2c);
1972             if (link0->fwnode_link.remote_port ==
1973                 link1->fwnode_link.remote_port) {
1974                 dev_err(dev, "SVideo: invalid link setup\n");
1975                 return -EINVAL;
1976             }
1977         }
1978 
1979         if (!(v4l2c->connector.analog.sdtv_stds & TVP5150_STD_MASK)) {
1980             dev_err(dev, "Unsupported tv-norm on connector %s\n",
1981                 v4l2c->name);
1982             return -EINVAL;
1983         }
1984     }
1985 
1986     return 0;
1987 }
1988 
1989 static int tvp5150_parse_dt(struct tvp5150 *decoder, struct device_node *np)
1990 {
1991     struct device *dev = decoder->sd.dev;
1992     struct v4l2_fwnode_endpoint bus_cfg = {
1993         .bus_type = V4L2_MBUS_UNKNOWN
1994     };
1995     struct device_node *ep_np;
1996     struct tvp5150_connector *tvpc;
1997     struct v4l2_fwnode_connector *v4l2c;
1998     unsigned int flags, ep_num;
1999     unsigned int i;
2000     int ret;
2001 
2002     /* At least 1 output and 1 input */
2003     ep_num = of_graph_get_endpoint_count(np);
2004     if (ep_num < 2 || ep_num > 5) {
2005         dev_err(dev, "At least 1 input and 1 output must be connected to the device.\n");
2006         return -EINVAL;
2007     }
2008 
2009     /* Layout if all connectors are used:
2010      *
2011      * tvp-5150 port@0 (AIP1A)
2012      *  endpoint@0 -----------> Comp0-Con  port
2013      *  endpoint@1 --------+--> Svideo-Con port
2014      * tvp-5150 port@1 (AIP1B) |
2015      *  endpoint@1 --------+
2016      *  endpoint@0 -----------> Comp1-Con  port
2017      * tvp-5150 port@2
2018      *  endpoint (video bitstream output at YOUT[0-7] parallel bus)
2019      */
2020     for_each_endpoint_of_node(np, ep_np) {
2021         struct fwnode_handle *ep_fwnode = of_fwnode_handle(ep_np);
2022         unsigned int next_connector = decoder->connectors_num;
2023         struct of_endpoint ep;
2024 
2025         of_graph_parse_endpoint(ep_np, &ep);
2026         if (ep.port > 1 || ep.id > 1) {
2027             dev_dbg(dev, "Ignore connector on port@%u/ep@%u\n",
2028                 ep.port, ep.id);
2029             continue;
2030         }
2031 
2032         tvpc = &decoder->connectors[next_connector];
2033         v4l2c = &tvpc->base;
2034 
2035         if (ep.port == 0 || (ep.port == 1 && ep.id == 0)) {
2036             ret = v4l2_fwnode_connector_parse(ep_fwnode, v4l2c);
2037             if (ret)
2038                 goto err_put;
2039             ret = v4l2_fwnode_connector_add_link(ep_fwnode, v4l2c);
2040             if (ret)
2041                 goto err_put;
2042             decoder->connectors_num++;
2043         } else {
2044             /* Adding the 2nd svideo link */
2045             for (i = 0; i < TVP5150_MAX_CONNECTORS; i++) {
2046                 tvpc = &decoder->connectors[i];
2047                 v4l2c = &tvpc->base;
2048                 if (v4l2c->type == V4L2_CONN_SVIDEO)
2049                     break;
2050             }
2051 
2052             ret = v4l2_fwnode_connector_add_link(ep_fwnode, v4l2c);
2053             if (ret)
2054                 goto err_put;
2055         }
2056     }
2057 
2058     ret = tvp5150_validate_connectors(decoder);
2059     if (ret)
2060         goto err_free;
2061 
2062     for (i = 0; i < decoder->connectors_num; i++) {
2063         tvpc = &decoder->connectors[i];
2064         v4l2c = &tvpc->base;
2065         tvpc->ent.flags = MEDIA_ENT_FL_CONNECTOR;
2066         tvpc->ent.function = v4l2c->type == V4L2_CONN_SVIDEO ?
2067             MEDIA_ENT_F_CONN_SVIDEO : MEDIA_ENT_F_CONN_COMPOSITE;
2068         tvpc->ent.name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
2069                         v4l2c->name, v4l2c->label ?
2070                         v4l2c->label : "");
2071     }
2072 
2073     ep_np = of_graph_get_endpoint_by_regs(np, TVP5150_PAD_VID_OUT, 0);
2074     if (!ep_np) {
2075         ret = -EINVAL;
2076         dev_err(dev, "Error no output endpoint available\n");
2077         goto err_free;
2078     }
2079     ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_np), &bus_cfg);
2080     of_node_put(ep_np);
2081     if (ret)
2082         goto err_free;
2083 
2084     flags = bus_cfg.bus.parallel.flags;
2085     if (bus_cfg.bus_type == V4L2_MBUS_PARALLEL &&
2086         !(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH &&
2087           flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH &&
2088           flags & V4L2_MBUS_FIELD_EVEN_LOW)) {
2089         ret = -EINVAL;
2090         goto err_free;
2091     }
2092 
2093     decoder->mbus_type = bus_cfg.bus_type;
2094 
2095     return 0;
2096 
2097 err_put:
2098     of_node_put(ep_np);
2099 err_free:
2100     for (i = 0; i < TVP5150_MAX_CONNECTORS; i++)
2101         v4l2_fwnode_connector_free(&decoder->connectors[i].base);
2102 
2103     return ret;
2104 }
2105 
2106 static const char * const tvp5150_test_patterns[2] = {
2107     "Disabled",
2108     "Black screen"
2109 };
2110 
2111 static int tvp5150_probe(struct i2c_client *c)
2112 {
2113     struct tvp5150 *core;
2114     struct v4l2_subdev *sd;
2115     struct device_node *np = c->dev.of_node;
2116     struct regmap *map;
2117     unsigned int i;
2118     int res;
2119 
2120     /* Check if the adapter supports the needed features */
2121     if (!i2c_check_functionality(c->adapter,
2122          I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
2123         return -EIO;
2124 
2125     res = tvp5150_init(c);
2126     if (res)
2127         return res;
2128 
2129     core = devm_kzalloc(&c->dev, sizeof(*core), GFP_KERNEL);
2130     if (!core)
2131         return -ENOMEM;
2132 
2133     map = devm_regmap_init_i2c(c, &tvp5150_config);
2134     if (IS_ERR(map))
2135         return PTR_ERR(map);
2136 
2137     core->regmap = map;
2138     sd = &core->sd;
2139     v4l2_i2c_subdev_init(sd, c, &tvp5150_ops);
2140     sd->internal_ops = &tvp5150_internal_ops;
2141     sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
2142 
2143     if (IS_ENABLED(CONFIG_OF) && np) {
2144         res = tvp5150_parse_dt(core, np);
2145         if (res) {
2146             dev_err(sd->dev, "DT parsing error: %d\n", res);
2147             return res;
2148         }
2149     } else {
2150         /* Default to BT.656 embedded sync */
2151         core->mbus_type = V4L2_MBUS_BT656;
2152     }
2153 
2154     res = tvp5150_mc_init(core);
2155     if (res)
2156         return res;
2157 
2158     res = tvp5150_detect_version(core);
2159     if (res < 0)
2160         return res;
2161 
2162     /*
2163      * Iterate over all available connectors in case they are supported and
2164      * successfully parsed. Fallback to default autodetect in case they
2165      * aren't supported.
2166      */
2167     for (i = 0; i < core->connectors_num; i++) {
2168         struct v4l2_fwnode_connector *v4l2c;
2169 
2170         v4l2c = &core->connectors[i].base;
2171         core->norm |= v4l2c->connector.analog.sdtv_stds;
2172     }
2173 
2174     if (!core->connectors_num)
2175         core->norm = V4L2_STD_ALL;
2176 
2177     core->detected_norm = V4L2_STD_UNKNOWN;
2178     core->input = TVP5150_COMPOSITE1;
2179     core->enable = true;
2180 
2181     v4l2_ctrl_handler_init(&core->hdl, 5);
2182     v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
2183             V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
2184     v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
2185             V4L2_CID_CONTRAST, 0, 255, 1, 128);
2186     v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
2187             V4L2_CID_SATURATION, 0, 255, 1, 128);
2188     v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
2189             V4L2_CID_HUE, -128, 127, 1, 0);
2190     v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
2191             V4L2_CID_PIXEL_RATE, 27000000,
2192             27000000, 1, 27000000);
2193     v4l2_ctrl_new_std_menu_items(&core->hdl, &tvp5150_ctrl_ops,
2194                      V4L2_CID_TEST_PATTERN,
2195                      ARRAY_SIZE(tvp5150_test_patterns) - 1,
2196                      0, 0, tvp5150_test_patterns);
2197     sd->ctrl_handler = &core->hdl;
2198     if (core->hdl.error) {
2199         res = core->hdl.error;
2200         goto err;
2201     }
2202 
2203     tvp5150_set_default(tvp5150_read_std(sd), &core->rect);
2204 
2205     core->irq = c->irq;
2206     tvp5150_reset(sd, 0);   /* Calls v4l2_ctrl_handler_setup() */
2207     if (c->irq) {
2208         res = devm_request_threaded_irq(&c->dev, c->irq, NULL,
2209                         tvp5150_isr, IRQF_TRIGGER_HIGH |
2210                         IRQF_ONESHOT, "tvp5150", core);
2211         if (res)
2212             goto err;
2213     }
2214 
2215     res = v4l2_async_register_subdev(sd);
2216     if (res < 0)
2217         goto err;
2218 
2219     if (debug > 1)
2220         tvp5150_log_status(sd);
2221 
2222     pm_runtime_set_active(&c->dev);
2223     pm_runtime_enable(&c->dev);
2224     pm_runtime_idle(&c->dev);
2225 
2226     return 0;
2227 
2228 err:
2229     v4l2_ctrl_handler_free(&core->hdl);
2230     return res;
2231 }
2232 
2233 static int tvp5150_remove(struct i2c_client *c)
2234 {
2235     struct v4l2_subdev *sd = i2c_get_clientdata(c);
2236     struct tvp5150 *decoder = to_tvp5150(sd);
2237     unsigned int i;
2238 
2239     dev_dbg_lvl(sd->dev, 1, debug,
2240         "tvp5150.c: removing tvp5150 adapter on address 0x%x\n",
2241         c->addr << 1);
2242 
2243     for (i = 0; i < decoder->connectors_num; i++)
2244         v4l2_fwnode_connector_free(&decoder->connectors[i].base);
2245     for (i = 0; i < decoder->connectors_num; i++) {
2246         media_device_unregister_entity(&decoder->connectors[i].ent);
2247         media_entity_cleanup(&decoder->connectors[i].ent);
2248     }
2249     v4l2_async_unregister_subdev(sd);
2250     v4l2_ctrl_handler_free(&decoder->hdl);
2251     pm_runtime_disable(&c->dev);
2252     pm_runtime_set_suspended(&c->dev);
2253 
2254     return 0;
2255 }
2256 
2257 /* ----------------------------------------------------------------------- */
2258 
2259 static const struct dev_pm_ops tvp5150_pm_ops = {
2260     SET_RUNTIME_PM_OPS(tvp5150_runtime_suspend,
2261                tvp5150_runtime_resume,
2262                NULL)
2263 };
2264 
2265 static const struct i2c_device_id tvp5150_id[] = {
2266     { "tvp5150", 0 },
2267     { }
2268 };
2269 MODULE_DEVICE_TABLE(i2c, tvp5150_id);
2270 
2271 #if IS_ENABLED(CONFIG_OF)
2272 static const struct of_device_id tvp5150_of_match[] = {
2273     { .compatible = "ti,tvp5150", },
2274     { /* sentinel */ },
2275 };
2276 MODULE_DEVICE_TABLE(of, tvp5150_of_match);
2277 #endif
2278 
2279 static struct i2c_driver tvp5150_driver = {
2280     .driver = {
2281         .of_match_table = of_match_ptr(tvp5150_of_match),
2282         .name   = "tvp5150",
2283         .pm = &tvp5150_pm_ops,
2284     },
2285     .probe_new  = tvp5150_probe,
2286     .remove     = tvp5150_remove,
2287     .id_table   = tvp5150_id,
2288 };
2289 
2290 module_i2c_driver(tvp5150_driver);