Guest User

adv7180.c

a guest
Apr 21st, 2013
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 29.67 KB | None | 0 0
  1. /*
  2.  * Copyright 2005-2011 Freescale Semiconductor, Inc. All Rights Reserved.
  3.  */
  4.  
  5. /*
  6.  * The code contained herein is licensed under the GNU General Public
  7.  * License. You may obtain a copy of the GNU General Public License
  8.  * Version 2 or later at the following locations:
  9.  *
  10.  * http://www.opensource.org/licenses/gpl-license.html
  11.  * http://www.gnu.org/copyleft/gpl.html
  12.  */
  13.  
  14. /*!
  15.  * @file adv7180.c
  16.  *
  17.  * @brief Analog Device ADV7180 video decoder functions
  18.  *
  19.  * @ingroup Camera
  20.  */
  21.  
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/ctype.h>
  26. #include <linux/types.h>
  27. #include <linux/delay.h>
  28. #include <linux/device.h>
  29. #include <linux/i2c.h>
  30. #include <linux/wait.h>
  31. #include <linux/videodev2.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/regulator/consumer.h>
  34. #include <linux/fsl_devices.h>
  35. #include <media/v4l2-chip-ident.h>
  36. #include <media/v4l2-int-device.h>
  37. #include "mxc_v4l2_capture.h"
  38.  
  39. static struct regulator *dvddio_regulator;
  40. static struct regulator *dvdd_regulator;
  41. static struct regulator *avdd_regulator;
  42. static struct regulator *pvdd_regulator;
  43. static struct mxc_tvin_platform_data *tvin_plat;
  44.  
  45. extern void gpio_sensor_active(void);
  46. extern void gpio_sensor_inactive(void);
  47.  
  48. static int adv7180_probe(struct i2c_client *adapter,
  49.              const struct i2c_device_id *id);
  50. static int adv7180_detach(struct i2c_client *client);
  51.  
  52. static const struct i2c_device_id adv7180_id[] = {
  53.     {"adv7180", 0},
  54.     {},
  55. };
  56.  
  57. MODULE_DEVICE_TABLE(i2c, adv7180_id);
  58.  
  59. static struct i2c_driver adv7180_i2c_driver = {
  60.     .driver = {
  61.            .owner = THIS_MODULE,
  62.            .name = "adv7180",
  63.            },
  64.     .probe = adv7180_probe,
  65.     .remove = adv7180_detach,
  66.     .id_table = adv7180_id,
  67. };
  68.  
  69. /*!
  70.  * Maintains the information on the current state of the sesor.
  71.  */
  72. struct sensor {
  73.     struct v4l2_int_device *v4l2_int_device;
  74.     struct i2c_client *i2c_client;
  75.     struct v4l2_pix_format pix;
  76.     struct v4l2_captureparm streamcap;
  77.     bool on;
  78.  
  79.     /* control settings */
  80.     int brightness;
  81.     int hue;
  82.     int contrast;
  83.     int saturation;
  84.     int red;
  85.     int green;
  86.     int blue;
  87.     int ae_mode;
  88.  
  89.     v4l2_std_id std_id;
  90. } adv7180_data;
  91.  
  92. /*! List of input video formats supported. The video formats is corresponding
  93.  * with v4l2 id in video_fmt_t
  94.  */
  95. typedef enum {
  96.     ADV7180_NTSC = 0,   /*!< Locked on (M) NTSC video signal. */
  97.     ADV7180_PAL,        /*!< (B, G, H, I, N)PAL video signal. */
  98.     ADV7180_NOT_LOCKED, /*!< Not locked on a signal. */
  99. } video_fmt_idx;
  100.  
  101. /*! Number of video standards supported (including 'not locked' signal). */
  102. #define ADV7180_STD_MAX     (ADV7180_PAL + 1)
  103.  
  104. /*! Video format structure. */
  105. typedef struct {
  106.     int v4l2_id;        /*!< Video for linux ID. */
  107.     char name[16];      /*!< Name (e.g., "NTSC", "PAL", etc.) */
  108.     u16 raw_width;      /*!< Raw width. */
  109.     u16 raw_height;     /*!< Raw height. */
  110.     u16 active_width;   /*!< Active width. */
  111.     u16 active_height;  /*!< Active height. */
  112. } video_fmt_t;
  113.  
  114. /*! Description of video formats supported.
  115.  *
  116.  *  PAL: raw=720x625, active=720x576.
  117.  *  NTSC: raw=720x525, active=720x480.
  118.  */
  119. static video_fmt_t video_fmts[] = {
  120.     {           /*! NTSC */
  121.      .v4l2_id = V4L2_STD_NTSC,
  122.      .name = "NTSC",
  123.      .raw_width = 720,  /* SENS_FRM_WIDTH */
  124.      .raw_height = 525, /* SENS_FRM_HEIGHT */
  125.      .active_width = 720,   /* ACT_FRM_WIDTH plus 1 */
  126.      .active_height = 480,  /* ACT_FRM_WIDTH plus 1 */
  127.      },
  128.     {           /*! (B, G, H, I, N) PAL */
  129.      .v4l2_id = V4L2_STD_PAL,
  130.      .name = "PAL",
  131.      .raw_width = 720,
  132.      .raw_height = 625,
  133.      .active_width = 720,
  134.      .active_height = 576,
  135.      },
  136.     {           /*! Unlocked standard */
  137.      .v4l2_id = V4L2_STD_ALL,
  138.      .name = "Autodetect",
  139.      .raw_width = 720,
  140.      .raw_height = 625,
  141.      .active_width = 720,
  142.      .active_height = 576,
  143.      },
  144. };
  145.  
  146. /*!* Standard index of ADV7180. */
  147. static video_fmt_idx video_idx = ADV7180_PAL;
  148.  
  149. /*! @brief This mutex is used to provide mutual exclusion.
  150.  *
  151.  *  Create a mutex that can be used to provide mutually exclusive
  152.  *  read/write access to the globally accessible data structures
  153.  *  and variables that were defined above.
  154.  */
  155. static DECLARE_MUTEX(mutex);
  156.  
  157. #define IF_NAME                    "adv7180"
  158. #define ADV7180_INPUT_CTL              0x00 /* Input Control */
  159. #define ADV7180_STATUS_1               0x10 /* Status #1 */
  160. #define ADV7180_BRIGHTNESS             0x0a /* Brightness */
  161. #define ADV7180_IDENT                  0x11 /* IDENT */
  162. #define ADV7180_VSYNC_FIELD_CTL_1      0x31 /* VSYNC Field Control #1 */
  163. #define ADV7180_MANUAL_WIN_CTL         0x3d /* Manual Window Control */
  164. #define ADV7180_SD_SATURATION_CB       0xe3 /* SD Saturation Cb */
  165. #define ADV7180_SD_SATURATION_CR       0xe4 /* SD Saturation Cr */
  166. #define ADV7180_PWR_MNG                0x0f     /* Power Management */
  167.  
  168. /* supported controls */
  169. /* This hasn't been fully implemented yet.
  170.  * This is how it should work, though. */
  171. static struct v4l2_queryctrl adv7180_qctrl[] = {
  172.     {
  173.     .id = V4L2_CID_BRIGHTNESS,
  174.     .type = V4L2_CTRL_TYPE_INTEGER,
  175.     .name = "Brightness",
  176.     .minimum = 0,       /* check this value */
  177.     .maximum = 255,     /* check this value */
  178.     .step = 1,      /* check this value */
  179.     .default_value = 127,   /* check this value */
  180.     .flags = 0,
  181.     }, {
  182.     .id = V4L2_CID_SATURATION,
  183.     .type = V4L2_CTRL_TYPE_INTEGER,
  184.     .name = "Saturation",
  185.     .minimum = 0,       /* check this value */
  186.     .maximum = 255,     /* check this value */
  187.     .step = 0x1,        /* check this value */
  188.     .default_value = 127,   /* check this value */
  189.     .flags = 0,
  190.     }
  191. };
  192.  
  193. /***********************************************************************
  194.  * I2C transfert.
  195.  ***********************************************************************/
  196.  
  197. /*! Read one register from a ADV7180 i2c slave device.
  198.  *
  199.  *  @param *reg     register in the device we wish to access.
  200.  *
  201.  *  @return            0 if success, an error code otherwise.
  202.  */
  203. static inline int adv7180_read(u8 reg)
  204. {
  205.     int val;
  206.     val = i2c_smbus_read_byte_data(adv7180_data.i2c_client, reg);
  207.     if (val < 0) {
  208.         dev_dbg(&adv7180_data.i2c_client->dev,
  209.             "%s:read reg error: reg=%2x \n", __func__, reg);
  210.         return -1;
  211.     }
  212.     return val;
  213. }
  214.  
  215. /*! Write one register of a ADV7180 i2c slave device.
  216.  *
  217.  *  @param *reg     register in the device we wish to access.
  218.  *
  219.  *  @return            0 if success, an error code otherwise.
  220.  */
  221. static int adv7180_write_reg(u8 reg, u8 val)
  222. {
  223.     if (i2c_smbus_write_byte_data(adv7180_data.i2c_client, reg, val) < 0) {
  224.         dev_dbg(&adv7180_data.i2c_client->dev,
  225.             "%s:write reg error:reg=%2x,val=%2x\n", __func__,
  226.             reg, val);
  227.         return -1;
  228.     }
  229.     return 0;
  230. }
  231.  
  232. /***********************************************************************
  233.  * mxc_v4l2_capture interface.
  234.  ***********************************************************************/
  235.  
  236. /*!
  237.  * Return attributes of current video standard.
  238.  * Since this device autodetects the current standard, this function also
  239.  * sets the values that need to be changed if the standard changes.
  240.  * There is no set std equivalent function.
  241.  *
  242.  *  @return     None.
  243.  */
  244. static void adv7180_get_std(v4l2_std_id *std)
  245. {
  246.     int tmp;
  247.     int idx;
  248.  
  249.     dev_dbg(&adv7180_data.i2c_client->dev, "In adv7180_get_std\n");
  250.  
  251.     /* Make sure power on */
  252.     if (tvin_plat->pwdn)
  253.         tvin_plat->pwdn(0);
  254.  
  255.     /* Read the AD_RESULT to get the detect output video standard */
  256.     tmp = adv7180_read(ADV7180_STATUS_1) & 0x70;
  257.  
  258.     down(&mutex);
  259.     if (tmp == 0x40) {
  260.         /* PAL */
  261.         *std = V4L2_STD_PAL;
  262.         idx = ADV7180_PAL;
  263.     } else if (tmp == 0) {
  264.         /*NTSC*/
  265.         *std = V4L2_STD_NTSC;
  266.         idx = ADV7180_NTSC;
  267.     } else {
  268.         *std = V4L2_STD_ALL;
  269.         idx = ADV7180_NOT_LOCKED;
  270.         dev_dbg(&adv7180_data.i2c_client->dev,
  271.             "Got invalid video standard! \n");
  272.     }
  273.     up(&mutex);
  274.  
  275.     /* This assumes autodetect which this device uses. */
  276.     if (*std != adv7180_data.std_id) {
  277.         video_idx = idx;
  278.         adv7180_data.std_id = *std;
  279.         adv7180_data.pix.width = video_fmts[video_idx].raw_width;
  280.         adv7180_data.pix.height = video_fmts[video_idx].raw_height;
  281.     }
  282. }
  283.  
  284. /***********************************************************************
  285.  * IOCTL Functions from v4l2_int_ioctl_desc.
  286.  ***********************************************************************/
  287.  
  288. /*!
  289.  * ioctl_g_ifparm - V4L2 sensor interface handler for vidioc_int_g_ifparm_num
  290.  * s: pointer to standard V4L2 device structure
  291.  * p: pointer to standard V4L2 vidioc_int_g_ifparm_num ioctl structure
  292.  *
  293.  * Gets slave interface parameters.
  294.  * Calculates the required xclk value to support the requested
  295.  * clock parameters in p.  This value is returned in the p
  296.  * parameter.
  297.  *
  298.  * vidioc_int_g_ifparm returns platform-specific information about the
  299.  * interface settings used by the sensor.
  300.  *
  301.  * Called on open.
  302.  */
  303. static int ioctl_g_ifparm(struct v4l2_int_device *s, struct v4l2_ifparm *p)
  304. {
  305.     dev_dbg(&adv7180_data.i2c_client->dev, "In adv7180:ioctl_g_ifparm\n");
  306.  
  307.     if (s == NULL) {
  308.         pr_err("   ERROR!! no slave device set!\n");
  309.         return -1;
  310.     }
  311.  
  312.     /* Initialize structure to 0s then set any non-0 values. */
  313.     memset(p, 0, sizeof(*p));
  314.     p->if_type = V4L2_IF_TYPE_BT656; /* This is the only possibility. */
  315.     p->u.bt656.mode = V4L2_IF_TYPE_BT656_MODE_NOBT_8BIT;
  316.     p->u.bt656.nobt_hs_inv = 1;
  317.  
  318.     /* ADV7180 has a dedicated clock so no clock settings needed. */
  319.  
  320.     return 0;
  321. }
  322.  
  323. /*!
  324.  * Sets the camera power.
  325.  *
  326.  * s  pointer to the camera device
  327.  * on if 1, power is to be turned on.  0 means power is to be turned off
  328.  *
  329.  * ioctl_s_power - V4L2 sensor interface handler for vidioc_int_s_power_num
  330.  * @s: pointer to standard V4L2 device structure
  331.  * @on: power state to which device is to be set
  332.  *
  333.  * Sets devices power state to requrested state, if possible.
  334.  * This is called on open, close, suspend and resume.
  335.  */
  336. static int ioctl_s_power(struct v4l2_int_device *s, int on)
  337. {
  338.     struct sensor *sensor = s->priv;
  339.  
  340.     dev_dbg(&adv7180_data.i2c_client->dev, "In adv7180:ioctl_s_power\n");
  341.  
  342.     if (on && !sensor->on) {
  343.         gpio_sensor_active();
  344.  
  345.         /* Make sure pwoer on */
  346.         if (tvin_plat->pwdn)
  347.             tvin_plat->pwdn(0);
  348.  
  349.         if (adv7180_write_reg(ADV7180_PWR_MNG, 0) != 0)
  350.             return -EIO;
  351.     } else if (!on && sensor->on) {
  352.         if (adv7180_write_reg(ADV7180_PWR_MNG, 0x24) != 0)
  353.             return -EIO;
  354.         gpio_sensor_inactive();
  355.     }
  356.  
  357.     sensor->on = on;
  358.  
  359.     return 0;
  360. }
  361.  
  362. /*!
  363.  * ioctl_g_parm - V4L2 sensor interface handler for VIDIOC_G_PARM ioctl
  364.  * @s: pointer to standard V4L2 device structure
  365.  * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
  366.  *
  367.  * Returns the sensor's video CAPTURE parameters.
  368.  */
  369. static int ioctl_g_parm(struct v4l2_int_device *s, struct v4l2_streamparm *a)
  370. {
  371.     struct sensor *sensor = s->priv;
  372.     struct v4l2_captureparm *cparm = &a->parm.capture;
  373.  
  374.     dev_dbg(&adv7180_data.i2c_client->dev, "In adv7180:ioctl_g_parm\n");
  375.  
  376.     switch (a->type) {
  377.     /* These are all the possible cases. */
  378.     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  379.         pr_debug("   type is V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
  380.         memset(a, 0, sizeof(*a));
  381.         a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  382.         cparm->capability = sensor->streamcap.capability;
  383.         cparm->timeperframe = sensor->streamcap.timeperframe;
  384.         cparm->capturemode = sensor->streamcap.capturemode;
  385.         break;
  386.  
  387.     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  388.     case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  389.     case V4L2_BUF_TYPE_VBI_CAPTURE:
  390.     case V4L2_BUF_TYPE_VBI_OUTPUT:
  391.     case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  392.     case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  393.         break;
  394.  
  395.     default:
  396.         pr_debug("ioctl_g_parm:type is unknown %d\n", a->type);
  397.         break;
  398.     }
  399.  
  400.     return 0;
  401. }
  402.  
  403. /*!
  404.  * ioctl_s_parm - V4L2 sensor interface handler for VIDIOC_S_PARM ioctl
  405.  * @s: pointer to standard V4L2 device structure
  406.  * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
  407.  *
  408.  * Configures the sensor to use the input parameters, if possible.  If
  409.  * not possible, reverts to the old parameters and returns the
  410.  * appropriate error code.
  411.  *
  412.  * This driver cannot change these settings.
  413.  */
  414. static int ioctl_s_parm(struct v4l2_int_device *s, struct v4l2_streamparm *a)
  415. {
  416.     dev_dbg(&adv7180_data.i2c_client->dev, "In adv7180:ioctl_s_parm\n");
  417.  
  418.     switch (a->type) {
  419.     /* These are all the possible cases. */
  420.     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  421.     case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  422.     case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  423.     case V4L2_BUF_TYPE_VBI_CAPTURE:
  424.     case V4L2_BUF_TYPE_VBI_OUTPUT:
  425.     case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  426.     case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  427.         break;
  428.  
  429.     default:
  430.         pr_debug("   type is unknown - %d\n", a->type);
  431.         break;
  432.     }
  433.  
  434.     return 0;
  435. }
  436.  
  437. /*!
  438.  * ioctl_g_fmt_cap - V4L2 sensor interface handler for ioctl_g_fmt_cap
  439.  * @s: pointer to standard V4L2 device structure
  440.  * @f: pointer to standard V4L2 v4l2_format structure
  441.  *
  442.  * Returns the sensor's current pixel format in the v4l2_format
  443.  * parameter.
  444.  */
  445. static int ioctl_g_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
  446. {
  447.     struct sensor *sensor = s->priv;
  448.  
  449.     dev_dbg(&adv7180_data.i2c_client->dev, "In adv7180:ioctl_g_fmt_cap\n");
  450.  
  451.     switch (f->type) {
  452.     case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  453.         pr_debug("   Returning size of %dx%d\n",
  454.              sensor->pix.width, sensor->pix.height);
  455.         f->fmt.pix = sensor->pix;
  456.         break;
  457.  
  458.     case V4L2_BUF_TYPE_PRIVATE: {
  459.         v4l2_std_id std;
  460.         adv7180_get_std(&std);
  461.         f->fmt.pix.pixelformat = (u32)std;
  462.         }
  463.         break;
  464.  
  465.     default:
  466.         f->fmt.pix = sensor->pix;
  467.         break;
  468.     }
  469.  
  470.     return 0;
  471. }
  472.  
  473. /*!
  474.  * ioctl_queryctrl - V4L2 sensor interface handler for VIDIOC_QUERYCTRL ioctl
  475.  * @s: pointer to standard V4L2 device structure
  476.  * @qc: standard V4L2 VIDIOC_QUERYCTRL ioctl structure
  477.  *
  478.  * If the requested control is supported, returns the control information
  479.  * from the video_control[] array.  Otherwise, returns -EINVAL if the
  480.  * control is not supported.
  481.  */
  482. static int ioctl_queryctrl(struct v4l2_int_device *s,
  483.                struct v4l2_queryctrl *qc)
  484. {
  485.     int i;
  486.  
  487.     dev_dbg(&adv7180_data.i2c_client->dev, "In adv7180:ioctl_queryctrl\n");
  488.  
  489.     for (i = 0; i < ARRAY_SIZE(adv7180_qctrl); i++)
  490.         if (qc->id && qc->id == adv7180_qctrl[i].id) {
  491.             memcpy(qc, &(adv7180_qctrl[i]),
  492.                 sizeof(*qc));
  493.             return 0;
  494.         }
  495.  
  496.     return -EINVAL;
  497. }
  498.  
  499. /*!
  500.  * ioctl_g_ctrl - V4L2 sensor interface handler for VIDIOC_G_CTRL ioctl
  501.  * @s: pointer to standard V4L2 device structure
  502.  * @vc: standard V4L2 VIDIOC_G_CTRL ioctl structure
  503.  *
  504.  * If the requested control is supported, returns the control's current
  505.  * value from the video_control[] array.  Otherwise, returns -EINVAL
  506.  * if the control is not supported.
  507.  */
  508. static int ioctl_g_ctrl(struct v4l2_int_device *s, struct v4l2_control *vc)
  509. {
  510.     int ret = 0;
  511.  
  512.     dev_dbg(&adv7180_data.i2c_client->dev, "In adv7180:ioctl_g_ctrl\n");
  513.  
  514.     /* Make sure power on */
  515.     if (tvin_plat->pwdn)
  516.         tvin_plat->pwdn(0);
  517.  
  518.     switch (vc->id) {
  519.     case V4L2_CID_BRIGHTNESS:
  520.         dev_dbg(&adv7180_data.i2c_client->dev,
  521.             "   V4L2_CID_BRIGHTNESS\n");
  522.         adv7180_data.brightness = adv7180_read(ADV7180_BRIGHTNESS);
  523.         vc->value = adv7180_data.brightness;
  524.         break;
  525.     case V4L2_CID_CONTRAST:
  526.         dev_dbg(&adv7180_data.i2c_client->dev,
  527.             "   V4L2_CID_CONTRAST\n");
  528.         vc->value = adv7180_data.contrast;
  529.         break;
  530.     case V4L2_CID_SATURATION:
  531.         dev_dbg(&adv7180_data.i2c_client->dev,
  532.             "   V4L2_CID_SATURATION\n");
  533.         adv7180_data.saturation = adv7180_read(ADV7180_SD_SATURATION_CB);
  534.         vc->value = adv7180_data.saturation;
  535.         break;
  536.     case V4L2_CID_HUE:
  537.         dev_dbg(&adv7180_data.i2c_client->dev,
  538.             "   V4L2_CID_HUE\n");
  539.         vc->value = adv7180_data.hue;
  540.         break;
  541.     case V4L2_CID_AUTO_WHITE_BALANCE:
  542.         dev_dbg(&adv7180_data.i2c_client->dev,
  543.             "   V4L2_CID_AUTO_WHITE_BALANCE\n");
  544.         break;
  545.     case V4L2_CID_DO_WHITE_BALANCE:
  546.         dev_dbg(&adv7180_data.i2c_client->dev,
  547.             "   V4L2_CID_DO_WHITE_BALANCE\n");
  548.         break;
  549.     case V4L2_CID_RED_BALANCE:
  550.         dev_dbg(&adv7180_data.i2c_client->dev,
  551.             "   V4L2_CID_RED_BALANCE\n");
  552.         vc->value = adv7180_data.red;
  553.         break;
  554.     case V4L2_CID_BLUE_BALANCE:
  555.         dev_dbg(&adv7180_data.i2c_client->dev,
  556.             "   V4L2_CID_BLUE_BALANCE\n");
  557.         vc->value = adv7180_data.blue;
  558.         break;
  559.     case V4L2_CID_GAMMA:
  560.         dev_dbg(&adv7180_data.i2c_client->dev,
  561.             "   V4L2_CID_GAMMA\n");
  562.         break;
  563.     case V4L2_CID_EXPOSURE:
  564.         dev_dbg(&adv7180_data.i2c_client->dev,
  565.             "   V4L2_CID_EXPOSURE\n");
  566.         vc->value = adv7180_data.ae_mode;
  567.         break;
  568.     case V4L2_CID_AUTOGAIN:
  569.         dev_dbg(&adv7180_data.i2c_client->dev,
  570.             "   V4L2_CID_AUTOGAIN\n");
  571.         break;
  572.     case V4L2_CID_GAIN:
  573.         dev_dbg(&adv7180_data.i2c_client->dev,
  574.             "   V4L2_CID_GAIN\n");
  575.         break;
  576.     case V4L2_CID_HFLIP:
  577.         dev_dbg(&adv7180_data.i2c_client->dev,
  578.             "   V4L2_CID_HFLIP\n");
  579.         break;
  580.     case V4L2_CID_VFLIP:
  581.         dev_dbg(&adv7180_data.i2c_client->dev,
  582.             "   V4L2_CID_VFLIP\n");
  583.         break;
  584.     default:
  585.         dev_dbg(&adv7180_data.i2c_client->dev,
  586.             "   Default case\n");
  587.         vc->value = 0;
  588.         ret = -EPERM;
  589.         break;
  590.     }
  591.  
  592.     return ret;
  593. }
  594.  
  595. /*!
  596.  * ioctl_s_ctrl - V4L2 sensor interface handler for VIDIOC_S_CTRL ioctl
  597.  * @s: pointer to standard V4L2 device structure
  598.  * @vc: standard V4L2 VIDIOC_S_CTRL ioctl structure
  599.  *
  600.  * If the requested control is supported, sets the control's current
  601.  * value in HW (and updates the video_control[] array).  Otherwise,
  602.  * returns -EINVAL if the control is not supported.
  603.  */
  604. static int ioctl_s_ctrl(struct v4l2_int_device *s, struct v4l2_control *vc)
  605. {
  606.     int retval = 0;
  607.     u8 tmp;
  608.  
  609.     dev_dbg(&adv7180_data.i2c_client->dev, "In adv7180:ioctl_s_ctrl\n");
  610.  
  611.     /* Make sure power on */
  612.     if (tvin_plat->pwdn)
  613.         tvin_plat->pwdn(0);
  614.  
  615.     switch (vc->id) {
  616.     case V4L2_CID_BRIGHTNESS:
  617.         dev_dbg(&adv7180_data.i2c_client->dev,
  618.             "   V4L2_CID_BRIGHTNESS\n");
  619.         tmp = vc->value;
  620.         adv7180_write_reg(ADV7180_BRIGHTNESS, tmp);
  621.         adv7180_data.brightness = vc->value;
  622.         break;
  623.     case V4L2_CID_CONTRAST:
  624.         dev_dbg(&adv7180_data.i2c_client->dev,
  625.             "   V4L2_CID_CONTRAST\n");
  626.         break;
  627.     case V4L2_CID_SATURATION:
  628.         dev_dbg(&adv7180_data.i2c_client->dev,
  629.             "   V4L2_CID_SATURATION\n");
  630.         tmp = vc->value;
  631.         adv7180_write_reg(ADV7180_SD_SATURATION_CB, tmp);
  632.         adv7180_write_reg(ADV7180_SD_SATURATION_CR, tmp);
  633.         adv7180_data.saturation = vc->value;
  634.         break;
  635.     case V4L2_CID_HUE:
  636.         dev_dbg(&adv7180_data.i2c_client->dev,
  637.             "   V4L2_CID_HUE\n");
  638.         break;
  639.     case V4L2_CID_AUTO_WHITE_BALANCE:
  640.         dev_dbg(&adv7180_data.i2c_client->dev,
  641.             "   V4L2_CID_AUTO_WHITE_BALANCE\n");
  642.         break;
  643.     case V4L2_CID_DO_WHITE_BALANCE:
  644.         dev_dbg(&adv7180_data.i2c_client->dev,
  645.             "   V4L2_CID_DO_WHITE_BALANCE\n");
  646.         break;
  647.     case V4L2_CID_RED_BALANCE:
  648.         dev_dbg(&adv7180_data.i2c_client->dev,
  649.             "   V4L2_CID_RED_BALANCE\n");
  650.         break;
  651.     case V4L2_CID_BLUE_BALANCE:
  652.         dev_dbg(&adv7180_data.i2c_client->dev,
  653.             "   V4L2_CID_BLUE_BALANCE\n");
  654.         break;
  655.     case V4L2_CID_GAMMA:
  656.         dev_dbg(&adv7180_data.i2c_client->dev,
  657.             "   V4L2_CID_GAMMA\n");
  658.         break;
  659.     case V4L2_CID_EXPOSURE:
  660.         dev_dbg(&adv7180_data.i2c_client->dev,
  661.             "   V4L2_CID_EXPOSURE\n");
  662.         break;
  663.     case V4L2_CID_AUTOGAIN:
  664.         dev_dbg(&adv7180_data.i2c_client->dev,
  665.             "   V4L2_CID_AUTOGAIN\n");
  666.         break;
  667.     case V4L2_CID_GAIN:
  668.         dev_dbg(&adv7180_data.i2c_client->dev,
  669.             "   V4L2_CID_GAIN\n");
  670.         break;
  671.     case V4L2_CID_HFLIP:
  672.         dev_dbg(&adv7180_data.i2c_client->dev,
  673.             "   V4L2_CID_HFLIP\n");
  674.         break;
  675.     case V4L2_CID_VFLIP:
  676.         dev_dbg(&adv7180_data.i2c_client->dev,
  677.             "   V4L2_CID_VFLIP\n");
  678.         break;
  679.     default:
  680.         dev_dbg(&adv7180_data.i2c_client->dev,
  681.             "   Default case\n");
  682.         retval = -EPERM;
  683.         break;
  684.     }
  685.  
  686.     return retval;
  687. }
  688.  
  689. /*!
  690.  * ioctl_g_input - V4L2 sensor interface handler for VIDIOC_G_INPUT ioctl
  691.  * @s: pointer to standard V4L2 device structure
  692.  * @in: number of input channel in the lowest tetrad
  693.  */
  694. static int ioctl_g_input(struct v4l2_int_device *s, int *in)
  695. {
  696.     int retval = 0;
  697.  
  698.     dev_dbg(&adv7180_data.i2c_client->dev, "In adv7180:ioctl_g_input\n");
  699.        
  700.     /* Make sure power on */
  701.     if (tvin_plat->pwdn)
  702.         tvin_plat->pwdn(0);
  703.    
  704.     /* Get CVBS input AIN number */
  705.     *in = (adv7180_read(ADV7180_INPUT_CTL)&0x0F) + 1;
  706.  
  707.     return retval;
  708. }
  709.  
  710. /*!
  711.  * ioctl_s_input - V4L2 sensor interface handler for VIDIOC_S_INPUT ioctl
  712.  * @s: pointer to standard V4L2 device structure
  713.  * @in: number of input channel in the lowest tetrad
  714.  */
  715. static int ioctl_s_input(struct v4l2_int_device *s, int *in)
  716. {
  717.     int in_channel = (*in - 1);
  718.     int retval = 0;
  719.  
  720.     dev_dbg(&adv7180_data.i2c_client->dev, "In adv7180:ioctl_s_input\n");
  721.    
  722.     /* Only AIN1-AIN6 are valid CVBS inputs (for 64-pin package) */
  723.     if(in_channel < 1 && in_channel > 6)
  724.         return -EINVAL;
  725.    
  726.     /* Make sure power on */
  727.     if (tvin_plat->pwdn)
  728.         tvin_plat->pwdn(0);
  729.    
  730.     /* Set CVBS input on AIN */
  731.     adv7180_write_reg(ADV7180_INPUT_CTL, in_channel);
  732.     dev_dbg(&adv7180_data.i2c_client->dev, "Set input to AIN: %02x\n", adv7180_read(ADV7180_INPUT_CTL));
  733.  
  734.     return retval;
  735. }
  736.  
  737. /*!
  738.  * ioctl_g_chip_ident - V4L2 sensor interface handler for
  739.  *          VIDIOC_DBG_G_CHIP_IDENT ioctl
  740.  * @s: pointer to standard V4L2 device structure
  741.  * @id: pointer to int
  742.  *
  743.  * Return 0.
  744.  */
  745. static int ioctl_g_chip_ident(struct v4l2_int_device *s, int *id)
  746. {
  747.     ((struct v4l2_dbg_chip_ident *)id)->match.type =
  748.                     V4L2_CHIP_MATCH_I2C_DRIVER;
  749.     strcpy(((struct v4l2_dbg_chip_ident *)id)->match.name,
  750.                         "adv7180_decoder");
  751.     ((struct v4l2_dbg_chip_ident *)id)->ident = V4L2_IDENT_ADV7180;
  752.  
  753.     return 0;
  754. }
  755.  
  756. /*!
  757.  * ioctl_init - V4L2 sensor interface handler for VIDIOC_INT_INIT
  758.  * @s: pointer to standard V4L2 device structure
  759.  */
  760. static int ioctl_init(struct v4l2_int_device *s)
  761. {
  762.     dev_dbg(&adv7180_data.i2c_client->dev, "In adv7180:ioctl_init\n");
  763.     return 0;
  764. }
  765.  
  766. /*!
  767.  * ioctl_dev_init - V4L2 sensor interface handler for vidioc_int_dev_init_num
  768.  * @s: pointer to standard V4L2 device structure
  769.  *
  770.  * Initialise the device when slave attaches to the master.
  771.  */
  772. static int ioctl_dev_init(struct v4l2_int_device *s)
  773. {
  774.     dev_dbg(&adv7180_data.i2c_client->dev, "In adv7180:ioctl_dev_init\n");
  775.     return 0;
  776. }
  777.  
  778. /*!
  779.  * This structure defines all the ioctls for this module.
  780.  */
  781. static struct v4l2_int_ioctl_desc adv7180_ioctl_desc[] = {
  782.  
  783.     {vidioc_int_dev_init_num, (v4l2_int_ioctl_func*)ioctl_dev_init},
  784.  
  785.     /*!
  786.      * Delinitialise the dev. at slave detach.
  787.      * The complement of ioctl_dev_init.
  788.      */
  789. /*  {vidioc_int_dev_exit_num, (v4l2_int_ioctl_func *)ioctl_dev_exit}, */
  790.  
  791.     {vidioc_int_s_power_num, (v4l2_int_ioctl_func*)ioctl_s_power},
  792.     {vidioc_int_g_ifparm_num, (v4l2_int_ioctl_func*)ioctl_g_ifparm},
  793. /*  {vidioc_int_g_needs_reset_num,
  794.                 (v4l2_int_ioctl_func *)ioctl_g_needs_reset}, */
  795. /*  {vidioc_int_reset_num, (v4l2_int_ioctl_func *)ioctl_reset}, */
  796.     {vidioc_int_init_num, (v4l2_int_ioctl_func*)ioctl_init},
  797.  
  798.     /*!
  799.      * VIDIOC_ENUM_FMT ioctl for the CAPTURE buffer type.
  800.      */
  801. /*  {vidioc_int_enum_fmt_cap_num,
  802.                 (v4l2_int_ioctl_func *)ioctl_enum_fmt_cap}, */
  803.  
  804.     /*!
  805.      * VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type.
  806.      * This ioctl is used to negotiate the image capture size and
  807.      * pixel format without actually making it take effect.
  808.      */
  809. /*  {vidioc_int_try_fmt_cap_num,
  810.                 (v4l2_int_ioctl_func *)ioctl_try_fmt_cap}, */
  811.  
  812.     {vidioc_int_g_fmt_cap_num, (v4l2_int_ioctl_func*)ioctl_g_fmt_cap},
  813.  
  814.     /*!
  815.      * If the requested format is supported, configures the HW to use that
  816.      * format, returns error code if format not supported or HW can't be
  817.      * correctly configured.
  818.      */
  819. /*  {vidioc_int_s_fmt_cap_num, (v4l2_int_ioctl_func *)ioctl_s_fmt_cap}, */
  820.  
  821.     {vidioc_int_g_parm_num, (v4l2_int_ioctl_func*)ioctl_g_parm},
  822.     {vidioc_int_s_parm_num, (v4l2_int_ioctl_func*)ioctl_s_parm},
  823.     {vidioc_int_queryctrl_num, (v4l2_int_ioctl_func*)ioctl_queryctrl},
  824.     {vidioc_int_g_ctrl_num, (v4l2_int_ioctl_func*)ioctl_g_ctrl},
  825.     {vidioc_int_s_ctrl_num, (v4l2_int_ioctl_func*)ioctl_s_ctrl},
  826.     {vidioc_int_g_input_num, (v4l2_int_ioctl_func*)ioctl_g_input},
  827.     {vidioc_int_s_input_num, (v4l2_int_ioctl_func*)ioctl_s_input},
  828.     {vidioc_int_g_chip_ident_num,
  829.                 (v4l2_int_ioctl_func *)ioctl_g_chip_ident},
  830. };
  831.  
  832. static struct v4l2_int_slave adv7180_slave = {
  833.     .ioctls = adv7180_ioctl_desc,
  834.     .num_ioctls = ARRAY_SIZE(adv7180_ioctl_desc),
  835. };
  836.  
  837. static struct v4l2_int_device adv7180_int_device = {
  838.     .module = THIS_MODULE,
  839.     .name = "adv7180",
  840.     .type = v4l2_int_type_slave,
  841.     .u = {
  842.         .slave = &adv7180_slave,
  843.     },
  844. };
  845.  
  846.  
  847. /***********************************************************************
  848.  * I2C client and driver.
  849.  ***********************************************************************/
  850.  
  851. /*! ADV7180 Reset function.
  852.  *
  853.  *  @return     None.
  854.  */
  855. static void adv7180_hard_reset(bool cvbs)
  856. {
  857.     dev_dbg(&adv7180_data.i2c_client->dev,
  858.         "In adv7180:adv7180_hard_reset\n");
  859.  
  860.     if (cvbs) {
  861.         /* Set CVBS input on AIN1 */
  862.         adv7180_write_reg(ADV7180_INPUT_CTL, 0x00);
  863.     } else {
  864.         /*
  865.          * Set YPbPr input on AIN1,4,5 and normal
  866.          * operations(autodection of all stds).
  867.          */
  868.         adv7180_write_reg(ADV7180_INPUT_CTL, 0x09);
  869.     }
  870.  
  871.     /* Datasheet recommends */
  872.     adv7180_write_reg(ADV7180_VSYNC_FIELD_CTL_1, 0x02);
  873.     adv7180_write_reg(ADV7180_MANUAL_WIN_CTL, 0xa2);
  874. }
  875.  
  876. /*! ADV7180 I2C attach function.
  877.  *
  878.  *  @param *adapter struct i2c_adapter *.
  879.  *
  880.  *  @return     Error code indicating success or failure.
  881.  */
  882.  
  883. /*!
  884.  * ADV7180 I2C probe function.
  885.  * Function set in i2c_driver struct.
  886.  * Called by insmod.
  887.  *
  888.  *  @param *adapter I2C adapter descriptor.
  889.  *
  890.  *  @return     Error code indicating success or failure.
  891.  */
  892. static int adv7180_probe(struct i2c_client *client,
  893.              const struct i2c_device_id *id)
  894. {
  895.     int rev_id;
  896.     int ret = 0;
  897.     tvin_plat = client->dev.platform_data;
  898.  
  899.     dev_dbg(&adv7180_data.i2c_client->dev, "In adv7180_probe\n");
  900.  
  901.     if (tvin_plat->dvddio_reg) {
  902.         dvddio_regulator =
  903.             regulator_get(&client->dev, tvin_plat->dvddio_reg);
  904.         if (!IS_ERR_VALUE((unsigned long)dvddio_regulator)) {
  905.             regulator_set_voltage(dvddio_regulator, 3300000, 3300000);
  906.             if (regulator_enable(dvddio_regulator) != 0)
  907.                 return -ENODEV;
  908.         }
  909.     }
  910.  
  911.     if (tvin_plat->dvdd_reg) {
  912.         dvdd_regulator =
  913.             regulator_get(&client->dev, tvin_plat->dvdd_reg);
  914.         if (!IS_ERR_VALUE((unsigned long)dvdd_regulator)) {
  915.             regulator_set_voltage(dvdd_regulator, 1800000, 1800000);
  916.             if (regulator_enable(dvdd_regulator) != 0)
  917.                 return -ENODEV;
  918.         }
  919.     }
  920.  
  921.     if (tvin_plat->avdd_reg) {
  922.         avdd_regulator =
  923.             regulator_get(&client->dev, tvin_plat->avdd_reg);
  924.         if (!IS_ERR_VALUE((unsigned long)avdd_regulator)) {
  925.             regulator_set_voltage(avdd_regulator, 1800000, 1800000);
  926.             if (regulator_enable(avdd_regulator) != 0)
  927.                 return -ENODEV;
  928.         }
  929.     }
  930.  
  931.     if (tvin_plat->pvdd_reg) {
  932.         pvdd_regulator =
  933.             regulator_get(&client->dev, tvin_plat->pvdd_reg);
  934.         if (!IS_ERR_VALUE((unsigned long)pvdd_regulator)) {
  935.             regulator_set_voltage(pvdd_regulator, 1800000, 1800000);
  936.             if (regulator_enable(pvdd_regulator) != 0)
  937.                 return -ENODEV;
  938.         }
  939.     }
  940.  
  941.  
  942.     if (tvin_plat->reset)
  943.         tvin_plat->reset();
  944.  
  945.     if (tvin_plat->pwdn)
  946.         tvin_plat->pwdn(0);
  947.  
  948.     msleep(1);
  949.  
  950.     /* Set initial values for the sensor struct. */
  951.     memset(&adv7180_data, 0, sizeof(adv7180_data));
  952.     adv7180_data.i2c_client = client;
  953.     adv7180_data.streamcap.timeperframe.denominator = 30;
  954.     adv7180_data.streamcap.timeperframe.numerator = 1;
  955.     adv7180_data.std_id = V4L2_STD_ALL;
  956.     video_idx = ADV7180_NOT_LOCKED;
  957.     adv7180_data.pix.width = video_fmts[video_idx].raw_width;
  958.     adv7180_data.pix.height = video_fmts[video_idx].raw_height;
  959.     adv7180_data.pix.pixelformat = V4L2_PIX_FMT_UYVY;  /* YUV422 */
  960.     adv7180_data.pix.priv = 1;  /* 1 is used to indicate TV in */
  961.     adv7180_data.on = true;
  962.  
  963.     gpio_sensor_active();
  964.  
  965.     dev_dbg(&adv7180_data.i2c_client->dev,
  966.         "%s:adv7180 probe i2c address is 0x%02X \n",
  967.         __func__, adv7180_data.i2c_client->addr);
  968.  
  969.     /*! Read the revision ID of the tvin chip */
  970.     rev_id = adv7180_read(ADV7180_IDENT);
  971.     dev_dbg(&adv7180_data.i2c_client->dev,
  972.         "%s:Analog Device adv7%2X0 detected! \n", __func__,
  973.         rev_id);
  974.  
  975.     /*! ADV7180 initialization. */
  976.     adv7180_hard_reset(tvin_plat->cvbs);
  977.  
  978.     pr_debug("   type is %d (expect %d)\n",
  979.          adv7180_int_device.type, v4l2_int_type_slave);
  980.     pr_debug("   num ioctls is %d\n",
  981.          adv7180_int_device.u.slave->num_ioctls);
  982.  
  983.     /* This function attaches this structure to the /dev/video0 device.
  984.      * The pointer in priv points to the mt9v111_data structure here.*/
  985.     adv7180_int_device.priv = &adv7180_data;
  986.     ret = v4l2_int_device_register(&adv7180_int_device);
  987.  
  988.     return ret;
  989. }
  990.  
  991. /*!
  992.  * ADV7180 I2C detach function.
  993.  * Called on rmmod.
  994.  *
  995.  *  @param *client  struct i2c_client*.
  996.  *
  997.  *  @return     Error code indicating success or failure.
  998.  */
  999. static int adv7180_detach(struct i2c_client *client)
  1000. {
  1001.     struct mxc_tvin_platform_data *plat_data = client->dev.platform_data;
  1002.  
  1003.     dev_dbg(&adv7180_data.i2c_client->dev,
  1004.         "%s:Removing %s video decoder @ 0x%02X from adapter %s \n",
  1005.         __func__, IF_NAME, client->addr << 1, client->adapter->name);
  1006.  
  1007.     if (plat_data->pwdn)
  1008.         plat_data->pwdn(1);
  1009.  
  1010.     if (dvddio_regulator) {
  1011.         regulator_disable(dvddio_regulator);
  1012.         regulator_put(dvddio_regulator);
  1013.     }
  1014.  
  1015.     if (dvdd_regulator) {
  1016.         regulator_disable(dvdd_regulator);
  1017.         regulator_put(dvdd_regulator);
  1018.     }
  1019.  
  1020.     if (avdd_regulator) {
  1021.         regulator_disable(avdd_regulator);
  1022.         regulator_put(avdd_regulator);
  1023.     }
  1024.  
  1025.     if (pvdd_regulator) {
  1026.         regulator_disable(pvdd_regulator);
  1027.         regulator_put(pvdd_regulator);
  1028.     }
  1029.  
  1030.     v4l2_int_device_unregister(&adv7180_int_device);
  1031.  
  1032.     return 0;
  1033. }
  1034.  
  1035. /*!
  1036.  * ADV7180 init function.
  1037.  * Called on insmod.
  1038.  *
  1039.  * @return    Error code indicating success or failure.
  1040.  */
  1041. static __init int adv7180_init(void)
  1042. {
  1043.     u8 err = 0;
  1044.  
  1045.     dev_dbg(&adv7180_data.i2c_client->dev, "In adv7180_init\n");
  1046.  
  1047.     /* Tells the i2c driver what functions to call for this driver. */
  1048.     err = i2c_add_driver(&adv7180_i2c_driver);
  1049.     if (err != 0)
  1050.         pr_err("%s:driver registration failed, error=%d \n",
  1051.             __func__, err);
  1052.  
  1053.     return err;
  1054. }
  1055.  
  1056. /*!
  1057.  * ADV7180 cleanup function.
  1058.  * Called on rmmod.
  1059.  *
  1060.  * @return   Error code indicating success or failure.
  1061.  */
  1062. static void __exit adv7180_clean(void)
  1063. {
  1064.     dev_dbg(&adv7180_data.i2c_client->dev, "In adv7180_clean\n");
  1065.     i2c_del_driver(&adv7180_i2c_driver);
  1066.     gpio_sensor_inactive();
  1067. }
  1068.  
  1069. module_init(adv7180_init);
  1070. module_exit(adv7180_clean);
  1071.  
  1072. MODULE_AUTHOR("Freescale Semiconductor");
  1073. MODULE_DESCRIPTION("Anolog Device ADV7180 video decoder driver");
  1074. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment