[media] adv7180.c: convert to v4l2 control framework
authorFederico Vaga <federico.vaga@gmail.com>
Wed, 11 Jul 2012 14:29:33 +0000 (11:29 -0300)
committerMauro Carvalho Chehab <mchehab@redhat.com>
Mon, 30 Jul 2012 23:22:34 +0000 (20:22 -0300)
[mchehab@redhat.com: fix checkpatch.pl ERROR:
 Macros with complex values should be enclosed in parenthesis]

Signed-off-by: Federico Vaga <federico.vaga@gmail.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
drivers/media/video/adv7180.c

index 174bffacf1173fd41136adc99147de807a2cb8b3..45ecf8db1eaecadbc6debf310d0d5313dd2bc185 100644 (file)
 #include <media/v4l2-ioctl.h>
 #include <linux/videodev2.h>
 #include <media/v4l2-device.h>
+#include <media/v4l2-ctrls.h>
 #include <media/v4l2-chip-ident.h>
 #include <linux/mutex.h>
 
-#define DRIVER_NAME "adv7180"
-
 #define ADV7180_INPUT_CONTROL_REG                      0x00
 #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM   0x00
 #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM_PED 0x10
 
 #define ADV7180_AUTODETECT_ENABLE_REG                  0x07
 #define ADV7180_AUTODETECT_DEFAULT                     0x7f
-
+/* Contrast */
 #define ADV7180_CON_REG                0x08    /*Unsigned */
-#define CON_REG_MIN            0
-#define CON_REG_DEF            128
-#define CON_REG_MAX            255
-
+#define ADV7180_CON_MIN                0
+#define ADV7180_CON_DEF                128
+#define ADV7180_CON_MAX                255
+/* Brightness*/
 #define ADV7180_BRI_REG                0x0a    /*Signed */
-#define BRI_REG_MIN            -128
-#define BRI_REG_DEF            0
-#define BRI_REG_MAX            127
-
+#define ADV7180_BRI_MIN                -128
+#define ADV7180_BRI_DEF                0
+#define ADV7180_BRI_MAX                127
+/* Hue */
 #define ADV7180_HUE_REG                0x0b    /*Signed, inverted */
-#define HUE_REG_MIN            -127
-#define HUE_REG_DEF            0
-#define HUE_REG_MAX            128
+#define ADV7180_HUE_MIN                -127
+#define ADV7180_HUE_DEF                0
+#define ADV7180_HUE_MAX                128
 
 #define ADV7180_ADI_CTRL_REG                           0x0e
 #define ADV7180_ADI_CTRL_IRQ_SPACE                     0x20
 #define ADV7180_ICONF1_ACTIVE_LOW      0x01
 #define ADV7180_ICONF1_PSYNC_ONLY      0x10
 #define ADV7180_ICONF1_ACTIVE_TO_CLR   0xC0
-
+/* Saturation */
 #define ADV7180_SD_SAT_CB_REG  0xe3    /*Unsigned */
 #define ADV7180_SD_SAT_CR_REG  0xe4    /*Unsigned */
-#define SAT_REG_MIN            0
-#define SAT_REG_DEF            128
-#define SAT_REG_MAX            255
+#define ADV7180_SAT_MIN                0
+#define ADV7180_SAT_DEF                128
+#define ADV7180_SAT_MAX                255
 
 #define ADV7180_IRQ1_LOCK      0x01
 #define ADV7180_IRQ1_UNLOCK    0x02
 #define ADV7180_NTSC_V_BIT_END_MANUAL_NVEND    0x4F
 
 struct adv7180_state {
+       struct v4l2_ctrl_handler ctrl_hdl;
        struct v4l2_subdev      sd;
        struct work_struct      work;
        struct mutex            mutex; /* mutual excl. when accessing chip */
        int                     irq;
        v4l2_std_id             curr_norm;
        bool                    autodetect;
-       s8                      brightness;
-       s16                     hue;
-       u8                      contrast;
-       u8                      saturation;
        u8                      input;
 };
+#define to_adv7180_sd(_ctrl) (&container_of(_ctrl->handler,            \
+                                           struct adv7180_state,       \
+                                           ctrl_hdl)->sd)
 
 static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
 {
@@ -237,7 +236,7 @@ static int adv7180_s_routing(struct v4l2_subdev *sd, u32 input,
        if (ret)
                return ret;
 
-       /*We cannot discriminate between LQFP and 40-pin LFCSP, so accept
+       /* We cannot discriminate between LQFP and 40-pin LFCSP, so accept
         * all inputs and let the card driver take care of validation
         */
        if ((input & ADV7180_INPUT_CONTROL_INSEL_MASK) != input)
@@ -316,117 +315,39 @@ out:
        return ret;
 }
 
-static int adv7180_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
-{
-       switch (qc->id) {
-       case V4L2_CID_BRIGHTNESS:
-               return v4l2_ctrl_query_fill(qc, BRI_REG_MIN, BRI_REG_MAX,
-                                           1, BRI_REG_DEF);
-       case V4L2_CID_HUE:
-               return v4l2_ctrl_query_fill(qc, HUE_REG_MIN, HUE_REG_MAX,
-                                           1, HUE_REG_DEF);
-       case V4L2_CID_CONTRAST:
-               return v4l2_ctrl_query_fill(qc, CON_REG_MIN, CON_REG_MAX,
-                                           1, CON_REG_DEF);
-       case V4L2_CID_SATURATION:
-               return v4l2_ctrl_query_fill(qc, SAT_REG_MIN, SAT_REG_MAX,
-                                           1, SAT_REG_DEF);
-       default:
-               break;
-       }
-
-       return -EINVAL;
-}
-
-static int adv7180_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
-{
-       struct adv7180_state *state = to_state(sd);
-       int ret = mutex_lock_interruptible(&state->mutex);
-       if (ret)
-               return ret;
-
-       switch (ctrl->id) {
-       case V4L2_CID_BRIGHTNESS:
-               ctrl->value = state->brightness;
-               break;
-       case V4L2_CID_HUE:
-               ctrl->value = state->hue;
-               break;
-       case V4L2_CID_CONTRAST:
-               ctrl->value = state->contrast;
-               break;
-       case V4L2_CID_SATURATION:
-               ctrl->value = state->saturation;
-               break;
-       default:
-               ret = -EINVAL;
-       }
-
-       mutex_unlock(&state->mutex);
-       return ret;
-}
-
-static int adv7180_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
+static int adv7180_s_ctrl(struct v4l2_ctrl *ctrl)
 {
+       struct v4l2_subdev *sd = to_adv7180_sd(ctrl);
        struct adv7180_state *state = to_state(sd);
        struct i2c_client *client = v4l2_get_subdevdata(sd);
        int ret = mutex_lock_interruptible(&state->mutex);
+       int val;
+
        if (ret)
                return ret;
-
+       val = ctrl->val;
        switch (ctrl->id) {
        case V4L2_CID_BRIGHTNESS:
-               if ((ctrl->value > BRI_REG_MAX)
-                   || (ctrl->value < BRI_REG_MIN)) {
-                       ret = -ERANGE;
-                       break;
-               }
-               state->brightness = ctrl->value;
-               ret = i2c_smbus_write_byte_data(client,
-                                               ADV7180_BRI_REG,
-                                               state->brightness);
+               ret = i2c_smbus_write_byte_data(client, ADV7180_BRI_REG, val);
                break;
        case V4L2_CID_HUE:
-               if ((ctrl->value > HUE_REG_MAX)
-                   || (ctrl->value < HUE_REG_MIN)) {
-                       ret = -ERANGE;
-                       break;
-               }
-               state->hue = ctrl->value;
                /*Hue is inverted according to HSL chart */
-               ret = i2c_smbus_write_byte_data(client,
-                                               ADV7180_HUE_REG, -state->hue);
+               ret = i2c_smbus_write_byte_data(client, ADV7180_HUE_REG, -val);
                break;
        case V4L2_CID_CONTRAST:
-               if ((ctrl->value > CON_REG_MAX)
-                   || (ctrl->value < CON_REG_MIN)) {
-                       ret = -ERANGE;
-                       break;
-               }
-               state->contrast = ctrl->value;
-               ret = i2c_smbus_write_byte_data(client,
-                                               ADV7180_CON_REG,
-                                               state->contrast);
+               ret = i2c_smbus_write_byte_data(client, ADV7180_CON_REG, val);
                break;
        case V4L2_CID_SATURATION:
-               if ((ctrl->value > SAT_REG_MAX)
-                   || (ctrl->value < SAT_REG_MIN)) {
-                       ret = -ERANGE;
-                       break;
-               }
                /*
                 *This could be V4L2_CID_BLUE_BALANCE/V4L2_CID_RED_BALANCE
                 *Let's not confuse the user, everybody understands saturation
                 */
-               state->saturation = ctrl->value;
-               ret = i2c_smbus_write_byte_data(client,
-                                               ADV7180_SD_SAT_CB_REG,
-                                               state->saturation);
+               ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CB_REG,
+                                               val);
                if (ret < 0)
                        break;
-               ret = i2c_smbus_write_byte_data(client,
-                                               ADV7180_SD_SAT_CR_REG,
-                                               state->saturation);
+               ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CR_REG,
+                                               val);
                break;
        default:
                ret = -EINVAL;
@@ -436,6 +357,42 @@ static int adv7180_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
        return ret;
 }
 
+static const struct v4l2_ctrl_ops adv7180_ctrl_ops = {
+       .s_ctrl = adv7180_s_ctrl,
+};
+
+static int adv7180_init_controls(struct adv7180_state *state)
+{
+       v4l2_ctrl_handler_init(&state->ctrl_hdl, 4);
+
+       v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
+                         V4L2_CID_BRIGHTNESS, ADV7180_BRI_MIN,
+                         ADV7180_BRI_MAX, 1, ADV7180_BRI_DEF);
+       v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
+                         V4L2_CID_CONTRAST, ADV7180_CON_MIN,
+                         ADV7180_CON_MAX, 1, ADV7180_CON_DEF);
+       v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
+                         V4L2_CID_SATURATION, ADV7180_SAT_MIN,
+                         ADV7180_SAT_MAX, 1, ADV7180_SAT_DEF);
+       v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
+                         V4L2_CID_HUE, ADV7180_HUE_MIN,
+                         ADV7180_HUE_MAX, 1, ADV7180_HUE_DEF);
+       state->sd.ctrl_handler = &state->ctrl_hdl;
+       if (state->ctrl_hdl.error) {
+               int err = state->ctrl_hdl.error;
+
+               v4l2_ctrl_handler_free(&state->ctrl_hdl);
+               return err;
+       }
+       v4l2_ctrl_handler_setup(&state->ctrl_hdl);
+
+       return 0;
+}
+static void adv7180_exit_controls(struct adv7180_state *state)
+{
+       v4l2_ctrl_handler_free(&state->ctrl_hdl);
+}
+
 static const struct v4l2_subdev_video_ops adv7180_video_ops = {
        .querystd = adv7180_querystd,
        .g_input_status = adv7180_g_input_status,
@@ -445,9 +402,9 @@ static const struct v4l2_subdev_video_ops adv7180_video_ops = {
 static const struct v4l2_subdev_core_ops adv7180_core_ops = {
        .g_chip_ident = adv7180_g_chip_ident,
        .s_std = adv7180_s_std,
-       .queryctrl = adv7180_queryctrl,
-       .g_ctrl = adv7180_g_ctrl,
-       .s_ctrl = adv7180_s_ctrl,
+       .queryctrl = v4l2_subdev_queryctrl,
+       .g_ctrl = v4l2_subdev_g_ctrl,
+       .s_ctrl = v4l2_subdev_s_ctrl,
 };
 
 static const struct v4l2_subdev_ops adv7180_ops = {
@@ -539,7 +496,7 @@ static int init_device(struct i2c_client *client, struct adv7180_state *state)
 
        /* register for interrupts */
        if (state->irq > 0) {
-               ret = request_irq(state->irq, adv7180_irq, 0, DRIVER_NAME,
+               ret = request_irq(state->irq, adv7180_irq, 0, KBUILD_MODNAME,
                                  state);
                if (ret)
                        return ret;
@@ -580,31 +537,6 @@ static int init_device(struct i2c_client *client, struct adv7180_state *state)
                        return ret;
        }
 
-       /*Set default value for controls */
-       ret = i2c_smbus_write_byte_data(client, ADV7180_BRI_REG,
-                                       state->brightness);
-       if (ret < 0)
-               return ret;
-
-       ret = i2c_smbus_write_byte_data(client, ADV7180_HUE_REG, state->hue);
-       if (ret < 0)
-               return ret;
-
-       ret = i2c_smbus_write_byte_data(client, ADV7180_CON_REG,
-                                       state->contrast);
-       if (ret < 0)
-               return ret;
-
-       ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CB_REG,
-                                       state->saturation);
-       if (ret < 0)
-               return ret;
-
-       ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CR_REG,
-                                       state->saturation);
-       if (ret < 0)
-               return ret;
-
        return 0;
 }
 
@@ -632,25 +564,26 @@ static __devinit int adv7180_probe(struct i2c_client *client,
        INIT_WORK(&state->work, adv7180_work);
        mutex_init(&state->mutex);
        state->autodetect = true;
-       state->brightness = BRI_REG_DEF;
-       state->hue = HUE_REG_DEF;
-       state->contrast = CON_REG_DEF;
-       state->saturation = SAT_REG_DEF;
        state->input = 0;
        sd = &state->sd;
        v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
 
-       ret = init_device(client, state);
-       if (0 != ret)
+       ret = adv7180_init_controls(state);
+       if (ret)
                goto err_unreg_subdev;
+       ret = init_device(client, state);
+       if (ret)
+               goto err_free_ctrl;
        return 0;
 
+err_free_ctrl:
+       adv7180_exit_controls(state);
 err_unreg_subdev:
        mutex_destroy(&state->mutex);
        v4l2_device_unregister_subdev(sd);
        kfree(state);
 err:
-       printk(KERN_ERR DRIVER_NAME ": Failed to probe: %d\n", ret);
+       printk(KERN_ERR KBUILD_MODNAME ": Failed to probe: %d\n", ret);
        return ret;
 }
 
@@ -678,7 +611,7 @@ static __devexit int adv7180_remove(struct i2c_client *client)
 }
 
 static const struct i2c_device_id adv7180_id[] = {
-       {DRIVER_NAME, 0},
+       {KBUILD_MODNAME, 0},
        {},
 };
 
@@ -716,7 +649,7 @@ MODULE_DEVICE_TABLE(i2c, adv7180_id);
 static struct i2c_driver adv7180_driver = {
        .driver = {
                   .owner = THIS_MODULE,
-                  .name = DRIVER_NAME,
+                  .name = KBUILD_MODNAME,
                   },
        .probe = adv7180_probe,
        .remove = __devexit_p(adv7180_remove),