diff mbox

[5/6,v4] Updated TVP7002 driver for DM365

Message ID 1252018364-17432-1-git-send-email-santiago.nunez@ridgerun.com (mailing list archive)
State Superseded
Headers show

Commit Message

santiago.nunez@ridgerun.com Sept. 3, 2009, 10:52 p.m. UTC
From: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>

This patch provides the implementation of the TVP7002 decoder
driver for DM365. Implemented revisions by Vaibhav Hiremath
and Hans Verkuil. Removed most of controls, cleared up logic,
cleaned up code. Tested on DM365 TI EVM.

Signed-off-by: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
---
 drivers/media/video/tvp7002.c | 1464 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 1464 insertions(+), 0 deletions(-)
 create mode 100644 drivers/media/video/tvp7002.c

Comments

nsnehaprabha@ti.com Sept. 4, 2009, 3:18 p.m. UTC | #1
Santiago,

> -----Original Message-----
> From: santiago.nunez@ridgerun.com [mailto:santiago.nunez@ridgerun.com]
> Sent: Thursday, September 03, 2009 6:53 PM
> To: davinci-linux-open-source@linux.davincidsp.com
> Cc: Karicheri, Muralidharan; todd.fischer@ridgerun.com;
> diego.dompe@ridgerun.com; clark.becker@ridgerun.com; Narnakaje,
> Snehaprabha; Santiago Nunez-Corrales
> Subject: [PATCH 5/6 v4] Updated TVP7002 driver for DM365
>
> From: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
>
> This patch provides the implementation of the TVP7002 decoder
> driver for DM365. Implemented revisions by Vaibhav Hiremath
> and Hans Verkuil. Removed most of controls, cleared up logic,
> cleaned up code. Tested on DM365 TI EVM.
>
> Signed-off-by: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
> ---
>  drivers/media/video/tvp7002.c | 1464

[...]

> +
> +/*
> + * tvp7002_get_video_mode() - V4L2 decoder interface handler for querystd
> + * @sd: pointer to standard V4L2 sub-device structure
> + *
> + * Returns the current standard detected by TVP7002. If no active input
> is
> + * detected, returns -1
> + */
> +static v4l2_std_id tvp7002_get_video_mode(struct v4l2_subdev *sd)
> +{
> +     int reg01, reg02, reg03;
> +
> +     reg01 = tvp7002_read(sd, TVP7002_HPLL_FDBK_DIV_MSBS);
> +     reg02 = tvp7002_read(sd, TVP7002_HPLL_FDBK_DIV_LSBS);
> +     reg03 = tvp7002_read(sd, TVP7002_HPLL_CRTL);
> +
> +     if (reg01 < 0 || reg02 < 0 || reg03 < 0)
> +             return V4L2_STD_UNKNOWN;
> +
> +     switch (reg01) {
> +     case 0x35:
> +             if (reg02 == 0xa0)
> +                     return V4L2_STD_NTSC;
> +             else
> +                     return V4L2_STD_PAL;
> +     case 0x36:
> +             if (reg02 == 0xa0)
> +                     return V4L2_STD_525P_60;
> +             else
> +                     return V4L2_STD_625P_50;
> +     case 0x67:
> +             return V4L2_STD_720P_60;
> +     case 0x7b:
> +             return V4L2_STD_720P_50;
> +     case 0x89:
> +             if (reg03 == 0x98)
> +                     return V4L2_STD_1080I_60;
> +             else
> +                     return V4L2_STD_1080P_60;
> +             break;
> +     case 0xa5:
> +             if (reg03 == 0x90)
> +                     return V4L2_STD_1080I_50;
> +             else
> +                     return V4L2_STD_1080P_50;
> +     default:
> +             return V4L2_STD_UNKNOWN;
> +     }
> +}
> +
> +/*
> + * tvp7002_querystd() - V4L2 decoder interface handler for querystd
> + * @sd: pointer to standard V4L2 sub-device structure
> + * @std_id: standard V4L2 std_id ioctl enum
> + *
> + * Returns the current standard detected by TVP7002. If no active input
> is
> + * detected, returns -EINVAL
> + */
> +static int tvp7002_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
> +{
> +     v4l2_std_id current_std;
> +     u8 sync_lock_status;
> +     int res;
> +
> +     if (std_id == NULL)
> +             return -EINVAL;
> +
> +     /* get the current standard */
> +     res = tvp7002_get_video_mode(sd);
> +     if (res == V4L2_STD_UNKNOWN)
> +             return -EINVAL;

There seems to be a bug here - You will mostly get V4L2_STD_UNKNOWN for HD resolutions. The tvp7002_get_video_mode() API returns v4l2_std_id (which is long long - 64 bits), but you are storing the return value in "int res". You should change it to -

v4l2_std_id res;

Note that I am trying to integrate your driver with VPIF capture on DM6467 and thus running into issues like this. It would be good to know - whether you have tested the TVP7002 driver on DM365 using a capture application.

Thanks
Sneha

> +     current_std = res;
> +
> +     /* check whether signal is locked */
> +     sync_lock_status = tvp7002_read(sd, TVP7002_SYNC_DETECT_STAT);
> +
> +     if (0x02 != (sync_lock_status & 0xff))
> +             return -EINVAL; /* No input detected */
> +
> +     *std_id = current_std;
> +
> +     v4l2_dbg(1, debug, sd, "Current STD: %d %d @ %d Hz\n",
> +             tvp7002_resolutions[tvp7002_from_std(current_std)].hres,
> +             tvp7002_resolutions[tvp7002_from_std(current_std)].vres,
> +             tvp7002_resolutions[tvp7002_from_std(current_std)].frate);
> +     return 0;
> +}
> +
> +/*
> + * tvp7002_try_fmt_cap() - V4L2 decoder interface handler for try_fmt
> + * @sd: pointer to standard V4L2 sub-device structure
> + * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
> + *
> + * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
> + * ioctl is used to negotiate the image capture size and pixel format
> + * without actually making it take effect.
> + */
> +static int tvp7002_try_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format
> *f)
> +{
> +     struct v4l2_pix_format *pix;
> +     v4l2_std_id current_std;
> +     int res;
> +
> +     if (f == NULL)
> +             return -EINVAL;
> +
> +     if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> +             /* only capture is supported */
> +             return -EINVAL;
> +
> +     pix = &f->fmt.pix;
> +
> +     /* Calculate height and width based on current standard */
> +     res = tvp7002_get_video_mode(sd);
> +     if (res < 0)
> +             return -EINVAL;
> +     current_std = res;
> +
> +     pix->width = NUM_ACTIVE_PIXELS(current_std);
> +     pix->height = NUM_ACTIVE_LINES(current_std);
> +
> +     pix->pixelformat = V4L2_PIX_FMT_UYVY;
> +
> +     pix->field = V4L2_FIELD_INTERLACED;
> +     pix->bytesperline = pix->width * 2;
> +     pix->sizeimage = pix->bytesperline * pix->height;
> +     pix->colorspace = V4L2_COLORSPACE_REC709;
> +     pix->priv = 0;
> +
> +     v4l2_dbg(1, debug, sd, "Try FMT: pixelformat - %s, bytesperline -
> %d"
> +                     "Width - %d, Height - %d",
> +                     "8-bit UYVY 4:2:2 Format", pix->bytesperline,
> +                     pix->width, pix->height);
> +     return 0;
> +}
> +
> +/**
> + * tvp7002_s_fmt_cap() - V4L2 decoder interface handler for s_fmt
> + * @sd: pointer to standard V4L2 sub-device structure
> + * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
> + *
> + * If the requested format is supported, configures the HW to use that
> + * format, returns error code if format not supported or HW can't be
> + * correctly configured.
> + */
> +static int tvp7002_s_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format
> *f)
> +{
> +     struct tvp7002 *decoder = to_tvp7002(sd);
> +     struct v4l2_pix_format *pix;
> +     int rval;
> +
> +     if (f == NULL)
> +             return -EINVAL;
> +
> +     if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> +             /* only capture is supported */
> +             return -EINVAL;
> +
> +     pix = &f->fmt.pix;
> +     rval = tvp7002_try_fmt_cap(sd, f);
> +     if (rval)
> +             return rval;
> +
> +     decoder->pix = *pix;
> +
> +     return rval;
> +}
> +
> +/*
> + * tvp7002_g_fmt() - V4L2 decoder interface handler for tvp7002_g_fmt
> + * @sd: pointer to standard V4L2 sub-device structure
> + * @f: pointer to standard V4L2 v4l2_format structure
> + *
> + * Returns the decoder's current pixel format in the v4l2_format
> + * parameter.
> + */
> +static int tvp7002_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
> +{
> +     struct tvp7002 *decoder = to_tvp7002(sd);
> +
> +     if (f == NULL)
> +             return -EINVAL;
> +
> +     if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> +             /* only capture is supported */
> +             return -EINVAL;
> +
> +     f->fmt.pix = decoder->pix;
> +
> +     v4l2_dbg(1, debug, sd, "Current FMT: bytesperline - %d"
> +                     "Width - %d, Height - %d",
> +                     decoder->pix.bytesperline,
> +                     decoder->pix.width, decoder->pix.height);
> +     return 0;
> +}
> +
> +/*
> + * tvp7002_s_ctrl() - Set a control
> + * @sd: ptr to v4l2_subdev struct
> + * @ctrl: ptr to v4l2_control struct
> + *
> + * Set a control for a TVP7002 decoder device.
> + * Returns zero when successful or -EINVAL if register access fails.
> + */
> +static int tvp7002_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
> +{
> +     struct tvp7002 *decoder = to_tvp7002(sd);
> +     int vmd = 0;
> +
> +     decoder->video_mode = std;
> +     vmd = tvp7002_from_std(std);
> +
> +     v4l2_dbg(1, debug, sd, "Set video std mode to %llx.\n", std);
> +
> +     return tvp7002_set_video_mode(sd, vmd);
> +}
> +
> +/*
> + * tvp7002_g_ctrl() - Get a control
> + * @sd: ptr to v4l2_subdev struct
> + * @ctrl: ptr to v4l2_control struct
> + *
> + * Get a control for a TVP7002 decoder device.
> + * Returns zero when successful or -EINVAL if register access fails.
> + */
> +static int tvp7002_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control
> *ctrl)
> +{
> +     int rval, gval, bval;
> +     int res;
> +
> +     v4l2_info(sd, "g_ctrl called\n");
> +
> +     switch (ctrl->id) {
> +     case V4L2_CID_GAIN:
> +             rval = tvp7002_read(sd, TVP7002_R_FINE_GAIN);
> +             gval = tvp7002_read(sd, TVP7002_G_FINE_GAIN);
> +             bval = tvp7002_read(sd, TVP7002_B_FINE_GAIN);
> +
> +             if (rval < 0 || gval < 0 || bval < 0) {
> +                     res = -1;
> +             } else if (rval != gval || rval != bval) {
> +                     res = -1;
> +             } else {
> +                     ctrl->value = rval & 0x0F;
> +                     res = ctrl->value;
> +             }
> +             break;
> +     default:
> +             res = -1;
> +             break;
> +     }
> +
> +     return res < 0 ? res : 0;
> +}
> +
> +/*
> + * tvp7002_s_ctrl() - Set a control
> + * @sd: ptr to v4l2_subdev struct
> + * @ctrl: ptr to v4l2_control struct
> + *
> + * Set a control in TVP7002 decoder device.
> + * Returns zero when successful or -EINVAL if register access fails.
> + */
> +static int tvp7002_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control
> *ctrl)
> +{
> +     int rval, gval, bval;
> +     int error;
> +     u8 i, n;
> +     n = ARRAY_SIZE(tvp7002_qctrl);
> +
> +     for (i = 0; i < n; i++)
> +             if (ctrl->id == tvp7002_qctrl[i].id)
> +                     break;
> +
> +     if (i == n)
> +             return -EINVAL;
> +
> +     if (ctrl->value < tvp7002_qctrl[i].minimum ||
> +                     ctrl->value > tvp7002_qctrl[i].maximum)
> +             return -ERANGE;
> +
> +     switch (ctrl->id) {
> +     case V4L2_CID_GAIN:
> +             rval = tvp7002_write(sd, TVP7002_R_FINE_GAIN,
> +                                                     ctrl->value & 0xff);
> +             gval = tvp7002_write(sd, TVP7002_G_FINE_GAIN,
> +                                                     ctrl->value & 0xff);
> +             bval = tvp7002_write(sd, TVP7002_B_FINE_GAIN,
> +                                                     ctrl->value & 0xff);
> +             if (rval < 0  || gval < 0 || bval < 0)
> +                     error = -1;
> +             else
> +                     error = rval;
> +             break;
> +     default:
> +             error = -1;
> +             break;
> +     }
> +
> +     return error < 0 ? -EINVAL : 0;
> +}
> +
> +/*
> + * tvp7002_g_register() - Get the value of a register
> + * @sd: ptr to v4l2_subdev struct
> + * @vreg: ptr to v4l2_dbg_register struct
> + *
> + * Get the value of a TVP7002 decoder device register.
> + * Returns zero when successful, -EINVAL if register read fails or
> + * access to I2C client fails, -EPERM if the call is not allowed
> + * by diabled CAP_SYS_ADMIN.
> + */
> +#ifdef CONFIG_VIDEO_ADV_DEBUG
> +static int tvp7002_g_register(struct v4l2_subdev *sd,
> +                                             struct v4l2_dbg_register *reg)
> +{
> +     struct i2c_client *client = v4l2_get_subdevdata(sd);
> +
> +     if (!v4l2_chip_match_i2c_client(client, &reg->match))
> +             return -EINVAL;
> +     if (!capable(CAP_SYS_ADMIN))
> +             return -EPERM;
> +
> +     return reg->val < 0 ? -EINVAL : 0;
> +}
> +
> +/*
> + * tvp7002_s_register() - set a control
> + * @sd: ptr to v4l2_subdev struct
> + * @ctrl: ptr to v4l2_control struct
> + *
> + * Get the value of a TVP7002 decoder device register.
> + * Returns zero when successful or -EINVAL if register read fails.
> + */
> +static int tvp7002_s_register(struct v4l2_subdev *sd,
> +                                             struct v4l2_dbg_register *reg)
> +{
> +     int wres;
> +     struct i2c_client *client = v4l2_get_subdevdata(sd);
> +
> +     if (!v4l2_chip_match_i2c_client(client, &reg->match))
> +             return -EINVAL;
> +     if (!capable(CAP_SYS_ADMIN))
> +             return -EPERM;
> +
> +     wres = tvp7002_write(sd, reg->reg & 0xff, reg->val & 0xff);
> +
> +     return wres < 0 ? -EINVAL : 0;
> +}
> +#endif
> +
> +/*
> + * tvp7002_queryctrl() - Query a control
> + * @sd: ptr to v4l2_subdev struct
> + * @ctrl: ptr to v4l2_queryctrl struct
> + *
> + * Query a control of a TVP7002 decoder device.
> + * Returns zero when successful or -EINVAL if register read fails.
> + */
> +static int tvp7002_queryctrl(struct v4l2_subdev *sd, struct
> v4l2_queryctrl *qc)
> +{
> +     int i, error;
> +     error = -EINVAL;
> +
> +     v4l2_info(sd, "queryctrl called\n");
> +
> +     for (i = 0; i < ARRAY_SIZE(tvp7002_qctrl); i++)
> +             if (qc->id && qc->id == tvp7002_qctrl[i].id) {
> +                     v4l2_ctrl_query_fill(qc, tvp7002_qctrl[i].minimum,
> +                                     tvp7002_qctrl[i].maximum,
> +                                     tvp7002_qctrl[i].step,
> +                                     tvp7002_qctrl[i].default_value);
> +                     error = 0;
> +                     break;
> +             }
> +
> +     return error;
> +}
> +
> +/*
> + * tvp7002_s_stream() - V4L2 decoder i/f handler for s_stream
> + * @sd: pointer to standard V4L2 sub-device structure
> + * @enable: streaming enable or disable
> + *
> + * Sets streaming to enable or disable, if possible.
> + */
> +static int tvp7002_s_stream(struct v4l2_subdev *sd, int enable)
> +{
> +     int err = 0;
> +     struct tvp7002 *decoder = to_tvp7002(sd);
> +
> +     if (decoder->streaming == enable)
> +             return 0;
> +
> +     if (enable) {
> +             /* Power Up Sequence */
> +             err = tvp7002_write(sd, TVP7002_PWR_CTL, 0x00);
> +             if (err) {
> +                     v4l2_err(sd, "Unable to turn on decoder\n");
> +                     err = -EINVAL;
> +             }
> +             err = tvp7002_write_inittab(sd, tvp7002_init_default);
> +             if (err < 0) {
> +                     v4l2_err(sd, "Unable to initialize\n");
> +                     err = -EINVAL;
> +             }
> +             /* Detect if not already detected */
> +             err = tvp7002_read(sd, TVP7002_CHIP_REV);
> +             if (err < 0) {
> +                     v4l2_err(sd, "Unable to detect decoder\n");
> +                     err = -EINVAL;
> +             }
> +             decoder->streaming = enable;
> +     } else {
> +             /* Power Down Sequence */
> +             err = tvp7002_write(sd, TVP7002_PWR_CTL, 0x40);
> +             if (err) {
> +                     v4l2_err(sd, "Unable to turn off decoder\n");
> +                     return err;
> +             }
> +             decoder->streaming = enable;
> +     }
> +
> +     return err;
> +}
> +
> +/* Specific video subsystem operation handlers */
> +static const struct v4l2_subdev_video_ops tvp7002_video_ops = {
> +     .querystd = tvp7002_querystd,
> +     .s_stream = tvp7002_s_stream,
> +     .g_fmt = tvp7002_g_fmt,
> +};
> +
> +/* V4L2 Operations handlers */
> +static const struct v4l2_subdev_core_ops tvp7002_core_ops = {
> +     .g_chip_ident = tvp7002_g_chip_ident,
> +     .log_status = tvp7002_log_status,
> +     .g_ctrl = tvp7002_g_ctrl,
> +     .s_ctrl = tvp7002_s_ctrl,
> +     .queryctrl = tvp7002_queryctrl,
> +     .s_std = tvp7002_s_std,
> +#ifdef CONFIG_VIDEO_ADV_DEBUG
> +     .g_register = tvp7002_g_register,
> +     .s_register = tvp7002_s_register,
> +#endif
> +};
> +
> +static const struct v4l2_subdev_ops tvp7002_ops = {
> +     .core = &tvp7002_core_ops,
> +     .video = &tvp7002_video_ops,
> +};
> +
> +/* Default driver settings */
> +static struct tvp7002 tvp7002_dev = {
> +     .streaming = 0,
> +
> +     .pix = {
> +             .width = 720,
> +             .height = 480,
> +             .pixelformat = V4L2_PIX_FMT_UYVY,
> +             .field = V4L2_FIELD_INTERLACED,
> +             .bytesperline = 720 * 2,
> +             .sizeimage = 720 * 2 * 480,
> +             .colorspace = V4L2_COLORSPACE_SMPTE170M,
> +             },
> +
> +     .video_mode = V4L2_STD_NTSC,
> +};
> +
> +/*
> + * tvp7002_reset - Reset a TVP7002 device
> + * @sd: ptr to v4l2_subdev struct
> + * @val: unsigned integer (not used)
> + *
> + * Reset the TVP7002 device
> + * Returns zero when successful or -EINVAL if register read fails.
> + */
> +static int tvp7002_reset(struct v4l2_subdev *sd, u32 val)
> +{
> +     struct tvp7002 *core = to_tvp7002(sd);
> +     int polarity;
> +     int error;
> +
> +     error = tvp7002_read(sd, TVP7002_CHIP_REV);
> +     if (error < 0) {
> +             error = -EINVAL;
> +             goto found_error;
> +     }
> +
> +     if (error == 0x02) {
> +             v4l2_info(sd, "rev. %02x detected.\n", error);
> +     } else {
> +             v4l2_info(sd, "unknown revision detected.\n");
> +             v4l2_info(sd, "revision number is %02x\n", error);
> +     }
> +
> +     /* Initializes TVP7002 to its default values */
> +     error = tvp7002_write_inittab(sd, tvp7002_init_default);
> +     if (error < 0) {
> +             error = -EINVAL;
> +             goto found_error;
> +     }
> +
> +     /* Set polarity information after registers have been set */
> +     polarity = 0x5b | core->pdata->hs_polarity << 5
> +                     | core->pdata->vs_polarity << 2;
> +     error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity);
> +     if (error < 0) {
> +             error = -EINVAL;
> +             goto found_error;
> +     }
> +
> +     polarity = 0x0  | core->pdata->fid_polarity << 2
> +                     | core->pdata->sog_polarity << 1
> +                     | core->pdata->clk_polarity;
> +     error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity);
> +     if (error < 0) {
> +             error = -EINVAL;
> +             goto found_error;
> +     }
> +
> +found_error:
> +     return error;
> +};
> +
> +/*
> + * tvp7002_probe - Reset a TVP7002 device
> + * @sd: ptr to v4l2_subdev struct
> + * @ctrl: ptr to i2c_device_id struct
> + *
> + * Reset the TVP7002 device
> + * Returns zero when successful or -EINVAL if register read fails.
> + */
> +static int tvp7002_probe(struct i2c_client *c, const struct i2c_device_id
> *id)
> +{
> +     struct v4l2_subdev *sd;
> +     struct tvp7002 *core;
> +     int polarity;
> +     int error;
> +
> +     /* Check if the adapter supports the needed features */
> +     if (!i2c_check_functionality(c->adapter,
> +             I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
> +             return -EIO;
> +
> +     if (!c->dev.platform_data) {
> +             v4l2_err(c, "No platform data!!\n");
> +             return -ENODEV;
> +     }
> +
> +     core = kzalloc(sizeof(struct tvp7002), GFP_KERNEL);
> +
> +     if (!core)
> +             return -ENOMEM;
> +
> +     *core = tvp7002_dev;
> +     sd = &core->sd;
> +     core->pdata = c->dev.platform_data;
> +
> +     v4l2_i2c_subdev_init(sd, c, &tvp7002_ops);
> +     v4l_info(c, "tvp7002 found @ 0x%02x (%s)\n",
> +                                     c->addr << 1, c->adapter->name);
> +
> +     /* Initializes TVP7002 to its default values */
> +     error = tvp7002_write_inittab(sd, tvp7002_init_default);
> +     if (error < 0) {
> +             error = -EINVAL;
> +             goto found_error;
> +     }
> +
> +     /* Set polarity information after registers have been set */
> +     polarity = 0x5b | core->pdata->hs_polarity << 5
> +                     | core->pdata->vs_polarity << 2;
> +     error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity);
> +     if (error < 0) {
> +             error = -EINVAL;
> +             goto found_error;
> +     }
> +
> +     polarity = 0x0  | core->pdata->fid_polarity << 2
> +                     | core->pdata->sog_polarity << 1
> +                     | core->pdata->clk_polarity;
> +     error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity);
> +     if (error < 0) {
> +             error = -EINVAL;
> +             goto found_error;
> +     }
> +
> +     if (debug > 1)
> +             tvp7002_log_status(sd);
> +
> +found_error:
> +     if (error < 0)
> +             kfree(core);
> +
> +     return error;
> +}
> +
> +/*
> + * tvp7002_remove - Remove TVP7002 device support
> + * @c: ptr to i2c_client struct
> + *
> + * Reset the TVP7002 device
> + * Returns zero when successful or -EINVAL if register read fails.
> + */
> +static int tvp7002_remove(struct i2c_client *c)
> +{
> +     struct v4l2_subdev *sd = i2c_get_clientdata(c);
> +
> +     v4l2_dbg(1, debug, sd, "removing tvp7002 adapter"
> +                             "on address 0x%x\n", c->addr << 1);
> +
> +     v4l2_device_unregister_subdev(sd);
> +     kfree(to_tvp7002(sd));
> +     return 0;
> +}
> +
> +/* I2C Device ID table */
> +static const struct i2c_device_id tvp7002_id[] = {
> +     { "tvp7002", 0 },
> +     { }
> +};
> +MODULE_DEVICE_TABLE(i2c, tvp7002_id);
> +
> +/* I2C driver data */
> +static struct i2c_driver tvp7002_driver = {
> +     .driver = {
> +             .owner = THIS_MODULE,
> +             .name = "tvp7002",
> +     },
> +     .probe = tvp7002_probe,
> +     .remove = tvp7002_remove,
> +     .id_table = tvp7002_id,
> +};
> +
> +/*
> + * tvp7002_init - Initialize driver via I2C interface
> + *
> + * Register the TVP7002 driver.
> + * Returns 0 on success or < 0 on failure.
> + */
> +static int __init tvp7002_init(void)
> +{
> +     return i2c_add_driver(&tvp7002_driver);
> +}
> +
> +/*
> + * tvp7002_exit - Remove driver via I2C interface
> + *
> + * Unregister the TVP7002 driver.
> + * Returns 0 on success or < 0 on failure.
> + */
> +static void __exit tvp7002_exit(void)
> +{
> +     i2c_del_driver(&tvp7002_driver);
> +}
> +
> +module_init(tvp7002_init);
> +module_exit(tvp7002_exit);
> --
> 1.6.0.4
>
Murali Karicheri Sept. 4, 2009, 7:41 p.m. UTC | #2
Santiago,

Also modify the querystd() by re-using the code from LSP 2.10 as I have asked very beginning. This logic is tested well and works.

Murali Karicheri
Software Design Engineer
Texas Instruments Inc.
Germantown, MD 20874
new phone: 301-407-9583
Old Phone : 301-515-3736 (will be deprecated)
email: m-karicheri2@ti.com

>-----Original Message-----
>From: Narnakaje, Snehaprabha
>Sent: Friday, September 04, 2009 11:18 AM
>To: santiago.nunez@ridgerun.com; davinci-linux-open-
>source@linux.davincidsp.com
>Cc: Karicheri, Muralidharan; todd.fischer@ridgerun.com;
>diego.dompe@ridgerun.com; clark.becker@ridgerun.com
>Subject: RE: [PATCH 5/6 v4] Updated TVP7002 driver for DM365
>
>Santiago,
>
>> -----Original Message-----
>> From: santiago.nunez@ridgerun.com [mailto:santiago.nunez@ridgerun.com]
>> Sent: Thursday, September 03, 2009 6:53 PM
>> To: davinci-linux-open-source@linux.davincidsp.com
>> Cc: Karicheri, Muralidharan; todd.fischer@ridgerun.com;
>> diego.dompe@ridgerun.com; clark.becker@ridgerun.com; Narnakaje,
>> Snehaprabha; Santiago Nunez-Corrales
>> Subject: [PATCH 5/6 v4] Updated TVP7002 driver for DM365
>>
>> From: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
>>
>> This patch provides the implementation of the TVP7002 decoder
>> driver for DM365. Implemented revisions by Vaibhav Hiremath
>> and Hans Verkuil. Removed most of controls, cleared up logic,
>> cleaned up code. Tested on DM365 TI EVM.
>>
>> Signed-off-by: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
>> ---
>>  drivers/media/video/tvp7002.c | 1464
>
>[...]
>
>> +
>> +/*
>> + * tvp7002_get_video_mode() - V4L2 decoder interface handler for
>querystd
>> + * @sd: pointer to standard V4L2 sub-device structure
>> + *
>> + * Returns the current standard detected by TVP7002. If no active input
>> is
>> + * detected, returns -1
>> + */
>> +static v4l2_std_id tvp7002_get_video_mode(struct v4l2_subdev *sd)
>> +{
>> +    int reg01, reg02, reg03;
>> +
>> +    reg01 = tvp7002_read(sd, TVP7002_HPLL_FDBK_DIV_MSBS);
>> +    reg02 = tvp7002_read(sd, TVP7002_HPLL_FDBK_DIV_LSBS);
>> +    reg03 = tvp7002_read(sd, TVP7002_HPLL_CRTL);
>> +
>> +    if (reg01 < 0 || reg02 < 0 || reg03 < 0)
>> +            return V4L2_STD_UNKNOWN;
>> +
>> +    switch (reg01) {
>> +    case 0x35:
>> +            if (reg02 == 0xa0)
>> +                    return V4L2_STD_NTSC;
>> +            else
>> +                    return V4L2_STD_PAL;
>> +    case 0x36:
>> +            if (reg02 == 0xa0)
>> +                    return V4L2_STD_525P_60;
>> +            else
>> +                    return V4L2_STD_625P_50;
>> +    case 0x67:
>> +            return V4L2_STD_720P_60;
>> +    case 0x7b:
>> +            return V4L2_STD_720P_50;
>> +    case 0x89:
>> +            if (reg03 == 0x98)
>> +                    return V4L2_STD_1080I_60;
>> +            else
>> +                    return V4L2_STD_1080P_60;
>> +            break;
>> +    case 0xa5:
>> +            if (reg03 == 0x90)
>> +                    return V4L2_STD_1080I_50;
>> +            else
>> +                    return V4L2_STD_1080P_50;
>> +    default:
>> +            return V4L2_STD_UNKNOWN;
>> +    }
>> +}
>> +
>> +/*
>> + * tvp7002_querystd() - V4L2 decoder interface handler for querystd
>> + * @sd: pointer to standard V4L2 sub-device structure
>> + * @std_id: standard V4L2 std_id ioctl enum
>> + *
>> + * Returns the current standard detected by TVP7002. If no active input
>> is
>> + * detected, returns -EINVAL
>> + */
>> +static int tvp7002_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
>> +{
>> +    v4l2_std_id current_std;
>> +    u8 sync_lock_status;
>> +    int res;
>> +
>> +    if (std_id == NULL)
>> +            return -EINVAL;
>> +
>> +    /* get the current standard */
>> +    res = tvp7002_get_video_mode(sd);
>> +    if (res == V4L2_STD_UNKNOWN)
>> +            return -EINVAL;
>
>There seems to be a bug here - You will mostly get V4L2_STD_UNKNOWN for HD
>resolutions. The tvp7002_get_video_mode() API returns v4l2_std_id (which is
>long long - 64 bits), but you are storing the return value in "int res".
>You should change it to -
>
>v4l2_std_id res;
>
>Note that I am trying to integrate your driver with VPIF capture on DM6467
>and thus running into issues like this. It would be good to know - whether
>you have tested the TVP7002 driver on DM365 using a capture application.
>
>Thanks
>Sneha
>
>> +    current_std = res;
>> +
>> +    /* check whether signal is locked */
>> +    sync_lock_status = tvp7002_read(sd, TVP7002_SYNC_DETECT_STAT);
>> +
>> +    if (0x02 != (sync_lock_status & 0xff))
>> +            return -EINVAL; /* No input detected */
>> +
>> +    *std_id = current_std;
>> +
>> +    v4l2_dbg(1, debug, sd, "Current STD: %d %d @ %d Hz\n",
>> +            tvp7002_resolutions[tvp7002_from_std(current_std)].hres,
>> +            tvp7002_resolutions[tvp7002_from_std(current_std)].vres,
>> +            tvp7002_resolutions[tvp7002_from_std(current_std)].frate);
>> +    return 0;
>> +}
>> +
>> +/*
>> + * tvp7002_try_fmt_cap() - V4L2 decoder interface handler for try_fmt
>> + * @sd: pointer to standard V4L2 sub-device structure
>> + * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
>> + *
>> + * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
>> + * ioctl is used to negotiate the image capture size and pixel format
>> + * without actually making it take effect.
>> + */
>> +static int tvp7002_try_fmt_cap(struct v4l2_subdev *sd, struct
>v4l2_format
>> *f)
>> +{
>> +    struct v4l2_pix_format *pix;
>> +    v4l2_std_id current_std;
>> +    int res;
>> +
>> +    if (f == NULL)
>> +            return -EINVAL;
>> +
>> +    if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
>> +            /* only capture is supported */
>> +            return -EINVAL;
>> +
>> +    pix = &f->fmt.pix;
>> +
>> +    /* Calculate height and width based on current standard */
>> +    res = tvp7002_get_video_mode(sd);
>> +    if (res < 0)
>> +            return -EINVAL;
>> +    current_std = res;
>> +
>> +    pix->width = NUM_ACTIVE_PIXELS(current_std);
>> +    pix->height = NUM_ACTIVE_LINES(current_std);
>> +
>> +    pix->pixelformat = V4L2_PIX_FMT_UYVY;
>> +
>> +    pix->field = V4L2_FIELD_INTERLACED;
>> +    pix->bytesperline = pix->width * 2;
>> +    pix->sizeimage = pix->bytesperline * pix->height;
>> +    pix->colorspace = V4L2_COLORSPACE_REC709;
>> +    pix->priv = 0;
>> +
>> +    v4l2_dbg(1, debug, sd, "Try FMT: pixelformat - %s, bytesperline -
>> %d"
>> +                    "Width - %d, Height - %d",
>> +                    "8-bit UYVY 4:2:2 Format", pix->bytesperline,
>> +                    pix->width, pix->height);
>> +    return 0;
>> +}
>> +
>> +/**
>> + * tvp7002_s_fmt_cap() - V4L2 decoder interface handler for s_fmt
>> + * @sd: pointer to standard V4L2 sub-device structure
>> + * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
>> + *
>> + * If the requested format is supported, configures the HW to use that
>> + * format, returns error code if format not supported or HW can't be
>> + * correctly configured.
>> + */
>> +static int tvp7002_s_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format
>> *f)
>> +{
>> +    struct tvp7002 *decoder = to_tvp7002(sd);
>> +    struct v4l2_pix_format *pix;
>> +    int rval;
>> +
>> +    if (f == NULL)
>> +            return -EINVAL;
>> +
>> +    if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
>> +            /* only capture is supported */
>> +            return -EINVAL;
>> +
>> +    pix = &f->fmt.pix;
>> +    rval = tvp7002_try_fmt_cap(sd, f);
>> +    if (rval)
>> +            return rval;
>> +
>> +    decoder->pix = *pix;
>> +
>> +    return rval;
>> +}
>> +
>> +/*
>> + * tvp7002_g_fmt() - V4L2 decoder interface handler for tvp7002_g_fmt
>> + * @sd: pointer to standard V4L2 sub-device structure
>> + * @f: pointer to standard V4L2 v4l2_format structure
>> + *
>> + * Returns the decoder's current pixel format in the v4l2_format
>> + * parameter.
>> + */
>> +static int tvp7002_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
>> +{
>> +    struct tvp7002 *decoder = to_tvp7002(sd);
>> +
>> +    if (f == NULL)
>> +            return -EINVAL;
>> +
>> +    if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
>> +            /* only capture is supported */
>> +            return -EINVAL;
>> +
>> +    f->fmt.pix = decoder->pix;
>> +
>> +    v4l2_dbg(1, debug, sd, "Current FMT: bytesperline - %d"
>> +                    "Width - %d, Height - %d",
>> +                    decoder->pix.bytesperline,
>> +                    decoder->pix.width, decoder->pix.height);
>> +    return 0;
>> +}
>> +
>> +/*
>> + * tvp7002_s_ctrl() - Set a control
>> + * @sd: ptr to v4l2_subdev struct
>> + * @ctrl: ptr to v4l2_control struct
>> + *
>> + * Set a control for a TVP7002 decoder device.
>> + * Returns zero when successful or -EINVAL if register access fails.
>> + */
>> +static int tvp7002_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
>> +{
>> +    struct tvp7002 *decoder = to_tvp7002(sd);
>> +    int vmd = 0;
>> +
>> +    decoder->video_mode = std;
>> +    vmd = tvp7002_from_std(std);
>> +
>> +    v4l2_dbg(1, debug, sd, "Set video std mode to %llx.\n", std);
>> +
>> +    return tvp7002_set_video_mode(sd, vmd);
>> +}
>> +
>> +/*
>> + * tvp7002_g_ctrl() - Get a control
>> + * @sd: ptr to v4l2_subdev struct
>> + * @ctrl: ptr to v4l2_control struct
>> + *
>> + * Get a control for a TVP7002 decoder device.
>> + * Returns zero when successful or -EINVAL if register access fails.
>> + */
>> +static int tvp7002_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control
>> *ctrl)
>> +{
>> +    int rval, gval, bval;
>> +    int res;
>> +
>> +    v4l2_info(sd, "g_ctrl called\n");
>> +
>> +    switch (ctrl->id) {
>> +    case V4L2_CID_GAIN:
>> +            rval = tvp7002_read(sd, TVP7002_R_FINE_GAIN);
>> +            gval = tvp7002_read(sd, TVP7002_G_FINE_GAIN);
>> +            bval = tvp7002_read(sd, TVP7002_B_FINE_GAIN);
>> +
>> +            if (rval < 0 || gval < 0 || bval < 0) {
>> +                    res = -1;
>> +            } else if (rval != gval || rval != bval) {
>> +                    res = -1;
>> +            } else {
>> +                    ctrl->value = rval & 0x0F;
>> +                    res = ctrl->value;
>> +            }
>> +            break;
>> +    default:
>> +            res = -1;
>> +            break;
>> +    }
>> +
>> +    return res < 0 ? res : 0;
>> +}
>> +
>> +/*
>> + * tvp7002_s_ctrl() - Set a control
>> + * @sd: ptr to v4l2_subdev struct
>> + * @ctrl: ptr to v4l2_control struct
>> + *
>> + * Set a control in TVP7002 decoder device.
>> + * Returns zero when successful or -EINVAL if register access fails.
>> + */
>> +static int tvp7002_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control
>> *ctrl)
>> +{
>> +    int rval, gval, bval;
>> +    int error;
>> +    u8 i, n;
>> +    n = ARRAY_SIZE(tvp7002_qctrl);
>> +
>> +    for (i = 0; i < n; i++)
>> +            if (ctrl->id == tvp7002_qctrl[i].id)
>> +                    break;
>> +
>> +    if (i == n)
>> +            return -EINVAL;
>> +
>> +    if (ctrl->value < tvp7002_qctrl[i].minimum ||
>> +                    ctrl->value > tvp7002_qctrl[i].maximum)
>> +            return -ERANGE;
>> +
>> +    switch (ctrl->id) {
>> +    case V4L2_CID_GAIN:
>> +            rval = tvp7002_write(sd, TVP7002_R_FINE_GAIN,
>> +                                                    ctrl->value & 0xff);
>> +            gval = tvp7002_write(sd, TVP7002_G_FINE_GAIN,
>> +                                                    ctrl->value & 0xff);
>> +            bval = tvp7002_write(sd, TVP7002_B_FINE_GAIN,
>> +                                                    ctrl->value & 0xff);
>> +            if (rval < 0  || gval < 0 || bval < 0)
>> +                    error = -1;
>> +            else
>> +                    error = rval;
>> +            break;
>> +    default:
>> +            error = -1;
>> +            break;
>> +    }
>> +
>> +    return error < 0 ? -EINVAL : 0;
>> +}
>> +
>> +/*
>> + * tvp7002_g_register() - Get the value of a register
>> + * @sd: ptr to v4l2_subdev struct
>> + * @vreg: ptr to v4l2_dbg_register struct
>> + *
>> + * Get the value of a TVP7002 decoder device register.
>> + * Returns zero when successful, -EINVAL if register read fails or
>> + * access to I2C client fails, -EPERM if the call is not allowed
>> + * by diabled CAP_SYS_ADMIN.
>> + */
>> +#ifdef CONFIG_VIDEO_ADV_DEBUG
>> +static int tvp7002_g_register(struct v4l2_subdev *sd,
>> +                                            struct v4l2_dbg_register *reg)
>> +{
>> +    struct i2c_client *client = v4l2_get_subdevdata(sd);
>> +
>> +    if (!v4l2_chip_match_i2c_client(client, &reg->match))
>> +            return -EINVAL;
>> +    if (!capable(CAP_SYS_ADMIN))
>> +            return -EPERM;
>> +
>> +    return reg->val < 0 ? -EINVAL : 0;
>> +}
>> +
>> +/*
>> + * tvp7002_s_register() - set a control
>> + * @sd: ptr to v4l2_subdev struct
>> + * @ctrl: ptr to v4l2_control struct
>> + *
>> + * Get the value of a TVP7002 decoder device register.
>> + * Returns zero when successful or -EINVAL if register read fails.
>> + */
>> +static int tvp7002_s_register(struct v4l2_subdev *sd,
>> +                                            struct v4l2_dbg_register *reg)
>> +{
>> +    int wres;
>> +    struct i2c_client *client = v4l2_get_subdevdata(sd);
>> +
>> +    if (!v4l2_chip_match_i2c_client(client, &reg->match))
>> +            return -EINVAL;
>> +    if (!capable(CAP_SYS_ADMIN))
>> +            return -EPERM;
>> +
>> +    wres = tvp7002_write(sd, reg->reg & 0xff, reg->val & 0xff);
>> +
>> +    return wres < 0 ? -EINVAL : 0;
>> +}
>> +#endif
>> +
>> +/*
>> + * tvp7002_queryctrl() - Query a control
>> + * @sd: ptr to v4l2_subdev struct
>> + * @ctrl: ptr to v4l2_queryctrl struct
>> + *
>> + * Query a control of a TVP7002 decoder device.
>> + * Returns zero when successful or -EINVAL if register read fails.
>> + */
>> +static int tvp7002_queryctrl(struct v4l2_subdev *sd, struct
>> v4l2_queryctrl *qc)
>> +{
>> +    int i, error;
>> +    error = -EINVAL;
>> +
>> +    v4l2_info(sd, "queryctrl called\n");
>> +
>> +    for (i = 0; i < ARRAY_SIZE(tvp7002_qctrl); i++)
>> +            if (qc->id && qc->id == tvp7002_qctrl[i].id) {
>> +                    v4l2_ctrl_query_fill(qc, tvp7002_qctrl[i].minimum,
>> +                                    tvp7002_qctrl[i].maximum,
>> +                                    tvp7002_qctrl[i].step,
>> +                                    tvp7002_qctrl[i].default_value);
>> +                    error = 0;
>> +                    break;
>> +            }
>> +
>> +    return error;
>> +}
>> +
>> +/*
>> + * tvp7002_s_stream() - V4L2 decoder i/f handler for s_stream
>> + * @sd: pointer to standard V4L2 sub-device structure
>> + * @enable: streaming enable or disable
>> + *
>> + * Sets streaming to enable or disable, if possible.
>> + */
>> +static int tvp7002_s_stream(struct v4l2_subdev *sd, int enable)
>> +{
>> +    int err = 0;
>> +    struct tvp7002 *decoder = to_tvp7002(sd);
>> +
>> +    if (decoder->streaming == enable)
>> +            return 0;
>> +
>> +    if (enable) {
>> +            /* Power Up Sequence */
>> +            err = tvp7002_write(sd, TVP7002_PWR_CTL, 0x00);
>> +            if (err) {
>> +                    v4l2_err(sd, "Unable to turn on decoder\n");
>> +                    err = -EINVAL;
>> +            }
>> +            err = tvp7002_write_inittab(sd, tvp7002_init_default);
>> +            if (err < 0) {
>> +                    v4l2_err(sd, "Unable to initialize\n");
>> +                    err = -EINVAL;
>> +            }
>> +            /* Detect if not already detected */
>> +            err = tvp7002_read(sd, TVP7002_CHIP_REV);
>> +            if (err < 0) {
>> +                    v4l2_err(sd, "Unable to detect decoder\n");
>> +                    err = -EINVAL;
>> +            }
>> +            decoder->streaming = enable;
>> +    } else {
>> +            /* Power Down Sequence */
>> +            err = tvp7002_write(sd, TVP7002_PWR_CTL, 0x40);
>> +            if (err) {
>> +                    v4l2_err(sd, "Unable to turn off decoder\n");
>> +                    return err;
>> +            }
>> +            decoder->streaming = enable;
>> +    }
>> +
>> +    return err;
>> +}
>> +
>> +/* Specific video subsystem operation handlers */
>> +static const struct v4l2_subdev_video_ops tvp7002_video_ops = {
>> +    .querystd = tvp7002_querystd,
>> +    .s_stream = tvp7002_s_stream,
>> +    .g_fmt = tvp7002_g_fmt,
>> +};
>> +
>> +/* V4L2 Operations handlers */
>> +static const struct v4l2_subdev_core_ops tvp7002_core_ops = {
>> +    .g_chip_ident = tvp7002_g_chip_ident,
>> +    .log_status = tvp7002_log_status,
>> +    .g_ctrl = tvp7002_g_ctrl,
>> +    .s_ctrl = tvp7002_s_ctrl,
>> +    .queryctrl = tvp7002_queryctrl,
>> +    .s_std = tvp7002_s_std,
>> +#ifdef CONFIG_VIDEO_ADV_DEBUG
>> +    .g_register = tvp7002_g_register,
>> +    .s_register = tvp7002_s_register,
>> +#endif
>> +};
>> +
>> +static const struct v4l2_subdev_ops tvp7002_ops = {
>> +    .core = &tvp7002_core_ops,
>> +    .video = &tvp7002_video_ops,
>> +};
>> +
>> +/* Default driver settings */
>> +static struct tvp7002 tvp7002_dev = {
>> +    .streaming = 0,
>> +
>> +    .pix = {
>> +            .width = 720,
>> +            .height = 480,
>> +            .pixelformat = V4L2_PIX_FMT_UYVY,
>> +            .field = V4L2_FIELD_INTERLACED,
>> +            .bytesperline = 720 * 2,
>> +            .sizeimage = 720 * 2 * 480,
>> +            .colorspace = V4L2_COLORSPACE_SMPTE170M,
>> +            },
>> +
>> +    .video_mode = V4L2_STD_NTSC,
>> +};
>> +
>> +/*
>> + * tvp7002_reset - Reset a TVP7002 device
>> + * @sd: ptr to v4l2_subdev struct
>> + * @val: unsigned integer (not used)
>> + *
>> + * Reset the TVP7002 device
>> + * Returns zero when successful or -EINVAL if register read fails.
>> + */
>> +static int tvp7002_reset(struct v4l2_subdev *sd, u32 val)
>> +{
>> +    struct tvp7002 *core = to_tvp7002(sd);
>> +    int polarity;
>> +    int error;
>> +
>> +    error = tvp7002_read(sd, TVP7002_CHIP_REV);
>> +    if (error < 0) {
>> +            error = -EINVAL;
>> +            goto found_error;
>> +    }
>> +
>> +    if (error == 0x02) {
>> +            v4l2_info(sd, "rev. %02x detected.\n", error);
>> +    } else {
>> +            v4l2_info(sd, "unknown revision detected.\n");
>> +            v4l2_info(sd, "revision number is %02x\n", error);
>> +    }
>> +
>> +    /* Initializes TVP7002 to its default values */
>> +    error = tvp7002_write_inittab(sd, tvp7002_init_default);
>> +    if (error < 0) {
>> +            error = -EINVAL;
>> +            goto found_error;
>> +    }
>> +
>> +    /* Set polarity information after registers have been set */
>> +    polarity = 0x5b | core->pdata->hs_polarity << 5
>> +                    | core->pdata->vs_polarity << 2;
>> +    error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity);
>> +    if (error < 0) {
>> +            error = -EINVAL;
>> +            goto found_error;
>> +    }
>> +
>> +    polarity = 0x0  | core->pdata->fid_polarity << 2
>> +                    | core->pdata->sog_polarity << 1
>> +                    | core->pdata->clk_polarity;
>> +    error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity);
>> +    if (error < 0) {
>> +            error = -EINVAL;
>> +            goto found_error;
>> +    }
>> +
>> +found_error:
>> +    return error;
>> +};
>> +
>> +/*
>> + * tvp7002_probe - Reset a TVP7002 device
>> + * @sd: ptr to v4l2_subdev struct
>> + * @ctrl: ptr to i2c_device_id struct
>> + *
>> + * Reset the TVP7002 device
>> + * Returns zero when successful or -EINVAL if register read fails.
>> + */
>> +static int tvp7002_probe(struct i2c_client *c, const struct
>i2c_device_id
>> *id)
>> +{
>> +    struct v4l2_subdev *sd;
>> +    struct tvp7002 *core;
>> +    int polarity;
>> +    int error;
>> +
>> +    /* Check if the adapter supports the needed features */
>> +    if (!i2c_check_functionality(c->adapter,
>> +            I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
>> +            return -EIO;
>> +
>> +    if (!c->dev.platform_data) {
>> +            v4l2_err(c, "No platform data!!\n");
>> +            return -ENODEV;
>> +    }
>> +
>> +    core = kzalloc(sizeof(struct tvp7002), GFP_KERNEL);
>> +
>> +    if (!core)
>> +            return -ENOMEM;
>> +
>> +    *core = tvp7002_dev;
>> +    sd = &core->sd;
>> +    core->pdata = c->dev.platform_data;
>> +
>> +    v4l2_i2c_subdev_init(sd, c, &tvp7002_ops);
>> +    v4l_info(c, "tvp7002 found @ 0x%02x (%s)\n",
>> +                                    c->addr << 1, c->adapter->name);
>> +
>> +    /* Initializes TVP7002 to its default values */
>> +    error = tvp7002_write_inittab(sd, tvp7002_init_default);
>> +    if (error < 0) {
>> +            error = -EINVAL;
>> +            goto found_error;
>> +    }
>> +
>> +    /* Set polarity information after registers have been set */
>> +    polarity = 0x5b | core->pdata->hs_polarity << 5
>> +                    | core->pdata->vs_polarity << 2;
>> +    error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity);
>> +    if (error < 0) {
>> +            error = -EINVAL;
>> +            goto found_error;
>> +    }
>> +
>> +    polarity = 0x0  | core->pdata->fid_polarity << 2
>> +                    | core->pdata->sog_polarity << 1
>> +                    | core->pdata->clk_polarity;
>> +    error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity);
>> +    if (error < 0) {
>> +            error = -EINVAL;
>> +            goto found_error;
>> +    }
>> +
>> +    if (debug > 1)
>> +            tvp7002_log_status(sd);
>> +
>> +found_error:
>> +    if (error < 0)
>> +            kfree(core);
>> +
>> +    return error;
>> +}
>> +
>> +/*
>> + * tvp7002_remove - Remove TVP7002 device support
>> + * @c: ptr to i2c_client struct
>> + *
>> + * Reset the TVP7002 device
>> + * Returns zero when successful or -EINVAL if register read fails.
>> + */
>> +static int tvp7002_remove(struct i2c_client *c)
>> +{
>> +    struct v4l2_subdev *sd = i2c_get_clientdata(c);
>> +
>> +    v4l2_dbg(1, debug, sd, "removing tvp7002 adapter"
>> +                            "on address 0x%x\n", c->addr << 1);
>> +
>> +    v4l2_device_unregister_subdev(sd);
>> +    kfree(to_tvp7002(sd));
>> +    return 0;
>> +}
>> +
>> +/* I2C Device ID table */
>> +static const struct i2c_device_id tvp7002_id[] = {
>> +    { "tvp7002", 0 },
>> +    { }
>> +};
>> +MODULE_DEVICE_TABLE(i2c, tvp7002_id);
>> +
>> +/* I2C driver data */
>> +static struct i2c_driver tvp7002_driver = {
>> +    .driver = {
>> +            .owner = THIS_MODULE,
>> +            .name = "tvp7002",
>> +    },
>> +    .probe = tvp7002_probe,
>> +    .remove = tvp7002_remove,
>> +    .id_table = tvp7002_id,
>> +};
>> +
>> +/*
>> + * tvp7002_init - Initialize driver via I2C interface
>> + *
>> + * Register the TVP7002 driver.
>> + * Returns 0 on success or < 0 on failure.
>> + */
>> +static int __init tvp7002_init(void)
>> +{
>> +    return i2c_add_driver(&tvp7002_driver);
>> +}
>> +
>> +/*
>> + * tvp7002_exit - Remove driver via I2C interface
>> + *
>> + * Unregister the TVP7002 driver.
>> + * Returns 0 on success or < 0 on failure.
>> + */
>> +static void __exit tvp7002_exit(void)
>> +{
>> +    i2c_del_driver(&tvp7002_driver);
>> +}
>> +
>> +module_init(tvp7002_init);
>> +module_exit(tvp7002_exit);
>> --
>> 1.6.0.4
>>
Santiago Nunez-Corrales Sept. 4, 2009, 8:20 p.m. UTC | #3
Murali,


Changing the querystd code. I found various errors w.r.t. LPS 2.10 from 
what  I was doing in my code. I am reading the wrong register, which is 
causing the code to return from the call incorrectly.

Regards,

Karicheri, Muralidharan wrote:
> Santiago,
>
> Also modify the querystd() by re-using the code from LSP 2.10 as I have asked very beginning. This logic is tested well and works.
>
> Murali Karicheri
> Software Design Engineer
> Texas Instruments Inc.
> Germantown, MD 20874
> new phone: 301-407-9583
> Old Phone : 301-515-3736 (will be deprecated)
> email: m-karicheri2@ti.com
>
>   
>> -----Original Message-----
>> From: Narnakaje, Snehaprabha
>> Sent: Friday, September 04, 2009 11:18 AM
>> To: santiago.nunez@ridgerun.com; davinci-linux-open-
>> source@linux.davincidsp.com
>> Cc: Karicheri, Muralidharan; todd.fischer@ridgerun.com;
>> diego.dompe@ridgerun.com; clark.becker@ridgerun.com
>> Subject: RE: [PATCH 5/6 v4] Updated TVP7002 driver for DM365
>>
>> Santiago,
>>
>>     
>>> -----Original Message-----
>>> From: santiago.nunez@ridgerun.com [mailto:santiago.nunez@ridgerun.com]
>>> Sent: Thursday, September 03, 2009 6:53 PM
>>> To: davinci-linux-open-source@linux.davincidsp.com
>>> Cc: Karicheri, Muralidharan; todd.fischer@ridgerun.com;
>>> diego.dompe@ridgerun.com; clark.becker@ridgerun.com; Narnakaje,
>>> Snehaprabha; Santiago Nunez-Corrales
>>> Subject: [PATCH 5/6 v4] Updated TVP7002 driver for DM365
>>>
>>> From: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
>>>
>>> This patch provides the implementation of the TVP7002 decoder
>>> driver for DM365. Implemented revisions by Vaibhav Hiremath
>>> and Hans Verkuil. Removed most of controls, cleared up logic,
>>> cleaned up code. Tested on DM365 TI EVM.
>>>
>>> Signed-off-by: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
>>> ---
>>>  drivers/media/video/tvp7002.c | 1464
>>>       
>> [...]
>>
>>     
>>> +
>>> +/*
>>> + * tvp7002_get_video_mode() - V4L2 decoder interface handler for
>>>       
>> querystd
>>     
>>> + * @sd: pointer to standard V4L2 sub-device structure
>>> + *
>>> + * Returns the current standard detected by TVP7002. If no active input
>>> is
>>> + * detected, returns -1
>>> + */
>>> +static v4l2_std_id tvp7002_get_video_mode(struct v4l2_subdev *sd)
>>> +{
>>> +    int reg01, reg02, reg03;
>>> +
>>> +    reg01 = tvp7002_read(sd, TVP7002_HPLL_FDBK_DIV_MSBS);
>>> +    reg02 = tvp7002_read(sd, TVP7002_HPLL_FDBK_DIV_LSBS);
>>> +    reg03 = tvp7002_read(sd, TVP7002_HPLL_CRTL);
>>> +
>>> +    if (reg01 < 0 || reg02 < 0 || reg03 < 0)
>>> +            return V4L2_STD_UNKNOWN;
>>> +
>>> +    switch (reg01) {
>>> +    case 0x35:
>>> +            if (reg02 == 0xa0)
>>> +                    return V4L2_STD_NTSC;
>>> +            else
>>> +                    return V4L2_STD_PAL;
>>> +    case 0x36:
>>> +            if (reg02 == 0xa0)
>>> +                    return V4L2_STD_525P_60;
>>> +            else
>>> +                    return V4L2_STD_625P_50;
>>> +    case 0x67:
>>> +            return V4L2_STD_720P_60;
>>> +    case 0x7b:
>>> +            return V4L2_STD_720P_50;
>>> +    case 0x89:
>>> +            if (reg03 == 0x98)
>>> +                    return V4L2_STD_1080I_60;
>>> +            else
>>> +                    return V4L2_STD_1080P_60;
>>> +            break;
>>> +    case 0xa5:
>>> +            if (reg03 == 0x90)
>>> +                    return V4L2_STD_1080I_50;
>>> +            else
>>> +                    return V4L2_STD_1080P_50;
>>> +    default:
>>> +            return V4L2_STD_UNKNOWN;
>>> +    }
>>> +}
>>> +
>>> +/*
>>> + * tvp7002_querystd() - V4L2 decoder interface handler for querystd
>>> + * @sd: pointer to standard V4L2 sub-device structure
>>> + * @std_id: standard V4L2 std_id ioctl enum
>>> + *
>>> + * Returns the current standard detected by TVP7002. If no active input
>>> is
>>> + * detected, returns -EINVAL
>>> + */
>>> +static int tvp7002_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
>>> +{
>>> +    v4l2_std_id current_std;
>>> +    u8 sync_lock_status;
>>> +    int res;
>>> +
>>> +    if (std_id == NULL)
>>> +            return -EINVAL;
>>> +
>>> +    /* get the current standard */
>>> +    res = tvp7002_get_video_mode(sd);
>>> +    if (res == V4L2_STD_UNKNOWN)
>>> +            return -EINVAL;
>>>       
>> There seems to be a bug here - You will mostly get V4L2_STD_UNKNOWN for HD
>> resolutions. The tvp7002_get_video_mode() API returns v4l2_std_id (which is
>> long long - 64 bits), but you are storing the return value in "int res".
>> You should change it to -
>>
>> v4l2_std_id res;
>>
>> Note that I am trying to integrate your driver with VPIF capture on DM6467
>> and thus running into issues like this. It would be good to know - whether
>> you have tested the TVP7002 driver on DM365 using a capture application.
>>
>> Thanks
>> Sneha
>>
>>     
>>> +    current_std = res;
>>> +
>>> +    /* check whether signal is locked */
>>> +    sync_lock_status = tvp7002_read(sd, TVP7002_SYNC_DETECT_STAT);
>>> +
>>> +    if (0x02 != (sync_lock_status & 0xff))
>>> +            return -EINVAL; /* No input detected */
>>> +
>>> +    *std_id = current_std;
>>> +
>>> +    v4l2_dbg(1, debug, sd, "Current STD: %d %d @ %d Hz\n",
>>> +            tvp7002_resolutions[tvp7002_from_std(current_std)].hres,
>>> +            tvp7002_resolutions[tvp7002_from_std(current_std)].vres,
>>> +            tvp7002_resolutions[tvp7002_from_std(current_std)].frate);
>>> +    return 0;
>>> +}
>>> +
>>> +/*
>>> + * tvp7002_try_fmt_cap() - V4L2 decoder interface handler for try_fmt
>>> + * @sd: pointer to standard V4L2 sub-device structure
>>> + * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
>>> + *
>>> + * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
>>> + * ioctl is used to negotiate the image capture size and pixel format
>>> + * without actually making it take effect.
>>> + */
>>> +static int tvp7002_try_fmt_cap(struct v4l2_subdev *sd, struct
>>>       
>> v4l2_format
>>     
>>> *f)
>>> +{
>>> +    struct v4l2_pix_format *pix;
>>> +    v4l2_std_id current_std;
>>> +    int res;
>>> +
>>> +    if (f == NULL)
>>> +            return -EINVAL;
>>> +
>>> +    if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
>>> +            /* only capture is supported */
>>> +            return -EINVAL;
>>> +
>>> +    pix = &f->fmt.pix;
>>> +
>>> +    /* Calculate height and width based on current standard */
>>> +    res = tvp7002_get_video_mode(sd);
>>> +    if (res < 0)
>>> +            return -EINVAL;
>>> +    current_std = res;
>>> +
>>> +    pix->width = NUM_ACTIVE_PIXELS(current_std);
>>> +    pix->height = NUM_ACTIVE_LINES(current_std);
>>> +
>>> +    pix->pixelformat = V4L2_PIX_FMT_UYVY;
>>> +
>>> +    pix->field = V4L2_FIELD_INTERLACED;
>>> +    pix->bytesperline = pix->width * 2;
>>> +    pix->sizeimage = pix->bytesperline * pix->height;
>>> +    pix->colorspace = V4L2_COLORSPACE_REC709;
>>> +    pix->priv = 0;
>>> +
>>> +    v4l2_dbg(1, debug, sd, "Try FMT: pixelformat - %s, bytesperline -
>>> %d"
>>> +                    "Width - %d, Height - %d",
>>> +                    "8-bit UYVY 4:2:2 Format", pix->bytesperline,
>>> +                    pix->width, pix->height);
>>> +    return 0;
>>> +}
>>> +
>>> +/**
>>> + * tvp7002_s_fmt_cap() - V4L2 decoder interface handler for s_fmt
>>> + * @sd: pointer to standard V4L2 sub-device structure
>>> + * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
>>> + *
>>> + * If the requested format is supported, configures the HW to use that
>>> + * format, returns error code if format not supported or HW can't be
>>> + * correctly configured.
>>> + */
>>> +static int tvp7002_s_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format
>>> *f)
>>> +{
>>> +    struct tvp7002 *decoder = to_tvp7002(sd);
>>> +    struct v4l2_pix_format *pix;
>>> +    int rval;
>>> +
>>> +    if (f == NULL)
>>> +            return -EINVAL;
>>> +
>>> +    if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
>>> +            /* only capture is supported */
>>> +            return -EINVAL;
>>> +
>>> +    pix = &f->fmt.pix;
>>> +    rval = tvp7002_try_fmt_cap(sd, f);
>>> +    if (rval)
>>> +            return rval;
>>> +
>>> +    decoder->pix = *pix;
>>> +
>>> +    return rval;
>>> +}
>>> +
>>> +/*
>>> + * tvp7002_g_fmt() - V4L2 decoder interface handler for tvp7002_g_fmt
>>> + * @sd: pointer to standard V4L2 sub-device structure
>>> + * @f: pointer to standard V4L2 v4l2_format structure
>>> + *
>>> + * Returns the decoder's current pixel format in the v4l2_format
>>> + * parameter.
>>> + */
>>> +static int tvp7002_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
>>> +{
>>> +    struct tvp7002 *decoder = to_tvp7002(sd);
>>> +
>>> +    if (f == NULL)
>>> +            return -EINVAL;
>>> +
>>> +    if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
>>> +            /* only capture is supported */
>>> +            return -EINVAL;
>>> +
>>> +    f->fmt.pix = decoder->pix;
>>> +
>>> +    v4l2_dbg(1, debug, sd, "Current FMT: bytesperline - %d"
>>> +                    "Width - %d, Height - %d",
>>> +                    decoder->pix.bytesperline,
>>> +                    decoder->pix.width, decoder->pix.height);
>>> +    return 0;
>>> +}
>>> +
>>> +/*
>>> + * tvp7002_s_ctrl() - Set a control
>>> + * @sd: ptr to v4l2_subdev struct
>>> + * @ctrl: ptr to v4l2_control struct
>>> + *
>>> + * Set a control for a TVP7002 decoder device.
>>> + * Returns zero when successful or -EINVAL if register access fails.
>>> + */
>>> +static int tvp7002_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
>>> +{
>>> +    struct tvp7002 *decoder = to_tvp7002(sd);
>>> +    int vmd = 0;
>>> +
>>> +    decoder->video_mode = std;
>>> +    vmd = tvp7002_from_std(std);
>>> +
>>> +    v4l2_dbg(1, debug, sd, "Set video std mode to %llx.\n", std);
>>> +
>>> +    return tvp7002_set_video_mode(sd, vmd);
>>> +}
>>> +
>>> +/*
>>> + * tvp7002_g_ctrl() - Get a control
>>> + * @sd: ptr to v4l2_subdev struct
>>> + * @ctrl: ptr to v4l2_control struct
>>> + *
>>> + * Get a control for a TVP7002 decoder device.
>>> + * Returns zero when successful or -EINVAL if register access fails.
>>> + */
>>> +static int tvp7002_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control
>>> *ctrl)
>>> +{
>>> +    int rval, gval, bval;
>>> +    int res;
>>> +
>>> +    v4l2_info(sd, "g_ctrl called\n");
>>> +
>>> +    switch (ctrl->id) {
>>> +    case V4L2_CID_GAIN:
>>> +            rval = tvp7002_read(sd, TVP7002_R_FINE_GAIN);
>>> +            gval = tvp7002_read(sd, TVP7002_G_FINE_GAIN);
>>> +            bval = tvp7002_read(sd, TVP7002_B_FINE_GAIN);
>>> +
>>> +            if (rval < 0 || gval < 0 || bval < 0) {
>>> +                    res = -1;
>>> +            } else if (rval != gval || rval != bval) {
>>> +                    res = -1;
>>> +            } else {
>>> +                    ctrl->value = rval & 0x0F;
>>> +                    res = ctrl->value;
>>> +            }
>>> +            break;
>>> +    default:
>>> +            res = -1;
>>> +            break;
>>> +    }
>>> +
>>> +    return res < 0 ? res : 0;
>>> +}
>>> +
>>> +/*
>>> + * tvp7002_s_ctrl() - Set a control
>>> + * @sd: ptr to v4l2_subdev struct
>>> + * @ctrl: ptr to v4l2_control struct
>>> + *
>>> + * Set a control in TVP7002 decoder device.
>>> + * Returns zero when successful or -EINVAL if register access fails.
>>> + */
>>> +static int tvp7002_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control
>>> *ctrl)
>>> +{
>>> +    int rval, gval, bval;
>>> +    int error;
>>> +    u8 i, n;
>>> +    n = ARRAY_SIZE(tvp7002_qctrl);
>>> +
>>> +    for (i = 0; i < n; i++)
>>> +            if (ctrl->id == tvp7002_qctrl[i].id)
>>> +                    break;
>>> +
>>> +    if (i == n)
>>> +            return -EINVAL;
>>> +
>>> +    if (ctrl->value < tvp7002_qctrl[i].minimum ||
>>> +                    ctrl->value > tvp7002_qctrl[i].maximum)
>>> +            return -ERANGE;
>>> +
>>> +    switch (ctrl->id) {
>>> +    case V4L2_CID_GAIN:
>>> +            rval = tvp7002_write(sd, TVP7002_R_FINE_GAIN,
>>> +                                                    ctrl->value & 0xff);
>>> +            gval = tvp7002_write(sd, TVP7002_G_FINE_GAIN,
>>> +                                                    ctrl->value & 0xff);
>>> +            bval = tvp7002_write(sd, TVP7002_B_FINE_GAIN,
>>> +                                                    ctrl->value & 0xff);
>>> +            if (rval < 0  || gval < 0 || bval < 0)
>>> +                    error = -1;
>>> +            else
>>> +                    error = rval;
>>> +            break;
>>> +    default:
>>> +            error = -1;
>>> +            break;
>>> +    }
>>> +
>>> +    return error < 0 ? -EINVAL : 0;
>>> +}
>>> +
>>> +/*
>>> + * tvp7002_g_register() - Get the value of a register
>>> + * @sd: ptr to v4l2_subdev struct
>>> + * @vreg: ptr to v4l2_dbg_register struct
>>> + *
>>> + * Get the value of a TVP7002 decoder device register.
>>> + * Returns zero when successful, -EINVAL if register read fails or
>>> + * access to I2C client fails, -EPERM if the call is not allowed
>>> + * by diabled CAP_SYS_ADMIN.
>>> + */
>>> +#ifdef CONFIG_VIDEO_ADV_DEBUG
>>> +static int tvp7002_g_register(struct v4l2_subdev *sd,
>>> +                                            struct v4l2_dbg_register *reg)
>>> +{
>>> +    struct i2c_client *client = v4l2_get_subdevdata(sd);
>>> +
>>> +    if (!v4l2_chip_match_i2c_client(client, &reg->match))
>>> +            return -EINVAL;
>>> +    if (!capable(CAP_SYS_ADMIN))
>>> +            return -EPERM;
>>> +
>>> +    return reg->val < 0 ? -EINVAL : 0;
>>> +}
>>> +
>>> +/*
>>> + * tvp7002_s_register() - set a control
>>> + * @sd: ptr to v4l2_subdev struct
>>> + * @ctrl: ptr to v4l2_control struct
>>> + *
>>> + * Get the value of a TVP7002 decoder device register.
>>> + * Returns zero when successful or -EINVAL if register read fails.
>>> + */
>>> +static int tvp7002_s_register(struct v4l2_subdev *sd,
>>> +                                            struct v4l2_dbg_register *reg)
>>> +{
>>> +    int wres;
>>> +    struct i2c_client *client = v4l2_get_subdevdata(sd);
>>> +
>>> +    if (!v4l2_chip_match_i2c_client(client, &reg->match))
>>> +            return -EINVAL;
>>> +    if (!capable(CAP_SYS_ADMIN))
>>> +            return -EPERM;
>>> +
>>> +    wres = tvp7002_write(sd, reg->reg & 0xff, reg->val & 0xff);
>>> +
>>> +    return wres < 0 ? -EINVAL : 0;
>>> +}
>>> +#endif
>>> +
>>> +/*
>>> + * tvp7002_queryctrl() - Query a control
>>> + * @sd: ptr to v4l2_subdev struct
>>> + * @ctrl: ptr to v4l2_queryctrl struct
>>> + *
>>> + * Query a control of a TVP7002 decoder device.
>>> + * Returns zero when successful or -EINVAL if register read fails.
>>> + */
>>> +static int tvp7002_queryctrl(struct v4l2_subdev *sd, struct
>>> v4l2_queryctrl *qc)
>>> +{
>>> +    int i, error;
>>> +    error = -EINVAL;
>>> +
>>> +    v4l2_info(sd, "queryctrl called\n");
>>> +
>>> +    for (i = 0; i < ARRAY_SIZE(tvp7002_qctrl); i++)
>>> +            if (qc->id && qc->id == tvp7002_qctrl[i].id) {
>>> +                    v4l2_ctrl_query_fill(qc, tvp7002_qctrl[i].minimum,
>>> +                                    tvp7002_qctrl[i].maximum,
>>> +                                    tvp7002_qctrl[i].step,
>>> +                                    tvp7002_qctrl[i].default_value);
>>> +                    error = 0;
>>> +                    break;
>>> +            }
>>> +
>>> +    return error;
>>> +}
>>> +
>>> +/*
>>> + * tvp7002_s_stream() - V4L2 decoder i/f handler for s_stream
>>> + * @sd: pointer to standard V4L2 sub-device structure
>>> + * @enable: streaming enable or disable
>>> + *
>>> + * Sets streaming to enable or disable, if possible.
>>> + */
>>> +static int tvp7002_s_stream(struct v4l2_subdev *sd, int enable)
>>> +{
>>> +    int err = 0;
>>> +    struct tvp7002 *decoder = to_tvp7002(sd);
>>> +
>>> +    if (decoder->streaming == enable)
>>> +            return 0;
>>> +
>>> +    if (enable) {
>>> +            /* Power Up Sequence */
>>> +            err = tvp7002_write(sd, TVP7002_PWR_CTL, 0x00);
>>> +            if (err) {
>>> +                    v4l2_err(sd, "Unable to turn on decoder\n");
>>> +                    err = -EINVAL;
>>> +            }
>>> +            err = tvp7002_write_inittab(sd, tvp7002_init_default);
>>> +            if (err < 0) {
>>> +                    v4l2_err(sd, "Unable to initialize\n");
>>> +                    err = -EINVAL;
>>> +            }
>>> +            /* Detect if not already detected */
>>> +            err = tvp7002_read(sd, TVP7002_CHIP_REV);
>>> +            if (err < 0) {
>>> +                    v4l2_err(sd, "Unable to detect decoder\n");
>>> +                    err = -EINVAL;
>>> +            }
>>> +            decoder->streaming = enable;
>>> +    } else {
>>> +            /* Power Down Sequence */
>>> +            err = tvp7002_write(sd, TVP7002_PWR_CTL, 0x40);
>>> +            if (err) {
>>> +                    v4l2_err(sd, "Unable to turn off decoder\n");
>>> +                    return err;
>>> +            }
>>> +            decoder->streaming = enable;
>>> +    }
>>> +
>>> +    return err;
>>> +}
>>> +
>>> +/* Specific video subsystem operation handlers */
>>> +static const struct v4l2_subdev_video_ops tvp7002_video_ops = {
>>> +    .querystd = tvp7002_querystd,
>>> +    .s_stream = tvp7002_s_stream,
>>> +    .g_fmt = tvp7002_g_fmt,
>>> +};
>>> +
>>> +/* V4L2 Operations handlers */
>>> +static const struct v4l2_subdev_core_ops tvp7002_core_ops = {
>>> +    .g_chip_ident = tvp7002_g_chip_ident,
>>> +    .log_status = tvp7002_log_status,
>>> +    .g_ctrl = tvp7002_g_ctrl,
>>> +    .s_ctrl = tvp7002_s_ctrl,
>>> +    .queryctrl = tvp7002_queryctrl,
>>> +    .s_std = tvp7002_s_std,
>>> +#ifdef CONFIG_VIDEO_ADV_DEBUG
>>> +    .g_register = tvp7002_g_register,
>>> +    .s_register = tvp7002_s_register,
>>> +#endif
>>> +};
>>> +
>>> +static const struct v4l2_subdev_ops tvp7002_ops = {
>>> +    .core = &tvp7002_core_ops,
>>> +    .video = &tvp7002_video_ops,
>>> +};
>>> +
>>> +/* Default driver settings */
>>> +static struct tvp7002 tvp7002_dev = {
>>> +    .streaming = 0,
>>> +
>>> +    .pix = {
>>> +            .width = 720,
>>> +            .height = 480,
>>> +            .pixelformat = V4L2_PIX_FMT_UYVY,
>>> +            .field = V4L2_FIELD_INTERLACED,
>>> +            .bytesperline = 720 * 2,
>>> +            .sizeimage = 720 * 2 * 480,
>>> +            .colorspace = V4L2_COLORSPACE_SMPTE170M,
>>> +            },
>>> +
>>> +    .video_mode = V4L2_STD_NTSC,
>>> +};
>>> +
>>> +/*
>>> + * tvp7002_reset - Reset a TVP7002 device
>>> + * @sd: ptr to v4l2_subdev struct
>>> + * @val: unsigned integer (not used)
>>> + *
>>> + * Reset the TVP7002 device
>>> + * Returns zero when successful or -EINVAL if register read fails.
>>> + */
>>> +static int tvp7002_reset(struct v4l2_subdev *sd, u32 val)
>>> +{
>>> +    struct tvp7002 *core = to_tvp7002(sd);
>>> +    int polarity;
>>> +    int error;
>>> +
>>> +    error = tvp7002_read(sd, TVP7002_CHIP_REV);
>>> +    if (error < 0) {
>>> +            error = -EINVAL;
>>> +            goto found_error;
>>> +    }
>>> +
>>> +    if (error == 0x02) {
>>> +            v4l2_info(sd, "rev. %02x detected.\n", error);
>>> +    } else {
>>> +            v4l2_info(sd, "unknown revision detected.\n");
>>> +            v4l2_info(sd, "revision number is %02x\n", error);
>>> +    }
>>> +
>>> +    /* Initializes TVP7002 to its default values */
>>> +    error = tvp7002_write_inittab(sd, tvp7002_init_default);
>>> +    if (error < 0) {
>>> +            error = -EINVAL;
>>> +            goto found_error;
>>> +    }
>>> +
>>> +    /* Set polarity information after registers have been set */
>>> +    polarity = 0x5b | core->pdata->hs_polarity << 5
>>> +                    | core->pdata->vs_polarity << 2;
>>> +    error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity);
>>> +    if (error < 0) {
>>> +            error = -EINVAL;
>>> +            goto found_error;
>>> +    }
>>> +
>>> +    polarity = 0x0  | core->pdata->fid_polarity << 2
>>> +                    | core->pdata->sog_polarity << 1
>>> +                    | core->pdata->clk_polarity;
>>> +    error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity);
>>> +    if (error < 0) {
>>> +            error = -EINVAL;
>>> +            goto found_error;
>>> +    }
>>> +
>>> +found_error:
>>> +    return error;
>>> +};
>>> +
>>> +/*
>>> + * tvp7002_probe - Reset a TVP7002 device
>>> + * @sd: ptr to v4l2_subdev struct
>>> + * @ctrl: ptr to i2c_device_id struct
>>> + *
>>> + * Reset the TVP7002 device
>>> + * Returns zero when successful or -EINVAL if register read fails.
>>> + */
>>> +static int tvp7002_probe(struct i2c_client *c, const struct
>>>       
>> i2c_device_id
>>     
>>> *id)
>>> +{
>>> +    struct v4l2_subdev *sd;
>>> +    struct tvp7002 *core;
>>> +    int polarity;
>>> +    int error;
>>> +
>>> +    /* Check if the adapter supports the needed features */
>>> +    if (!i2c_check_functionality(c->adapter,
>>> +            I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
>>> +            return -EIO;
>>> +
>>> +    if (!c->dev.platform_data) {
>>> +            v4l2_err(c, "No platform data!!\n");
>>> +            return -ENODEV;
>>> +    }
>>> +
>>> +    core = kzalloc(sizeof(struct tvp7002), GFP_KERNEL);
>>> +
>>> +    if (!core)
>>> +            return -ENOMEM;
>>> +
>>> +    *core = tvp7002_dev;
>>> +    sd = &core->sd;
>>> +    core->pdata = c->dev.platform_data;
>>> +
>>> +    v4l2_i2c_subdev_init(sd, c, &tvp7002_ops);
>>> +    v4l_info(c, "tvp7002 found @ 0x%02x (%s)\n",
>>> +                                    c->addr << 1, c->adapter->name);
>>> +
>>> +    /* Initializes TVP7002 to its default values */
>>> +    error = tvp7002_write_inittab(sd, tvp7002_init_default);
>>> +    if (error < 0) {
>>> +            error = -EINVAL;
>>> +            goto found_error;
>>> +    }
>>> +
>>> +    /* Set polarity information after registers have been set */
>>> +    polarity = 0x5b | core->pdata->hs_polarity << 5
>>> +                    | core->pdata->vs_polarity << 2;
>>> +    error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity);
>>> +    if (error < 0) {
>>> +            error = -EINVAL;
>>> +            goto found_error;
>>> +    }
>>> +
>>> +    polarity = 0x0  | core->pdata->fid_polarity << 2
>>> +                    | core->pdata->sog_polarity << 1
>>> +                    | core->pdata->clk_polarity;
>>> +    error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity);
>>> +    if (error < 0) {
>>> +            error = -EINVAL;
>>> +            goto found_error;
>>> +    }
>>> +
>>> +    if (debug > 1)
>>> +            tvp7002_log_status(sd);
>>> +
>>> +found_error:
>>> +    if (error < 0)
>>> +            kfree(core);
>>> +
>>> +    return error;
>>> +}
>>> +
>>> +/*
>>> + * tvp7002_remove - Remove TVP7002 device support
>>> + * @c: ptr to i2c_client struct
>>> + *
>>> + * Reset the TVP7002 device
>>> + * Returns zero when successful or -EINVAL if register read fails.
>>> + */
>>> +static int tvp7002_remove(struct i2c_client *c)
>>> +{
>>> +    struct v4l2_subdev *sd = i2c_get_clientdata(c);
>>> +
>>> +    v4l2_dbg(1, debug, sd, "removing tvp7002 adapter"
>>> +                            "on address 0x%x\n", c->addr << 1);
>>> +
>>> +    v4l2_device_unregister_subdev(sd);
>>> +    kfree(to_tvp7002(sd));
>>> +    return 0;
>>> +}
>>> +
>>> +/* I2C Device ID table */
>>> +static const struct i2c_device_id tvp7002_id[] = {
>>> +    { "tvp7002", 0 },
>>> +    { }
>>> +};
>>> +MODULE_DEVICE_TABLE(i2c, tvp7002_id);
>>> +
>>> +/* I2C driver data */
>>> +static struct i2c_driver tvp7002_driver = {
>>> +    .driver = {
>>> +            .owner = THIS_MODULE,
>>> +            .name = "tvp7002",
>>> +    },
>>> +    .probe = tvp7002_probe,
>>> +    .remove = tvp7002_remove,
>>> +    .id_table = tvp7002_id,
>>> +};
>>> +
>>> +/*
>>> + * tvp7002_init - Initialize driver via I2C interface
>>> + *
>>> + * Register the TVP7002 driver.
>>> + * Returns 0 on success or < 0 on failure.
>>> + */
>>> +static int __init tvp7002_init(void)
>>> +{
>>> +    return i2c_add_driver(&tvp7002_driver);
>>> +}
>>> +
>>> +/*
>>> + * tvp7002_exit - Remove driver via I2C interface
>>> + *
>>> + * Unregister the TVP7002 driver.
>>> + * Returns 0 on success or < 0 on failure.
>>> + */
>>> +static void __exit tvp7002_exit(void)
>>> +{
>>> +    i2c_del_driver(&tvp7002_driver);
>>> +}
>>> +
>>> +module_init(tvp7002_init);
>>> +module_exit(tvp7002_exit);
>>> --
>>> 1.6.0.4
>>>
>>>       
>
>
Murali Karicheri Sept. 4, 2009, 8:34 p.m. UTC | #4
Santiago,

What is your plan to resolve this?

Murali Karicheri
Software Design Engineer
Texas Instruments Inc.
Germantown, MD 20874
new phone: 301-407-9583
Old Phone : 301-515-3736 (will be deprecated)
email: m-karicheri2@ti.com

>-----Original Message-----
>From: Santiago Nunez-Corrales [mailto:snunez@ridgerun.com]
>Sent: Friday, September 04, 2009 4:21 PM
>To: Karicheri, Muralidharan
>Cc: Narnakaje, Snehaprabha; davinci-linux-open-source@linux.davincidsp.com;
>todd.fischer@ridgerun.com; diego.dompe@ridgerun.com;
>clark.becker@ridgerun.com
>Subject: Re: [PATCH 5/6 v4] Updated TVP7002 driver for DM365
>
>Murali,
>
>
>Changing the querystd code. I found various errors w.r.t. LPS 2.10 from
>what  I was doing in my code. I am reading the wrong register, which is
>causing the code to return from the call incorrectly.
>
>Regards,
>
>Karicheri, Muralidharan wrote:
>> Santiago,
>>
>> Also modify the querystd() by re-using the code from LSP 2.10 as I have
>asked very beginning. This logic is tested well and works.
>>
>> Murali Karicheri
>> Software Design Engineer
>> Texas Instruments Inc.
>> Germantown, MD 20874
>> new phone: 301-407-9583
>> Old Phone : 301-515-3736 (will be deprecated)
>> email: m-karicheri2@ti.com
>>
>>
>>> -----Original Message-----
>>> From: Narnakaje, Snehaprabha
>>> Sent: Friday, September 04, 2009 11:18 AM
>>> To: santiago.nunez@ridgerun.com; davinci-linux-open-
>>> source@linux.davincidsp.com
>>> Cc: Karicheri, Muralidharan; todd.fischer@ridgerun.com;
>>> diego.dompe@ridgerun.com; clark.becker@ridgerun.com
>>> Subject: RE: [PATCH 5/6 v4] Updated TVP7002 driver for DM365
>>>
>>> Santiago,
>>>
>>>
>>>> -----Original Message-----
>>>> From: santiago.nunez@ridgerun.com [mailto:santiago.nunez@ridgerun.com]
>>>> Sent: Thursday, September 03, 2009 6:53 PM
>>>> To: davinci-linux-open-source@linux.davincidsp.com
>>>> Cc: Karicheri, Muralidharan; todd.fischer@ridgerun.com;
>>>> diego.dompe@ridgerun.com; clark.becker@ridgerun.com; Narnakaje,
>>>> Snehaprabha; Santiago Nunez-Corrales
>>>> Subject: [PATCH 5/6 v4] Updated TVP7002 driver for DM365
>>>>
>>>> From: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
>>>>
>>>> This patch provides the implementation of the TVP7002 decoder
>>>> driver for DM365. Implemented revisions by Vaibhav Hiremath
>>>> and Hans Verkuil. Removed most of controls, cleared up logic,
>>>> cleaned up code. Tested on DM365 TI EVM.
>>>>
>>>> Signed-off-by: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
>>>> ---
>>>>  drivers/media/video/tvp7002.c | 1464
>>>>
>>> [...]
>>>
>>>
>>>> +
>>>> +/*
>>>> + * tvp7002_get_video_mode() - V4L2 decoder interface handler for
>>>>
>>> querystd
>>>
>>>> + * @sd: pointer to standard V4L2 sub-device structure
>>>> + *
>>>> + * Returns the current standard detected by TVP7002. If no active
>input
>>>> is
>>>> + * detected, returns -1
>>>> + */
>>>> +static v4l2_std_id tvp7002_get_video_mode(struct v4l2_subdev *sd)
>>>> +{
>>>> +    int reg01, reg02, reg03;
>>>> +
>>>> +    reg01 = tvp7002_read(sd, TVP7002_HPLL_FDBK_DIV_MSBS);
>>>> +    reg02 = tvp7002_read(sd, TVP7002_HPLL_FDBK_DIV_LSBS);
>>>> +    reg03 = tvp7002_read(sd, TVP7002_HPLL_CRTL);
>>>> +
>>>> +    if (reg01 < 0 || reg02 < 0 || reg03 < 0)
>>>> +            return V4L2_STD_UNKNOWN;
>>>> +
>>>> +    switch (reg01) {
>>>> +    case 0x35:
>>>> +            if (reg02 == 0xa0)
>>>> +                    return V4L2_STD_NTSC;
>>>> +            else
>>>> +                    return V4L2_STD_PAL;
>>>> +    case 0x36:
>>>> +            if (reg02 == 0xa0)
>>>> +                    return V4L2_STD_525P_60;
>>>> +            else
>>>> +                    return V4L2_STD_625P_50;
>>>> +    case 0x67:
>>>> +            return V4L2_STD_720P_60;
>>>> +    case 0x7b:
>>>> +            return V4L2_STD_720P_50;
>>>> +    case 0x89:
>>>> +            if (reg03 == 0x98)
>>>> +                    return V4L2_STD_1080I_60;
>>>> +            else
>>>> +                    return V4L2_STD_1080P_60;
>>>> +            break;
>>>> +    case 0xa5:
>>>> +            if (reg03 == 0x90)
>>>> +                    return V4L2_STD_1080I_50;
>>>> +            else
>>>> +                    return V4L2_STD_1080P_50;
>>>> +    default:
>>>> +            return V4L2_STD_UNKNOWN;
>>>> +    }
>>>> +}
>>>> +
>>>> +/*
>>>> + * tvp7002_querystd() - V4L2 decoder interface handler for querystd
>>>> + * @sd: pointer to standard V4L2 sub-device structure
>>>> + * @std_id: standard V4L2 std_id ioctl enum
>>>> + *
>>>> + * Returns the current standard detected by TVP7002. If no active
>input
>>>> is
>>>> + * detected, returns -EINVAL
>>>> + */
>>>> +static int tvp7002_querystd(struct v4l2_subdev *sd, v4l2_std_id
>*std_id)
>>>> +{
>>>> +    v4l2_std_id current_std;
>>>> +    u8 sync_lock_status;
>>>> +    int res;
>>>> +
>>>> +    if (std_id == NULL)
>>>> +            return -EINVAL;
>>>> +
>>>> +    /* get the current standard */
>>>> +    res = tvp7002_get_video_mode(sd);
>>>> +    if (res == V4L2_STD_UNKNOWN)
>>>> +            return -EINVAL;
>>>>
>>> There seems to be a bug here - You will mostly get V4L2_STD_UNKNOWN for
>HD
>>> resolutions. The tvp7002_get_video_mode() API returns v4l2_std_id (which
>is
>>> long long - 64 bits), but you are storing the return value in "int res".
>>> You should change it to -
>>>
>>> v4l2_std_id res;
>>>
>>> Note that I am trying to integrate your driver with VPIF capture on
>DM6467
>>> and thus running into issues like this. It would be good to know -
>whether
>>> you have tested the TVP7002 driver on DM365 using a capture application.
>>>
>>> Thanks
>>> Sneha
>>>
>>>
>>>> +    current_std = res;
>>>> +
>>>> +    /* check whether signal is locked */
>>>> +    sync_lock_status = tvp7002_read(sd, TVP7002_SYNC_DETECT_STAT);
>>>> +
>>>> +    if (0x02 != (sync_lock_status & 0xff))
>>>> +            return -EINVAL; /* No input detected */
>>>> +
>>>> +    *std_id = current_std;
>>>> +
>>>> +    v4l2_dbg(1, debug, sd, "Current STD: %d %d @ %d Hz\n",
>>>> +            tvp7002_resolutions[tvp7002_from_std(current_std)].hres,
>>>> +            tvp7002_resolutions[tvp7002_from_std(current_std)].vres,
>>>> +            tvp7002_resolutions[tvp7002_from_std(current_std)].frate);
>>>> +    return 0;
>>>> +}
>>>> +
>>>> +/*
>>>> + * tvp7002_try_fmt_cap() - V4L2 decoder interface handler for try_fmt
>>>> + * @sd: pointer to standard V4L2 sub-device structure
>>>> + * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
>>>> + *
>>>> + * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type.
>This
>>>> + * ioctl is used to negotiate the image capture size and pixel format
>>>> + * without actually making it take effect.
>>>> + */
>>>> +static int tvp7002_try_fmt_cap(struct v4l2_subdev *sd, struct
>>>>
>>> v4l2_format
>>>
>>>> *f)
>>>> +{
>>>> +    struct v4l2_pix_format *pix;
>>>> +    v4l2_std_id current_std;
>>>> +    int res;
>>>> +
>>>> +    if (f == NULL)
>>>> +            return -EINVAL;
>>>> +
>>>> +    if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
>>>> +            /* only capture is supported */
>>>> +            return -EINVAL;
>>>> +
>>>> +    pix = &f->fmt.pix;
>>>> +
>>>> +    /* Calculate height and width based on current standard */
>>>> +    res = tvp7002_get_video_mode(sd);
>>>> +    if (res < 0)
>>>> +            return -EINVAL;
>>>> +    current_std = res;
>>>> +
>>>> +    pix->width = NUM_ACTIVE_PIXELS(current_std);
>>>> +    pix->height = NUM_ACTIVE_LINES(current_std);
>>>> +
>>>> +    pix->pixelformat = V4L2_PIX_FMT_UYVY;
>>>> +
>>>> +    pix->field = V4L2_FIELD_INTERLACED;
>>>> +    pix->bytesperline = pix->width * 2;
>>>> +    pix->sizeimage = pix->bytesperline * pix->height;
>>>> +    pix->colorspace = V4L2_COLORSPACE_REC709;
>>>> +    pix->priv = 0;
>>>> +
>>>> +    v4l2_dbg(1, debug, sd, "Try FMT: pixelformat - %s, bytesperline -
>>>> %d"
>>>> +                    "Width - %d, Height - %d",
>>>> +                    "8-bit UYVY 4:2:2 Format", pix->bytesperline,
>>>> +                    pix->width, pix->height);
>>>> +    return 0;
>>>> +}
>>>> +
>>>> +/**
>>>> + * tvp7002_s_fmt_cap() - V4L2 decoder interface handler for s_fmt
>>>> + * @sd: pointer to standard V4L2 sub-device structure
>>>> + * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
>>>> + *
>>>> + * If the requested format is supported, configures the HW to use that
>>>> + * format, returns error code if format not supported or HW can't be
>>>> + * correctly configured.
>>>> + */
>>>> +static int tvp7002_s_fmt_cap(struct v4l2_subdev *sd, struct
>v4l2_format
>>>> *f)
>>>> +{
>>>> +    struct tvp7002 *decoder = to_tvp7002(sd);
>>>> +    struct v4l2_pix_format *pix;
>>>> +    int rval;
>>>> +
>>>> +    if (f == NULL)
>>>> +            return -EINVAL;
>>>> +
>>>> +    if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
>>>> +            /* only capture is supported */
>>>> +            return -EINVAL;
>>>> +
>>>> +    pix = &f->fmt.pix;
>>>> +    rval = tvp7002_try_fmt_cap(sd, f);
>>>> +    if (rval)
>>>> +            return rval;
>>>> +
>>>> +    decoder->pix = *pix;
>>>> +
>>>> +    return rval;
>>>> +}
>>>> +
>>>> +/*
>>>> + * tvp7002_g_fmt() - V4L2 decoder interface handler for tvp7002_g_fmt
>>>> + * @sd: pointer to standard V4L2 sub-device structure
>>>> + * @f: pointer to standard V4L2 v4l2_format structure
>>>> + *
>>>> + * Returns the decoder's current pixel format in the v4l2_format
>>>> + * parameter.
>>>> + */
>>>> +static int tvp7002_g_fmt(struct v4l2_subdev *sd, struct v4l2_format
>*f)
>>>> +{
>>>> +    struct tvp7002 *decoder = to_tvp7002(sd);
>>>> +
>>>> +    if (f == NULL)
>>>> +            return -EINVAL;
>>>> +
>>>> +    if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
>>>> +            /* only capture is supported */
>>>> +            return -EINVAL;
>>>> +
>>>> +    f->fmt.pix = decoder->pix;
>>>> +
>>>> +    v4l2_dbg(1, debug, sd, "Current FMT: bytesperline - %d"
>>>> +                    "Width - %d, Height - %d",
>>>> +                    decoder->pix.bytesperline,
>>>> +                    decoder->pix.width, decoder->pix.height);
>>>> +    return 0;
>>>> +}
>>>> +
>>>> +/*
>>>> + * tvp7002_s_ctrl() - Set a control
>>>> + * @sd: ptr to v4l2_subdev struct
>>>> + * @ctrl: ptr to v4l2_control struct
>>>> + *
>>>> + * Set a control for a TVP7002 decoder device.
>>>> + * Returns zero when successful or -EINVAL if register access fails.
>>>> + */
>>>> +static int tvp7002_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
>>>> +{
>>>> +    struct tvp7002 *decoder = to_tvp7002(sd);
>>>> +    int vmd = 0;
>>>> +
>>>> +    decoder->video_mode = std;
>>>> +    vmd = tvp7002_from_std(std);
>>>> +
>>>> +    v4l2_dbg(1, debug, sd, "Set video std mode to %llx.\n", std);
>>>> +
>>>> +    return tvp7002_set_video_mode(sd, vmd);
>>>> +}
>>>> +
>>>> +/*
>>>> + * tvp7002_g_ctrl() - Get a control
>>>> + * @sd: ptr to v4l2_subdev struct
>>>> + * @ctrl: ptr to v4l2_control struct
>>>> + *
>>>> + * Get a control for a TVP7002 decoder device.
>>>> + * Returns zero when successful or -EINVAL if register access fails.
>>>> + */
>>>> +static int tvp7002_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control
>>>> *ctrl)
>>>> +{
>>>> +    int rval, gval, bval;
>>>> +    int res;
>>>> +
>>>> +    v4l2_info(sd, "g_ctrl called\n");
>>>> +
>>>> +    switch (ctrl->id) {
>>>> +    case V4L2_CID_GAIN:
>>>> +            rval = tvp7002_read(sd, TVP7002_R_FINE_GAIN);
>>>> +            gval = tvp7002_read(sd, TVP7002_G_FINE_GAIN);
>>>> +            bval = tvp7002_read(sd, TVP7002_B_FINE_GAIN);
>>>> +
>>>> +            if (rval < 0 || gval < 0 || bval < 0) {
>>>> +                    res = -1;
>>>> +            } else if (rval != gval || rval != bval) {
>>>> +                    res = -1;
>>>> +            } else {
>>>> +                    ctrl->value = rval & 0x0F;
>>>> +                    res = ctrl->value;
>>>> +            }
>>>> +            break;
>>>> +    default:
>>>> +            res = -1;
>>>> +            break;
>>>> +    }
>>>> +
>>>> +    return res < 0 ? res : 0;
>>>> +}
>>>> +
>>>> +/*
>>>> + * tvp7002_s_ctrl() - Set a control
>>>> + * @sd: ptr to v4l2_subdev struct
>>>> + * @ctrl: ptr to v4l2_control struct
>>>> + *
>>>> + * Set a control in TVP7002 decoder device.
>>>> + * Returns zero when successful or -EINVAL if register access fails.
>>>> + */
>>>> +static int tvp7002_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control
>>>> *ctrl)
>>>> +{
>>>> +    int rval, gval, bval;
>>>> +    int error;
>>>> +    u8 i, n;
>>>> +    n = ARRAY_SIZE(tvp7002_qctrl);
>>>> +
>>>> +    for (i = 0; i < n; i++)
>>>> +            if (ctrl->id == tvp7002_qctrl[i].id)
>>>> +                    break;
>>>> +
>>>> +    if (i == n)
>>>> +            return -EINVAL;
>>>> +
>>>> +    if (ctrl->value < tvp7002_qctrl[i].minimum ||
>>>> +                    ctrl->value > tvp7002_qctrl[i].maximum)
>>>> +            return -ERANGE;
>>>> +
>>>> +    switch (ctrl->id) {
>>>> +    case V4L2_CID_GAIN:
>>>> +            rval = tvp7002_write(sd, TVP7002_R_FINE_GAIN,
>>>> +                                                    ctrl->value &
>0xff);
>>>> +            gval = tvp7002_write(sd, TVP7002_G_FINE_GAIN,
>>>> +                                                    ctrl->value &
>0xff);
>>>> +            bval = tvp7002_write(sd, TVP7002_B_FINE_GAIN,
>>>> +                                                    ctrl->value &
>0xff);
>>>> +            if (rval < 0  || gval < 0 || bval < 0)
>>>> +                    error = -1;
>>>> +            else
>>>> +                    error = rval;
>>>> +            break;
>>>> +    default:
>>>> +            error = -1;
>>>> +            break;
>>>> +    }
>>>> +
>>>> +    return error < 0 ? -EINVAL : 0;
>>>> +}
>>>> +
>>>> +/*
>>>> + * tvp7002_g_register() - Get the value of a register
>>>> + * @sd: ptr to v4l2_subdev struct
>>>> + * @vreg: ptr to v4l2_dbg_register struct
>>>> + *
>>>> + * Get the value of a TVP7002 decoder device register.
>>>> + * Returns zero when successful, -EINVAL if register read fails or
>>>> + * access to I2C client fails, -EPERM if the call is not allowed
>>>> + * by diabled CAP_SYS_ADMIN.
>>>> + */
>>>> +#ifdef CONFIG_VIDEO_ADV_DEBUG
>>>> +static int tvp7002_g_register(struct v4l2_subdev *sd,
>>>> +                                            struct v4l2_dbg_register
>*reg)
>>>> +{
>>>> +    struct i2c_client *client = v4l2_get_subdevdata(sd);
>>>> +
>>>> +    if (!v4l2_chip_match_i2c_client(client, &reg->match))
>>>> +            return -EINVAL;
>>>> +    if (!capable(CAP_SYS_ADMIN))
>>>> +            return -EPERM;
>>>> +
>>>> +    return reg->val < 0 ? -EINVAL : 0;
>>>> +}
>>>> +
>>>> +/*
>>>> + * tvp7002_s_register() - set a control
>>>> + * @sd: ptr to v4l2_subdev struct
>>>> + * @ctrl: ptr to v4l2_control struct
>>>> + *
>>>> + * Get the value of a TVP7002 decoder device register.
>>>> + * Returns zero when successful or -EINVAL if register read fails.
>>>> + */
>>>> +static int tvp7002_s_register(struct v4l2_subdev *sd,
>>>> +                                            struct v4l2_dbg_register
>*reg)
>>>> +{
>>>> +    int wres;
>>>> +    struct i2c_client *client = v4l2_get_subdevdata(sd);
>>>> +
>>>> +    if (!v4l2_chip_match_i2c_client(client, &reg->match))
>>>> +            return -EINVAL;
>>>> +    if (!capable(CAP_SYS_ADMIN))
>>>> +            return -EPERM;
>>>> +
>>>> +    wres = tvp7002_write(sd, reg->reg & 0xff, reg->val & 0xff);
>>>> +
>>>> +    return wres < 0 ? -EINVAL : 0;
>>>> +}
>>>> +#endif
>>>> +
>>>> +/*
>>>> + * tvp7002_queryctrl() - Query a control
>>>> + * @sd: ptr to v4l2_subdev struct
>>>> + * @ctrl: ptr to v4l2_queryctrl struct
>>>> + *
>>>> + * Query a control of a TVP7002 decoder device.
>>>> + * Returns zero when successful or -EINVAL if register read fails.
>>>> + */
>>>> +static int tvp7002_queryctrl(struct v4l2_subdev *sd, struct
>>>> v4l2_queryctrl *qc)
>>>> +{
>>>> +    int i, error;
>>>> +    error = -EINVAL;
>>>> +
>>>> +    v4l2_info(sd, "queryctrl called\n");
>>>> +
>>>> +    for (i = 0; i < ARRAY_SIZE(tvp7002_qctrl); i++)
>>>> +            if (qc->id && qc->id == tvp7002_qctrl[i].id) {
>>>> +                    v4l2_ctrl_query_fill(qc, tvp7002_qctrl[i].minimum,
>>>> +                                    tvp7002_qctrl[i].maximum,
>>>> +                                    tvp7002_qctrl[i].step,
>>>> +                                    tvp7002_qctrl[i].default_value);
>>>> +                    error = 0;
>>>> +                    break;
>>>> +            }
>>>> +
>>>> +    return error;
>>>> +}
>>>> +
>>>> +/*
>>>> + * tvp7002_s_stream() - V4L2 decoder i/f handler for s_stream
>>>> + * @sd: pointer to standard V4L2 sub-device structure
>>>> + * @enable: streaming enable or disable
>>>> + *
>>>> + * Sets streaming to enable or disable, if possible.
>>>> + */
>>>> +static int tvp7002_s_stream(struct v4l2_subdev *sd, int enable)
>>>> +{
>>>> +    int err = 0;
>>>> +    struct tvp7002 *decoder = to_tvp7002(sd);
>>>> +
>>>> +    if (decoder->streaming == enable)
>>>> +            return 0;
>>>> +
>>>> +    if (enable) {
>>>> +            /* Power Up Sequence */
>>>> +            err = tvp7002_write(sd, TVP7002_PWR_CTL, 0x00);
>>>> +            if (err) {
>>>> +                    v4l2_err(sd, "Unable to turn on decoder\n");
>>>> +                    err = -EINVAL;
>>>> +            }
>>>> +            err = tvp7002_write_inittab(sd, tvp7002_init_default);
>>>> +            if (err < 0) {
>>>> +                    v4l2_err(sd, "Unable to initialize\n");
>>>> +                    err = -EINVAL;
>>>> +            }
>>>> +            /* Detect if not already detected */
>>>> +            err = tvp7002_read(sd, TVP7002_CHIP_REV);
>>>> +            if (err < 0) {
>>>> +                    v4l2_err(sd, "Unable to detect decoder\n");
>>>> +                    err = -EINVAL;
>>>> +            }
>>>> +            decoder->streaming = enable;
>>>> +    } else {
>>>> +            /* Power Down Sequence */
>>>> +            err = tvp7002_write(sd, TVP7002_PWR_CTL, 0x40);
>>>> +            if (err) {
>>>> +                    v4l2_err(sd, "Unable to turn off decoder\n");
>>>> +                    return err;
>>>> +            }
>>>> +            decoder->streaming = enable;
>>>> +    }
>>>> +
>>>> +    return err;
>>>> +}
>>>> +
>>>> +/* Specific video subsystem operation handlers */
>>>> +static const struct v4l2_subdev_video_ops tvp7002_video_ops = {
>>>> +    .querystd = tvp7002_querystd,
>>>> +    .s_stream = tvp7002_s_stream,
>>>> +    .g_fmt = tvp7002_g_fmt,
>>>> +};
>>>> +
>>>> +/* V4L2 Operations handlers */
>>>> +static const struct v4l2_subdev_core_ops tvp7002_core_ops = {
>>>> +    .g_chip_ident = tvp7002_g_chip_ident,
>>>> +    .log_status = tvp7002_log_status,
>>>> +    .g_ctrl = tvp7002_g_ctrl,
>>>> +    .s_ctrl = tvp7002_s_ctrl,
>>>> +    .queryctrl = tvp7002_queryctrl,
>>>> +    .s_std = tvp7002_s_std,
>>>> +#ifdef CONFIG_VIDEO_ADV_DEBUG
>>>> +    .g_register = tvp7002_g_register,
>>>> +    .s_register = tvp7002_s_register,
>>>> +#endif
>>>> +};
>>>> +
>>>> +static const struct v4l2_subdev_ops tvp7002_ops = {
>>>> +    .core = &tvp7002_core_ops,
>>>> +    .video = &tvp7002_video_ops,
>>>> +};
>>>> +
>>>> +/* Default driver settings */
>>>> +static struct tvp7002 tvp7002_dev = {
>>>> +    .streaming = 0,
>>>> +
>>>> +    .pix = {
>>>> +            .width = 720,
>>>> +            .height = 480,
>>>> +            .pixelformat = V4L2_PIX_FMT_UYVY,
>>>> +            .field = V4L2_FIELD_INTERLACED,
>>>> +            .bytesperline = 720 * 2,
>>>> +            .sizeimage = 720 * 2 * 480,
>>>> +            .colorspace = V4L2_COLORSPACE_SMPTE170M,
>>>> +            },
>>>> +
>>>> +    .video_mode = V4L2_STD_NTSC,
>>>> +};
>>>> +
>>>> +/*
>>>> + * tvp7002_reset - Reset a TVP7002 device
>>>> + * @sd: ptr to v4l2_subdev struct
>>>> + * @val: unsigned integer (not used)
>>>> + *
>>>> + * Reset the TVP7002 device
>>>> + * Returns zero when successful or -EINVAL if register read fails.
>>>> + */
>>>> +static int tvp7002_reset(struct v4l2_subdev *sd, u32 val)
>>>> +{
>>>> +    struct tvp7002 *core = to_tvp7002(sd);
>>>> +    int polarity;
>>>> +    int error;
>>>> +
>>>> +    error = tvp7002_read(sd, TVP7002_CHIP_REV);
>>>> +    if (error < 0) {
>>>> +            error = -EINVAL;
>>>> +            goto found_error;
>>>> +    }
>>>> +
>>>> +    if (error == 0x02) {
>>>> +            v4l2_info(sd, "rev. %02x detected.\n", error);
>>>> +    } else {
>>>> +            v4l2_info(sd, "unknown revision detected.\n");
>>>> +            v4l2_info(sd, "revision number is %02x\n", error);
>>>> +    }
>>>> +
>>>> +    /* Initializes TVP7002 to its default values */
>>>> +    error = tvp7002_write_inittab(sd, tvp7002_init_default);
>>>> +    if (error < 0) {
>>>> +            error = -EINVAL;
>>>> +            goto found_error;
>>>> +    }
>>>> +
>>>> +    /* Set polarity information after registers have been set */
>>>> +    polarity = 0x5b | core->pdata->hs_polarity << 5
>>>> +                    | core->pdata->vs_polarity << 2;
>>>> +    error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity);
>>>> +    if (error < 0) {
>>>> +            error = -EINVAL;
>>>> +            goto found_error;
>>>> +    }
>>>> +
>>>> +    polarity = 0x0  | core->pdata->fid_polarity << 2
>>>> +                    | core->pdata->sog_polarity << 1
>>>> +                    | core->pdata->clk_polarity;
>>>> +    error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity);
>>>> +    if (error < 0) {
>>>> +            error = -EINVAL;
>>>> +            goto found_error;
>>>> +    }
>>>> +
>>>> +found_error:
>>>> +    return error;
>>>> +};
>>>> +
>>>> +/*
>>>> + * tvp7002_probe - Reset a TVP7002 device
>>>> + * @sd: ptr to v4l2_subdev struct
>>>> + * @ctrl: ptr to i2c_device_id struct
>>>> + *
>>>> + * Reset the TVP7002 device
>>>> + * Returns zero when successful or -EINVAL if register read fails.
>>>> + */
>>>> +static int tvp7002_probe(struct i2c_client *c, const struct
>>>>
>>> i2c_device_id
>>>
>>>> *id)
>>>> +{
>>>> +    struct v4l2_subdev *sd;
>>>> +    struct tvp7002 *core;
>>>> +    int polarity;
>>>> +    int error;
>>>> +
>>>> +    /* Check if the adapter supports the needed features */
>>>> +    if (!i2c_check_functionality(c->adapter,
>>>> +            I2C_FUNC_SMBUS_READ_BYTE |
>I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
>>>> +            return -EIO;
>>>> +
>>>> +    if (!c->dev.platform_data) {
>>>> +            v4l2_err(c, "No platform data!!\n");
>>>> +            return -ENODEV;
>>>> +    }
>>>> +
>>>> +    core = kzalloc(sizeof(struct tvp7002), GFP_KERNEL);
>>>> +
>>>> +    if (!core)
>>>> +            return -ENOMEM;
>>>> +
>>>> +    *core = tvp7002_dev;
>>>> +    sd = &core->sd;
>>>> +    core->pdata = c->dev.platform_data;
>>>> +
>>>> +    v4l2_i2c_subdev_init(sd, c, &tvp7002_ops);
>>>> +    v4l_info(c, "tvp7002 found @ 0x%02x (%s)\n",
>>>> +                                    c->addr << 1, c->adapter->name);
>>>> +
>>>> +    /* Initializes TVP7002 to its default values */
>>>> +    error = tvp7002_write_inittab(sd, tvp7002_init_default);
>>>> +    if (error < 0) {
>>>> +            error = -EINVAL;
>>>> +            goto found_error;
>>>> +    }
>>>> +
>>>> +    /* Set polarity information after registers have been set */
>>>> +    polarity = 0x5b | core->pdata->hs_polarity << 5
>>>> +                    | core->pdata->vs_polarity << 2;
>>>> +    error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity);
>>>> +    if (error < 0) {
>>>> +            error = -EINVAL;
>>>> +            goto found_error;
>>>> +    }
>>>> +
>>>> +    polarity = 0x0  | core->pdata->fid_polarity << 2
>>>> +                    | core->pdata->sog_polarity << 1
>>>> +                    | core->pdata->clk_polarity;
>>>> +    error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity);
>>>> +    if (error < 0) {
>>>> +            error = -EINVAL;
>>>> +            goto found_error;
>>>> +    }
>>>> +
>>>> +    if (debug > 1)
>>>> +            tvp7002_log_status(sd);
>>>> +
>>>> +found_error:
>>>> +    if (error < 0)
>>>> +            kfree(core);
>>>> +
>>>> +    return error;
>>>> +}
>>>> +
>>>> +/*
>>>> + * tvp7002_remove - Remove TVP7002 device support
>>>> + * @c: ptr to i2c_client struct
>>>> + *
>>>> + * Reset the TVP7002 device
>>>> + * Returns zero when successful or -EINVAL if register read fails.
>>>> + */
>>>> +static int tvp7002_remove(struct i2c_client *c)
>>>> +{
>>>> +    struct v4l2_subdev *sd = i2c_get_clientdata(c);
>>>> +
>>>> +    v4l2_dbg(1, debug, sd, "removing tvp7002 adapter"
>>>> +                            "on address 0x%x\n", c->addr << 1);
>>>> +
>>>> +    v4l2_device_unregister_subdev(sd);
>>>> +    kfree(to_tvp7002(sd));
>>>> +    return 0;
>>>> +}
>>>> +
>>>> +/* I2C Device ID table */
>>>> +static const struct i2c_device_id tvp7002_id[] = {
>>>> +    { "tvp7002", 0 },
>>>> +    { }
>>>> +};
>>>> +MODULE_DEVICE_TABLE(i2c, tvp7002_id);
>>>> +
>>>> +/* I2C driver data */
>>>> +static struct i2c_driver tvp7002_driver = {
>>>> +    .driver = {
>>>> +            .owner = THIS_MODULE,
>>>> +            .name = "tvp7002",
>>>> +    },
>>>> +    .probe = tvp7002_probe,
>>>> +    .remove = tvp7002_remove,
>>>> +    .id_table = tvp7002_id,
>>>> +};
>>>> +
>>>> +/*
>>>> + * tvp7002_init - Initialize driver via I2C interface
>>>> + *
>>>> + * Register the TVP7002 driver.
>>>> + * Returns 0 on success or < 0 on failure.
>>>> + */
>>>> +static int __init tvp7002_init(void)
>>>> +{
>>>> +    return i2c_add_driver(&tvp7002_driver);
>>>> +}
>>>> +
>>>> +/*
>>>> + * tvp7002_exit - Remove driver via I2C interface
>>>> + *
>>>> + * Unregister the TVP7002 driver.
>>>> + * Returns 0 on success or < 0 on failure.
>>>> + */
>>>> +static void __exit tvp7002_exit(void)
>>>> +{
>>>> +    i2c_del_driver(&tvp7002_driver);
>>>> +}
>>>> +
>>>> +module_init(tvp7002_init);
>>>> +module_exit(tvp7002_exit);
>>>> --
>>>> 1.6.0.4
>>>>
>>>>
>>
>>
>
>
>--
>Santiago Nunez-Corrales, Eng.
>RidgeRun Engineering, LLC
>
>Guayabos, Curridabat
>San Jose, Costa Rica
>+(506) 2271 1487
>+(506) 8313 0536
>http://www.ridgerun.com
>
>
Santiago Nunez-Corrales Sept. 4, 2009, 8:44 p.m. UTC | #5
Murali,


My current plan is:


1. Adapt my current code with the code in the LSP 2.10. In particular, 
use registers TVP7002_L_P_FRAME_STAT and TVP7002_CLK_P_L_STAT for 
detecting input configurations.
2. Review currently supported standards in the driver, check them and 
update any pertinent change. In particular, double-check register values 
with LSP code.
3. Add debugging functions for detection of standards.
4. Test module integrity (i.e. compilation).
5. Test module loading.
6. Use a component input to check detection of standards (480i to 1080p) 
and obtained data.
7. Fix and repeat from 4 until no errors are found.
8. Have a user space application to test video decoding.


Regards,



Karicheri, Muralidharan wrote:
> Santiago,
>
> What is your plan to resolve this?
>
> Murali Karicheri
> Software Design Engineer
> Texas Instruments Inc.
> Germantown, MD 20874
> new phone: 301-407-9583
> Old Phone : 301-515-3736 (will be deprecated)
> email: m-karicheri2@ti.com
>
>   
>> -----Original Message-----
>> From: Santiago Nunez-Corrales [mailto:snunez@ridgerun.com]
>> Sent: Friday, September 04, 2009 4:21 PM
>> To: Karicheri, Muralidharan
>> Cc: Narnakaje, Snehaprabha; davinci-linux-open-source@linux.davincidsp.com;
>> todd.fischer@ridgerun.com; diego.dompe@ridgerun.com;
>> clark.becker@ridgerun.com
>> Subject: Re: [PATCH 5/6 v4] Updated TVP7002 driver for DM365
>>
>> Murali,
>>
>>
>> Changing the querystd code. I found various errors w.r.t. LPS 2.10 from
>> what  I was doing in my code. I am reading the wrong register, which is
>> causing the code to return from the call incorrectly.
>>
>> Regards,
>>
>> Karicheri, Muralidharan wrote:
>>     
>>> Santiago,
>>>
>>> Also modify the querystd() by re-using the code from LSP 2.10 as I have
>>>       
>> asked very beginning. This logic is tested well and works.
>>     
>>> Murali Karicheri
>>> Software Design Engineer
>>> Texas Instruments Inc.
>>> Germantown, MD 20874
>>> new phone: 301-407-9583
>>> Old Phone : 301-515-3736 (will be deprecated)
>>> email: m-karicheri2@ti.com
>>>
>>>
>>>       
>>>> -----Original Message-----
>>>> From: Narnakaje, Snehaprabha
>>>> Sent: Friday, September 04, 2009 11:18 AM
>>>> To: santiago.nunez@ridgerun.com; davinci-linux-open-
>>>> source@linux.davincidsp.com
>>>> Cc: Karicheri, Muralidharan; todd.fischer@ridgerun.com;
>>>> diego.dompe@ridgerun.com; clark.becker@ridgerun.com
>>>> Subject: RE: [PATCH 5/6 v4] Updated TVP7002 driver for DM365
>>>>
>>>> Santiago,
>>>>
>>>>
>>>>         
>>>>> -----Original Message-----
>>>>> From: santiago.nunez@ridgerun.com [mailto:santiago.nunez@ridgerun.com]
>>>>> Sent: Thursday, September 03, 2009 6:53 PM
>>>>> To: davinci-linux-open-source@linux.davincidsp.com
>>>>> Cc: Karicheri, Muralidharan; todd.fischer@ridgerun.com;
>>>>> diego.dompe@ridgerun.com; clark.becker@ridgerun.com; Narnakaje,
>>>>> Snehaprabha; Santiago Nunez-Corrales
>>>>> Subject: [PATCH 5/6 v4] Updated TVP7002 driver for DM365
>>>>>
>>>>> From: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
>>>>>
>>>>> This patch provides the implementation of the TVP7002 decoder
>>>>> driver for DM365. Implemented revisions by Vaibhav Hiremath
>>>>> and Hans Verkuil. Removed most of controls, cleared up logic,
>>>>> cleaned up code. Tested on DM365 TI EVM.
>>>>>
>>>>> Signed-off-by: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
>>>>> ---
>>>>>  drivers/media/video/tvp7002.c | 1464
>>>>>
>>>>>           
>>>> [...]
>>>>
>>>>
>>>>         
>>>>> +
>>>>> +/*
>>>>> + * tvp7002_get_video_mode() - V4L2 decoder interface handler for
>>>>>
>>>>>           
>>>> querystd
>>>>
>>>>         
>>>>> + * @sd: pointer to standard V4L2 sub-device structure
>>>>> + *
>>>>> + * Returns the current standard detected by TVP7002. If no active
>>>>>           
>> input
>>     
>>>>> is
>>>>> + * detected, returns -1
>>>>> + */
>>>>> +static v4l2_std_id tvp7002_get_video_mode(struct v4l2_subdev *sd)
>>>>> +{
>>>>> +    int reg01, reg02, reg03;
>>>>> +
>>>>> +    reg01 = tvp7002_read(sd, TVP7002_HPLL_FDBK_DIV_MSBS);
>>>>> +    reg02 = tvp7002_read(sd, TVP7002_HPLL_FDBK_DIV_LSBS);
>>>>> +    reg03 = tvp7002_read(sd, TVP7002_HPLL_CRTL);
>>>>> +
>>>>> +    if (reg01 < 0 || reg02 < 0 || reg03 < 0)
>>>>> +            return V4L2_STD_UNKNOWN;
>>>>> +
>>>>> +    switch (reg01) {
>>>>> +    case 0x35:
>>>>> +            if (reg02 == 0xa0)
>>>>> +                    return V4L2_STD_NTSC;
>>>>> +            else
>>>>> +                    return V4L2_STD_PAL;
>>>>> +    case 0x36:
>>>>> +            if (reg02 == 0xa0)
>>>>> +                    return V4L2_STD_525P_60;
>>>>> +            else
>>>>> +                    return V4L2_STD_625P_50;
>>>>> +    case 0x67:
>>>>> +            return V4L2_STD_720P_60;
>>>>> +    case 0x7b:
>>>>> +            return V4L2_STD_720P_50;
>>>>> +    case 0x89:
>>>>> +            if (reg03 == 0x98)
>>>>> +                    return V4L2_STD_1080I_60;
>>>>> +            else
>>>>> +                    return V4L2_STD_1080P_60;
>>>>> +            break;
>>>>> +    case 0xa5:
>>>>> +            if (reg03 == 0x90)
>>>>> +                    return V4L2_STD_1080I_50;
>>>>> +            else
>>>>> +                    return V4L2_STD_1080P_50;
>>>>> +    default:
>>>>> +            return V4L2_STD_UNKNOWN;
>>>>> +    }
>>>>> +}
>>>>> +
>>>>> +/*
>>>>> + * tvp7002_querystd() - V4L2 decoder interface handler for querystd
>>>>> + * @sd: pointer to standard V4L2 sub-device structure
>>>>> + * @std_id: standard V4L2 std_id ioctl enum
>>>>> + *
>>>>> + * Returns the current standard detected by TVP7002. If no active
>>>>>           
>> input
>>     
>>>>> is
>>>>> + * detected, returns -EINVAL
>>>>> + */
>>>>> +static int tvp7002_querystd(struct v4l2_subdev *sd, v4l2_std_id
>>>>>           
>> *std_id)
>>     
>>>>> +{
>>>>> +    v4l2_std_id current_std;
>>>>> +    u8 sync_lock_status;
>>>>> +    int res;
>>>>> +
>>>>> +    if (std_id == NULL)
>>>>> +            return -EINVAL;
>>>>> +
>>>>> +    /* get the current standard */
>>>>> +    res = tvp7002_get_video_mode(sd);
>>>>> +    if (res == V4L2_STD_UNKNOWN)
>>>>> +            return -EINVAL;
>>>>>
>>>>>           
>>>> There seems to be a bug here - You will mostly get V4L2_STD_UNKNOWN for
>>>>         
>> HD
>>     
>>>> resolutions. The tvp7002_get_video_mode() API returns v4l2_std_id (which
>>>>         
>> is
>>     
>>>> long long - 64 bits), but you are storing the return value in "int res".
>>>> You should change it to -
>>>>
>>>> v4l2_std_id res;
>>>>
>>>> Note that I am trying to integrate your driver with VPIF capture on
>>>>         
>> DM6467
>>     
>>>> and thus running into issues like this. It would be good to know -
>>>>         
>> whether
>>     
>>>> you have tested the TVP7002 driver on DM365 using a capture application.
>>>>
>>>> Thanks
>>>> Sneha
>>>>
>>>>
>>>>         
>>>>> +    current_std = res;
>>>>> +
>>>>> +    /* check whether signal is locked */
>>>>> +    sync_lock_status = tvp7002_read(sd, TVP7002_SYNC_DETECT_STAT);
>>>>> +
>>>>> +    if (0x02 != (sync_lock_status & 0xff))
>>>>> +            return -EINVAL; /* No input detected */
>>>>> +
>>>>> +    *std_id = current_std;
>>>>> +
>>>>> +    v4l2_dbg(1, debug, sd, "Current STD: %d %d @ %d Hz\n",
>>>>> +            tvp7002_resolutions[tvp7002_from_std(current_std)].hres,
>>>>> +            tvp7002_resolutions[tvp7002_from_std(current_std)].vres,
>>>>> +            tvp7002_resolutions[tvp7002_from_std(current_std)].frate);
>>>>> +    return 0;
>>>>> +}
>>>>> +
>>>>> +/*
>>>>> + * tvp7002_try_fmt_cap() - V4L2 decoder interface handler for try_fmt
>>>>> + * @sd: pointer to standard V4L2 sub-device structure
>>>>> + * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
>>>>> + *
>>>>> + * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type.
>>>>>           
>> This
>>     
>>>>> + * ioctl is used to negotiate the image capture size and pixel format
>>>>> + * without actually making it take effect.
>>>>> + */
>>>>> +static int tvp7002_try_fmt_cap(struct v4l2_subdev *sd, struct
>>>>>
>>>>>           
>>>> v4l2_format
>>>>
>>>>         
>>>>> *f)
>>>>> +{
>>>>> +    struct v4l2_pix_format *pix;
>>>>> +    v4l2_std_id current_std;
>>>>> +    int res;
>>>>> +
>>>>> +    if (f == NULL)
>>>>> +            return -EINVAL;
>>>>> +
>>>>> +    if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
>>>>> +            /* only capture is supported */
>>>>> +            return -EINVAL;
>>>>> +
>>>>> +    pix = &f->fmt.pix;
>>>>> +
>>>>> +    /* Calculate height and width based on current standard */
>>>>> +    res = tvp7002_get_video_mode(sd);
>>>>> +    if (res < 0)
>>>>> +            return -EINVAL;
>>>>> +    current_std = res;
>>>>> +
>>>>> +    pix->width = NUM_ACTIVE_PIXELS(current_std);
>>>>> +    pix->height = NUM_ACTIVE_LINES(current_std);
>>>>> +
>>>>> +    pix->pixelformat = V4L2_PIX_FMT_UYVY;
>>>>> +
>>>>> +    pix->field = V4L2_FIELD_INTERLACED;
>>>>> +    pix->bytesperline = pix->width * 2;
>>>>> +    pix->sizeimage = pix->bytesperline * pix->height;
>>>>> +    pix->colorspace = V4L2_COLORSPACE_REC709;
>>>>> +    pix->priv = 0;
>>>>> +
>>>>> +    v4l2_dbg(1, debug, sd, "Try FMT: pixelformat - %s, bytesperline -
>>>>> %d"
>>>>> +                    "Width - %d, Height - %d",
>>>>> +                    "8-bit UYVY 4:2:2 Format", pix->bytesperline,
>>>>> +                    pix->width, pix->height);
>>>>> +    return 0;
>>>>> +}
>>>>> +
>>>>> +/**
>>>>> + * tvp7002_s_fmt_cap() - V4L2 decoder interface handler for s_fmt
>>>>> + * @sd: pointer to standard V4L2 sub-device structure
>>>>> + * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
>>>>> + *
>>>>> + * If the requested format is supported, configures the HW to use that
>>>>> + * format, returns error code if format not supported or HW can't be
>>>>> + * correctly configured.
>>>>> + */
>>>>> +static int tvp7002_s_fmt_cap(struct v4l2_subdev *sd, struct
>>>>>           
>> v4l2_format
>>     
>>>>> *f)
>>>>> +{
>>>>> +    struct tvp7002 *decoder = to_tvp7002(sd);
>>>>> +    struct v4l2_pix_format *pix;
>>>>> +    int rval;
>>>>> +
>>>>> +    if (f == NULL)
>>>>> +            return -EINVAL;
>>>>> +
>>>>> +    if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
>>>>> +            /* only capture is supported */
>>>>> +            return -EINVAL;
>>>>> +
>>>>> +    pix = &f->fmt.pix;
>>>>> +    rval = tvp7002_try_fmt_cap(sd, f);
>>>>> +    if (rval)
>>>>> +            return rval;
>>>>> +
>>>>> +    decoder->pix = *pix;
>>>>> +
>>>>> +    return rval;
>>>>> +}
>>>>> +
>>>>> +/*
>>>>> + * tvp7002_g_fmt() - V4L2 decoder interface handler for tvp7002_g_fmt
>>>>> + * @sd: pointer to standard V4L2 sub-device structure
>>>>> + * @f: pointer to standard V4L2 v4l2_format structure
>>>>> + *
>>>>> + * Returns the decoder's current pixel format in the v4l2_format
>>>>> + * parameter.
>>>>> + */
>>>>> +static int tvp7002_g_fmt(struct v4l2_subdev *sd, struct v4l2_format
>>>>>           
>> *f)
>>     
>>>>> +{
>>>>> +    struct tvp7002 *decoder = to_tvp7002(sd);
>>>>> +
>>>>> +    if (f == NULL)
>>>>> +            return -EINVAL;
>>>>> +
>>>>> +    if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
>>>>> +            /* only capture is supported */
>>>>> +            return -EINVAL;
>>>>> +
>>>>> +    f->fmt.pix = decoder->pix;
>>>>> +
>>>>> +    v4l2_dbg(1, debug, sd, "Current FMT: bytesperline - %d"
>>>>> +                    "Width - %d, Height - %d",
>>>>> +                    decoder->pix.bytesperline,
>>>>> +                    decoder->pix.width, decoder->pix.height);
>>>>> +    return 0;
>>>>> +}
>>>>> +
>>>>> +/*
>>>>> + * tvp7002_s_ctrl() - Set a control
>>>>> + * @sd: ptr to v4l2_subdev struct
>>>>> + * @ctrl: ptr to v4l2_control struct
>>>>> + *
>>>>> + * Set a control for a TVP7002 decoder device.
>>>>> + * Returns zero when successful or -EINVAL if register access fails.
>>>>> + */
>>>>> +static int tvp7002_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
>>>>> +{
>>>>> +    struct tvp7002 *decoder = to_tvp7002(sd);
>>>>> +    int vmd = 0;
>>>>> +
>>>>> +    decoder->video_mode = std;
>>>>> +    vmd = tvp7002_from_std(std);
>>>>> +
>>>>> +    v4l2_dbg(1, debug, sd, "Set video std mode to %llx.\n", std);
>>>>> +
>>>>> +    return tvp7002_set_video_mode(sd, vmd);
>>>>> +}
>>>>> +
>>>>> +/*
>>>>> + * tvp7002_g_ctrl() - Get a control
>>>>> + * @sd: ptr to v4l2_subdev struct
>>>>> + * @ctrl: ptr to v4l2_control struct
>>>>> + *
>>>>> + * Get a control for a TVP7002 decoder device.
>>>>> + * Returns zero when successful or -EINVAL if register access fails.
>>>>> + */
>>>>> +static int tvp7002_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control
>>>>> *ctrl)
>>>>> +{
>>>>> +    int rval, gval, bval;
>>>>> +    int res;
>>>>> +
>>>>> +    v4l2_info(sd, "g_ctrl called\n");
>>>>> +
>>>>> +    switch (ctrl->id) {
>>>>> +    case V4L2_CID_GAIN:
>>>>> +            rval = tvp7002_read(sd, TVP7002_R_FINE_GAIN);
>>>>> +            gval = tvp7002_read(sd, TVP7002_G_FINE_GAIN);
>>>>> +            bval = tvp7002_read(sd, TVP7002_B_FINE_GAIN);
>>>>> +
>>>>> +            if (rval < 0 || gval < 0 || bval < 0) {
>>>>> +                    res = -1;
>>>>> +            } else if (rval != gval || rval != bval) {
>>>>> +                    res = -1;
>>>>> +            } else {
>>>>> +                    ctrl->value = rval & 0x0F;
>>>>> +                    res = ctrl->value;
>>>>> +            }
>>>>> +            break;
>>>>> +    default:
>>>>> +            res = -1;
>>>>> +            break;
>>>>> +    }
>>>>> +
>>>>> +    return res < 0 ? res : 0;
>>>>> +}
>>>>> +
>>>>> +/*
>>>>> + * tvp7002_s_ctrl() - Set a control
>>>>> + * @sd: ptr to v4l2_subdev struct
>>>>> + * @ctrl: ptr to v4l2_control struct
>>>>> + *
>>>>> + * Set a control in TVP7002 decoder device.
>>>>> + * Returns zero when successful or -EINVAL if register access fails.
>>>>> + */
>>>>> +static int tvp7002_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control
>>>>> *ctrl)
>>>>> +{
>>>>> +    int rval, gval, bval;
>>>>> +    int error;
>>>>> +    u8 i, n;
>>>>> +    n = ARRAY_SIZE(tvp7002_qctrl);
>>>>> +
>>>>> +    for (i = 0; i < n; i++)
>>>>> +            if (ctrl->id == tvp7002_qctrl[i].id)
>>>>> +                    break;
>>>>> +
>>>>> +    if (i == n)
>>>>> +            return -EINVAL;
>>>>> +
>>>>> +    if (ctrl->value < tvp7002_qctrl[i].minimum ||
>>>>> +                    ctrl->value > tvp7002_qctrl[i].maximum)
>>>>> +            return -ERANGE;
>>>>> +
>>>>> +    switch (ctrl->id) {
>>>>> +    case V4L2_CID_GAIN:
>>>>> +            rval = tvp7002_write(sd, TVP7002_R_FINE_GAIN,
>>>>> +                                                    ctrl->value &
>>>>>           
>> 0xff);
>>     
>>>>> +            gval = tvp7002_write(sd, TVP7002_G_FINE_GAIN,
>>>>> +                                                    ctrl->value &
>>>>>           
>> 0xff);
>>     
>>>>> +            bval = tvp7002_write(sd, TVP7002_B_FINE_GAIN,
>>>>> +                                                    ctrl->value &
>>>>>           
>> 0xff);
>>     
>>>>> +            if (rval < 0  || gval < 0 || bval < 0)
>>>>> +                    error = -1;
>>>>> +            else
>>>>> +                    error = rval;
>>>>> +            break;
>>>>> +    default:
>>>>> +            error = -1;
>>>>> +            break;
>>>>> +    }
>>>>> +
>>>>> +    return error < 0 ? -EINVAL : 0;
>>>>> +}
>>>>> +
>>>>> +/*
>>>>> + * tvp7002_g_register() - Get the value of a register
>>>>> + * @sd: ptr to v4l2_subdev struct
>>>>> + * @vreg: ptr to v4l2_dbg_register struct
>>>>> + *
>>>>> + * Get the value of a TVP7002 decoder device register.
>>>>> + * Returns zero when successful, -EINVAL if register read fails or
>>>>> + * access to I2C client fails, -EPERM if the call is not allowed
>>>>> + * by diabled CAP_SYS_ADMIN.
>>>>> + */
>>>>> +#ifdef CONFIG_VIDEO_ADV_DEBUG
>>>>> +static int tvp7002_g_register(struct v4l2_subdev *sd,
>>>>> +                                            struct v4l2_dbg_register
>>>>>           
>> *reg)
>>     
>>>>> +{
>>>>> +    struct i2c_client *client = v4l2_get_subdevdata(sd);
>>>>> +
>>>>> +    if (!v4l2_chip_match_i2c_client(client, &reg->match))
>>>>> +            return -EINVAL;
>>>>> +    if (!capable(CAP_SYS_ADMIN))
>>>>> +            return -EPERM;
>>>>> +
>>>>> +    return reg->val < 0 ? -EINVAL : 0;
>>>>> +}
>>>>> +
>>>>> +/*
>>>>> + * tvp7002_s_register() - set a control
>>>>> + * @sd: ptr to v4l2_subdev struct
>>>>> + * @ctrl: ptr to v4l2_control struct
>>>>> + *
>>>>> + * Get the value of a TVP7002 decoder device register.
>>>>> + * Returns zero when successful or -EINVAL if register read fails.
>>>>> + */
>>>>> +static int tvp7002_s_register(struct v4l2_subdev *sd,
>>>>> +                                            struct v4l2_dbg_register
>>>>>           
>> *reg)
>>     
>>>>> +{
>>>>> +    int wres;
>>>>> +    struct i2c_client *client = v4l2_get_subdevdata(sd);
>>>>> +
>>>>> +    if (!v4l2_chip_match_i2c_client(client, &reg->match))
>>>>> +            return -EINVAL;
>>>>> +    if (!capable(CAP_SYS_ADMIN))
>>>>> +            return -EPERM;
>>>>> +
>>>>> +    wres = tvp7002_write(sd, reg->reg & 0xff, reg->val & 0xff);
>>>>> +
>>>>> +    return wres < 0 ? -EINVAL : 0;
>>>>> +}
>>>>> +#endif
>>>>> +
>>>>> +/*
>>>>> + * tvp7002_queryctrl() - Query a control
>>>>> + * @sd: ptr to v4l2_subdev struct
>>>>> + * @ctrl: ptr to v4l2_queryctrl struct
>>>>> + *
>>>>> + * Query a control of a TVP7002 decoder device.
>>>>> + * Returns zero when successful or -EINVAL if register read fails.
>>>>> + */
>>>>> +static int tvp7002_queryctrl(struct v4l2_subdev *sd, struct
>>>>> v4l2_queryctrl *qc)
>>>>> +{
>>>>> +    int i, error;
>>>>> +    error = -EINVAL;
>>>>> +
>>>>> +    v4l2_info(sd, "queryctrl called\n");
>>>>> +
>>>>> +    for (i = 0; i < ARRAY_SIZE(tvp7002_qctrl); i++)
>>>>> +            if (qc->id && qc->id == tvp7002_qctrl[i].id) {
>>>>> +                    v4l2_ctrl_query_fill(qc, tvp7002_qctrl[i].minimum,
>>>>> +                                    tvp7002_qctrl[i].maximum,
>>>>> +                                    tvp7002_qctrl[i].step,
>>>>> +                                    tvp7002_qctrl[i].default_value);
>>>>> +                    error = 0;
>>>>> +                    break;
>>>>> +            }
>>>>> +
>>>>> +    return error;
>>>>> +}
>>>>> +
>>>>> +/*
>>>>> + * tvp7002_s_stream() - V4L2 decoder i/f handler for s_stream
>>>>> + * @sd: pointer to standard V4L2 sub-device structure
>>>>> + * @enable: streaming enable or disable
>>>>> + *
>>>>> + * Sets streaming to enable or disable, if possible.
>>>>> + */
>>>>> +static int tvp7002_s_stream(struct v4l2_subdev *sd, int enable)
>>>>> +{
>>>>> +    int err = 0;
>>>>> +    struct tvp7002 *decoder = to_tvp7002(sd);
>>>>> +
>>>>> +    if (decoder->streaming == enable)
>>>>> +            return 0;
>>>>> +
>>>>> +    if (enable) {
>>>>> +            /* Power Up Sequence */
>>>>> +            err = tvp7002_write(sd, TVP7002_PWR_CTL, 0x00);
>>>>> +            if (err) {
>>>>> +                    v4l2_err(sd, "Unable to turn on decoder\n");
>>>>> +                    err = -EINVAL;
>>>>> +            }
>>>>> +            err = tvp7002_write_inittab(sd, tvp7002_init_default);
>>>>> +            if (err < 0) {
>>>>> +                    v4l2_err(sd, "Unable to initialize\n");
>>>>> +                    err = -EINVAL;
>>>>> +            }
>>>>> +            /* Detect if not already detected */
>>>>> +            err = tvp7002_read(sd, TVP7002_CHIP_REV);
>>>>> +            if (err < 0) {
>>>>> +                    v4l2_err(sd, "Unable to detect decoder\n");
>>>>> +                    err = -EINVAL;
>>>>> +            }
>>>>> +            decoder->streaming = enable;
>>>>> +    } else {
>>>>> +            /* Power Down Sequence */
>>>>> +            err = tvp7002_write(sd, TVP7002_PWR_CTL, 0x40);
>>>>> +            if (err) {
>>>>> +                    v4l2_err(sd, "Unable to turn off decoder\n");
>>>>> +                    return err;
>>>>> +            }
>>>>> +            decoder->streaming = enable;
>>>>> +    }
>>>>> +
>>>>> +    return err;
>>>>> +}
>>>>> +
>>>>> +/* Specific video subsystem operation handlers */
>>>>> +static const struct v4l2_subdev_video_ops tvp7002_video_ops = {
>>>>> +    .querystd = tvp7002_querystd,
>>>>> +    .s_stream = tvp7002_s_stream,
>>>>> +    .g_fmt = tvp7002_g_fmt,
>>>>> +};
>>>>> +
>>>>> +/* V4L2 Operations handlers */
>>>>> +static const struct v4l2_subdev_core_ops tvp7002_core_ops = {
>>>>> +    .g_chip_ident = tvp7002_g_chip_ident,
>>>>> +    .log_status = tvp7002_log_status,
>>>>> +    .g_ctrl = tvp7002_g_ctrl,
>>>>> +    .s_ctrl = tvp7002_s_ctrl,
>>>>> +    .queryctrl = tvp7002_queryctrl,
>>>>> +    .s_std = tvp7002_s_std,
>>>>> +#ifdef CONFIG_VIDEO_ADV_DEBUG
>>>>> +    .g_register = tvp7002_g_register,
>>>>> +    .s_register = tvp7002_s_register,
>>>>> +#endif
>>>>> +};
>>>>> +
>>>>> +static const struct v4l2_subdev_ops tvp7002_ops = {
>>>>> +    .core = &tvp7002_core_ops,
>>>>> +    .video = &tvp7002_video_ops,
>>>>> +};
>>>>> +
>>>>> +/* Default driver settings */
>>>>> +static struct tvp7002 tvp7002_dev = {
>>>>> +    .streaming = 0,
>>>>> +
>>>>> +    .pix = {
>>>>> +            .width = 720,
>>>>> +            .height = 480,
>>>>> +            .pixelformat = V4L2_PIX_FMT_UYVY,
>>>>> +            .field = V4L2_FIELD_INTERLACED,
>>>>> +            .bytesperline = 720 * 2,
>>>>> +            .sizeimage = 720 * 2 * 480,
>>>>> +            .colorspace = V4L2_COLORSPACE_SMPTE170M,
>>>>> +            },
>>>>> +
>>>>> +    .video_mode = V4L2_STD_NTSC,
>>>>> +};
>>>>> +
>>>>> +/*
>>>>> + * tvp7002_reset - Reset a TVP7002 device
>>>>> + * @sd: ptr to v4l2_subdev struct
>>>>> + * @val: unsigned integer (not used)
>>>>> + *
>>>>> + * Reset the TVP7002 device
>>>>> + * Returns zero when successful or -EINVAL if register read fails.
>>>>> + */
>>>>> +static int tvp7002_reset(struct v4l2_subdev *sd, u32 val)
>>>>> +{
>>>>> +    struct tvp7002 *core = to_tvp7002(sd);
>>>>> +    int polarity;
>>>>> +    int error;
>>>>> +
>>>>> +    error = tvp7002_read(sd, TVP7002_CHIP_REV);
>>>>> +    if (error < 0) {
>>>>> +            error = -EINVAL;
>>>>> +            goto found_error;
>>>>> +    }
>>>>> +
>>>>> +    if (error == 0x02) {
>>>>> +            v4l2_info(sd, "rev. %02x detected.\n", error);
>>>>> +    } else {
>>>>> +            v4l2_info(sd, "unknown revision detected.\n");
>>>>> +            v4l2_info(sd, "revision number is %02x\n", error);
>>>>> +    }
>>>>> +
>>>>> +    /* Initializes TVP7002 to its default values */
>>>>> +    error = tvp7002_write_inittab(sd, tvp7002_init_default);
>>>>> +    if (error < 0) {
>>>>> +            error = -EINVAL;
>>>>> +            goto found_error;
>>>>> +    }
>>>>> +
>>>>> +    /* Set polarity information after registers have been set */
>>>>> +    polarity = 0x5b | core->pdata->hs_polarity << 5
>>>>> +                    | core->pdata->vs_polarity << 2;
>>>>> +    error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity);
>>>>> +    if (error < 0) {
>>>>> +            error = -EINVAL;
>>>>> +            goto found_error;
>>>>> +    }
>>>>> +
>>>>> +    polarity = 0x0  | core->pdata->fid_polarity << 2
>>>>> +                    | core->pdata->sog_polarity << 1
>>>>> +                    | core->pdata->clk_polarity;
>>>>> +    error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity);
>>>>> +    if (error < 0) {
>>>>> +            error = -EINVAL;
>>>>> +            goto found_error;
>>>>> +    }
>>>>> +
>>>>> +found_error:
>>>>> +    return error;
>>>>> +};
>>>>> +
>>>>> +/*
>>>>> + * tvp7002_probe - Reset a TVP7002 device
>>>>> + * @sd: ptr to v4l2_subdev struct
>>>>> + * @ctrl: ptr to i2c_device_id struct
>>>>> + *
>>>>> + * Reset the TVP7002 device
>>>>> + * Returns zero when successful or -EINVAL if register read fails.
>>>>> + */
>>>>> +static int tvp7002_probe(struct i2c_client *c, const struct
>>>>>
>>>>>           
>>>> i2c_device_id
>>>>
>>>>         
>>>>> *id)
>>>>> +{
>>>>> +    struct v4l2_subdev *sd;
>>>>> +    struct tvp7002 *core;
>>>>> +    int polarity;
>>>>> +    int error;
>>>>> +
>>>>> +    /* Check if the adapter supports the needed features */
>>>>> +    if (!i2c_check_functionality(c->adapter,
>>>>> +            I2C_FUNC_SMBUS_READ_BYTE |
>>>>>           
>> I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
>>     
>>>>> +            return -EIO;
>>>>> +
>>>>> +    if (!c->dev.platform_data) {
>>>>> +            v4l2_err(c, "No platform data!!\n");
>>>>> +            return -ENODEV;
>>>>> +    }
>>>>> +
>>>>> +    core = kzalloc(sizeof(struct tvp7002), GFP_KERNEL);
>>>>> +
>>>>> +    if (!core)
>>>>> +            return -ENOMEM;
>>>>> +
>>>>> +    *core = tvp7002_dev;
>>>>> +    sd = &core->sd;
>>>>> +    core->pdata = c->dev.platform_data;
>>>>> +
>>>>> +    v4l2_i2c_subdev_init(sd, c, &tvp7002_ops);
>>>>> +    v4l_info(c, "tvp7002 found @ 0x%02x (%s)\n",
>>>>> +                                    c->addr << 1, c->adapter->name);
>>>>> +
>>>>> +    /* Initializes TVP7002 to its default values */
>>>>> +    error = tvp7002_write_inittab(sd, tvp7002_init_default);
>>>>> +    if (error < 0) {
>>>>> +            error = -EINVAL;
>>>>> +            goto found_error;
>>>>> +    }
>>>>> +
>>>>> +    /* Set polarity information after registers have been set */
>>>>> +    polarity = 0x5b | core->pdata->hs_polarity << 5
>>>>> +                    | core->pdata->vs_polarity << 2;
>>>>> +    error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity);
>>>>> +    if (error < 0) {
>>>>> +            error = -EINVAL;
>>>>> +            goto found_error;
>>>>> +    }
>>>>> +
>>>>> +    polarity = 0x0  | core->pdata->fid_polarity << 2
>>>>> +                    | core->pdata->sog_polarity << 1
>>>>> +                    | core->pdata->clk_polarity;
>>>>> +    error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity);
>>>>> +    if (error < 0) {
>>>>> +            error = -EINVAL;
>>>>> +            goto found_error;
>>>>> +    }
>>>>> +
>>>>> +    if (debug > 1)
>>>>> +            tvp7002_log_status(sd);
>>>>> +
>>>>> +found_error:
>>>>> +    if (error < 0)
>>>>> +            kfree(core);
>>>>> +
>>>>> +    return error;
>>>>> +}
>>>>> +
>>>>> +/*
>>>>> + * tvp7002_remove - Remove TVP7002 device support
>>>>> + * @c: ptr to i2c_client struct
>>>>> + *
>>>>> + * Reset the TVP7002 device
>>>>> + * Returns zero when successful or -EINVAL if register read fails.
>>>>> + */
>>>>> +static int tvp7002_remove(struct i2c_client *c)
>>>>> +{
>>>>> +    struct v4l2_subdev *sd = i2c_get_clientdata(c);
>>>>> +
>>>>> +    v4l2_dbg(1, debug, sd, "removing tvp7002 adapter"
>>>>> +                            "on address 0x%x\n", c->addr << 1);
>>>>> +
>>>>> +    v4l2_device_unregister_subdev(sd);
>>>>> +    kfree(to_tvp7002(sd));
>>>>> +    return 0;
>>>>> +}
>>>>> +
>>>>> +/* I2C Device ID table */
>>>>> +static const struct i2c_device_id tvp7002_id[] = {
>>>>> +    { "tvp7002", 0 },
>>>>> +    { }
>>>>> +};
>>>>> +MODULE_DEVICE_TABLE(i2c, tvp7002_id);
>>>>> +
>>>>> +/* I2C driver data */
>>>>> +static struct i2c_driver tvp7002_driver = {
>>>>> +    .driver = {
>>>>> +            .owner = THIS_MODULE,
>>>>> +            .name = "tvp7002",
>>>>> +    },
>>>>> +    .probe = tvp7002_probe,
>>>>> +    .remove = tvp7002_remove,
>>>>> +    .id_table = tvp7002_id,
>>>>> +};
>>>>> +
>>>>> +/*
>>>>> + * tvp7002_init - Initialize driver via I2C interface
>>>>> + *
>>>>> + * Register the TVP7002 driver.
>>>>> + * Returns 0 on success or < 0 on failure.
>>>>> + */
>>>>> +static int __init tvp7002_init(void)
>>>>> +{
>>>>> +    return i2c_add_driver(&tvp7002_driver);
>>>>> +}
>>>>> +
>>>>> +/*
>>>>> + * tvp7002_exit - Remove driver via I2C interface
>>>>> + *
>>>>> + * Unregister the TVP7002 driver.
>>>>> + * Returns 0 on success or < 0 on failure.
>>>>> + */
>>>>> +static void __exit tvp7002_exit(void)
>>>>> +{
>>>>> +    i2c_del_driver(&tvp7002_driver);
>>>>> +}
>>>>> +
>>>>> +module_init(tvp7002_init);
>>>>> +module_exit(tvp7002_exit);
>>>>> --
>>>>> 1.6.0.4
>>>>>
>>>>>
>>>>>           
>>>       
>> --
>> Santiago Nunez-Corrales, Eng.
>> RidgeRun Engineering, LLC
>>
>> Guayabos, Curridabat
>> San Jose, Costa Rica
>> +(506) 2271 1487
>> +(506) 8313 0536
>> http://www.ridgerun.com
>>
>>
>>     
>
>
nsnehaprabha@ti.com Sept. 4, 2009, 9:01 p.m. UTC | #6
Santiago,

I am attaching a test application for I2C, which can be used to dump registers for a device connected to I2C.

This is how you use it -
usage:
i2c CHIP -r ADDR ADDR_SIZE COUNT

Example (for TVP7002 address - 0x5c)
./i2c 0x5c -r 0 0 0x60

I am also attaching the dump from LSP 2.10, while running the loopback application for 720p resolution. You can find the applications at PSP_02_10_00_14/examples folder of the release.

You can dump the TVP7002 address space and compare it with the one from LSP 2.10.

Thanks
sneha


> -----Original Message-----
> From: Santiago Nunez-Corrales [mailto:snunez@ridgerun.com]
> Sent: Friday, September 04, 2009 4:45 PM
> To: Karicheri, Muralidharan
> Cc: Narnakaje, Snehaprabha; davinci-linux-open-
> source@linux.davincidsp.com; todd.fischer@ridgerun.com;
> diego.dompe@ridgerun.com; clark.becker@ridgerun.com
> Subject: Re: [PATCH 5/6 v4] Updated TVP7002 driver for DM365
>
> Murali,
>
>
> My current plan is:
>
>
> 1. Adapt my current code with the code in the LSP 2.10. In particular,
> use registers TVP7002_L_P_FRAME_STAT and TVP7002_CLK_P_L_STAT for
> detecting input configurations.
> 2. Review currently supported standards in the driver, check them and
> update any pertinent change. In particular, double-check register values
> with LSP code.
> 3. Add debugging functions for detection of standards.
> 4. Test module integrity (i.e. compilation).
> 5. Test module loading.
> 6. Use a component input to check detection of standards (480i to 1080p)
> and obtained data.
> 7. Fix and repeat from 4 until no errors are found.
> 8. Have a user space application to test video decoding.
>
>
> Regards,
>
>
>
> Karicheri, Muralidharan wrote:
> > Santiago,
> >
> > What is your plan to resolve this?
> >
> > Murali Karicheri
> > Software Design Engineer
> > Texas Instruments Inc.
> > Germantown, MD 20874
> > new phone: 301-407-9583
> > Old Phone : 301-515-3736 (will be deprecated)
> > email: m-karicheri2@ti.com
> >
> >
> >> -----Original Message-----
> >> From: Santiago Nunez-Corrales [mailto:snunez@ridgerun.com]
> >> Sent: Friday, September 04, 2009 4:21 PM
> >> To: Karicheri, Muralidharan
> >> Cc: Narnakaje, Snehaprabha; davinci-linux-open-
> source@linux.davincidsp.com;
> >> todd.fischer@ridgerun.com; diego.dompe@ridgerun.com;
> >> clark.becker@ridgerun.com
> >> Subject: Re: [PATCH 5/6 v4] Updated TVP7002 driver for DM365
> >>
> >> Murali,
> >>
> >>
> >> Changing the querystd code. I found various errors w.r.t. LPS 2.10 from
> >> what  I was doing in my code. I am reading the wrong register, which is
> >> causing the code to return from the call incorrectly.
> >>
> >> Regards,
> >>
> >> Karicheri, Muralidharan wrote:
> >>
> >>> Santiago,
> >>>
> >>> Also modify the querystd() by re-using the code from LSP 2.10 as I
> have
> >>>
> >> asked very beginning. This logic is tested well and works.
> >>
> >>> Murali Karicheri
> >>> Software Design Engineer
> >>> Texas Instruments Inc.
> >>> Germantown, MD 20874
> >>> new phone: 301-407-9583
> >>> Old Phone : 301-515-3736 (will be deprecated)
> >>> email: m-karicheri2@ti.com
> >>>
> >>>
> >>>
> >>>> -----Original Message-----
> >>>> From: Narnakaje, Snehaprabha
> >>>> Sent: Friday, September 04, 2009 11:18 AM
> >>>> To: santiago.nunez@ridgerun.com; davinci-linux-open-
> >>>> source@linux.davincidsp.com
> >>>> Cc: Karicheri, Muralidharan; todd.fischer@ridgerun.com;
> >>>> diego.dompe@ridgerun.com; clark.becker@ridgerun.com
> >>>> Subject: RE: [PATCH 5/6 v4] Updated TVP7002 driver for DM365
> >>>>
> >>>> Santiago,
> >>>>
> >>>>
> >>>>
> >>>>> -----Original Message-----
> >>>>> From: santiago.nunez@ridgerun.com
> [mailto:santiago.nunez@ridgerun.com]
> >>>>> Sent: Thursday, September 03, 2009 6:53 PM
> >>>>> To: davinci-linux-open-source@linux.davincidsp.com
> >>>>> Cc: Karicheri, Muralidharan; todd.fischer@ridgerun.com;
> >>>>> diego.dompe@ridgerun.com; clark.becker@ridgerun.com; Narnakaje,
> >>>>> Snehaprabha; Santiago Nunez-Corrales
> >>>>> Subject: [PATCH 5/6 v4] Updated TVP7002 driver for DM365
> >>>>>
> >>>>> From: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
> >>>>>
> >>>>> This patch provides the implementation of the TVP7002 decoder
> >>>>> driver for DM365. Implemented revisions by Vaibhav Hiremath
> >>>>> and Hans Verkuil. Removed most of controls, cleared up logic,
> >>>>> cleaned up code. Tested on DM365 TI EVM.
> >>>>>
> >>>>> Signed-off-by: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
> >>>>> ---
> >>>>>  drivers/media/video/tvp7002.c | 1464
> >>>>>
> >>>>>
> >>>> [...]
> >>>>
> >>>>
> >>>>
> >>>>> +
> >>>>> +/*
> >>>>> + * tvp7002_get_video_mode() - V4L2 decoder interface handler for
> >>>>>
> >>>>>
> >>>> querystd
> >>>>
> >>>>
> >>>>> + * @sd: pointer to standard V4L2 sub-device structure
> >>>>> + *
> >>>>> + * Returns the current standard detected by TVP7002. If no active
> >>>>>
> >> input
> >>
> >>>>> is
> >>>>> + * detected, returns -1
> >>>>> + */
> >>>>> +static v4l2_std_id tvp7002_get_video_mode(struct v4l2_subdev *sd)
> >>>>> +{
> >>>>> +    int reg01, reg02, reg03;
> >>>>> +
> >>>>> +    reg01 = tvp7002_read(sd, TVP7002_HPLL_FDBK_DIV_MSBS);
> >>>>> +    reg02 = tvp7002_read(sd, TVP7002_HPLL_FDBK_DIV_LSBS);
> >>>>> +    reg03 = tvp7002_read(sd, TVP7002_HPLL_CRTL);
> >>>>> +
> >>>>> +    if (reg01 < 0 || reg02 < 0 || reg03 < 0)
> >>>>> +            return V4L2_STD_UNKNOWN;
> >>>>> +
> >>>>> +    switch (reg01) {
> >>>>> +    case 0x35:
> >>>>> +            if (reg02 == 0xa0)
> >>>>> +                    return V4L2_STD_NTSC;
> >>>>> +            else
> >>>>> +                    return V4L2_STD_PAL;
> >>>>> +    case 0x36:
> >>>>> +            if (reg02 == 0xa0)
> >>>>> +                    return V4L2_STD_525P_60;
> >>>>> +            else
> >>>>> +                    return V4L2_STD_625P_50;
> >>>>> +    case 0x67:
> >>>>> +            return V4L2_STD_720P_60;
> >>>>> +    case 0x7b:
> >>>>> +            return V4L2_STD_720P_50;
> >>>>> +    case 0x89:
> >>>>> +            if (reg03 == 0x98)
> >>>>> +                    return V4L2_STD_1080I_60;
> >>>>> +            else
> >>>>> +                    return V4L2_STD_1080P_60;
> >>>>> +            break;
> >>>>> +    case 0xa5:
> >>>>> +            if (reg03 == 0x90)
> >>>>> +                    return V4L2_STD_1080I_50;
> >>>>> +            else
> >>>>> +                    return V4L2_STD_1080P_50;
> >>>>> +    default:
> >>>>> +            return V4L2_STD_UNKNOWN;
> >>>>> +    }
> >>>>> +}
> >>>>> +
> >>>>> +/*
> >>>>> + * tvp7002_querystd() - V4L2 decoder interface handler for querystd
> >>>>> + * @sd: pointer to standard V4L2 sub-device structure
> >>>>> + * @std_id: standard V4L2 std_id ioctl enum
> >>>>> + *
> >>>>> + * Returns the current standard detected by TVP7002. If no active
> >>>>>
> >> input
> >>
> >>>>> is
> >>>>> + * detected, returns -EINVAL
> >>>>> + */
> >>>>> +static int tvp7002_querystd(struct v4l2_subdev *sd, v4l2_std_id
> >>>>>
> >> *std_id)
> >>
> >>>>> +{
> >>>>> +    v4l2_std_id current_std;
> >>>>> +    u8 sync_lock_status;
> >>>>> +    int res;
> >>>>> +
> >>>>> +    if (std_id == NULL)
> >>>>> +            return -EINVAL;
> >>>>> +
> >>>>> +    /* get the current standard */
> >>>>> +    res = tvp7002_get_video_mode(sd);
> >>>>> +    if (res == V4L2_STD_UNKNOWN)
> >>>>> +            return -EINVAL;
> >>>>>
> >>>>>
> >>>> There seems to be a bug here - You will mostly get V4L2_STD_UNKNOWN
> for
> >>>>
> >> HD
> >>
> >>>> resolutions. The tvp7002_get_video_mode() API returns v4l2_std_id
> (which
> >>>>
> >> is
> >>
> >>>> long long - 64 bits), but you are storing the return value in "int
> res".
> >>>> You should change it to -
> >>>>
> >>>> v4l2_std_id res;
> >>>>
> >>>> Note that I am trying to integrate your driver with VPIF capture on
> >>>>
> >> DM6467
> >>
> >>>> and thus running into issues like this. It would be good to know -
> >>>>
> >> whether
> >>
> >>>> you have tested the TVP7002 driver on DM365 using a capture
> application.
> >>>>
> >>>> Thanks
> >>>> Sneha
> >>>>
> >>>>
> >>>>
> >>>>> +    current_std = res;
> >>>>> +
> >>>>> +    /* check whether signal is locked */
> >>>>> +    sync_lock_status = tvp7002_read(sd, TVP7002_SYNC_DETECT_STAT);
> >>>>> +
> >>>>> +    if (0x02 != (sync_lock_status & 0xff))
> >>>>> +            return -EINVAL; /* No input detected */
> >>>>> +
> >>>>> +    *std_id = current_std;
> >>>>> +
> >>>>> +    v4l2_dbg(1, debug, sd, "Current STD: %d %d @ %d Hz\n",
> >>>>> +
> tvp7002_resolutions[tvp7002_from_std(current_std)].hres,
> >>>>> +
> tvp7002_resolutions[tvp7002_from_std(current_std)].vres,
> >>>>> +
> tvp7002_resolutions[tvp7002_from_std(current_std)].frate);
> >>>>> +    return 0;
> >>>>> +}
> >>>>> +
> >>>>> +/*
> >>>>> + * tvp7002_try_fmt_cap() - V4L2 decoder interface handler for
> try_fmt
> >>>>> + * @sd: pointer to standard V4L2 sub-device structure
> >>>>> + * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
> >>>>> + *
> >>>>> + * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type.
> >>>>>
> >> This
> >>
> >>>>> + * ioctl is used to negotiate the image capture size and pixel
> format
> >>>>> + * without actually making it take effect.
> >>>>> + */
> >>>>> +static int tvp7002_try_fmt_cap(struct v4l2_subdev *sd, struct
> >>>>>
> >>>>>
> >>>> v4l2_format
> >>>>
> >>>>
> >>>>> *f)
> >>>>> +{
> >>>>> +    struct v4l2_pix_format *pix;
> >>>>> +    v4l2_std_id current_std;
> >>>>> +    int res;
> >>>>> +
> >>>>> +    if (f == NULL)
> >>>>> +            return -EINVAL;
> >>>>> +
> >>>>> +    if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> >>>>> +            /* only capture is supported */
> >>>>> +            return -EINVAL;
> >>>>> +
> >>>>> +    pix = &f->fmt.pix;
> >>>>> +
> >>>>> +    /* Calculate height and width based on current standard */
> >>>>> +    res = tvp7002_get_video_mode(sd);
> >>>>> +    if (res < 0)
> >>>>> +            return -EINVAL;
> >>>>> +    current_std = res;
> >>>>> +
> >>>>> +    pix->width = NUM_ACTIVE_PIXELS(current_std);
> >>>>> +    pix->height = NUM_ACTIVE_LINES(current_std);
> >>>>> +
> >>>>> +    pix->pixelformat = V4L2_PIX_FMT_UYVY;
> >>>>> +
> >>>>> +    pix->field = V4L2_FIELD_INTERLACED;
> >>>>> +    pix->bytesperline = pix->width * 2;
> >>>>> +    pix->sizeimage = pix->bytesperline * pix->height;
> >>>>> +    pix->colorspace = V4L2_COLORSPACE_REC709;
> >>>>> +    pix->priv = 0;
> >>>>> +
> >>>>> +    v4l2_dbg(1, debug, sd, "Try FMT: pixelformat - %s, bytesperline
> -
> >>>>> %d"
> >>>>> +                    "Width - %d, Height - %d",
> >>>>> +                    "8-bit UYVY 4:2:2 Format", pix->bytesperline,
> >>>>> +                    pix->width, pix->height);
> >>>>> +    return 0;
> >>>>> +}
> >>>>> +
> >>>>> +/**
> >>>>> + * tvp7002_s_fmt_cap() - V4L2 decoder interface handler for s_fmt
> >>>>> + * @sd: pointer to standard V4L2 sub-device structure
> >>>>> + * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
> >>>>> + *
> >>>>> + * If the requested format is supported, configures the HW to use
> that
> >>>>> + * format, returns error code if format not supported or HW can't
> be
> >>>>> + * correctly configured.
> >>>>> + */
> >>>>> +static int tvp7002_s_fmt_cap(struct v4l2_subdev *sd, struct
> >>>>>
> >> v4l2_format
> >>
> >>>>> *f)
> >>>>> +{
> >>>>> +    struct tvp7002 *decoder = to_tvp7002(sd);
> >>>>> +    struct v4l2_pix_format *pix;
> >>>>> +    int rval;
> >>>>> +
> >>>>> +    if (f == NULL)
> >>>>> +            return -EINVAL;
> >>>>> +
> >>>>> +    if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> >>>>> +            /* only capture is supported */
> >>>>> +            return -EINVAL;
> >>>>> +
> >>>>> +    pix = &f->fmt.pix;
> >>>>> +    rval = tvp7002_try_fmt_cap(sd, f);
> >>>>> +    if (rval)
> >>>>> +            return rval;
> >>>>> +
> >>>>> +    decoder->pix = *pix;
> >>>>> +
> >>>>> +    return rval;
> >>>>> +}
> >>>>> +
> >>>>> +/*
> >>>>> + * tvp7002_g_fmt() - V4L2 decoder interface handler for
> tvp7002_g_fmt
> >>>>> + * @sd: pointer to standard V4L2 sub-device structure
> >>>>> + * @f: pointer to standard V4L2 v4l2_format structure
> >>>>> + *
> >>>>> + * Returns the decoder's current pixel format in the v4l2_format
> >>>>> + * parameter.
> >>>>> + */
> >>>>> +static int tvp7002_g_fmt(struct v4l2_subdev *sd, struct v4l2_format
> >>>>>
> >> *f)
> >>
> >>>>> +{
> >>>>> +    struct tvp7002 *decoder = to_tvp7002(sd);
> >>>>> +
> >>>>> +    if (f == NULL)
> >>>>> +            return -EINVAL;
> >>>>> +
> >>>>> +    if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> >>>>> +            /* only capture is supported */
> >>>>> +            return -EINVAL;
> >>>>> +
> >>>>> +    f->fmt.pix = decoder->pix;
> >>>>> +
> >>>>> +    v4l2_dbg(1, debug, sd, "Current FMT: bytesperline - %d"
> >>>>> +                    "Width - %d, Height - %d",
> >>>>> +                    decoder->pix.bytesperline,
> >>>>> +                    decoder->pix.width, decoder->pix.height);
> >>>>> +    return 0;
> >>>>> +}
> >>>>> +
> >>>>> +/*
> >>>>> + * tvp7002_s_ctrl() - Set a control
> >>>>> + * @sd: ptr to v4l2_subdev struct
> >>>>> + * @ctrl: ptr to v4l2_control struct
> >>>>> + *
> >>>>> + * Set a control for a TVP7002 decoder device.
> >>>>> + * Returns zero when successful or -EINVAL if register access
> fails.
> >>>>> + */
> >>>>> +static int tvp7002_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
> >>>>> +{
> >>>>> +    struct tvp7002 *decoder = to_tvp7002(sd);
> >>>>> +    int vmd = 0;
> >>>>> +
> >>>>> +    decoder->video_mode = std;
> >>>>> +    vmd = tvp7002_from_std(std);
> >>>>> +
> >>>>> +    v4l2_dbg(1, debug, sd, "Set video std mode to %llx.\n", std);
> >>>>> +
> >>>>> +    return tvp7002_set_video_mode(sd, vmd);
> >>>>> +}
> >>>>> +
> >>>>> +/*
> >>>>> + * tvp7002_g_ctrl() - Get a control
> >>>>> + * @sd: ptr to v4l2_subdev struct
> >>>>> + * @ctrl: ptr to v4l2_control struct
> >>>>> + *
> >>>>> + * Get a control for a TVP7002 decoder device.
> >>>>> + * Returns zero when successful or -EINVAL if register access
> fails.
> >>>>> + */
> >>>>> +static int tvp7002_g_ctrl(struct v4l2_subdev *sd, struct
> v4l2_control
> >>>>> *ctrl)
> >>>>> +{
> >>>>> +    int rval, gval, bval;
> >>>>> +    int res;
> >>>>> +
> >>>>> +    v4l2_info(sd, "g_ctrl called\n");
> >>>>> +
> >>>>> +    switch (ctrl->id) {
> >>>>> +    case V4L2_CID_GAIN:
> >>>>> +            rval = tvp7002_read(sd, TVP7002_R_FINE_GAIN);
> >>>>> +            gval = tvp7002_read(sd, TVP7002_G_FINE_GAIN);
> >>>>> +            bval = tvp7002_read(sd, TVP7002_B_FINE_GAIN);
> >>>>> +
> >>>>> +            if (rval < 0 || gval < 0 || bval < 0) {
> >>>>> +                    res = -1;
> >>>>> +            } else if (rval != gval || rval != bval) {
> >>>>> +                    res = -1;
> >>>>> +            } else {
> >>>>> +                    ctrl->value = rval & 0x0F;
> >>>>> +                    res = ctrl->value;
> >>>>> +            }
> >>>>> +            break;
> >>>>> +    default:
> >>>>> +            res = -1;
> >>>>> +            break;
> >>>>> +    }
> >>>>> +
> >>>>> +    return res < 0 ? res : 0;
> >>>>> +}
> >>>>> +
> >>>>> +/*
> >>>>> + * tvp7002_s_ctrl() - Set a control
> >>>>> + * @sd: ptr to v4l2_subdev struct
> >>>>> + * @ctrl: ptr to v4l2_control struct
> >>>>> + *
> >>>>> + * Set a control in TVP7002 decoder device.
> >>>>> + * Returns zero when successful or -EINVAL if register access
> fails.
> >>>>> + */
> >>>>> +static int tvp7002_s_ctrl(struct v4l2_subdev *sd, struct
> v4l2_control
> >>>>> *ctrl)
> >>>>> +{
> >>>>> +    int rval, gval, bval;
> >>>>> +    int error;
> >>>>> +    u8 i, n;
> >>>>> +    n = ARRAY_SIZE(tvp7002_qctrl);
> >>>>> +
> >>>>> +    for (i = 0; i < n; i++)
> >>>>> +            if (ctrl->id == tvp7002_qctrl[i].id)
> >>>>> +                    break;
> >>>>> +
> >>>>> +    if (i == n)
> >>>>> +            return -EINVAL;
> >>>>> +
> >>>>> +    if (ctrl->value < tvp7002_qctrl[i].minimum ||
> >>>>> +                    ctrl->value > tvp7002_qctrl[i].maximum)
> >>>>> +            return -ERANGE;
> >>>>> +
> >>>>> +    switch (ctrl->id) {
> >>>>> +    case V4L2_CID_GAIN:
> >>>>> +            rval = tvp7002_write(sd, TVP7002_R_FINE_GAIN,
> >>>>> +                                                    ctrl->value &
> >>>>>
> >> 0xff);
> >>
> >>>>> +            gval = tvp7002_write(sd, TVP7002_G_FINE_GAIN,
> >>>>> +                                                    ctrl->value &
> >>>>>
> >> 0xff);
> >>
> >>>>> +            bval = tvp7002_write(sd, TVP7002_B_FINE_GAIN,
> >>>>> +                                                    ctrl->value &
> >>>>>
> >> 0xff);
> >>
> >>>>> +            if (rval < 0  || gval < 0 || bval < 0)
> >>>>> +                    error = -1;
> >>>>> +            else
> >>>>> +                    error = rval;
> >>>>> +            break;
> >>>>> +    default:
> >>>>> +            error = -1;
> >>>>> +            break;
> >>>>> +    }
> >>>>> +
> >>>>> +    return error < 0 ? -EINVAL : 0;
> >>>>> +}
> >>>>> +
> >>>>> +/*
> >>>>> + * tvp7002_g_register() - Get the value of a register
> >>>>> + * @sd: ptr to v4l2_subdev struct
> >>>>> + * @vreg: ptr to v4l2_dbg_register struct
> >>>>> + *
> >>>>> + * Get the value of a TVP7002 decoder device register.
> >>>>> + * Returns zero when successful, -EINVAL if register read fails or
> >>>>> + * access to I2C client fails, -EPERM if the call is not allowed
> >>>>> + * by diabled CAP_SYS_ADMIN.
> >>>>> + */
> >>>>> +#ifdef CONFIG_VIDEO_ADV_DEBUG
> >>>>> +static int tvp7002_g_register(struct v4l2_subdev *sd,
> >>>>> +                                            struct
> v4l2_dbg_register
> >>>>>
> >> *reg)
> >>
> >>>>> +{
> >>>>> +    struct i2c_client *client = v4l2_get_subdevdata(sd);
> >>>>> +
> >>>>> +    if (!v4l2_chip_match_i2c_client(client, &reg->match))
> >>>>> +            return -EINVAL;
> >>>>> +    if (!capable(CAP_SYS_ADMIN))
> >>>>> +            return -EPERM;
> >>>>> +
> >>>>> +    return reg->val < 0 ? -EINVAL : 0;
> >>>>> +}
> >>>>> +
> >>>>> +/*
> >>>>> + * tvp7002_s_register() - set a control
> >>>>> + * @sd: ptr to v4l2_subdev struct
> >>>>> + * @ctrl: ptr to v4l2_control struct
> >>>>> + *
> >>>>> + * Get the value of a TVP7002 decoder device register.
> >>>>> + * Returns zero when successful or -EINVAL if register read fails.
> >>>>> + */
> >>>>> +static int tvp7002_s_register(struct v4l2_subdev *sd,
> >>>>> +                                            struct
> v4l2_dbg_register
> >>>>>
> >> *reg)
> >>
> >>>>> +{
> >>>>> +    int wres;
> >>>>> +    struct i2c_client *client = v4l2_get_subdevdata(sd);
> >>>>> +
> >>>>> +    if (!v4l2_chip_match_i2c_client(client, &reg->match))
> >>>>> +            return -EINVAL;
> >>>>> +    if (!capable(CAP_SYS_ADMIN))
> >>>>> +            return -EPERM;
> >>>>> +
> >>>>> +    wres = tvp7002_write(sd, reg->reg & 0xff, reg->val & 0xff);
> >>>>> +
> >>>>> +    return wres < 0 ? -EINVAL : 0;
> >>>>> +}
> >>>>> +#endif
> >>>>> +
> >>>>> +/*
> >>>>> + * tvp7002_queryctrl() - Query a control
> >>>>> + * @sd: ptr to v4l2_subdev struct
> >>>>> + * @ctrl: ptr to v4l2_queryctrl struct
> >>>>> + *
> >>>>> + * Query a control of a TVP7002 decoder device.
> >>>>> + * Returns zero when successful or -EINVAL if register read fails.
> >>>>> + */
> >>>>> +static int tvp7002_queryctrl(struct v4l2_subdev *sd, struct
> >>>>> v4l2_queryctrl *qc)
> >>>>> +{
> >>>>> +    int i, error;
> >>>>> +    error = -EINVAL;
> >>>>> +
> >>>>> +    v4l2_info(sd, "queryctrl called\n");
> >>>>> +
> >>>>> +    for (i = 0; i < ARRAY_SIZE(tvp7002_qctrl); i++)
> >>>>> +            if (qc->id && qc->id == tvp7002_qctrl[i].id) {
> >>>>> +                    v4l2_ctrl_query_fill(qc,
> tvp7002_qctrl[i].minimum,
> >>>>> +                                    tvp7002_qctrl[i].maximum,
> >>>>> +                                    tvp7002_qctrl[i].step,
> >>>>> +
> tvp7002_qctrl[i].default_value);
> >>>>> +                    error = 0;
> >>>>> +                    break;
> >>>>> +            }
> >>>>> +
> >>>>> +    return error;
> >>>>> +}
> >>>>> +
> >>>>> +/*
> >>>>> + * tvp7002_s_stream() - V4L2 decoder i/f handler for s_stream
> >>>>> + * @sd: pointer to standard V4L2 sub-device structure
> >>>>> + * @enable: streaming enable or disable
> >>>>> + *
> >>>>> + * Sets streaming to enable or disable, if possible.
> >>>>> + */
> >>>>> +static int tvp7002_s_stream(struct v4l2_subdev *sd, int enable)
> >>>>> +{
> >>>>> +    int err = 0;
> >>>>> +    struct tvp7002 *decoder = to_tvp7002(sd);
> >>>>> +
> >>>>> +    if (decoder->streaming == enable)
> >>>>> +            return 0;
> >>>>> +
> >>>>> +    if (enable) {
> >>>>> +            /* Power Up Sequence */
> >>>>> +            err = tvp7002_write(sd, TVP7002_PWR_CTL, 0x00);
> >>>>> +            if (err) {
> >>>>> +                    v4l2_err(sd, "Unable to turn on decoder\n");
> >>>>> +                    err = -EINVAL;
> >>>>> +            }
> >>>>> +            err = tvp7002_write_inittab(sd, tvp7002_init_default);
> >>>>> +            if (err < 0) {
> >>>>> +                    v4l2_err(sd, "Unable to initialize\n");
> >>>>> +                    err = -EINVAL;
> >>>>> +            }
> >>>>> +            /* Detect if not already detected */
> >>>>> +            err = tvp7002_read(sd, TVP7002_CHIP_REV);
> >>>>> +            if (err < 0) {
> >>>>> +                    v4l2_err(sd, "Unable to detect decoder\n");
> >>>>> +                    err = -EINVAL;
> >>>>> +            }
> >>>>> +            decoder->streaming = enable;
> >>>>> +    } else {
> >>>>> +            /* Power Down Sequence */
> >>>>> +            err = tvp7002_write(sd, TVP7002_PWR_CTL, 0x40);
> >>>>> +            if (err) {
> >>>>> +                    v4l2_err(sd, "Unable to turn off decoder\n");
> >>>>> +                    return err;
> >>>>> +            }
> >>>>> +            decoder->streaming = enable;
> >>>>> +    }
> >>>>> +
> >>>>> +    return err;
> >>>>> +}
> >>>>> +
> >>>>> +/* Specific video subsystem operation handlers */
> >>>>> +static const struct v4l2_subdev_video_ops tvp7002_video_ops = {
> >>>>> +    .querystd = tvp7002_querystd,
> >>>>> +    .s_stream = tvp7002_s_stream,
> >>>>> +    .g_fmt = tvp7002_g_fmt,
> >>>>> +};
> >>>>> +
> >>>>> +/* V4L2 Operations handlers */
> >>>>> +static const struct v4l2_subdev_core_ops tvp7002_core_ops = {
> >>>>> +    .g_chip_ident = tvp7002_g_chip_ident,
> >>>>> +    .log_status = tvp7002_log_status,
> >>>>> +    .g_ctrl = tvp7002_g_ctrl,
> >>>>> +    .s_ctrl = tvp7002_s_ctrl,
> >>>>> +    .queryctrl = tvp7002_queryctrl,
> >>>>> +    .s_std = tvp7002_s_std,
> >>>>> +#ifdef CONFIG_VIDEO_ADV_DEBUG
> >>>>> +    .g_register = tvp7002_g_register,
> >>>>> +    .s_register = tvp7002_s_register,
> >>>>> +#endif
> >>>>> +};
> >>>>> +
> >>>>> +static const struct v4l2_subdev_ops tvp7002_ops = {
> >>>>> +    .core = &tvp7002_core_ops,
> >>>>> +    .video = &tvp7002_video_ops,
> >>>>> +};
> >>>>> +
> >>>>> +/* Default driver settings */
> >>>>> +static struct tvp7002 tvp7002_dev = {
> >>>>> +    .streaming = 0,
> >>>>> +
> >>>>> +    .pix = {
> >>>>> +            .width = 720,
> >>>>> +            .height = 480,
> >>>>> +            .pixelformat = V4L2_PIX_FMT_UYVY,
> >>>>> +            .field = V4L2_FIELD_INTERLACED,
> >>>>> +            .bytesperline = 720 * 2,
> >>>>> +            .sizeimage = 720 * 2 * 480,
> >>>>> +            .colorspace = V4L2_COLORSPACE_SMPTE170M,
> >>>>> +            },
> >>>>> +
> >>>>> +    .video_mode = V4L2_STD_NTSC,
> >>>>> +};
> >>>>> +
> >>>>> +/*
> >>>>> + * tvp7002_reset - Reset a TVP7002 device
> >>>>> + * @sd: ptr to v4l2_subdev struct
> >>>>> + * @val: unsigned integer (not used)
> >>>>> + *
> >>>>> + * Reset the TVP7002 device
> >>>>> + * Returns zero when successful or -EINVAL if register read fails.
> >>>>> + */
> >>>>> +static int tvp7002_reset(struct v4l2_subdev *sd, u32 val)
> >>>>> +{
> >>>>> +    struct tvp7002 *core = to_tvp7002(sd);
> >>>>> +    int polarity;
> >>>>> +    int error;
> >>>>> +
> >>>>> +    error = tvp7002_read(sd, TVP7002_CHIP_REV);
> >>>>> +    if (error < 0) {
> >>>>> +            error = -EINVAL;
> >>>>> +            goto found_error;
> >>>>> +    }
> >>>>> +
> >>>>> +    if (error == 0x02) {
> >>>>> +            v4l2_info(sd, "rev. %02x detected.\n", error);
> >>>>> +    } else {
> >>>>> +            v4l2_info(sd, "unknown revision detected.\n");
> >>>>> +            v4l2_info(sd, "revision number is %02x\n", error);
> >>>>> +    }
> >>>>> +
> >>>>> +    /* Initializes TVP7002 to its default values */
> >>>>> +    error = tvp7002_write_inittab(sd, tvp7002_init_default);
> >>>>> +    if (error < 0) {
> >>>>> +            error = -EINVAL;
> >>>>> +            goto found_error;
> >>>>> +    }
> >>>>> +
> >>>>> +    /* Set polarity information after registers have been set */
> >>>>> +    polarity = 0x5b | core->pdata->hs_polarity << 5
> >>>>> +                    | core->pdata->vs_polarity << 2;
> >>>>> +    error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity);
> >>>>> +    if (error < 0) {
> >>>>> +            error = -EINVAL;
> >>>>> +            goto found_error;
> >>>>> +    }
> >>>>> +
> >>>>> +    polarity = 0x0  | core->pdata->fid_polarity << 2
> >>>>> +                    | core->pdata->sog_polarity << 1
> >>>>> +                    | core->pdata->clk_polarity;
> >>>>> +    error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity);
> >>>>> +    if (error < 0) {
> >>>>> +            error = -EINVAL;
> >>>>> +            goto found_error;
> >>>>> +    }
> >>>>> +
> >>>>> +found_error:
> >>>>> +    return error;
> >>>>> +};
> >>>>> +
> >>>>> +/*
> >>>>> + * tvp7002_probe - Reset a TVP7002 device
> >>>>> + * @sd: ptr to v4l2_subdev struct
> >>>>> + * @ctrl: ptr to i2c_device_id struct
> >>>>> + *
> >>>>> + * Reset the TVP7002 device
> >>>>> + * Returns zero when successful or -EINVAL if register read fails.
> >>>>> + */
> >>>>> +static int tvp7002_probe(struct i2c_client *c, const struct
> >>>>>
> >>>>>
> >>>> i2c_device_id
> >>>>
> >>>>
> >>>>> *id)
> >>>>> +{
> >>>>> +    struct v4l2_subdev *sd;
> >>>>> +    struct tvp7002 *core;
> >>>>> +    int polarity;
> >>>>> +    int error;
> >>>>> +
> >>>>> +    /* Check if the adapter supports the needed features */
> >>>>> +    if (!i2c_check_functionality(c->adapter,
> >>>>> +            I2C_FUNC_SMBUS_READ_BYTE |
> >>>>>
> >> I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
> >>
> >>>>> +            return -EIO;
> >>>>> +
> >>>>> +    if (!c->dev.platform_data) {
> >>>>> +            v4l2_err(c, "No platform data!!\n");
> >>>>> +            return -ENODEV;
> >>>>> +    }
> >>>>> +
> >>>>> +    core = kzalloc(sizeof(struct tvp7002), GFP_KERNEL);
> >>>>> +
> >>>>> +    if (!core)
> >>>>> +            return -ENOMEM;
> >>>>> +
> >>>>> +    *core = tvp7002_dev;
> >>>>> +    sd = &core->sd;
> >>>>> +    core->pdata = c->dev.platform_data;
> >>>>> +
> >>>>> +    v4l2_i2c_subdev_init(sd, c, &tvp7002_ops);
> >>>>> +    v4l_info(c, "tvp7002 found @ 0x%02x (%s)\n",
> >>>>> +                                    c->addr << 1, c->adapter-
> >name);
> >>>>> +
> >>>>> +    /* Initializes TVP7002 to its default values */
> >>>>> +    error = tvp7002_write_inittab(sd, tvp7002_init_default);
> >>>>> +    if (error < 0) {
> >>>>> +            error = -EINVAL;
> >>>>> +            goto found_error;
> >>>>> +    }
> >>>>> +
> >>>>> +    /* Set polarity information after registers have been set */
> >>>>> +    polarity = 0x5b | core->pdata->hs_polarity << 5
> >>>>> +                    | core->pdata->vs_polarity << 2;
> >>>>> +    error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity);
> >>>>> +    if (error < 0) {
> >>>>> +            error = -EINVAL;
> >>>>> +            goto found_error;
> >>>>> +    }
> >>>>> +
> >>>>> +    polarity = 0x0  | core->pdata->fid_polarity << 2
> >>>>> +                    | core->pdata->sog_polarity << 1
> >>>>> +                    | core->pdata->clk_polarity;
> >>>>> +    error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity);
> >>>>> +    if (error < 0) {
> >>>>> +            error = -EINVAL;
> >>>>> +            goto found_error;
> >>>>> +    }
> >>>>> +
> >>>>> +    if (debug > 1)
> >>>>> +            tvp7002_log_status(sd);
> >>>>> +
> >>>>> +found_error:
> >>>>> +    if (error < 0)
> >>>>> +            kfree(core);
> >>>>> +
> >>>>> +    return error;
> >>>>> +}
> >>>>> +
> >>>>> +/*
> >>>>> + * tvp7002_remove - Remove TVP7002 device support
> >>>>> + * @c: ptr to i2c_client struct
> >>>>> + *
> >>>>> + * Reset the TVP7002 device
> >>>>> + * Returns zero when successful or -EINVAL if register read fails.
> >>>>> + */
> >>>>> +static int tvp7002_remove(struct i2c_client *c)
> >>>>> +{
> >>>>> +    struct v4l2_subdev *sd = i2c_get_clientdata(c);
> >>>>> +
> >>>>> +    v4l2_dbg(1, debug, sd, "removing tvp7002 adapter"
> >>>>> +                            "on address 0x%x\n", c->addr << 1);
> >>>>> +
> >>>>> +    v4l2_device_unregister_subdev(sd);
> >>>>> +    kfree(to_tvp7002(sd));
> >>>>> +    return 0;
> >>>>> +}
> >>>>> +
> >>>>> +/* I2C Device ID table */
> >>>>> +static const struct i2c_device_id tvp7002_id[] = {
> >>>>> +    { "tvp7002", 0 },
> >>>>> +    { }
> >>>>> +};
> >>>>> +MODULE_DEVICE_TABLE(i2c, tvp7002_id);
> >>>>> +
> >>>>> +/* I2C driver data */
> >>>>> +static struct i2c_driver tvp7002_driver = {
> >>>>> +    .driver = {
> >>>>> +            .owner = THIS_MODULE,
> >>>>> +            .name = "tvp7002",
> >>>>> +    },
> >>>>> +    .probe = tvp7002_probe,
> >>>>> +    .remove = tvp7002_remove,
> >>>>> +    .id_table = tvp7002_id,
> >>>>> +};
> >>>>> +
> >>>>> +/*
> >>>>> + * tvp7002_init - Initialize driver via I2C interface
> >>>>> + *
> >>>>> + * Register the TVP7002 driver.
> >>>>> + * Returns 0 on success or < 0 on failure.
> >>>>> + */
> >>>>> +static int __init tvp7002_init(void)
> >>>>> +{
> >>>>> +    return i2c_add_driver(&tvp7002_driver);
> >>>>> +}
> >>>>> +
> >>>>> +/*
> >>>>> + * tvp7002_exit - Remove driver via I2C interface
> >>>>> + *
> >>>>> + * Unregister the TVP7002 driver.
> >>>>> + * Returns 0 on success or < 0 on failure.
> >>>>> + */
> >>>>> +static void __exit tvp7002_exit(void)
> >>>>> +{
> >>>>> +    i2c_del_driver(&tvp7002_driver);
> >>>>> +}
> >>>>> +
> >>>>> +module_init(tvp7002_init);
> >>>>> +module_exit(tvp7002_exit);
> >>>>> --
> >>>>> 1.6.0.4
> >>>>>
> >>>>>
> >>>>>
> >>>
> >> --
> >> Santiago Nunez-Corrales, Eng.
> >> RidgeRun Engineering, LLC
> >>
> >> Guayabos, Curridabat
> >> San Jose, Costa Rica
> >> +(506) 2271 1487
> >> +(506) 8313 0536
> >> http://www.ridgerun.com
> >>
> >>
> >>
> >
> >
>
>
> --
> Santiago Nunez-Corrales, Eng.
> RidgeRun Engineering, LLC
>
> Guayabos, Curridabat
> San Jose, Costa Rica
> +(506) 2271 1487
> +(506) 8313 0536
> http://www.ridgerun.com
>
>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <linux/i2c.h>
#include <linux/i2c-dev.h>

#include <unistd.h>
#include <sys/ioctl.h>

unsigned char bufAddr[5];
unsigned char* buf = NULL;

void help()
{
	printf("usage:\n");
	printf("i2c CHIP -r ADDR ADDR_SIZE COUNT\n");
	printf("i2c CHIP -w ADDR ADDR_SIZE VALUE\n");
	printf("i2c CHIP -sr\n");
	printf("i2c CHIP -sw VALUE\n");
}


/**
 * Super-safe conversion of a string into an unsigned.
 * It exits the application on failure.
 */
unsigned long getUnsigned(const char* szParam)
{
	char* szEndPtr = NULL;

	unsigned long uResult = strtoul(szParam, &szEndPtr, 0);

	if( (szParam[0] != '\0') &&
		(*szEndPtr == '\0'))
	{
		if(uResult == ULONG_MAX)
		{
			printf("Too large integer: %s\n", szParam);
			exit(1);
		}

		return uResult;
	}

	printf("Invalid value : %s\n", szParam);
	exit(1);
}



int main_short(int argc, char** argv)
{
  int iFd;
  int iRes;
  
  char* szCmd = NULL;
  if(argc == 3)
    {
      szCmd = argv[2];
      if(strcmp(szCmd, "-sr") != 0)
	{
	  help();
	  return 1;
	}
    }
  else if(argc == 4)
    {
      szCmd = argv[2];
      if(strcmp(szCmd, "-sw") != 0)
	{
	  help();
	  return 1;
	}
    }
  else
    {
      help();
      return 1;
    }
  
  /* We open the I2C bus. */
  iFd = open("/dev/i2c-0", O_RDWR);
  if(iFd < 0)
    {
      printf("Cannot open I2C device\n");
      return 1;
    }
  
  char* szChip = argv[1];
  char* szData = NULL;
  unsigned char ucChip;
  unsigned char ucData;
  ucChip = (unsigned char)(getUnsigned(szChip));
  
  if(argc == 4)
    {
      szData = argv[3];
      ucData = (unsigned char)(getUnsigned(szData));
    }
  
  struct i2c_msg msgs[1];
  struct i2c_rdwr_ioctl_data data;
  
  msgs[0].addr = ucChip;
  msgs[0].flags = (argc == 3) ? I2C_M_RD : 0;
  msgs[0].len = 1;
  msgs[0].buf = &ucData;
  
  data.msgs = msgs;
  data.nmsgs = 1;
  
  iRes = ioctl(iFd, I2C_RDWR, &data);
  
  if(iRes >= 0)
    {
      if(argc == 3)
	printf("0x%x\n", ucData);
    }
  else
    printf("IOCTL failed iRes=%i\n", iRes);
  
  close(iFd);
  
  return 0;
}

int main(int argc, char** argv)
{
  int iFd;
  int iRes;
  int i;
  
  /* We open the I2C bus. */
  iFd = open("/dev/i2c-0", O_RDWR);
  if(iFd < 0)
    {
      printf("Cannot open I2C device\n");
      return 1;
    }
  
  unsigned char ucChip;
  int bRead = 0;
  unsigned long uAddr;
  unsigned long uAddrSize;
  unsigned long uValueOrCount;

  if(argc == 3 || argc == 4)
    return main_short(argc, argv);
  else 
    if(argc != 6)
      {
	help();
	return 1;
      }
  
  char* szChip = argv[1];
  char* szReadWrite = argv[2];
  char* szAddr = argv[3];
  char* szAddrSize = argv[4];
  char* szValueOrCount = argv[5];

  ucChip = (unsigned char)(getUnsigned(szChip));
  
  if(strncmp(szReadWrite, "-r", 2) == 0)
    bRead = 1;
  else if(strncmp(szReadWrite, "-w", 2) == 0)
    bRead = 0;
  else
    {
      help();
      return 1;
    }

  uAddr = getUnsigned(szAddr);
  uAddrSize = getUnsigned(szAddrSize);
  if(uAddrSize > 4)
    {
      help();
      return 1;
    }

  uValueOrCount = getUnsigned(szValueOrCount);
  
  if(!bRead && uValueOrCount > 255)
    {
      help();
      return 1;
    }
  
  if(bRead)
    {
      buf = (unsigned char*)malloc(uValueOrCount);
      if(buf == NULL)
	{
	  printf("Cannot allocate %lu bytes\n", uValueOrCount);
	  return 1;
	}
    }

  struct i2c_msg msgs[2];
  struct i2c_rdwr_ioctl_data data;
  
  if(bRead)
    {
      for(i=0; i < uAddrSize; i++)
	bufAddr[i] = ((unsigned char*)&uAddr)[uAddrSize-1-i];
      
      msgs[0].addr = ucChip;
      msgs[0].flags = 0;
      msgs[0].len = uAddrSize;
      msgs[0].buf = bufAddr;
      
      msgs[1].addr = ucChip;
      msgs[1].flags = I2C_M_RD /* | I2C_M_REV_DIR_ADDR */;
      msgs[1].len = uValueOrCount;
      msgs[1].buf = buf;		
      
      data.msgs = msgs;
      data.nmsgs = 2;
    }
  else
    {
      for(i=0; i < uAddrSize; i++)
	bufAddr[i] = ((unsigned char*)&uAddr)[uAddrSize-1-i];
      bufAddr[uAddrSize] = uValueOrCount;
      
      msgs[0].addr = ucChip;
      msgs[0].flags = 0;
      msgs[0].len = uAddrSize + 1;
      msgs[0].buf = bufAddr;
      
      data.msgs = msgs;
      data.nmsgs = 1;
    }
  
  
  iRes = ioctl(iFd, I2C_RDWR, &data);
  
  if(iRes >= 0)
    {
      if(bRead)
	{
	  for(i=0; i<uValueOrCount; i++)
	    printf("0x%x\n", buf[i]);
	}
      else
	printf("Success !\n");
    }
  else
    printf("IOCTL failed iRes=%i\n", iRes);
  
  close(iFd);
  
  return 0;
}
# ./i2c-lsp210 0x5c -r 0 0 0x60
0x2
0x67
0x20
0xa0
0x80
0x32
0x20
0x60
0x0
0x0
0x0
0x80
0x80
0x80
0x20
0x2e
0x5d
0x47
0x0
0x0
0x6f
0x47
0x1
0x0
0x1
0x0
0x67
0x77
0x7
0x0
0x10
0x10
0x10
0x8
0x0
0x4a
0x25
0x39
0x80
0x8
0x53
0x8
0x7
0x0
0x50
0x0
0x80
0x8c
0x4
0x5a
0x18
0x60
0x3
0x10
0x0
0xee
0x22
0x8b
0x0
0x3
0x5
0x3
0x4
0x1
0x47
0x1
0x4b
0x6
0x5
0x0
0x2d
0x0
0x0
0x0
0xe3
0x16
0x4f
0x2
0xce
0x6
0xab
0xf3
0x0
0x10
0x55
0xfc
0x78
0xf1
0x88
0xfe
0x0
0x10
0x0
0x0
0x0
0x0
Hans Verkuil Sept. 6, 2009, 5:50 p.m. UTC | #7
On Friday 04 September 2009 00:52:44 santiago.nunez@ridgerun.com wrote:
> From: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
> 
> This patch provides the implementation of the TVP7002 decoder
> driver for DM365. Implemented revisions by Vaibhav Hiremath
> and Hans Verkuil. Removed most of controls, cleared up logic,
> cleaned up code. Tested on DM365 TI EVM.

Hi,

I was abroad and have not been able to review this. Since there were several
other comments I think it is better if I skip this round and review the next
patch series.

One quick note though: don't declare the tvp7002_qctrl array. Look at how it
is done in tvp514x.c.

Regards,

	Hans

> 
> Signed-off-by: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
> ---
>  drivers/media/video/tvp7002.c | 1464 +++++++++++++++++++++++++++++++++++++++++
>  1 files changed, 1464 insertions(+), 0 deletions(-)
>  create mode 100644 drivers/media/video/tvp7002.c
> 
> diff --git a/drivers/media/video/tvp7002.c b/drivers/media/video/tvp7002.c
> new file mode 100644
> index 0000000..e6f1fe1
> --- /dev/null
> +++ b/drivers/media/video/tvp7002.c
> @@ -0,0 +1,1464 @@
> +/* Texas Instruments Triple 8-/10-BIT 165-/110-MSPS Video and Graphics
> + * Digitizer with Horizontal PLL registers
> + *
> + * Copyright (C) 2009 Texas Instruments Inc
> + * Author: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
> + *
> + * This code is partially based upon the TVP5150 driver
> + * written by Mauro Carvalho Chehab (mchehab@infradead.org)
> + * and the TVP514x driver written by Vaibhav Hiremath <hvaibhav@ti.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write to the Free Software
> + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
> + */
> +
> +#include <linux/i2c.h>
> +#include <linux/videodev2.h>
> +#include <linux/delay.h>
> +#include <media/v4l2-device.h>
> +#include <media/tvp7002.h>
> +#include <media/v4l2-chip-ident.h>
> +#include <media/davinci/videohd.h>
> +#include "tvp7002_reg.h"
> +
> +MODULE_DESCRIPTION("TI TVP7002 Video and Graphics Digitizer driver");
> +MODULE_AUTHOR("Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>");
> +MODULE_LICENSE("GPL");
> +
> +/* I2C retry attempts */
> +#define I2C_RETRY_COUNT                 (5)
> +
> +/* Debugging information */
> +
> +static int debug;
> +module_param(debug, int, 0);
> +MODULE_PARM_DESC(debug, "Debug level (0-2)");
> +
> +/* Struct for handling resolutions and associate register values */
> +struct tvp7002_resol {
> +	v4l2_std_id id;
> +	int hres;
> +	int vres;
> +	int frate;
> +	int lrate;
> +	int prate;
> +	u8 reg01;
> +	u8 reg02;
> +	u8 reg03;
> +	u8 reg04;
> +};
> +
> +/* Struct for handling register values */
> +struct i2c_reg_value {
> +	u8 reg;
> +	u8 value;
> +};
> +
> +/* Register default values (according to tvp7002 datasheet) */
> +static const struct i2c_reg_value tvp7002_init_default[] = {
> +	/* 0x00: read only */
> +	{ TVP7002_HPLL_FDBK_DIV_MSBS, 0x67 },
> +	{ TVP7002_HPLL_FDBK_DIV_LSBS, 0x20 },
> +	{ TVP7002_HPLL_CRTL, 0xa8 },
> +	{ TVP7002_HPLL_PHASE_SEL, 0x80 },
> +	{ TVP7002_CLAMP_START, 0x32 },
> +	{ TVP7002_CLAMP_W, 0x20 },
> +	{ TVP7002_HSYNC_OUT_W, 0x20 },
> +	{ TVP7002_B_FINE_GAIN, 0x00 },
> +	{ TVP7002_G_FINE_GAIN, 0x00 },
> +	{ TVP7002_R_FINE_GAIN, 0x00 },
> +	{ TVP7002_B_FINE_OFF_MSBS, 0x80 },
> +	{ TVP7002_G_FINE_OFF_MSBS, 0x80 },
> +	{ TVP7002_R_FINE_OFF_MSBS, 0x80 },
> +	{ TVP7002_SYNC_CTL_1, 0x5b },
> +	{ TVP7002_HPLL_AND_CLAMP_CTL, 0x2e },
> +	{ TVP7002_SYNC_ON_G_THRS, 0x5d },
> +	{ TVP7002_SYNC_SEPARATOR_THRS, 0x20 },
> +	{ TVP7002_HPLL_PRE_COAST, 0x00 },
> +	{ TVP7002_HPLL_POST_COAST, 0x00 },
> +	/* 0x14: read only */
> +	{ TVP7002_OUT_FORMATTER, 0x00 },
> +	{ TVP7002_MISC_CTL_1, 0x11 },
> +	{ TVP7002_MISC_CTL_2, 0x03 },
> +	{ TVP7002_MISC_CTL_3, 0x00 },
> +	{ TVP7002_IN_MUX_SEL_1, 0x00 },
> +	{ TVP7002_IN_MUX_SEL_2, 0xc2 },
> +	{ TVP7002_B_AND_G_COARSE_GAIN, 0x77 },
> +	{ TVP7002_R_COARSE_GAIN, 0x07 },
> +	{ TVP7002_COARSE_CLAMP_CTL, 0x00 },
> +	{ TVP7002_FINE_OFF_LSBS, 0x00 },
> +	{ TVP7002_B_COARSE_OFF, 0x10 },
> +	{ TVP7002_G_COARSE_OFF, 0x10 },
> +	{ TVP7002_R_COARSE_OFF, 0x10 },
> +	{ TVP7002_HSOUT_OUT_START, 0x0d	},
> +	{ TVP7002_MISC_CTL_4, 0x0d },
> +	/* 0x23: read only */
> +	/* 0x24: read only */
> +	/* 0x25: read only */
> +	{ TVP7002_AUTO_LVL_CTL_ENABLE, 0x80 },
> +	/* 0x27: read only */
> +	{ TVP7002_AUTO_LVL_CTL_FILTER, 0x53 },
> +	{ TVP7002_FINE_CLAMP_CTL, 0x07 },
> +	{ TVP7002_PWR_CTL, 0x00 },
> +	{ TVP7002_ADC_SETUP, 0x50 },
> +	{ TVP7002_COARSE_CLAMP_CTL, 0x00 },
> +	{ TVP7002_SOG_CLAMP, 0x80 },
> +	{ TVP7002_RGB_COARSE_CLAMP_CTL, 0x8c },
> +	{ TVP7002_SOG_COARSE_CLAMP_CTL, 0x04 },
> +	{ TVP7002_ALC_PLACEMENT, 0x5a },
> +	{ TVP7002_MVIS_STRIPPER_W, 0x03 },
> +	{ TVP7002_VSYNC_ALGN, 0x10 },
> +	{ TVP7002_SYNC_BYPASS, 0x00 },
> +	/* 0x37: read only */
> +	/* 0x38: read only */
> +	/* 0x39: read only */
> +	/* 0x3a: read only */
> +	/* 0x3b: read only */
> +	/* 0x3c: read only */
> +	{ TVP7002_L_LENGTH_TOL, 0x03 },
> +	{ TVP7002_VIDEO_BWTH_CTL, 0x00 },
> +	{ TVP7002_AVID_START_PIXEL_LSBS, 0x01 },
> +	{ TVP7002_AVID_START_PIXEL_MSBS, 0x2c },
> +	{ TVP7002_AVID_STOP_PIXEL_LSBS, 0x06 },
> +	{ TVP7002_AVID_STOP_PIXEL_MSBS, 0x2c },
> +	{ TVP7002_VBLK_F_0_START_L_OFF, 0x05 },
> +	{ TVP7002_VBLK_F_1_START_L_OFF, 0x05 },
> +	{ TVP7002_VBLK_F_0_DURATION, 0x1e },
> +	{ TVP7002_VBLK_F_1_DURATION, 0x1e },
> +	{ TVP7002_FBIT_F_0_START_L_OFF, 0x00 },
> +	{ TVP7002_FBIT_F_1_START_L_OFF, 0x00 },
> +	{ TVP7002_YUV_Y_G_COEF_LSBS, 0xe3 },
> +	{ TVP7002_YUV_Y_G_COEF_MSBS, 0x16 },
> +	{ TVP7002_YUV_Y_B_COEF_LSBS, 0x4f },
> +	{ TVP7002_YUV_Y_B_COEF_MSBS, 0x02 },
> +	{ TVP7002_YUV_Y_R_COEF_LSBS, 0xce },
> +	{ TVP7002_YUV_Y_R_COEF_MSBS, 0x06 },
> +	{ TVP7002_YUV_U_G_COEF_LSBS, 0xab },
> +	{ TVP7002_YUV_U_G_COEF_MSBS, 0xf3 },
> +	{ TVP7002_YUV_U_B_COEF_LSBS, 0x00 },
> +	{ TVP7002_YUV_U_B_COEF_MSBS, 0x10 },
> +	{ TVP7002_YUV_U_R_COEF_LSBS, 0x55 },
> +	{ TVP7002_YUV_U_R_COEF_MSBS, 0xfc },
> +	{ TVP7002_YUV_V_G_COEF_LSBS, 0x78 },
> +	{ TVP7002_YUV_V_G_COEF_MSBS, 0xf1 },
> +	{ TVP7002_YUV_V_B_COEF_LSBS, 0x88 },
> +	{ TVP7002_YUV_V_B_COEF_MSBS, 0xfe },
> +	{ TVP7002_YUV_V_R_COEF_LSBS, 0x00 },
> +	{ TVP7002_YUV_V_R_COEF_MSBS, 0x10 },
> +	{ 0x5c, 0x00 }	/* end of registers */
> +};
> +
> +/* Available resolutions */
> +static struct tvp7002_resol tvp7002_resolutions[] = {
> +	{
> +		.id = V4L2_STD_NTSC,
> +		.hres = 720,
> +		.vres = 480,
> +		.frate = 30,
> +		.lrate = 15,
> +		.prate = 14,
> +		.reg01 = 0x35,
> +		.reg02 = 0xa0,
> +		.reg03 = TVP7002_VCO_RANGE_ULOW | 0x18,
> +		.reg04 = 0x80,
> +	},
> +	{
> +		.id = V4L2_STD_PAL,
> +		.hres = 720,
> +		.vres = 576,
> +		.frate = 25,
> +		.lrate = 16,
> +		.prate = 14,
> +		.reg01 = 0x36,
> +		.reg02 = 0x00,
> +		.reg03 = TVP7002_VCO_RANGE_ULOW | 0x18,
> +		.reg04 = 0x80,
> +	},
> +	{
> +		.id = V4L2_STD_525P_60,
> +		.hres = 720,
> +		.vres = 480,
> +		.frate = 60,
> +		.lrate = 31,
> +		.prate = 27,
> +		.reg01 = 0x35,
> +		.reg02 = 0xa0,
> +		.reg03 = TVP7002_VCO_RANGE_ULOW | 0x18,
> +		.reg04 = 0x80,
> +	},
> +	{
> +		.id = V4L2_STD_625P_50,
> +		.hres = 720,
> +		.vres = 576,
> +		.frate = 50,
> +		.lrate = 31,
> +		.prate = 27,
> +		.reg01 = 0x36,
> +		.reg02 = 0x00,
> +		.reg03 = TVP7002_VCO_RANGE_ULOW | 0x18,
> +		.reg04 = 0x80,
> +	},
> +	{
> +		.id = V4L2_STD_720P_60,
> +		.hres = 1280,
> +		.vres = 720,
> +		.frate = 60,
> +		.lrate = 45,
> +		.prate = 74,
> +		.reg01 = 0x67,
> +		.reg02 = 0x20,
> +		.reg03 = TVP7002_VCO_RANGE_MED | 0x20,
> +		.reg04 = 0x80,
> +	},
> +	{
> +		.id = V4L2_STD_720P_50,
> +		.hres = 1280,
> +		.vres = 720,
> +		.frate = 50,
> +		.lrate = 38,
> +		.prate = 74,
> +		.reg01 = 0x7b,
> +		.reg02 = 0xc0,
> +		.reg03 = TVP7002_VCO_RANGE_MED | 0x18,
> +		.reg04 = 0x80,
> +	},
> +	{
> +		.id = V4L2_STD_1080I_60,
> +		.hres = 1920,
> +		.vres = 1080,
> +		.frate = 60,
> +		.lrate = 34,
> +		.prate = 74,
> +		.reg01 = 0x89,
> +		.reg02 = 0x80,
> +		.reg03 = TVP7002_VCO_RANGE_MED | 0x18,
> +		.reg04 = 0x80,
> +	},
> +	{
> +		.id = V4L2_STD_1080I_50,
> +		.hres = 1920,
> +		.vres = 1080,
> +		.frate = 50,
> +		.lrate = 28,
> +		.prate = 74,
> +		.reg01 = 0xa5,
> +		.reg02 = 0x00,
> +		.reg03 = TVP7002_VCO_RANGE_MED | 0x10,
> +		.reg04 = 0x80,
> +	},
> +	{
> +		.id = V4L2_STD_1080P_60,
> +		.hres = 1920,
> +		.vres = 1080,
> +		.frate = 60,
> +		.lrate = 68,
> +		.prate = 149,
> +		.reg01 = 0x89,
> +		.reg02 = 0x80,
> +		.reg03 = TVP7002_VCO_RANGE_HIGH | 0x20,
> +		.reg04 = 0x80,
> +	},
> +	{
> +		.id = V4L2_STD_1080P_50,
> +		.hres = 1920,
> +		.vres = 1080,
> +		.frate = 50,
> +		.lrate = 56,
> +		.prate = 149,
> +		.reg01 = 0xa5,
> +		.reg02 = 0x00,
> +		.reg03 = TVP7002_VCO_RANGE_HIGH | 0x18,
> +		.reg04 = 0x80,
> +	},
> +};
> +
> +/*
> + * tvp7002_from_std - Map video standard to register information
> + * @std: v4l2_std_id (u64) integer
> + *
> + * Returns index of std or -1 in case of error.
> + */
> +int tvp7002_from_std(v4l2_std_id std)
> +{
> +	int res;
> +
> +	switch (std) {
> +	case V4L2_STD_525P_60:
> +		res = TVP7002_STD_480P;
> +		break;
> +	case V4L2_STD_NTSC:
> +		res = TVP7002_STD_480I;
> +		break;
> +	case V4L2_STD_625P_50:
> +		res = TVP7002_STD_576P;
> +		break;
> +	case V4L2_STD_PAL:
> +		res = TVP7002_STD_576I;
> +		break;
> +	case V4L2_STD_720P_50:
> +		res = TVP7002_STD_720P_50;
> +		break;
> +	case V4L2_STD_720P_60:
> +		res = TVP7002_STD_720P_60;
> +		break;
> +	case V4L2_STD_1080I_50:
> +		res = TVP7002_STD_1080I_50;
> +		break;
> +	case V4L2_STD_1080I_60:
> +		res = TVP7002_STD_1080I_60;
> +		break;
> +	case V4L2_STD_1080P_50:
> +		res = TVP7002_STD_1080P_50;
> +		break;
> +	case V4L2_STD_1080P_60:
> +		res = TVP7002_STD_1080P_60;
> +		break;
> +	default:
> +		res = -1;
> +		break;
> +	}
> +
> +	return res;
> +}
> +
> +/*
> + * tvp7002_colorspace - Find the color space of a video format
> + * @std: v4l2_std_id (u64) integer
> + *
> + * Returns color space for a standard id
> + */
> +enum v4l2_colorspace tvp7002_colorspace(v4l2_std_id std)
> +{
> +
> +	switch (std) {
> +	case V4L2_STD_525P_60:
> +	case V4L2_STD_NTSC:
> +	case V4L2_STD_625P_50:
> +	case V4L2_STD_PAL:
> +		return V4L2_COLORSPACE_SMPTE170M;
> +	case V4L2_STD_720P_50:
> +	case V4L2_STD_720P_60:
> +	case V4L2_STD_1080I_50:
> +	case V4L2_STD_1080I_60:
> +	case V4L2_STD_1080P_50:
> +	case V4L2_STD_1080P_60:
> +		return V4L2_COLORSPACE_REC709;
> +	default:
> +		return 0;
> +	}
> +}
> +
> +#define NUM_ACTIVE_PIXELS(fmt) \
> +		tvp7002_resolutions[tvp7002_from_std((fmt))].hres
> +
> +#define NUM_ACTIVE_LINES(fmt) \
> +		tvp7002_resolutions[tvp7002_from_std((fmt))].vres
> +
> +/* Device definition */
> +struct tvp7002 {
> +	struct v4l2_subdev sd;
> +	v4l2_std_id video_mode;
> +	struct v4l2_pix_format pix;
> +	const struct tvp7002_config *pdata;
> +	int streaming;
> +};
> +
> +/* Supported controls */
> +static struct v4l2_queryctrl tvp7002_qctrl[] = {
> +	{
> +		/* This gain control uses fine grain in TVP7002 */
> +		.id = V4L2_CID_GAIN,
> +		.type = V4L2_CTRL_TYPE_INTEGER,
> +		.name = "Gain for RGB channels",
> +		.minimum = 0,
> +		.maximum = 255,
> +		.step = 1,
> +		.default_value = 0,
> +		.flags = 0,
> +	},
> +};
> +
> +/*
> + * to_tvp7002 - Obtain device handler TVP7002
> + * @sd: ptr to v4l2_subdev struct
> + *
> + * Returns device handler tvp7002.
> + */
> +static struct tvp7002 *to_tvp7002(struct v4l2_subdev *sd)
> +{
> +	return container_of(sd, struct tvp7002, sd);
> +}
> +
> +/*
> + * tvp7002_read - Read a value from a register in an TVP7002
> + * @sd: ptr to v4l2_subdev struct
> + * @reg: TVP7002 register address
> + *
> + * Returns value read if successful, or non-zero (-1) otherwise.
> + */
> +static int tvp7002_read(struct v4l2_subdev *sd, unsigned char addr)
> +{
> +	struct i2c_client *c = v4l2_get_subdevdata(sd);
> +	int retry;
> +	int error;
> +
> +	for (retry = 0; retry < I2C_RETRY_COUNT; retry++) {
> +		error = i2c_smbus_read_byte_data(c, addr);
> +
> +		if (error >= 0)
> +			return 0;
> +		msleep_interruptible(10);
> +	}
> +	v4l2_err(sd, "TVP7002 read error %d\n", error);
> +	return error;
> +}
> +
> +/*
> + * tvp7002_write() - Write a value to a register in TVP7002
> + * @sd: ptr to v4l2_subdev struct
> + * @addr: TVP7002 register address
> + * @value: value to be written to the register
> + *
> + * Write a value to a register in an TVP7002 decoder device.
> + * Returns zero if successful, or non-zero otherwise.
> + */
> +static int tvp7002_write(struct v4l2_subdev *sd, u8 addr, u8 value)
> +{
> +	struct i2c_client *c = v4l2_get_subdevdata(sd);
> +	int retry;
> +	int error;
> +
> +	for (retry = 0; retry < I2C_RETRY_COUNT; retry++) {
> +		error = i2c_smbus_write_byte_data(c, addr, value);
> +
> +		if (error >= 0)
> +			return 0;
> +		msleep_interruptible(10);
> +	}
> +	v4l2_err(sd, "TVP7002 write error %d\n", error);
> +	return error;
> +}
> +
> +/*
> + * dump_reg_range() - Dump information from TVP7002 registers
> + * @sd: ptr to v4l2_subdev struct
> + * @init: TVP7002 start address
> + * @end: TVP7002 end address
> + *
> + * Dump values at a specified register range
> + * Returns nothing.
> + */
> +static void dump_reg_range(struct v4l2_subdev *sd, u8 init, const u8 end)
> +{
> +	int i = 0;
> +	int result;
> +
> +	while (init != (u8)(end + 1)) {
> +		result = tvp7002_read(sd, init);
> +
> +		if (result < 0)
> +			v4l2_err(sd, "reg 0x%02x unreadable\n", i);
> +		else
> +			v4l2_info(sd, "@0x%02x = %02x\n", i, result);
> +
> +		init++;
> +		i++;
> +	}
> +}
> +
> +/*
> + * tvp7002_log_chk() - Check reading the value of a register
> + * @sd: ptr to v4l2_subdev struct
> + * @reg: register to read
> + * @message: register name/function
> + *
> + * Check procedure for reading a register.
> + * Returns nothing (void).
> + */
> +static inline void tvp7002_log_chk(struct v4l2_subdev *sd, u8 reg,
> +							const char *message)
> +{
> +	int res;
> +
> +	res = tvp7002_read(sd, reg);
> +
> +	if ((res) >= 0)
> +		v4l2_info(sd, "%s = 0x%02x\n", message, res);
> +}
> +
> +/*
> + * tvp7002_log_status() - Print information about register settings
> + * @sd: ptr to v4l2_subdev struct
> + *
> + * Log register values of a TVP7002 decoder device.
> + * Returns zero or -EINVAL if read operation fails.
> + */
> +static int tvp7002_log_status(struct v4l2_subdev *sd)
> +{
> +	tvp7002_log_chk(sd, TVP7002_CHIP_REV, "Chip revision number");
> +	tvp7002_log_chk(sd, TVP7002_HPLL_FDBK_DIV_LSBS,
> +						"H-PLL feedback div LSB");
> +	tvp7002_log_chk(sd, TVP7002_HPLL_FDBK_DIV_MSBS,
> +						"H-PLL feedback div MSB");
> +	tvp7002_log_chk(sd, TVP7002_HPLL_FDBK_DIV_MSBS,
> +						"VCO freq range selector");
> +	tvp7002_log_chk(sd, TVP7002_HPLL_PHASE_SEL,
> +						"ADC sampling clk phase sel");
> +	tvp7002_log_chk(sd, TVP7002_CLAMP_START, "Clamp start");
> +	tvp7002_log_chk(sd, TVP7002_CLAMP_W, "Clamp width");
> +	tvp7002_log_chk(sd, TVP7002_HSYNC_OUT_W, "HSYNC output width");
> +	tvp7002_log_chk(sd, TVP7002_B_FINE_GAIN, "Digital fine grain B ch");
> +	tvp7002_log_chk(sd, TVP7002_G_FINE_GAIN, "Digital fine grain G ch");
> +	tvp7002_log_chk(sd, TVP7002_R_FINE_GAIN, "Digital fine grain R ch");
> +	tvp7002_log_chk(sd, TVP7002_B_FINE_OFF_MSBS,
> +						"Digital fine grain off B ch");
> +	tvp7002_log_chk(sd, TVP7002_G_FINE_OFF_MSBS,
> +						"Digital fine grain off G ch");
> +	tvp7002_log_chk(sd, TVP7002_R_FINE_OFF_MSBS,
> +						"Digital fine grain off R ch");
> +	tvp7002_log_chk(sd, TVP7002_FINE_OFF_LSBS, "Dig fine grain off LSBs");
> +	tvp7002_log_chk(sd, TVP7002_SYNC_CTL_1, "Sync control 1");
> +	tvp7002_log_chk(sd, TVP7002_HPLL_AND_CLAMP_CTL,
> +						"H-PLL and clamp control");
> +	tvp7002_log_chk(sd, TVP7002_SYNC_ON_G_THRS, "Sync-On-Green threshold");
> +	tvp7002_log_chk(sd, TVP7002_SYNC_SEPARATOR_THRS,
> +						"Sync separator thrshold");
> +	tvp7002_log_chk(sd, TVP7002_HPLL_PRE_COAST, "H-PLL pre-coast");
> +	tvp7002_log_chk(sd, TVP7002_HPLL_POST_COAST, "H-PLL post-coast");
> +	tvp7002_log_chk(sd, TVP7002_SYNC_DETECT_STAT, "Sync detect status");
> +	tvp7002_log_chk(sd, TVP7002_OUT_FORMATTER, "Output formatter");
> +	tvp7002_log_chk(sd, TVP7002_MISC_CTL_1, "Miscelaneous control 1");
> +	tvp7002_log_chk(sd, TVP7002_MISC_CTL_2, "Miscelaneous control 2");
> +	tvp7002_log_chk(sd, TVP7002_MISC_CTL_3, "Miscelaneous control 3");
> +	tvp7002_log_chk(sd, TVP7002_IN_MUX_SEL_1, "Input Mux Selector 1");
> +	tvp7002_log_chk(sd, TVP7002_IN_MUX_SEL_2, "Input Mux Selector 2");
> +	tvp7002_log_chk(sd, TVP7002_B_AND_G_COARSE_GAIN, "B, G coarse gain");
> +	tvp7002_log_chk(sd, TVP7002_R_COARSE_GAIN, "R coarse gain");
> +	tvp7002_log_chk(sd, TVP7002_B_COARSE_OFF, "Coarse offset for B ch");
> +	tvp7002_log_chk(sd, TVP7002_G_COARSE_OFF, "Coarse offset for G ch");
> +	tvp7002_log_chk(sd, TVP7002_R_COARSE_OFF, "Coarse offset for R ch");
> +	tvp7002_log_chk(sd, TVP7002_HSOUT_OUT_START,
> +						"HSYNC lead edge out start");
> +	tvp7002_log_chk(sd, TVP7002_MISC_CTL_4, "Miscelaneous control 4");
> +	tvp7002_log_chk(sd, TVP7002_B_DGTL_ALC_OUT_LSBS,
> +						"Flt ALC out B ch LSBs");
> +	tvp7002_log_chk(sd, TVP7002_G_DGTL_ALC_OUT_LSBS,
> +						"Flt ALC out G ch LSBs");
> +	tvp7002_log_chk(sd, TVP7002_R_DGTL_ALC_OUT_LSBS,
> +						"Flt ALC out R ch LSBs");
> +	tvp7002_log_chk(sd, TVP7002_AUTO_LVL_CTL_ENABLE,
> +						"Auto level ctrl enable");
> +	tvp7002_log_chk(sd, TVP7002_DGTL_ALC_OUT_MSBS,
> +						"Filt ALC out RGB chs MSB");
> +	tvp7002_log_chk(sd, TVP7002_AUTO_LVL_CTL_FILTER,
> +						"Auto level ctrl filter");
> +	tvp7002_log_chk(sd, TVP7002_FINE_CLAMP_CTL, "Fine clamp control");
> +	tvp7002_log_chk(sd, TVP7002_PWR_CTL, "Power control");
> +	tvp7002_log_chk(sd, TVP7002_ADC_SETUP, "ADC setup");
> +	tvp7002_log_chk(sd, TVP7002_COARSE_CLAMP_CTL, "Coarse clamp ctrl");
> +	tvp7002_log_chk(sd, TVP7002_SOG_CLAMP, "Sync-On-Green clamp");
> +	tvp7002_log_chk(sd, TVP7002_RGB_COARSE_CLAMP_CTL,
> +						"RGB coarse clamp ctrl");
> +	tvp7002_log_chk(sd, TVP7002_SOG_COARSE_CLAMP_CTL,
> +						"SOG coarse clamp ctrl");
> +	tvp7002_log_chk(sd, TVP7002_ALC_PLACEMENT, "ALC placement");
> +	tvp7002_log_chk(sd, TVP7002_MVIS_STRIPPER_W,
> +						"Macrovision stripper width");
> +	tvp7002_log_chk(sd, TVP7002_SYNC_BYPASS, "Sync bypass");
> +	tvp7002_log_chk(sd, TVP7002_L_FRAME_STAT_LSBS,
> +						"Lines p Frame status LSBs");
> +	tvp7002_log_chk(sd, TVP7002_L_FRAME_STAT_MSBS,
> +						"Lines p Frame status MSBs");
> +	tvp7002_log_chk(sd, TVP7002_CLK_L_STAT_LSBS, "Clks p line stat LSBs");
> +	tvp7002_log_chk(sd, TVP7002_CLK_L_STAT_MSBS, "Clks p line stat MSBs");
> +	tvp7002_log_chk(sd, TVP7002_HSYNC_W, "HSYNC width");
> +	tvp7002_log_chk(sd, TVP7002_VSYNC_W, "VSYNC width");
> +	tvp7002_log_chk(sd, TVP7002_L_LENGTH_TOL, "Line length tolerance");
> +	tvp7002_log_chk(sd, TVP7002_VIDEO_BWTH_CTL, "Video bandwth control");
> +	tvp7002_log_chk(sd, TVP7002_AVID_START_PIXEL_LSBS,
> +						"AVID start pixel LSBs");
> +	tvp7002_log_chk(sd, TVP7002_AVID_START_PIXEL_MSBS,
> +						"AVID start pixel MSBs");
> +	tvp7002_log_chk(sd, TVP7002_AVID_STOP_PIXEL_LSBS,
> +						"AVID stop pixel LSBs");
> +	tvp7002_log_chk(sd, TVP7002_AVID_STOP_PIXEL_MSBS,
> +						"AVID stop pixel MSBs");
> +	tvp7002_log_chk(sd, TVP7002_VBLK_F_0_START_L_OFF,
> +						"VBLK start line off 0");
> +	tvp7002_log_chk(sd, TVP7002_VBLK_F_1_START_L_OFF,
> +						"VBLK start line off 1");
> +	tvp7002_log_chk(sd, TVP7002_VBLK_F_0_DURATION, "VBLK duration 0");
> +	tvp7002_log_chk(sd, TVP7002_VBLK_F_1_DURATION, "VBLK duration 1");
> +	tvp7002_log_chk(sd, TVP7002_FBIT_F_0_START_L_OFF,
> +						"FBIT start line off 0");
> +	tvp7002_log_chk(sd, TVP7002_FBIT_F_1_START_L_OFF,
> +						"FBIT start line off 1");
> +	tvp7002_log_chk(sd, TVP7002_YUV_Y_G_COEF_LSBS, "YUV Y G LSBs");
> +	tvp7002_log_chk(sd, TVP7002_YUV_Y_G_COEF_MSBS, "YUV Y G MSBs");
> +	tvp7002_log_chk(sd, TVP7002_YUV_Y_B_COEF_LSBS, "YUV Y B LSBs");
> +	tvp7002_log_chk(sd, TVP7002_YUV_Y_B_COEF_MSBS, "YUV Y B MSBs");
> +	tvp7002_log_chk(sd, TVP7002_YUV_Y_R_COEF_LSBS, "YUV Y R LSBs");
> +	tvp7002_log_chk(sd, TVP7002_YUV_Y_R_COEF_MSBS, "YUV Y R MSBs");
> +	tvp7002_log_chk(sd, TVP7002_YUV_U_G_COEF_LSBS, "YUV U G LSBs");
> +	tvp7002_log_chk(sd, TVP7002_YUV_U_G_COEF_MSBS, "YUV U G MSBs");
> +	tvp7002_log_chk(sd, TVP7002_YUV_U_B_COEF_LSBS, "YUV U B LSBs");
> +	tvp7002_log_chk(sd, TVP7002_YUV_U_B_COEF_MSBS, "YUV U B MSBs");
> +	tvp7002_log_chk(sd, TVP7002_YUV_U_R_COEF_LSBS, "YUV U R LSBs");
> +	tvp7002_log_chk(sd, TVP7002_YUV_U_R_COEF_MSBS, "YUV U R MSBs");
> +	tvp7002_log_chk(sd, TVP7002_YUV_V_G_COEF_LSBS, "YUV V G LSBs");
> +	tvp7002_log_chk(sd, TVP7002_YUV_V_G_COEF_MSBS, "YUV V G MSBs");
> +	tvp7002_log_chk(sd, TVP7002_YUV_V_B_COEF_LSBS, "YUV V B LSBs");
> +	tvp7002_log_chk(sd, TVP7002_YUV_V_B_COEF_MSBS, "YUV V B MSBs");
> +	tvp7002_log_chk(sd, TVP7002_YUV_V_R_COEF_LSBS, "YUV V R LSBs");
> +	tvp7002_log_chk(sd, TVP7002_YUV_V_R_COEF_MSBS, "YUV V R MSBs");
> +
> +	return 0;
> +}
> +
> +/*
> + * tvp7002_g_chip_ident() - Get chip identification number
> + * @sd: ptr to v4l2_subdev struct
> + * @chip: ptr to v4l2_dbg_chip_ident struct
> + *
> + * Obtains the chip's identification number.
> + * Returns zero or -EINVAL if read operation fails.
> + */
> +static int tvp7002_g_chip_ident(struct v4l2_subdev *sd,
> +					struct v4l2_dbg_chip_ident *chip)
> +{
> +	int rev;
> +	struct i2c_client *client = v4l2_get_subdevdata(sd);
> +
> +	rev = tvp7002_read(sd, TVP7002_CHIP_REV);
> +
> +	if (rev < 0)
> +		return -EINVAL;
> +
> +	return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_TVP7002,
> +									rev);
> +}
> +
> +/*
> + * tvp7002_write_inittab() - Write initialization values
> + * @sd: ptr to v4l2_subdev struct
> + * @regs: ptr to i2c_reg_value struct
> + *
> + * Write initialization values.
> + * Returns zero or -EINVAL if read operation fails.
> + */
> +static int tvp7002_write_inittab(struct v4l2_subdev *sd,
> +					const struct i2c_reg_value *regs)
> +{
> +	int i;
> +	int error;
> +
> +	/* Initialize the first (defined) registers */
> +	while (regs->reg != 0x5c) {
> +		error = tvp7002_write(sd, regs->reg, regs->value);
> +		if (error < 0)
> +			return -EINVAL;
> +		regs++;
> +	}
> +	/* Initialize the last unnamed registers */
> +	for (i = 0x5c; i <= 0xff; i++) {
> +		error = tvp7002_write(sd, i, 0x00);
> +		if (error < 0)
> +			return -EINVAL;
> +		regs++;
> +	}
> +
> +	return 0;
> +}
> +
> +/*
> + * tvp7002_write_err() - Write a register value with error code
> + * @sd: pointer to standard V4L2 sub-device structure
> + * @reg: destination register
> + * @val: value to be written
> + * @error: pointer to error value
> + *
> + * Write a value in a register and save error value in
> + */
> +static inline void tvp7002_write_err(struct v4l2_subdev *sd, u8 reg,
> +							u8 val, int *err)
> +{
> +	if (!*err)
> +		*err = tvp7002_write(sd, reg, val);
> +}
> +
> +/*
> + * tvp7002_set_video_mode() - Set video mode
> + * @sd: pointer to standard V4L2 sub-device structure
> + * @sdf: index to structure describing format
> + *
> + * Set video standard according to index
> + * Returns 0 if operation is successful or -EINVAL otherwise
> + */
> +static int tvp7002_set_video_mode(struct v4l2_subdev *sd, int sdf)
> +{
> +	int error = 0;
> +
> +	if (sdf < TVP7002_STD_480I || sdf > TVP7002_STD_1080P_50) {
> +		v4l2_err(sd, "sf out of range\n");
> +		return -ERANGE;
> +	}
> +
> +	/* Print specific information about current format */
> +	v4l2_info(sd, "Setting standard display format...\n");
> +	v4l2_info(sd, "hres = %d vres=%d fr=%d lr=%d prate=%d\n",
> +			tvp7002_resolutions[sdf].hres,
> +			tvp7002_resolutions[sdf].vres,
> +			tvp7002_resolutions[sdf].frate,
> +			tvp7002_resolutions[sdf].lrate,
> +			tvp7002_resolutions[sdf].prate);
> +
> +	/* Set registers accordingly */
> +	tvp7002_write_err(sd, TVP7002_HPLL_FDBK_DIV_MSBS,
> +				tvp7002_resolutions[sdf].reg01, &error);
> +	tvp7002_write_err(sd, TVP7002_HPLL_FDBK_DIV_LSBS,
> +				tvp7002_resolutions[sdf].reg02, &error);
> +	tvp7002_write_err(sd, TVP7002_HPLL_CRTL,
> +				tvp7002_resolutions[sdf].reg03, &error);
> +	tvp7002_write_err(sd, TVP7002_HPLL_PHASE_SEL,
> +				tvp7002_resolutions[sdf].reg04, &error);
> +
> +	/* Set SD/HD mode registers */
> +	if (sdf < TVP7002_STD_720P_60) {
> +		tvp7002_write_err(sd, TVP7002_CLAMP_START, 0x06, &error);
> +		tvp7002_write_err(sd, TVP7002_CLAMP_W, 0x10, &error);
> +		tvp7002_write_err(sd, TVP7002_HPLL_PRE_COAST, 0x03, &error);
> +		tvp7002_write_err(sd, TVP7002_HPLL_POST_COAST, 0x03, &error);
> +		tvp7002_write_err(sd, TVP7002_IN_MUX_SEL_2, 0x17, &error);
> +
> +		if (sdf < TVP7002_STD_480P) {
> +			tvp7002_write_err(sd, TVP7002_HSOUT_OUT_START, 0x0c,
> +								&error);
> +			tvp7002_write_err(sd, TVP7002_MVIS_STRIPPER_W, 0x24,
> +								&error);
> +		} else {
> +			tvp7002_write_err(sd, TVP7002_HSOUT_OUT_START, 0x0a,
> +								&error);
> +			tvp7002_write_err(sd, TVP7002_MVIS_STRIPPER_W, 0x12,
> +								&error);
> +		}
> +		tvp7002_write_err(sd, TVP7002_MISC_CTL_4, 0x08, &error);
> +		tvp7002_write_err(sd, TVP7002_ALC_PLACEMENT, 0x18, &error);
> +	} else {
> +		tvp7002_write_err(sd, TVP7002_CLAMP_START, 0x32, &error);
> +		tvp7002_write_err(sd, TVP7002_CLAMP_W, 0x20, &error);
> +		tvp7002_write_err(sd, TVP7002_HPLL_PRE_COAST, 0x01, &error);
> +		tvp7002_write_err(sd, TVP7002_HPLL_POST_COAST, 0x00, &error);
> +		tvp7002_write_err(sd, TVP7002_IN_MUX_SEL_2, 0xc7, &error);
> +
> +		if (sdf < TVP7002_STD_1080I_60)
> +			tvp7002_write_err(sd, TVP7002_HSOUT_OUT_START,
> +								0x35, &error);
> +		else
> +			tvp7002_write_err(sd, TVP7002_HSOUT_OUT_START,
> +								0x39, &error);
> +
> +		tvp7002_write_err(sd, TVP7002_MISC_CTL_4, 0x00, &error);
> +		tvp7002_write_err(sd, TVP7002_ALC_PLACEMENT, 0x5a, &error);
> +
> +		if (sdf < TVP7002_STD_1080P_60)
> +			tvp7002_write_err(sd, TVP7002_MVIS_STRIPPER_W,
> +								0x07, &error);
> +		else
> +			tvp7002_write_err(sd, TVP7002_MVIS_STRIPPER_W,
> +								0x03, &error);
> +	}
> +	if (sdf < TVP7002_STD_1080P_60) {
> +		tvp7002_write_err(sd, TVP7002_ADC_SETUP, 0x50, &error);
> +		tvp7002_write_err(sd, TVP7002_VIDEO_BWTH_CTL, 0x0f, &error);
> +	} else {
> +		tvp7002_write_err(sd, TVP7002_ADC_SETUP, 0x80, &error);
> +		tvp7002_write_err(sd, TVP7002_VIDEO_BWTH_CTL, 0x00, &error);
> +	}
> +	/* Set up registers that hold the same value regardless of the
> +	 * SD mode
> +	 */
> +	tvp7002_write_err(sd, TVP7002_SYNC_ON_G_THRS, 0x5d, &error);
> +	tvp7002_write_err(sd, TVP7002_SYNC_SEPARATOR_THRS, 0x40, &error);
> +	tvp7002_write_err(sd, TVP7002_MISC_CTL_2, 0x00, &error);
> +	tvp7002_write_err(sd, TVP7002_MISC_CTL_3, 0x01, &error);
> +	tvp7002_write_err(sd, TVP7002_IN_MUX_SEL_1, 0x00, &error);
> +	tvp7002_write_err(sd, TVP7002_VSYNC_ALGN, 0x00, &error);
> +	tvp7002_write_err(sd, TVP7002_L_LENGTH_TOL, 0x06, &error);
> +	tvp7002_write_err(sd, TVP7002_SYNC_SEPARATOR_THRS, 0x40, &error);
> +	tvp7002_write_err(sd, TVP7002_MISC_CTL_2, 0x00, &error);
> +	tvp7002_write_err(sd, TVP7002_MISC_CTL_3, 0x01, &error);
> +	tvp7002_write_err(sd, TVP7002_IN_MUX_SEL_1, 0x00, &error);
> +	tvp7002_write_err(sd, TVP7002_VSYNC_ALGN, 0x00, &error);
> +	tvp7002_write_err(sd, TVP7002_L_LENGTH_TOL, 0x06, &error);
> +	return error;
> +}
> +
> +/*
> + * tvp7002_get_video_mode() - V4L2 decoder interface handler for querystd
> + * @sd: pointer to standard V4L2 sub-device structure
> + *
> + * Returns the current standard detected by TVP7002. If no active input is
> + * detected, returns -1
> + */
> +static v4l2_std_id tvp7002_get_video_mode(struct v4l2_subdev *sd)
> +{
> +	int reg01, reg02, reg03;
> +
> +	reg01 = tvp7002_read(sd, TVP7002_HPLL_FDBK_DIV_MSBS);
> +	reg02 = tvp7002_read(sd, TVP7002_HPLL_FDBK_DIV_LSBS);
> +	reg03 = tvp7002_read(sd, TVP7002_HPLL_CRTL);
> +
> +	if (reg01 < 0 || reg02 < 0 || reg03 < 0)
> +		return V4L2_STD_UNKNOWN;
> +
> +	switch (reg01) {
> +	case 0x35:
> +		if (reg02 == 0xa0)
> +			return V4L2_STD_NTSC;
> +		else
> +			return V4L2_STD_PAL;
> +	case 0x36:
> +		if (reg02 == 0xa0)
> +			return V4L2_STD_525P_60;
> +		else
> +			return V4L2_STD_625P_50;
> +	case 0x67:
> +		return V4L2_STD_720P_60;
> +	case 0x7b:
> +		return V4L2_STD_720P_50;
> +	case 0x89:
> +		if (reg03 == 0x98)
> +			return V4L2_STD_1080I_60;
> +		else
> +			return V4L2_STD_1080P_60;
> +		break;
> +	case 0xa5:
> +		if (reg03 == 0x90)
> +			return V4L2_STD_1080I_50;
> +		else
> +			return V4L2_STD_1080P_50;
> +	default:
> +		return V4L2_STD_UNKNOWN;
> +	}
> +}
> +
> +/*
> + * tvp7002_querystd() - V4L2 decoder interface handler for querystd
> + * @sd: pointer to standard V4L2 sub-device structure
> + * @std_id: standard V4L2 std_id ioctl enum
> + *
> + * Returns the current standard detected by TVP7002. If no active input is
> + * detected, returns -EINVAL
> + */
> +static int tvp7002_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
> +{
> +	v4l2_std_id current_std;
> +	u8 sync_lock_status;
> +	int res;
> +
> +	if (std_id == NULL)
> +		return -EINVAL;
> +
> +	/* get the current standard */
> +	res = tvp7002_get_video_mode(sd);
> +	if (res == V4L2_STD_UNKNOWN)
> +		return -EINVAL;
> +	current_std = res;
> +
> +	/* check whether signal is locked */
> +	sync_lock_status = tvp7002_read(sd, TVP7002_SYNC_DETECT_STAT);
> +
> +	if (0x02 != (sync_lock_status & 0xff))
> +		return -EINVAL;	/* No input detected */
> +
> +	*std_id = current_std;
> +
> +	v4l2_dbg(1, debug, sd, "Current STD: %d %d @ %d Hz\n",
> +		tvp7002_resolutions[tvp7002_from_std(current_std)].hres,
> +		tvp7002_resolutions[tvp7002_from_std(current_std)].vres,
> +		tvp7002_resolutions[tvp7002_from_std(current_std)].frate);
> +	return 0;
> +}
> +
> +/*
> + * tvp7002_try_fmt_cap() - V4L2 decoder interface handler for try_fmt
> + * @sd: pointer to standard V4L2 sub-device structure
> + * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
> + *
> + * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
> + * ioctl is used to negotiate the image capture size and pixel format
> + * without actually making it take effect.
> + */
> +static int tvp7002_try_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
> +{
> +	struct v4l2_pix_format *pix;
> +	v4l2_std_id current_std;
> +	int res;
> +
> +	if (f == NULL)
> +		return -EINVAL;
> +
> +	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> +		/* only capture is supported */
> +		return -EINVAL;
> +
> +	pix = &f->fmt.pix;
> +
> +	/* Calculate height and width based on current standard */
> +	res = tvp7002_get_video_mode(sd);
> +	if (res < 0)
> +		return -EINVAL;
> +	current_std = res;
> +
> +	pix->width = NUM_ACTIVE_PIXELS(current_std);
> +	pix->height = NUM_ACTIVE_LINES(current_std);
> +
> +	pix->pixelformat = V4L2_PIX_FMT_UYVY;
> +
> +	pix->field = V4L2_FIELD_INTERLACED;
> +	pix->bytesperline = pix->width * 2;
> +	pix->sizeimage = pix->bytesperline * pix->height;
> +	pix->colorspace = V4L2_COLORSPACE_REC709;
> +	pix->priv = 0;
> +
> +	v4l2_dbg(1, debug, sd, "Try FMT: pixelformat - %s, bytesperline - %d"
> +			"Width - %d, Height - %d",
> +			"8-bit UYVY 4:2:2 Format", pix->bytesperline,
> +			pix->width, pix->height);
> +	return 0;
> +}
> +
> +/**
> + * tvp7002_s_fmt_cap() - V4L2 decoder interface handler for s_fmt
> + * @sd: pointer to standard V4L2 sub-device structure
> + * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
> + *
> + * If the requested format is supported, configures the HW to use that
> + * format, returns error code if format not supported or HW can't be
> + * correctly configured.
> + */
> +static int tvp7002_s_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
> +{
> +	struct tvp7002 *decoder = to_tvp7002(sd);
> +	struct v4l2_pix_format *pix;
> +	int rval;
> +
> +	if (f == NULL)
> +		return -EINVAL;
> +
> +	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> +		/* only capture is supported */
> +		return -EINVAL;
> +
> +	pix = &f->fmt.pix;
> +	rval = tvp7002_try_fmt_cap(sd, f);
> +	if (rval)
> +		return rval;
> +
> +	decoder->pix = *pix;
> +
> +	return rval;
> +}
> +
> +/*
> + * tvp7002_g_fmt() - V4L2 decoder interface handler for tvp7002_g_fmt
> + * @sd: pointer to standard V4L2 sub-device structure
> + * @f: pointer to standard V4L2 v4l2_format structure
> + *
> + * Returns the decoder's current pixel format in the v4l2_format
> + * parameter.
> + */
> +static int tvp7002_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
> +{
> +	struct tvp7002 *decoder = to_tvp7002(sd);
> +
> +	if (f == NULL)
> +		return -EINVAL;
> +
> +	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
> +		/* only capture is supported */
> +		return -EINVAL;
> +
> +	f->fmt.pix = decoder->pix;
> +
> +	v4l2_dbg(1, debug, sd, "Current FMT: bytesperline - %d"
> +			"Width - %d, Height - %d",
> +			decoder->pix.bytesperline,
> +			decoder->pix.width, decoder->pix.height);
> +	return 0;
> +}
> +
> +/*
> + * tvp7002_s_ctrl() - Set a control
> + * @sd: ptr to v4l2_subdev struct
> + * @ctrl: ptr to v4l2_control struct
> + *
> + * Set a control for a TVP7002 decoder device.
> + * Returns zero when successful or -EINVAL if register access fails.
> + */
> +static int tvp7002_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
> +{
> +	struct tvp7002 *decoder = to_tvp7002(sd);
> +	int vmd = 0;
> +
> +	decoder->video_mode = std;
> +	vmd = tvp7002_from_std(std);
> +
> +	v4l2_dbg(1, debug, sd, "Set video std mode to %llx.\n", std);
> +
> +	return tvp7002_set_video_mode(sd, vmd);
> +}
> +
> +/*
> + * tvp7002_g_ctrl() - Get a control
> + * @sd: ptr to v4l2_subdev struct
> + * @ctrl: ptr to v4l2_control struct
> + *
> + * Get a control for a TVP7002 decoder device.
> + * Returns zero when successful or -EINVAL if register access fails.
> + */
> +static int tvp7002_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
> +{
> +	int rval, gval, bval;
> +	int res;
> +
> +	v4l2_info(sd, "g_ctrl called\n");
> +
> +	switch (ctrl->id) {
> +	case V4L2_CID_GAIN:
> +		rval = tvp7002_read(sd, TVP7002_R_FINE_GAIN);
> +		gval = tvp7002_read(sd, TVP7002_G_FINE_GAIN);
> +		bval = tvp7002_read(sd, TVP7002_B_FINE_GAIN);
> +
> +		if (rval < 0 || gval < 0 || bval < 0) {
> +			res = -1;
> +		} else if (rval != gval || rval != bval) {
> +			res = -1;
> +		} else {
> +			ctrl->value = rval & 0x0F;
> +			res = ctrl->value;
> +		}
> +		break;
> +	default:
> +		res = -1;
> +		break;
> +	}
> +
> +	return res < 0 ? res : 0;
> +}
> +
> +/*
> + * tvp7002_s_ctrl() - Set a control
> + * @sd: ptr to v4l2_subdev struct
> + * @ctrl: ptr to v4l2_control struct
> + *
> + * Set a control in TVP7002 decoder device.
> + * Returns zero when successful or -EINVAL if register access fails.
> + */
> +static int tvp7002_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
> +{
> +	int rval, gval, bval;
> +	int error;
> +	u8 i, n;
> +	n = ARRAY_SIZE(tvp7002_qctrl);
> +
> +	for (i = 0; i < n; i++)
> +		if (ctrl->id == tvp7002_qctrl[i].id)
> +			break;
> +
> +	if (i == n)
> +		return -EINVAL;
> +
> +	if (ctrl->value < tvp7002_qctrl[i].minimum ||
> +			ctrl->value > tvp7002_qctrl[i].maximum)
> +		return -ERANGE;
> +
> +	switch (ctrl->id) {
> +	case V4L2_CID_GAIN:
> +		rval = tvp7002_write(sd, TVP7002_R_FINE_GAIN,
> +							ctrl->value & 0xff);
> +		gval = tvp7002_write(sd, TVP7002_G_FINE_GAIN,
> +							ctrl->value & 0xff);
> +		bval = tvp7002_write(sd, TVP7002_B_FINE_GAIN,
> +							ctrl->value & 0xff);
> +		if (rval < 0  || gval < 0 || bval < 0)
> +			error = -1;
> +		else
> +			error = rval;
> +		break;
> +	default:
> +		error = -1;
> +		break;
> +	}
> +
> +	return error < 0 ? -EINVAL : 0;
> +}
> +
> +/*
> + * tvp7002_g_register() - Get the value of a register
> + * @sd: ptr to v4l2_subdev struct
> + * @vreg: ptr to v4l2_dbg_register struct
> + *
> + * Get the value of a TVP7002 decoder device register.
> + * Returns zero when successful, -EINVAL if register read fails or
> + * access to I2C client fails, -EPERM if the call is not allowed
> + * by diabled CAP_SYS_ADMIN.
> + */
> +#ifdef CONFIG_VIDEO_ADV_DEBUG
> +static int tvp7002_g_register(struct v4l2_subdev *sd,
> +						struct v4l2_dbg_register *reg)
> +{
> +	struct i2c_client *client = v4l2_get_subdevdata(sd);
> +
> +	if (!v4l2_chip_match_i2c_client(client, &reg->match))
> +		return -EINVAL;
> +	if (!capable(CAP_SYS_ADMIN))
> +		return -EPERM;
> +
> +	return reg->val < 0 ? -EINVAL : 0;
> +}
> +
> +/*
> + * tvp7002_s_register() - set a control
> + * @sd: ptr to v4l2_subdev struct
> + * @ctrl: ptr to v4l2_control struct
> + *
> + * Get the value of a TVP7002 decoder device register.
> + * Returns zero when successful or -EINVAL if register read fails.
> + */
> +static int tvp7002_s_register(struct v4l2_subdev *sd,
> +						struct v4l2_dbg_register *reg)
> +{
> +	int wres;
> +	struct i2c_client *client = v4l2_get_subdevdata(sd);
> +
> +	if (!v4l2_chip_match_i2c_client(client, &reg->match))
> +		return -EINVAL;
> +	if (!capable(CAP_SYS_ADMIN))
> +		return -EPERM;
> +
> +	wres = tvp7002_write(sd, reg->reg & 0xff, reg->val & 0xff);
> +
> +	return wres < 0 ? -EINVAL : 0;
> +}
> +#endif
> +
> +/*
> + * tvp7002_queryctrl() - Query a control
> + * @sd: ptr to v4l2_subdev struct
> + * @ctrl: ptr to v4l2_queryctrl struct
> + *
> + * Query a control of a TVP7002 decoder device.
> + * Returns zero when successful or -EINVAL if register read fails.
> + */
> +static int tvp7002_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
> +{
> +	int i, error;
> +	error = -EINVAL;
> +
> +	v4l2_info(sd, "queryctrl called\n");
> +
> +	for (i = 0; i < ARRAY_SIZE(tvp7002_qctrl); i++)
> +		if (qc->id && qc->id == tvp7002_qctrl[i].id) {
> +			v4l2_ctrl_query_fill(qc, tvp7002_qctrl[i].minimum,
> +					tvp7002_qctrl[i].maximum,
> +					tvp7002_qctrl[i].step,
> +					tvp7002_qctrl[i].default_value);
> +			error = 0;
> +			break;
> +		}
> +
> +	return error;
> +}
> +
> +/*
> + * tvp7002_s_stream() - V4L2 decoder i/f handler for s_stream
> + * @sd: pointer to standard V4L2 sub-device structure
> + * @enable: streaming enable or disable
> + *
> + * Sets streaming to enable or disable, if possible.
> + */
> +static int tvp7002_s_stream(struct v4l2_subdev *sd, int enable)
> +{
> +	int err = 0;
> +	struct tvp7002 *decoder = to_tvp7002(sd);
> +
> +	if (decoder->streaming == enable)
> +		return 0;
> +
> +	if (enable) {
> +		/* Power Up Sequence */
> +		err = tvp7002_write(sd, TVP7002_PWR_CTL, 0x00);
> +		if (err) {
> +			v4l2_err(sd, "Unable to turn on decoder\n");
> +			err = -EINVAL;
> +		}
> +		err = tvp7002_write_inittab(sd, tvp7002_init_default);
> +		if (err < 0) {
> +			v4l2_err(sd, "Unable to initialize\n");
> +			err = -EINVAL;
> +		}
> +		/* Detect if not already detected */
> +		err = tvp7002_read(sd, TVP7002_CHIP_REV);
> +		if (err < 0) {
> +			v4l2_err(sd, "Unable to detect decoder\n");
> +			err = -EINVAL;
> +		}
> +		decoder->streaming = enable;
> +	} else {
> +		/* Power Down Sequence */
> +		err = tvp7002_write(sd, TVP7002_PWR_CTL, 0x40);
> +		if (err) {
> +			v4l2_err(sd, "Unable to turn off decoder\n");
> +			return err;
> +		}
> +		decoder->streaming = enable;
> +	}
> +
> +	return err;
> +}
> +
> +/* Specific video subsystem operation handlers */
> +static const struct v4l2_subdev_video_ops tvp7002_video_ops = {
> +	.querystd = tvp7002_querystd,
> +	.s_stream = tvp7002_s_stream,
> +	.g_fmt = tvp7002_g_fmt,
> +};
> +
> +/* V4L2 Operations handlers */
> +static const struct v4l2_subdev_core_ops tvp7002_core_ops = {
> +	.g_chip_ident = tvp7002_g_chip_ident,
> +	.log_status = tvp7002_log_status,
> +	.g_ctrl = tvp7002_g_ctrl,
> +	.s_ctrl = tvp7002_s_ctrl,
> +	.queryctrl = tvp7002_queryctrl,
> +	.s_std = tvp7002_s_std,
> +#ifdef CONFIG_VIDEO_ADV_DEBUG
> +	.g_register = tvp7002_g_register,
> +	.s_register = tvp7002_s_register,
> +#endif
> +};
> +
> +static const struct v4l2_subdev_ops tvp7002_ops = {
> +	.core = &tvp7002_core_ops,
> +	.video = &tvp7002_video_ops,
> +};
> +
> +/* Default driver settings */
> +static struct tvp7002 tvp7002_dev = {
> +	.streaming = 0,
> +
> +	.pix = {
> +		.width = 720,
> +		.height = 480,
> +		.pixelformat = V4L2_PIX_FMT_UYVY,
> +		.field = V4L2_FIELD_INTERLACED,
> +		.bytesperline = 720 * 2,
> +		.sizeimage = 720 * 2 * 480,
> +		.colorspace = V4L2_COLORSPACE_SMPTE170M,
> +		},
> +
> +	.video_mode = V4L2_STD_NTSC,
> +};
> +
> +/*
> + * tvp7002_reset - Reset a TVP7002 device
> + * @sd: ptr to v4l2_subdev struct
> + * @val: unsigned integer (not used)
> + *
> + * Reset the TVP7002 device
> + * Returns zero when successful or -EINVAL if register read fails.
> + */
> +static int tvp7002_reset(struct v4l2_subdev *sd, u32 val)
> +{
> +	struct tvp7002 *core = to_tvp7002(sd);
> +	int polarity;
> +	int error;
> +
> +	error = tvp7002_read(sd, TVP7002_CHIP_REV);
> +	if (error < 0) {
> +		error = -EINVAL;
> +		goto found_error;
> +	}
> +
> +	if (error == 0x02) {
> +		v4l2_info(sd, "rev. %02x detected.\n", error);
> +	} else {
> +		v4l2_info(sd, "unknown revision detected.\n");
> +		v4l2_info(sd, "revision number is %02x\n", error);
> +	}
> +
> +	/* Initializes TVP7002 to its default values */
> +	error = tvp7002_write_inittab(sd, tvp7002_init_default);
> +	if (error < 0) {
> +		error = -EINVAL;
> +		goto found_error;
> +	}
> +
> +	/* Set polarity information after registers have been set */
> +	polarity = 0x5b | core->pdata->hs_polarity << 5
> +			| core->pdata->vs_polarity << 2;
> +	error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity);
> +	if (error < 0) {
> +		error = -EINVAL;
> +		goto found_error;
> +	}
> +
> +	polarity = 0x0  | core->pdata->fid_polarity << 2
> +			| core->pdata->sog_polarity << 1
> +			| core->pdata->clk_polarity;
> +	error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity);
> +	if (error < 0) {
> +		error = -EINVAL;
> +		goto found_error;
> +	}
> +
> +found_error:
> +	return error;
> +};
> +
> +/*
> + * tvp7002_probe - Reset a TVP7002 device
> + * @sd: ptr to v4l2_subdev struct
> + * @ctrl: ptr to i2c_device_id struct
> + *
> + * Reset the TVP7002 device
> + * Returns zero when successful or -EINVAL if register read fails.
> + */
> +static int tvp7002_probe(struct i2c_client *c, const struct i2c_device_id *id)
> +{
> +	struct v4l2_subdev *sd;
> +	struct tvp7002 *core;
> +	int polarity;
> +	int error;
> +
> +	/* Check if the adapter supports the needed features */
> +	if (!i2c_check_functionality(c->adapter,
> +		I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
> +		return -EIO;
> +
> +	if (!c->dev.platform_data) {
> +		v4l2_err(c, "No platform data!!\n");
> +		return -ENODEV;
> +	}
> +
> +	core = kzalloc(sizeof(struct tvp7002), GFP_KERNEL);
> +
> +	if (!core)
> +		return -ENOMEM;
> +
> +	*core = tvp7002_dev;
> +	sd = &core->sd;
> +	core->pdata = c->dev.platform_data;
> +
> +	v4l2_i2c_subdev_init(sd, c, &tvp7002_ops);
> +	v4l_info(c, "tvp7002 found @ 0x%02x (%s)\n",
> +					c->addr << 1, c->adapter->name);
> +
> +	/* Initializes TVP7002 to its default values */
> +	error = tvp7002_write_inittab(sd, tvp7002_init_default);
> +	if (error < 0) {
> +		error = -EINVAL;
> +		goto found_error;
> +	}
> +
> +	/* Set polarity information after registers have been set */
> +	polarity = 0x5b | core->pdata->hs_polarity << 5
> +			| core->pdata->vs_polarity << 2;
> +	error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity);
> +	if (error < 0) {
> +		error = -EINVAL;
> +		goto found_error;
> +	}
> +
> +	polarity = 0x0  | core->pdata->fid_polarity << 2
> +			| core->pdata->sog_polarity << 1
> +			| core->pdata->clk_polarity;
> +	error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity);
> +	if (error < 0) {
> +		error = -EINVAL;
> +		goto found_error;
> +	}
> +
> +	if (debug > 1)
> +		tvp7002_log_status(sd);
> +
> +found_error:
> +	if (error < 0)
> +		kfree(core);
> +
> +	return error;
> +}
> +
> +/*
> + * tvp7002_remove - Remove TVP7002 device support
> + * @c: ptr to i2c_client struct
> + *
> + * Reset the TVP7002 device
> + * Returns zero when successful or -EINVAL if register read fails.
> + */
> +static int tvp7002_remove(struct i2c_client *c)
> +{
> +	struct v4l2_subdev *sd = i2c_get_clientdata(c);
> +
> +	v4l2_dbg(1, debug, sd, "removing tvp7002 adapter"
> +				"on address 0x%x\n", c->addr << 1);
> +
> +	v4l2_device_unregister_subdev(sd);
> +	kfree(to_tvp7002(sd));
> +	return 0;
> +}
> +
> +/* I2C Device ID table */
> +static const struct i2c_device_id tvp7002_id[] = {
> +	{ "tvp7002", 0 },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(i2c, tvp7002_id);
> +
> +/* I2C driver data */
> +static struct i2c_driver tvp7002_driver = {
> +	.driver = {
> +		.owner = THIS_MODULE,
> +		.name = "tvp7002",
> +	},
> +	.probe = tvp7002_probe,
> +	.remove = tvp7002_remove,
> +	.id_table = tvp7002_id,
> +};
> +
> +/*
> + * tvp7002_init - Initialize driver via I2C interface
> + *
> + * Register the TVP7002 driver.
> + * Returns 0 on success or < 0 on failure.
> + */
> +static int __init tvp7002_init(void)
> +{
> +	return i2c_add_driver(&tvp7002_driver);
> +}
> +
> +/*
> + * tvp7002_exit - Remove driver via I2C interface
> + *
> + * Unregister the TVP7002 driver.
> + * Returns 0 on success or < 0 on failure.
> + */
> +static void __exit tvp7002_exit(void)
> +{
> +	i2c_del_driver(&tvp7002_driver);
> +}
> +
> +module_init(tvp7002_init);
> +module_exit(tvp7002_exit);
diff mbox

Patch

diff --git a/drivers/media/video/tvp7002.c b/drivers/media/video/tvp7002.c
new file mode 100644
index 0000000..e6f1fe1
--- /dev/null
+++ b/drivers/media/video/tvp7002.c
@@ -0,0 +1,1464 @@ 
+/* Texas Instruments Triple 8-/10-BIT 165-/110-MSPS Video and Graphics
+ * Digitizer with Horizontal PLL registers
+ *
+ * Copyright (C) 2009 Texas Instruments Inc
+ * Author: Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>
+ *
+ * This code is partially based upon the TVP5150 driver
+ * written by Mauro Carvalho Chehab (mchehab@infradead.org)
+ * and the TVP514x driver written by Vaibhav Hiremath <hvaibhav@ti.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <linux/i2c.h>
+#include <linux/videodev2.h>
+#include <linux/delay.h>
+#include <media/v4l2-device.h>
+#include <media/tvp7002.h>
+#include <media/v4l2-chip-ident.h>
+#include <media/davinci/videohd.h>
+#include "tvp7002_reg.h"
+
+MODULE_DESCRIPTION("TI TVP7002 Video and Graphics Digitizer driver");
+MODULE_AUTHOR("Santiago Nunez-Corrales <santiago.nunez@ridgerun.com>");
+MODULE_LICENSE("GPL");
+
+/* I2C retry attempts */
+#define I2C_RETRY_COUNT                 (5)
+
+/* Debugging information */
+
+static int debug;
+module_param(debug, int, 0);
+MODULE_PARM_DESC(debug, "Debug level (0-2)");
+
+/* Struct for handling resolutions and associate register values */
+struct tvp7002_resol {
+	v4l2_std_id id;
+	int hres;
+	int vres;
+	int frate;
+	int lrate;
+	int prate;
+	u8 reg01;
+	u8 reg02;
+	u8 reg03;
+	u8 reg04;
+};
+
+/* Struct for handling register values */
+struct i2c_reg_value {
+	u8 reg;
+	u8 value;
+};
+
+/* Register default values (according to tvp7002 datasheet) */
+static const struct i2c_reg_value tvp7002_init_default[] = {
+	/* 0x00: read only */
+	{ TVP7002_HPLL_FDBK_DIV_MSBS, 0x67 },
+	{ TVP7002_HPLL_FDBK_DIV_LSBS, 0x20 },
+	{ TVP7002_HPLL_CRTL, 0xa8 },
+	{ TVP7002_HPLL_PHASE_SEL, 0x80 },
+	{ TVP7002_CLAMP_START, 0x32 },
+	{ TVP7002_CLAMP_W, 0x20 },
+	{ TVP7002_HSYNC_OUT_W, 0x20 },
+	{ TVP7002_B_FINE_GAIN, 0x00 },
+	{ TVP7002_G_FINE_GAIN, 0x00 },
+	{ TVP7002_R_FINE_GAIN, 0x00 },
+	{ TVP7002_B_FINE_OFF_MSBS, 0x80 },
+	{ TVP7002_G_FINE_OFF_MSBS, 0x80 },
+	{ TVP7002_R_FINE_OFF_MSBS, 0x80 },
+	{ TVP7002_SYNC_CTL_1, 0x5b },
+	{ TVP7002_HPLL_AND_CLAMP_CTL, 0x2e },
+	{ TVP7002_SYNC_ON_G_THRS, 0x5d },
+	{ TVP7002_SYNC_SEPARATOR_THRS, 0x20 },
+	{ TVP7002_HPLL_PRE_COAST, 0x00 },
+	{ TVP7002_HPLL_POST_COAST, 0x00 },
+	/* 0x14: read only */
+	{ TVP7002_OUT_FORMATTER, 0x00 },
+	{ TVP7002_MISC_CTL_1, 0x11 },
+	{ TVP7002_MISC_CTL_2, 0x03 },
+	{ TVP7002_MISC_CTL_3, 0x00 },
+	{ TVP7002_IN_MUX_SEL_1, 0x00 },
+	{ TVP7002_IN_MUX_SEL_2, 0xc2 },
+	{ TVP7002_B_AND_G_COARSE_GAIN, 0x77 },
+	{ TVP7002_R_COARSE_GAIN, 0x07 },
+	{ TVP7002_COARSE_CLAMP_CTL, 0x00 },
+	{ TVP7002_FINE_OFF_LSBS, 0x00 },
+	{ TVP7002_B_COARSE_OFF, 0x10 },
+	{ TVP7002_G_COARSE_OFF, 0x10 },
+	{ TVP7002_R_COARSE_OFF, 0x10 },
+	{ TVP7002_HSOUT_OUT_START, 0x0d	},
+	{ TVP7002_MISC_CTL_4, 0x0d },
+	/* 0x23: read only */
+	/* 0x24: read only */
+	/* 0x25: read only */
+	{ TVP7002_AUTO_LVL_CTL_ENABLE, 0x80 },
+	/* 0x27: read only */
+	{ TVP7002_AUTO_LVL_CTL_FILTER, 0x53 },
+	{ TVP7002_FINE_CLAMP_CTL, 0x07 },
+	{ TVP7002_PWR_CTL, 0x00 },
+	{ TVP7002_ADC_SETUP, 0x50 },
+	{ TVP7002_COARSE_CLAMP_CTL, 0x00 },
+	{ TVP7002_SOG_CLAMP, 0x80 },
+	{ TVP7002_RGB_COARSE_CLAMP_CTL, 0x8c },
+	{ TVP7002_SOG_COARSE_CLAMP_CTL, 0x04 },
+	{ TVP7002_ALC_PLACEMENT, 0x5a },
+	{ TVP7002_MVIS_STRIPPER_W, 0x03 },
+	{ TVP7002_VSYNC_ALGN, 0x10 },
+	{ TVP7002_SYNC_BYPASS, 0x00 },
+	/* 0x37: read only */
+	/* 0x38: read only */
+	/* 0x39: read only */
+	/* 0x3a: read only */
+	/* 0x3b: read only */
+	/* 0x3c: read only */
+	{ TVP7002_L_LENGTH_TOL, 0x03 },
+	{ TVP7002_VIDEO_BWTH_CTL, 0x00 },
+	{ TVP7002_AVID_START_PIXEL_LSBS, 0x01 },
+	{ TVP7002_AVID_START_PIXEL_MSBS, 0x2c },
+	{ TVP7002_AVID_STOP_PIXEL_LSBS, 0x06 },
+	{ TVP7002_AVID_STOP_PIXEL_MSBS, 0x2c },
+	{ TVP7002_VBLK_F_0_START_L_OFF, 0x05 },
+	{ TVP7002_VBLK_F_1_START_L_OFF, 0x05 },
+	{ TVP7002_VBLK_F_0_DURATION, 0x1e },
+	{ TVP7002_VBLK_F_1_DURATION, 0x1e },
+	{ TVP7002_FBIT_F_0_START_L_OFF, 0x00 },
+	{ TVP7002_FBIT_F_1_START_L_OFF, 0x00 },
+	{ TVP7002_YUV_Y_G_COEF_LSBS, 0xe3 },
+	{ TVP7002_YUV_Y_G_COEF_MSBS, 0x16 },
+	{ TVP7002_YUV_Y_B_COEF_LSBS, 0x4f },
+	{ TVP7002_YUV_Y_B_COEF_MSBS, 0x02 },
+	{ TVP7002_YUV_Y_R_COEF_LSBS, 0xce },
+	{ TVP7002_YUV_Y_R_COEF_MSBS, 0x06 },
+	{ TVP7002_YUV_U_G_COEF_LSBS, 0xab },
+	{ TVP7002_YUV_U_G_COEF_MSBS, 0xf3 },
+	{ TVP7002_YUV_U_B_COEF_LSBS, 0x00 },
+	{ TVP7002_YUV_U_B_COEF_MSBS, 0x10 },
+	{ TVP7002_YUV_U_R_COEF_LSBS, 0x55 },
+	{ TVP7002_YUV_U_R_COEF_MSBS, 0xfc },
+	{ TVP7002_YUV_V_G_COEF_LSBS, 0x78 },
+	{ TVP7002_YUV_V_G_COEF_MSBS, 0xf1 },
+	{ TVP7002_YUV_V_B_COEF_LSBS, 0x88 },
+	{ TVP7002_YUV_V_B_COEF_MSBS, 0xfe },
+	{ TVP7002_YUV_V_R_COEF_LSBS, 0x00 },
+	{ TVP7002_YUV_V_R_COEF_MSBS, 0x10 },
+	{ 0x5c, 0x00 }	/* end of registers */
+};
+
+/* Available resolutions */
+static struct tvp7002_resol tvp7002_resolutions[] = {
+	{
+		.id = V4L2_STD_NTSC,
+		.hres = 720,
+		.vres = 480,
+		.frate = 30,
+		.lrate = 15,
+		.prate = 14,
+		.reg01 = 0x35,
+		.reg02 = 0xa0,
+		.reg03 = TVP7002_VCO_RANGE_ULOW | 0x18,
+		.reg04 = 0x80,
+	},
+	{
+		.id = V4L2_STD_PAL,
+		.hres = 720,
+		.vres = 576,
+		.frate = 25,
+		.lrate = 16,
+		.prate = 14,
+		.reg01 = 0x36,
+		.reg02 = 0x00,
+		.reg03 = TVP7002_VCO_RANGE_ULOW | 0x18,
+		.reg04 = 0x80,
+	},
+	{
+		.id = V4L2_STD_525P_60,
+		.hres = 720,
+		.vres = 480,
+		.frate = 60,
+		.lrate = 31,
+		.prate = 27,
+		.reg01 = 0x35,
+		.reg02 = 0xa0,
+		.reg03 = TVP7002_VCO_RANGE_ULOW | 0x18,
+		.reg04 = 0x80,
+	},
+	{
+		.id = V4L2_STD_625P_50,
+		.hres = 720,
+		.vres = 576,
+		.frate = 50,
+		.lrate = 31,
+		.prate = 27,
+		.reg01 = 0x36,
+		.reg02 = 0x00,
+		.reg03 = TVP7002_VCO_RANGE_ULOW | 0x18,
+		.reg04 = 0x80,
+	},
+	{
+		.id = V4L2_STD_720P_60,
+		.hres = 1280,
+		.vres = 720,
+		.frate = 60,
+		.lrate = 45,
+		.prate = 74,
+		.reg01 = 0x67,
+		.reg02 = 0x20,
+		.reg03 = TVP7002_VCO_RANGE_MED | 0x20,
+		.reg04 = 0x80,
+	},
+	{
+		.id = V4L2_STD_720P_50,
+		.hres = 1280,
+		.vres = 720,
+		.frate = 50,
+		.lrate = 38,
+		.prate = 74,
+		.reg01 = 0x7b,
+		.reg02 = 0xc0,
+		.reg03 = TVP7002_VCO_RANGE_MED | 0x18,
+		.reg04 = 0x80,
+	},
+	{
+		.id = V4L2_STD_1080I_60,
+		.hres = 1920,
+		.vres = 1080,
+		.frate = 60,
+		.lrate = 34,
+		.prate = 74,
+		.reg01 = 0x89,
+		.reg02 = 0x80,
+		.reg03 = TVP7002_VCO_RANGE_MED | 0x18,
+		.reg04 = 0x80,
+	},
+	{
+		.id = V4L2_STD_1080I_50,
+		.hres = 1920,
+		.vres = 1080,
+		.frate = 50,
+		.lrate = 28,
+		.prate = 74,
+		.reg01 = 0xa5,
+		.reg02 = 0x00,
+		.reg03 = TVP7002_VCO_RANGE_MED | 0x10,
+		.reg04 = 0x80,
+	},
+	{
+		.id = V4L2_STD_1080P_60,
+		.hres = 1920,
+		.vres = 1080,
+		.frate = 60,
+		.lrate = 68,
+		.prate = 149,
+		.reg01 = 0x89,
+		.reg02 = 0x80,
+		.reg03 = TVP7002_VCO_RANGE_HIGH | 0x20,
+		.reg04 = 0x80,
+	},
+	{
+		.id = V4L2_STD_1080P_50,
+		.hres = 1920,
+		.vres = 1080,
+		.frate = 50,
+		.lrate = 56,
+		.prate = 149,
+		.reg01 = 0xa5,
+		.reg02 = 0x00,
+		.reg03 = TVP7002_VCO_RANGE_HIGH | 0x18,
+		.reg04 = 0x80,
+	},
+};
+
+/*
+ * tvp7002_from_std - Map video standard to register information
+ * @std: v4l2_std_id (u64) integer
+ *
+ * Returns index of std or -1 in case of error.
+ */
+int tvp7002_from_std(v4l2_std_id std)
+{
+	int res;
+
+	switch (std) {
+	case V4L2_STD_525P_60:
+		res = TVP7002_STD_480P;
+		break;
+	case V4L2_STD_NTSC:
+		res = TVP7002_STD_480I;
+		break;
+	case V4L2_STD_625P_50:
+		res = TVP7002_STD_576P;
+		break;
+	case V4L2_STD_PAL:
+		res = TVP7002_STD_576I;
+		break;
+	case V4L2_STD_720P_50:
+		res = TVP7002_STD_720P_50;
+		break;
+	case V4L2_STD_720P_60:
+		res = TVP7002_STD_720P_60;
+		break;
+	case V4L2_STD_1080I_50:
+		res = TVP7002_STD_1080I_50;
+		break;
+	case V4L2_STD_1080I_60:
+		res = TVP7002_STD_1080I_60;
+		break;
+	case V4L2_STD_1080P_50:
+		res = TVP7002_STD_1080P_50;
+		break;
+	case V4L2_STD_1080P_60:
+		res = TVP7002_STD_1080P_60;
+		break;
+	default:
+		res = -1;
+		break;
+	}
+
+	return res;
+}
+
+/*
+ * tvp7002_colorspace - Find the color space of a video format
+ * @std: v4l2_std_id (u64) integer
+ *
+ * Returns color space for a standard id
+ */
+enum v4l2_colorspace tvp7002_colorspace(v4l2_std_id std)
+{
+
+	switch (std) {
+	case V4L2_STD_525P_60:
+	case V4L2_STD_NTSC:
+	case V4L2_STD_625P_50:
+	case V4L2_STD_PAL:
+		return V4L2_COLORSPACE_SMPTE170M;
+	case V4L2_STD_720P_50:
+	case V4L2_STD_720P_60:
+	case V4L2_STD_1080I_50:
+	case V4L2_STD_1080I_60:
+	case V4L2_STD_1080P_50:
+	case V4L2_STD_1080P_60:
+		return V4L2_COLORSPACE_REC709;
+	default:
+		return 0;
+	}
+}
+
+#define NUM_ACTIVE_PIXELS(fmt) \
+		tvp7002_resolutions[tvp7002_from_std((fmt))].hres
+
+#define NUM_ACTIVE_LINES(fmt) \
+		tvp7002_resolutions[tvp7002_from_std((fmt))].vres
+
+/* Device definition */
+struct tvp7002 {
+	struct v4l2_subdev sd;
+	v4l2_std_id video_mode;
+	struct v4l2_pix_format pix;
+	const struct tvp7002_config *pdata;
+	int streaming;
+};
+
+/* Supported controls */
+static struct v4l2_queryctrl tvp7002_qctrl[] = {
+	{
+		/* This gain control uses fine grain in TVP7002 */
+		.id = V4L2_CID_GAIN,
+		.type = V4L2_CTRL_TYPE_INTEGER,
+		.name = "Gain for RGB channels",
+		.minimum = 0,
+		.maximum = 255,
+		.step = 1,
+		.default_value = 0,
+		.flags = 0,
+	},
+};
+
+/*
+ * to_tvp7002 - Obtain device handler TVP7002
+ * @sd: ptr to v4l2_subdev struct
+ *
+ * Returns device handler tvp7002.
+ */
+static struct tvp7002 *to_tvp7002(struct v4l2_subdev *sd)
+{
+	return container_of(sd, struct tvp7002, sd);
+}
+
+/*
+ * tvp7002_read - Read a value from a register in an TVP7002
+ * @sd: ptr to v4l2_subdev struct
+ * @reg: TVP7002 register address
+ *
+ * Returns value read if successful, or non-zero (-1) otherwise.
+ */
+static int tvp7002_read(struct v4l2_subdev *sd, unsigned char addr)
+{
+	struct i2c_client *c = v4l2_get_subdevdata(sd);
+	int retry;
+	int error;
+
+	for (retry = 0; retry < I2C_RETRY_COUNT; retry++) {
+		error = i2c_smbus_read_byte_data(c, addr);
+
+		if (error >= 0)
+			return 0;
+		msleep_interruptible(10);
+	}
+	v4l2_err(sd, "TVP7002 read error %d\n", error);
+	return error;
+}
+
+/*
+ * tvp7002_write() - Write a value to a register in TVP7002
+ * @sd: ptr to v4l2_subdev struct
+ * @addr: TVP7002 register address
+ * @value: value to be written to the register
+ *
+ * Write a value to a register in an TVP7002 decoder device.
+ * Returns zero if successful, or non-zero otherwise.
+ */
+static int tvp7002_write(struct v4l2_subdev *sd, u8 addr, u8 value)
+{
+	struct i2c_client *c = v4l2_get_subdevdata(sd);
+	int retry;
+	int error;
+
+	for (retry = 0; retry < I2C_RETRY_COUNT; retry++) {
+		error = i2c_smbus_write_byte_data(c, addr, value);
+
+		if (error >= 0)
+			return 0;
+		msleep_interruptible(10);
+	}
+	v4l2_err(sd, "TVP7002 write error %d\n", error);
+	return error;
+}
+
+/*
+ * dump_reg_range() - Dump information from TVP7002 registers
+ * @sd: ptr to v4l2_subdev struct
+ * @init: TVP7002 start address
+ * @end: TVP7002 end address
+ *
+ * Dump values at a specified register range
+ * Returns nothing.
+ */
+static void dump_reg_range(struct v4l2_subdev *sd, u8 init, const u8 end)
+{
+	int i = 0;
+	int result;
+
+	while (init != (u8)(end + 1)) {
+		result = tvp7002_read(sd, init);
+
+		if (result < 0)
+			v4l2_err(sd, "reg 0x%02x unreadable\n", i);
+		else
+			v4l2_info(sd, "@0x%02x = %02x\n", i, result);
+
+		init++;
+		i++;
+	}
+}
+
+/*
+ * tvp7002_log_chk() - Check reading the value of a register
+ * @sd: ptr to v4l2_subdev struct
+ * @reg: register to read
+ * @message: register name/function
+ *
+ * Check procedure for reading a register.
+ * Returns nothing (void).
+ */
+static inline void tvp7002_log_chk(struct v4l2_subdev *sd, u8 reg,
+							const char *message)
+{
+	int res;
+
+	res = tvp7002_read(sd, reg);
+
+	if ((res) >= 0)
+		v4l2_info(sd, "%s = 0x%02x\n", message, res);
+}
+
+/*
+ * tvp7002_log_status() - Print information about register settings
+ * @sd: ptr to v4l2_subdev struct
+ *
+ * Log register values of a TVP7002 decoder device.
+ * Returns zero or -EINVAL if read operation fails.
+ */
+static int tvp7002_log_status(struct v4l2_subdev *sd)
+{
+	tvp7002_log_chk(sd, TVP7002_CHIP_REV, "Chip revision number");
+	tvp7002_log_chk(sd, TVP7002_HPLL_FDBK_DIV_LSBS,
+						"H-PLL feedback div LSB");
+	tvp7002_log_chk(sd, TVP7002_HPLL_FDBK_DIV_MSBS,
+						"H-PLL feedback div MSB");
+	tvp7002_log_chk(sd, TVP7002_HPLL_FDBK_DIV_MSBS,
+						"VCO freq range selector");
+	tvp7002_log_chk(sd, TVP7002_HPLL_PHASE_SEL,
+						"ADC sampling clk phase sel");
+	tvp7002_log_chk(sd, TVP7002_CLAMP_START, "Clamp start");
+	tvp7002_log_chk(sd, TVP7002_CLAMP_W, "Clamp width");
+	tvp7002_log_chk(sd, TVP7002_HSYNC_OUT_W, "HSYNC output width");
+	tvp7002_log_chk(sd, TVP7002_B_FINE_GAIN, "Digital fine grain B ch");
+	tvp7002_log_chk(sd, TVP7002_G_FINE_GAIN, "Digital fine grain G ch");
+	tvp7002_log_chk(sd, TVP7002_R_FINE_GAIN, "Digital fine grain R ch");
+	tvp7002_log_chk(sd, TVP7002_B_FINE_OFF_MSBS,
+						"Digital fine grain off B ch");
+	tvp7002_log_chk(sd, TVP7002_G_FINE_OFF_MSBS,
+						"Digital fine grain off G ch");
+	tvp7002_log_chk(sd, TVP7002_R_FINE_OFF_MSBS,
+						"Digital fine grain off R ch");
+	tvp7002_log_chk(sd, TVP7002_FINE_OFF_LSBS, "Dig fine grain off LSBs");
+	tvp7002_log_chk(sd, TVP7002_SYNC_CTL_1, "Sync control 1");
+	tvp7002_log_chk(sd, TVP7002_HPLL_AND_CLAMP_CTL,
+						"H-PLL and clamp control");
+	tvp7002_log_chk(sd, TVP7002_SYNC_ON_G_THRS, "Sync-On-Green threshold");
+	tvp7002_log_chk(sd, TVP7002_SYNC_SEPARATOR_THRS,
+						"Sync separator thrshold");
+	tvp7002_log_chk(sd, TVP7002_HPLL_PRE_COAST, "H-PLL pre-coast");
+	tvp7002_log_chk(sd, TVP7002_HPLL_POST_COAST, "H-PLL post-coast");
+	tvp7002_log_chk(sd, TVP7002_SYNC_DETECT_STAT, "Sync detect status");
+	tvp7002_log_chk(sd, TVP7002_OUT_FORMATTER, "Output formatter");
+	tvp7002_log_chk(sd, TVP7002_MISC_CTL_1, "Miscelaneous control 1");
+	tvp7002_log_chk(sd, TVP7002_MISC_CTL_2, "Miscelaneous control 2");
+	tvp7002_log_chk(sd, TVP7002_MISC_CTL_3, "Miscelaneous control 3");
+	tvp7002_log_chk(sd, TVP7002_IN_MUX_SEL_1, "Input Mux Selector 1");
+	tvp7002_log_chk(sd, TVP7002_IN_MUX_SEL_2, "Input Mux Selector 2");
+	tvp7002_log_chk(sd, TVP7002_B_AND_G_COARSE_GAIN, "B, G coarse gain");
+	tvp7002_log_chk(sd, TVP7002_R_COARSE_GAIN, "R coarse gain");
+	tvp7002_log_chk(sd, TVP7002_B_COARSE_OFF, "Coarse offset for B ch");
+	tvp7002_log_chk(sd, TVP7002_G_COARSE_OFF, "Coarse offset for G ch");
+	tvp7002_log_chk(sd, TVP7002_R_COARSE_OFF, "Coarse offset for R ch");
+	tvp7002_log_chk(sd, TVP7002_HSOUT_OUT_START,
+						"HSYNC lead edge out start");
+	tvp7002_log_chk(sd, TVP7002_MISC_CTL_4, "Miscelaneous control 4");
+	tvp7002_log_chk(sd, TVP7002_B_DGTL_ALC_OUT_LSBS,
+						"Flt ALC out B ch LSBs");
+	tvp7002_log_chk(sd, TVP7002_G_DGTL_ALC_OUT_LSBS,
+						"Flt ALC out G ch LSBs");
+	tvp7002_log_chk(sd, TVP7002_R_DGTL_ALC_OUT_LSBS,
+						"Flt ALC out R ch LSBs");
+	tvp7002_log_chk(sd, TVP7002_AUTO_LVL_CTL_ENABLE,
+						"Auto level ctrl enable");
+	tvp7002_log_chk(sd, TVP7002_DGTL_ALC_OUT_MSBS,
+						"Filt ALC out RGB chs MSB");
+	tvp7002_log_chk(sd, TVP7002_AUTO_LVL_CTL_FILTER,
+						"Auto level ctrl filter");
+	tvp7002_log_chk(sd, TVP7002_FINE_CLAMP_CTL, "Fine clamp control");
+	tvp7002_log_chk(sd, TVP7002_PWR_CTL, "Power control");
+	tvp7002_log_chk(sd, TVP7002_ADC_SETUP, "ADC setup");
+	tvp7002_log_chk(sd, TVP7002_COARSE_CLAMP_CTL, "Coarse clamp ctrl");
+	tvp7002_log_chk(sd, TVP7002_SOG_CLAMP, "Sync-On-Green clamp");
+	tvp7002_log_chk(sd, TVP7002_RGB_COARSE_CLAMP_CTL,
+						"RGB coarse clamp ctrl");
+	tvp7002_log_chk(sd, TVP7002_SOG_COARSE_CLAMP_CTL,
+						"SOG coarse clamp ctrl");
+	tvp7002_log_chk(sd, TVP7002_ALC_PLACEMENT, "ALC placement");
+	tvp7002_log_chk(sd, TVP7002_MVIS_STRIPPER_W,
+						"Macrovision stripper width");
+	tvp7002_log_chk(sd, TVP7002_SYNC_BYPASS, "Sync bypass");
+	tvp7002_log_chk(sd, TVP7002_L_FRAME_STAT_LSBS,
+						"Lines p Frame status LSBs");
+	tvp7002_log_chk(sd, TVP7002_L_FRAME_STAT_MSBS,
+						"Lines p Frame status MSBs");
+	tvp7002_log_chk(sd, TVP7002_CLK_L_STAT_LSBS, "Clks p line stat LSBs");
+	tvp7002_log_chk(sd, TVP7002_CLK_L_STAT_MSBS, "Clks p line stat MSBs");
+	tvp7002_log_chk(sd, TVP7002_HSYNC_W, "HSYNC width");
+	tvp7002_log_chk(sd, TVP7002_VSYNC_W, "VSYNC width");
+	tvp7002_log_chk(sd, TVP7002_L_LENGTH_TOL, "Line length tolerance");
+	tvp7002_log_chk(sd, TVP7002_VIDEO_BWTH_CTL, "Video bandwth control");
+	tvp7002_log_chk(sd, TVP7002_AVID_START_PIXEL_LSBS,
+						"AVID start pixel LSBs");
+	tvp7002_log_chk(sd, TVP7002_AVID_START_PIXEL_MSBS,
+						"AVID start pixel MSBs");
+	tvp7002_log_chk(sd, TVP7002_AVID_STOP_PIXEL_LSBS,
+						"AVID stop pixel LSBs");
+	tvp7002_log_chk(sd, TVP7002_AVID_STOP_PIXEL_MSBS,
+						"AVID stop pixel MSBs");
+	tvp7002_log_chk(sd, TVP7002_VBLK_F_0_START_L_OFF,
+						"VBLK start line off 0");
+	tvp7002_log_chk(sd, TVP7002_VBLK_F_1_START_L_OFF,
+						"VBLK start line off 1");
+	tvp7002_log_chk(sd, TVP7002_VBLK_F_0_DURATION, "VBLK duration 0");
+	tvp7002_log_chk(sd, TVP7002_VBLK_F_1_DURATION, "VBLK duration 1");
+	tvp7002_log_chk(sd, TVP7002_FBIT_F_0_START_L_OFF,
+						"FBIT start line off 0");
+	tvp7002_log_chk(sd, TVP7002_FBIT_F_1_START_L_OFF,
+						"FBIT start line off 1");
+	tvp7002_log_chk(sd, TVP7002_YUV_Y_G_COEF_LSBS, "YUV Y G LSBs");
+	tvp7002_log_chk(sd, TVP7002_YUV_Y_G_COEF_MSBS, "YUV Y G MSBs");
+	tvp7002_log_chk(sd, TVP7002_YUV_Y_B_COEF_LSBS, "YUV Y B LSBs");
+	tvp7002_log_chk(sd, TVP7002_YUV_Y_B_COEF_MSBS, "YUV Y B MSBs");
+	tvp7002_log_chk(sd, TVP7002_YUV_Y_R_COEF_LSBS, "YUV Y R LSBs");
+	tvp7002_log_chk(sd, TVP7002_YUV_Y_R_COEF_MSBS, "YUV Y R MSBs");
+	tvp7002_log_chk(sd, TVP7002_YUV_U_G_COEF_LSBS, "YUV U G LSBs");
+	tvp7002_log_chk(sd, TVP7002_YUV_U_G_COEF_MSBS, "YUV U G MSBs");
+	tvp7002_log_chk(sd, TVP7002_YUV_U_B_COEF_LSBS, "YUV U B LSBs");
+	tvp7002_log_chk(sd, TVP7002_YUV_U_B_COEF_MSBS, "YUV U B MSBs");
+	tvp7002_log_chk(sd, TVP7002_YUV_U_R_COEF_LSBS, "YUV U R LSBs");
+	tvp7002_log_chk(sd, TVP7002_YUV_U_R_COEF_MSBS, "YUV U R MSBs");
+	tvp7002_log_chk(sd, TVP7002_YUV_V_G_COEF_LSBS, "YUV V G LSBs");
+	tvp7002_log_chk(sd, TVP7002_YUV_V_G_COEF_MSBS, "YUV V G MSBs");
+	tvp7002_log_chk(sd, TVP7002_YUV_V_B_COEF_LSBS, "YUV V B LSBs");
+	tvp7002_log_chk(sd, TVP7002_YUV_V_B_COEF_MSBS, "YUV V B MSBs");
+	tvp7002_log_chk(sd, TVP7002_YUV_V_R_COEF_LSBS, "YUV V R LSBs");
+	tvp7002_log_chk(sd, TVP7002_YUV_V_R_COEF_MSBS, "YUV V R MSBs");
+
+	return 0;
+}
+
+/*
+ * tvp7002_g_chip_ident() - Get chip identification number
+ * @sd: ptr to v4l2_subdev struct
+ * @chip: ptr to v4l2_dbg_chip_ident struct
+ *
+ * Obtains the chip's identification number.
+ * Returns zero or -EINVAL if read operation fails.
+ */
+static int tvp7002_g_chip_ident(struct v4l2_subdev *sd,
+					struct v4l2_dbg_chip_ident *chip)
+{
+	int rev;
+	struct i2c_client *client = v4l2_get_subdevdata(sd);
+
+	rev = tvp7002_read(sd, TVP7002_CHIP_REV);
+
+	if (rev < 0)
+		return -EINVAL;
+
+	return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_TVP7002,
+									rev);
+}
+
+/*
+ * tvp7002_write_inittab() - Write initialization values
+ * @sd: ptr to v4l2_subdev struct
+ * @regs: ptr to i2c_reg_value struct
+ *
+ * Write initialization values.
+ * Returns zero or -EINVAL if read operation fails.
+ */
+static int tvp7002_write_inittab(struct v4l2_subdev *sd,
+					const struct i2c_reg_value *regs)
+{
+	int i;
+	int error;
+
+	/* Initialize the first (defined) registers */
+	while (regs->reg != 0x5c) {
+		error = tvp7002_write(sd, regs->reg, regs->value);
+		if (error < 0)
+			return -EINVAL;
+		regs++;
+	}
+	/* Initialize the last unnamed registers */
+	for (i = 0x5c; i <= 0xff; i++) {
+		error = tvp7002_write(sd, i, 0x00);
+		if (error < 0)
+			return -EINVAL;
+		regs++;
+	}
+
+	return 0;
+}
+
+/*
+ * tvp7002_write_err() - Write a register value with error code
+ * @sd: pointer to standard V4L2 sub-device structure
+ * @reg: destination register
+ * @val: value to be written
+ * @error: pointer to error value
+ *
+ * Write a value in a register and save error value in
+ */
+static inline void tvp7002_write_err(struct v4l2_subdev *sd, u8 reg,
+							u8 val, int *err)
+{
+	if (!*err)
+		*err = tvp7002_write(sd, reg, val);
+}
+
+/*
+ * tvp7002_set_video_mode() - Set video mode
+ * @sd: pointer to standard V4L2 sub-device structure
+ * @sdf: index to structure describing format
+ *
+ * Set video standard according to index
+ * Returns 0 if operation is successful or -EINVAL otherwise
+ */
+static int tvp7002_set_video_mode(struct v4l2_subdev *sd, int sdf)
+{
+	int error = 0;
+
+	if (sdf < TVP7002_STD_480I || sdf > TVP7002_STD_1080P_50) {
+		v4l2_err(sd, "sf out of range\n");
+		return -ERANGE;
+	}
+
+	/* Print specific information about current format */
+	v4l2_info(sd, "Setting standard display format...\n");
+	v4l2_info(sd, "hres = %d vres=%d fr=%d lr=%d prate=%d\n",
+			tvp7002_resolutions[sdf].hres,
+			tvp7002_resolutions[sdf].vres,
+			tvp7002_resolutions[sdf].frate,
+			tvp7002_resolutions[sdf].lrate,
+			tvp7002_resolutions[sdf].prate);
+
+	/* Set registers accordingly */
+	tvp7002_write_err(sd, TVP7002_HPLL_FDBK_DIV_MSBS,
+				tvp7002_resolutions[sdf].reg01, &error);
+	tvp7002_write_err(sd, TVP7002_HPLL_FDBK_DIV_LSBS,
+				tvp7002_resolutions[sdf].reg02, &error);
+	tvp7002_write_err(sd, TVP7002_HPLL_CRTL,
+				tvp7002_resolutions[sdf].reg03, &error);
+	tvp7002_write_err(sd, TVP7002_HPLL_PHASE_SEL,
+				tvp7002_resolutions[sdf].reg04, &error);
+
+	/* Set SD/HD mode registers */
+	if (sdf < TVP7002_STD_720P_60) {
+		tvp7002_write_err(sd, TVP7002_CLAMP_START, 0x06, &error);
+		tvp7002_write_err(sd, TVP7002_CLAMP_W, 0x10, &error);
+		tvp7002_write_err(sd, TVP7002_HPLL_PRE_COAST, 0x03, &error);
+		tvp7002_write_err(sd, TVP7002_HPLL_POST_COAST, 0x03, &error);
+		tvp7002_write_err(sd, TVP7002_IN_MUX_SEL_2, 0x17, &error);
+
+		if (sdf < TVP7002_STD_480P) {
+			tvp7002_write_err(sd, TVP7002_HSOUT_OUT_START, 0x0c,
+								&error);
+			tvp7002_write_err(sd, TVP7002_MVIS_STRIPPER_W, 0x24,
+								&error);
+		} else {
+			tvp7002_write_err(sd, TVP7002_HSOUT_OUT_START, 0x0a,
+								&error);
+			tvp7002_write_err(sd, TVP7002_MVIS_STRIPPER_W, 0x12,
+								&error);
+		}
+		tvp7002_write_err(sd, TVP7002_MISC_CTL_4, 0x08, &error);
+		tvp7002_write_err(sd, TVP7002_ALC_PLACEMENT, 0x18, &error);
+	} else {
+		tvp7002_write_err(sd, TVP7002_CLAMP_START, 0x32, &error);
+		tvp7002_write_err(sd, TVP7002_CLAMP_W, 0x20, &error);
+		tvp7002_write_err(sd, TVP7002_HPLL_PRE_COAST, 0x01, &error);
+		tvp7002_write_err(sd, TVP7002_HPLL_POST_COAST, 0x00, &error);
+		tvp7002_write_err(sd, TVP7002_IN_MUX_SEL_2, 0xc7, &error);
+
+		if (sdf < TVP7002_STD_1080I_60)
+			tvp7002_write_err(sd, TVP7002_HSOUT_OUT_START,
+								0x35, &error);
+		else
+			tvp7002_write_err(sd, TVP7002_HSOUT_OUT_START,
+								0x39, &error);
+
+		tvp7002_write_err(sd, TVP7002_MISC_CTL_4, 0x00, &error);
+		tvp7002_write_err(sd, TVP7002_ALC_PLACEMENT, 0x5a, &error);
+
+		if (sdf < TVP7002_STD_1080P_60)
+			tvp7002_write_err(sd, TVP7002_MVIS_STRIPPER_W,
+								0x07, &error);
+		else
+			tvp7002_write_err(sd, TVP7002_MVIS_STRIPPER_W,
+								0x03, &error);
+	}
+	if (sdf < TVP7002_STD_1080P_60) {
+		tvp7002_write_err(sd, TVP7002_ADC_SETUP, 0x50, &error);
+		tvp7002_write_err(sd, TVP7002_VIDEO_BWTH_CTL, 0x0f, &error);
+	} else {
+		tvp7002_write_err(sd, TVP7002_ADC_SETUP, 0x80, &error);
+		tvp7002_write_err(sd, TVP7002_VIDEO_BWTH_CTL, 0x00, &error);
+	}
+	/* Set up registers that hold the same value regardless of the
+	 * SD mode
+	 */
+	tvp7002_write_err(sd, TVP7002_SYNC_ON_G_THRS, 0x5d, &error);
+	tvp7002_write_err(sd, TVP7002_SYNC_SEPARATOR_THRS, 0x40, &error);
+	tvp7002_write_err(sd, TVP7002_MISC_CTL_2, 0x00, &error);
+	tvp7002_write_err(sd, TVP7002_MISC_CTL_3, 0x01, &error);
+	tvp7002_write_err(sd, TVP7002_IN_MUX_SEL_1, 0x00, &error);
+	tvp7002_write_err(sd, TVP7002_VSYNC_ALGN, 0x00, &error);
+	tvp7002_write_err(sd, TVP7002_L_LENGTH_TOL, 0x06, &error);
+	tvp7002_write_err(sd, TVP7002_SYNC_SEPARATOR_THRS, 0x40, &error);
+	tvp7002_write_err(sd, TVP7002_MISC_CTL_2, 0x00, &error);
+	tvp7002_write_err(sd, TVP7002_MISC_CTL_3, 0x01, &error);
+	tvp7002_write_err(sd, TVP7002_IN_MUX_SEL_1, 0x00, &error);
+	tvp7002_write_err(sd, TVP7002_VSYNC_ALGN, 0x00, &error);
+	tvp7002_write_err(sd, TVP7002_L_LENGTH_TOL, 0x06, &error);
+	return error;
+}
+
+/*
+ * tvp7002_get_video_mode() - V4L2 decoder interface handler for querystd
+ * @sd: pointer to standard V4L2 sub-device structure
+ *
+ * Returns the current standard detected by TVP7002. If no active input is
+ * detected, returns -1
+ */
+static v4l2_std_id tvp7002_get_video_mode(struct v4l2_subdev *sd)
+{
+	int reg01, reg02, reg03;
+
+	reg01 = tvp7002_read(sd, TVP7002_HPLL_FDBK_DIV_MSBS);
+	reg02 = tvp7002_read(sd, TVP7002_HPLL_FDBK_DIV_LSBS);
+	reg03 = tvp7002_read(sd, TVP7002_HPLL_CRTL);
+
+	if (reg01 < 0 || reg02 < 0 || reg03 < 0)
+		return V4L2_STD_UNKNOWN;
+
+	switch (reg01) {
+	case 0x35:
+		if (reg02 == 0xa0)
+			return V4L2_STD_NTSC;
+		else
+			return V4L2_STD_PAL;
+	case 0x36:
+		if (reg02 == 0xa0)
+			return V4L2_STD_525P_60;
+		else
+			return V4L2_STD_625P_50;
+	case 0x67:
+		return V4L2_STD_720P_60;
+	case 0x7b:
+		return V4L2_STD_720P_50;
+	case 0x89:
+		if (reg03 == 0x98)
+			return V4L2_STD_1080I_60;
+		else
+			return V4L2_STD_1080P_60;
+		break;
+	case 0xa5:
+		if (reg03 == 0x90)
+			return V4L2_STD_1080I_50;
+		else
+			return V4L2_STD_1080P_50;
+	default:
+		return V4L2_STD_UNKNOWN;
+	}
+}
+
+/*
+ * tvp7002_querystd() - V4L2 decoder interface handler for querystd
+ * @sd: pointer to standard V4L2 sub-device structure
+ * @std_id: standard V4L2 std_id ioctl enum
+ *
+ * Returns the current standard detected by TVP7002. If no active input is
+ * detected, returns -EINVAL
+ */
+static int tvp7002_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
+{
+	v4l2_std_id current_std;
+	u8 sync_lock_status;
+	int res;
+
+	if (std_id == NULL)
+		return -EINVAL;
+
+	/* get the current standard */
+	res = tvp7002_get_video_mode(sd);
+	if (res == V4L2_STD_UNKNOWN)
+		return -EINVAL;
+	current_std = res;
+
+	/* check whether signal is locked */
+	sync_lock_status = tvp7002_read(sd, TVP7002_SYNC_DETECT_STAT);
+
+	if (0x02 != (sync_lock_status & 0xff))
+		return -EINVAL;	/* No input detected */
+
+	*std_id = current_std;
+
+	v4l2_dbg(1, debug, sd, "Current STD: %d %d @ %d Hz\n",
+		tvp7002_resolutions[tvp7002_from_std(current_std)].hres,
+		tvp7002_resolutions[tvp7002_from_std(current_std)].vres,
+		tvp7002_resolutions[tvp7002_from_std(current_std)].frate);
+	return 0;
+}
+
+/*
+ * tvp7002_try_fmt_cap() - V4L2 decoder interface handler for try_fmt
+ * @sd: pointer to standard V4L2 sub-device structure
+ * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
+ *
+ * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
+ * ioctl is used to negotiate the image capture size and pixel format
+ * without actually making it take effect.
+ */
+static int tvp7002_try_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
+{
+	struct v4l2_pix_format *pix;
+	v4l2_std_id current_std;
+	int res;
+
+	if (f == NULL)
+		return -EINVAL;
+
+	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		/* only capture is supported */
+		return -EINVAL;
+
+	pix = &f->fmt.pix;
+
+	/* Calculate height and width based on current standard */
+	res = tvp7002_get_video_mode(sd);
+	if (res < 0)
+		return -EINVAL;
+	current_std = res;
+
+	pix->width = NUM_ACTIVE_PIXELS(current_std);
+	pix->height = NUM_ACTIVE_LINES(current_std);
+
+	pix->pixelformat = V4L2_PIX_FMT_UYVY;
+
+	pix->field = V4L2_FIELD_INTERLACED;
+	pix->bytesperline = pix->width * 2;
+	pix->sizeimage = pix->bytesperline * pix->height;
+	pix->colorspace = V4L2_COLORSPACE_REC709;
+	pix->priv = 0;
+
+	v4l2_dbg(1, debug, sd, "Try FMT: pixelformat - %s, bytesperline - %d"
+			"Width - %d, Height - %d",
+			"8-bit UYVY 4:2:2 Format", pix->bytesperline,
+			pix->width, pix->height);
+	return 0;
+}
+
+/**
+ * tvp7002_s_fmt_cap() - V4L2 decoder interface handler for s_fmt
+ * @sd: pointer to standard V4L2 sub-device structure
+ * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
+ *
+ * If the requested format is supported, configures the HW to use that
+ * format, returns error code if format not supported or HW can't be
+ * correctly configured.
+ */
+static int tvp7002_s_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
+{
+	struct tvp7002 *decoder = to_tvp7002(sd);
+	struct v4l2_pix_format *pix;
+	int rval;
+
+	if (f == NULL)
+		return -EINVAL;
+
+	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		/* only capture is supported */
+		return -EINVAL;
+
+	pix = &f->fmt.pix;
+	rval = tvp7002_try_fmt_cap(sd, f);
+	if (rval)
+		return rval;
+
+	decoder->pix = *pix;
+
+	return rval;
+}
+
+/*
+ * tvp7002_g_fmt() - V4L2 decoder interface handler for tvp7002_g_fmt
+ * @sd: pointer to standard V4L2 sub-device structure
+ * @f: pointer to standard V4L2 v4l2_format structure
+ *
+ * Returns the decoder's current pixel format in the v4l2_format
+ * parameter.
+ */
+static int tvp7002_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
+{
+	struct tvp7002 *decoder = to_tvp7002(sd);
+
+	if (f == NULL)
+		return -EINVAL;
+
+	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		/* only capture is supported */
+		return -EINVAL;
+
+	f->fmt.pix = decoder->pix;
+
+	v4l2_dbg(1, debug, sd, "Current FMT: bytesperline - %d"
+			"Width - %d, Height - %d",
+			decoder->pix.bytesperline,
+			decoder->pix.width, decoder->pix.height);
+	return 0;
+}
+
+/*
+ * tvp7002_s_ctrl() - Set a control
+ * @sd: ptr to v4l2_subdev struct
+ * @ctrl: ptr to v4l2_control struct
+ *
+ * Set a control for a TVP7002 decoder device.
+ * Returns zero when successful or -EINVAL if register access fails.
+ */
+static int tvp7002_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
+{
+	struct tvp7002 *decoder = to_tvp7002(sd);
+	int vmd = 0;
+
+	decoder->video_mode = std;
+	vmd = tvp7002_from_std(std);
+
+	v4l2_dbg(1, debug, sd, "Set video std mode to %llx.\n", std);
+
+	return tvp7002_set_video_mode(sd, vmd);
+}
+
+/*
+ * tvp7002_g_ctrl() - Get a control
+ * @sd: ptr to v4l2_subdev struct
+ * @ctrl: ptr to v4l2_control struct
+ *
+ * Get a control for a TVP7002 decoder device.
+ * Returns zero when successful or -EINVAL if register access fails.
+ */
+static int tvp7002_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
+{
+	int rval, gval, bval;
+	int res;
+
+	v4l2_info(sd, "g_ctrl called\n");
+
+	switch (ctrl->id) {
+	case V4L2_CID_GAIN:
+		rval = tvp7002_read(sd, TVP7002_R_FINE_GAIN);
+		gval = tvp7002_read(sd, TVP7002_G_FINE_GAIN);
+		bval = tvp7002_read(sd, TVP7002_B_FINE_GAIN);
+
+		if (rval < 0 || gval < 0 || bval < 0) {
+			res = -1;
+		} else if (rval != gval || rval != bval) {
+			res = -1;
+		} else {
+			ctrl->value = rval & 0x0F;
+			res = ctrl->value;
+		}
+		break;
+	default:
+		res = -1;
+		break;
+	}
+
+	return res < 0 ? res : 0;
+}
+
+/*
+ * tvp7002_s_ctrl() - Set a control
+ * @sd: ptr to v4l2_subdev struct
+ * @ctrl: ptr to v4l2_control struct
+ *
+ * Set a control in TVP7002 decoder device.
+ * Returns zero when successful or -EINVAL if register access fails.
+ */
+static int tvp7002_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
+{
+	int rval, gval, bval;
+	int error;
+	u8 i, n;
+	n = ARRAY_SIZE(tvp7002_qctrl);
+
+	for (i = 0; i < n; i++)
+		if (ctrl->id == tvp7002_qctrl[i].id)
+			break;
+
+	if (i == n)
+		return -EINVAL;
+
+	if (ctrl->value < tvp7002_qctrl[i].minimum ||
+			ctrl->value > tvp7002_qctrl[i].maximum)
+		return -ERANGE;
+
+	switch (ctrl->id) {
+	case V4L2_CID_GAIN:
+		rval = tvp7002_write(sd, TVP7002_R_FINE_GAIN,
+							ctrl->value & 0xff);
+		gval = tvp7002_write(sd, TVP7002_G_FINE_GAIN,
+							ctrl->value & 0xff);
+		bval = tvp7002_write(sd, TVP7002_B_FINE_GAIN,
+							ctrl->value & 0xff);
+		if (rval < 0  || gval < 0 || bval < 0)
+			error = -1;
+		else
+			error = rval;
+		break;
+	default:
+		error = -1;
+		break;
+	}
+
+	return error < 0 ? -EINVAL : 0;
+}
+
+/*
+ * tvp7002_g_register() - Get the value of a register
+ * @sd: ptr to v4l2_subdev struct
+ * @vreg: ptr to v4l2_dbg_register struct
+ *
+ * Get the value of a TVP7002 decoder device register.
+ * Returns zero when successful, -EINVAL if register read fails or
+ * access to I2C client fails, -EPERM if the call is not allowed
+ * by diabled CAP_SYS_ADMIN.
+ */
+#ifdef CONFIG_VIDEO_ADV_DEBUG
+static int tvp7002_g_register(struct v4l2_subdev *sd,
+						struct v4l2_dbg_register *reg)
+{
+	struct i2c_client *client = v4l2_get_subdevdata(sd);
+
+	if (!v4l2_chip_match_i2c_client(client, &reg->match))
+		return -EINVAL;
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
+	return reg->val < 0 ? -EINVAL : 0;
+}
+
+/*
+ * tvp7002_s_register() - set a control
+ * @sd: ptr to v4l2_subdev struct
+ * @ctrl: ptr to v4l2_control struct
+ *
+ * Get the value of a TVP7002 decoder device register.
+ * Returns zero when successful or -EINVAL if register read fails.
+ */
+static int tvp7002_s_register(struct v4l2_subdev *sd,
+						struct v4l2_dbg_register *reg)
+{
+	int wres;
+	struct i2c_client *client = v4l2_get_subdevdata(sd);
+
+	if (!v4l2_chip_match_i2c_client(client, &reg->match))
+		return -EINVAL;
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
+	wres = tvp7002_write(sd, reg->reg & 0xff, reg->val & 0xff);
+
+	return wres < 0 ? -EINVAL : 0;
+}
+#endif
+
+/*
+ * tvp7002_queryctrl() - Query a control
+ * @sd: ptr to v4l2_subdev struct
+ * @ctrl: ptr to v4l2_queryctrl struct
+ *
+ * Query a control of a TVP7002 decoder device.
+ * Returns zero when successful or -EINVAL if register read fails.
+ */
+static int tvp7002_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
+{
+	int i, error;
+	error = -EINVAL;
+
+	v4l2_info(sd, "queryctrl called\n");
+
+	for (i = 0; i < ARRAY_SIZE(tvp7002_qctrl); i++)
+		if (qc->id && qc->id == tvp7002_qctrl[i].id) {
+			v4l2_ctrl_query_fill(qc, tvp7002_qctrl[i].minimum,
+					tvp7002_qctrl[i].maximum,
+					tvp7002_qctrl[i].step,
+					tvp7002_qctrl[i].default_value);
+			error = 0;
+			break;
+		}
+
+	return error;
+}
+
+/*
+ * tvp7002_s_stream() - V4L2 decoder i/f handler for s_stream
+ * @sd: pointer to standard V4L2 sub-device structure
+ * @enable: streaming enable or disable
+ *
+ * Sets streaming to enable or disable, if possible.
+ */
+static int tvp7002_s_stream(struct v4l2_subdev *sd, int enable)
+{
+	int err = 0;
+	struct tvp7002 *decoder = to_tvp7002(sd);
+
+	if (decoder->streaming == enable)
+		return 0;
+
+	if (enable) {
+		/* Power Up Sequence */
+		err = tvp7002_write(sd, TVP7002_PWR_CTL, 0x00);
+		if (err) {
+			v4l2_err(sd, "Unable to turn on decoder\n");
+			err = -EINVAL;
+		}
+		err = tvp7002_write_inittab(sd, tvp7002_init_default);
+		if (err < 0) {
+			v4l2_err(sd, "Unable to initialize\n");
+			err = -EINVAL;
+		}
+		/* Detect if not already detected */
+		err = tvp7002_read(sd, TVP7002_CHIP_REV);
+		if (err < 0) {
+			v4l2_err(sd, "Unable to detect decoder\n");
+			err = -EINVAL;
+		}
+		decoder->streaming = enable;
+	} else {
+		/* Power Down Sequence */
+		err = tvp7002_write(sd, TVP7002_PWR_CTL, 0x40);
+		if (err) {
+			v4l2_err(sd, "Unable to turn off decoder\n");
+			return err;
+		}
+		decoder->streaming = enable;
+	}
+
+	return err;
+}
+
+/* Specific video subsystem operation handlers */
+static const struct v4l2_subdev_video_ops tvp7002_video_ops = {
+	.querystd = tvp7002_querystd,
+	.s_stream = tvp7002_s_stream,
+	.g_fmt = tvp7002_g_fmt,
+};
+
+/* V4L2 Operations handlers */
+static const struct v4l2_subdev_core_ops tvp7002_core_ops = {
+	.g_chip_ident = tvp7002_g_chip_ident,
+	.log_status = tvp7002_log_status,
+	.g_ctrl = tvp7002_g_ctrl,
+	.s_ctrl = tvp7002_s_ctrl,
+	.queryctrl = tvp7002_queryctrl,
+	.s_std = tvp7002_s_std,
+#ifdef CONFIG_VIDEO_ADV_DEBUG
+	.g_register = tvp7002_g_register,
+	.s_register = tvp7002_s_register,
+#endif
+};
+
+static const struct v4l2_subdev_ops tvp7002_ops = {
+	.core = &tvp7002_core_ops,
+	.video = &tvp7002_video_ops,
+};
+
+/* Default driver settings */
+static struct tvp7002 tvp7002_dev = {
+	.streaming = 0,
+
+	.pix = {
+		.width = 720,
+		.height = 480,
+		.pixelformat = V4L2_PIX_FMT_UYVY,
+		.field = V4L2_FIELD_INTERLACED,
+		.bytesperline = 720 * 2,
+		.sizeimage = 720 * 2 * 480,
+		.colorspace = V4L2_COLORSPACE_SMPTE170M,
+		},
+
+	.video_mode = V4L2_STD_NTSC,
+};
+
+/*
+ * tvp7002_reset - Reset a TVP7002 device
+ * @sd: ptr to v4l2_subdev struct
+ * @val: unsigned integer (not used)
+ *
+ * Reset the TVP7002 device
+ * Returns zero when successful or -EINVAL if register read fails.
+ */
+static int tvp7002_reset(struct v4l2_subdev *sd, u32 val)
+{
+	struct tvp7002 *core = to_tvp7002(sd);
+	int polarity;
+	int error;
+
+	error = tvp7002_read(sd, TVP7002_CHIP_REV);
+	if (error < 0) {
+		error = -EINVAL;
+		goto found_error;
+	}
+
+	if (error == 0x02) {
+		v4l2_info(sd, "rev. %02x detected.\n", error);
+	} else {
+		v4l2_info(sd, "unknown revision detected.\n");
+		v4l2_info(sd, "revision number is %02x\n", error);
+	}
+
+	/* Initializes TVP7002 to its default values */
+	error = tvp7002_write_inittab(sd, tvp7002_init_default);
+	if (error < 0) {
+		error = -EINVAL;
+		goto found_error;
+	}
+
+	/* Set polarity information after registers have been set */
+	polarity = 0x5b | core->pdata->hs_polarity << 5
+			| core->pdata->vs_polarity << 2;
+	error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity);
+	if (error < 0) {
+		error = -EINVAL;
+		goto found_error;
+	}
+
+	polarity = 0x0  | core->pdata->fid_polarity << 2
+			| core->pdata->sog_polarity << 1
+			| core->pdata->clk_polarity;
+	error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity);
+	if (error < 0) {
+		error = -EINVAL;
+		goto found_error;
+	}
+
+found_error:
+	return error;
+};
+
+/*
+ * tvp7002_probe - Reset a TVP7002 device
+ * @sd: ptr to v4l2_subdev struct
+ * @ctrl: ptr to i2c_device_id struct
+ *
+ * Reset the TVP7002 device
+ * Returns zero when successful or -EINVAL if register read fails.
+ */
+static int tvp7002_probe(struct i2c_client *c, const struct i2c_device_id *id)
+{
+	struct v4l2_subdev *sd;
+	struct tvp7002 *core;
+	int polarity;
+	int error;
+
+	/* Check if the adapter supports the needed features */
+	if (!i2c_check_functionality(c->adapter,
+		I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
+		return -EIO;
+
+	if (!c->dev.platform_data) {
+		v4l2_err(c, "No platform data!!\n");
+		return -ENODEV;
+	}
+
+	core = kzalloc(sizeof(struct tvp7002), GFP_KERNEL);
+
+	if (!core)
+		return -ENOMEM;
+
+	*core = tvp7002_dev;
+	sd = &core->sd;
+	core->pdata = c->dev.platform_data;
+
+	v4l2_i2c_subdev_init(sd, c, &tvp7002_ops);
+	v4l_info(c, "tvp7002 found @ 0x%02x (%s)\n",
+					c->addr << 1, c->adapter->name);
+
+	/* Initializes TVP7002 to its default values */
+	error = tvp7002_write_inittab(sd, tvp7002_init_default);
+	if (error < 0) {
+		error = -EINVAL;
+		goto found_error;
+	}
+
+	/* Set polarity information after registers have been set */
+	polarity = 0x5b | core->pdata->hs_polarity << 5
+			| core->pdata->vs_polarity << 2;
+	error = tvp7002_write(sd, TVP7002_SYNC_CTL_1, polarity);
+	if (error < 0) {
+		error = -EINVAL;
+		goto found_error;
+	}
+
+	polarity = 0x0  | core->pdata->fid_polarity << 2
+			| core->pdata->sog_polarity << 1
+			| core->pdata->clk_polarity;
+	error = tvp7002_write(sd, TVP7002_MISC_CTL_3, polarity);
+	if (error < 0) {
+		error = -EINVAL;
+		goto found_error;
+	}
+
+	if (debug > 1)
+		tvp7002_log_status(sd);
+
+found_error:
+	if (error < 0)
+		kfree(core);
+
+	return error;
+}
+
+/*
+ * tvp7002_remove - Remove TVP7002 device support
+ * @c: ptr to i2c_client struct
+ *
+ * Reset the TVP7002 device
+ * Returns zero when successful or -EINVAL if register read fails.
+ */
+static int tvp7002_remove(struct i2c_client *c)
+{
+	struct v4l2_subdev *sd = i2c_get_clientdata(c);
+
+	v4l2_dbg(1, debug, sd, "removing tvp7002 adapter"
+				"on address 0x%x\n", c->addr << 1);
+
+	v4l2_device_unregister_subdev(sd);
+	kfree(to_tvp7002(sd));
+	return 0;
+}
+
+/* I2C Device ID table */
+static const struct i2c_device_id tvp7002_id[] = {
+	{ "tvp7002", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, tvp7002_id);
+
+/* I2C driver data */
+static struct i2c_driver tvp7002_driver = {
+	.driver = {
+		.owner = THIS_MODULE,
+		.name = "tvp7002",
+	},
+	.probe = tvp7002_probe,
+	.remove = tvp7002_remove,
+	.id_table = tvp7002_id,
+};
+
+/*
+ * tvp7002_init - Initialize driver via I2C interface
+ *
+ * Register the TVP7002 driver.
+ * Returns 0 on success or < 0 on failure.
+ */
+static int __init tvp7002_init(void)
+{
+	return i2c_add_driver(&tvp7002_driver);
+}
+
+/*
+ * tvp7002_exit - Remove driver via I2C interface
+ *
+ * Unregister the TVP7002 driver.
+ * Returns 0 on success or < 0 on failure.
+ */
+static void __exit tvp7002_exit(void)
+{
+	i2c_del_driver(&tvp7002_driver);
+}
+
+module_init(tvp7002_init);
+module_exit(tvp7002_exit);