.link_validate = v4l2_subdev_link_validate,
 };
 
+static int imx290_ctrl_init(struct imx290 *imx290)
+{
+       int ret;
+
+       v4l2_ctrl_handler_init(&imx290->ctrls, 5);
+       imx290->ctrls.lock = &imx290->lock;
+
+       /*
+        * The sensor has an analog gain and a digital gain, both controlled
+        * through a single gain value, expressed in 0.3dB increments. Values
+        * from 0.0dB (0) to 30.0dB (100) apply analog gain only, higher values
+        * up to 72.0dB (240) add further digital gain. Limit the range to
+        * analog gain only, support for digital gain can be added separately
+        * if needed.
+        *
+        * The IMX327 and IMX462 are largely compatible with the IMX290, but
+        * have an analog gain range of 0.0dB to 29.4dB and 42dB of digital
+        * gain. When support for those sensors gets added to the driver, the
+        * gain control should be adjusted accordingly.
+        */
+       v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
+                         V4L2_CID_GAIN, 0, 100, 1, 0);
+
+       v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
+                         V4L2_CID_EXPOSURE, 1, IMX290_VMAX_DEFAULT - 2, 1,
+                         IMX290_VMAX_DEFAULT - 2);
+
+       imx290->link_freq =
+               v4l2_ctrl_new_int_menu(&imx290->ctrls, &imx290_ctrl_ops,
+                                      V4L2_CID_LINK_FREQ,
+                                      imx290_link_freqs_num(imx290) - 1, 0,
+                                      imx290_link_freqs_ptr(imx290));
+       if (imx290->link_freq)
+               imx290->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
+
+       imx290->pixel_rate = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
+                                              V4L2_CID_PIXEL_RATE,
+                                              1, INT_MAX, 1,
+                                              imx290_calc_pixel_rate(imx290));
+
+       v4l2_ctrl_new_std_menu_items(&imx290->ctrls, &imx290_ctrl_ops,
+                                    V4L2_CID_TEST_PATTERN,
+                                    ARRAY_SIZE(imx290_test_pattern_menu) - 1,
+                                    0, 0, imx290_test_pattern_menu);
+
+       imx290->sd.ctrl_handler = &imx290->ctrls;
+
+       if (imx290->ctrls.error) {
+               ret = imx290->ctrls.error;
+               v4l2_ctrl_handler_free(&imx290->ctrls);
+               return ret;
+       }
+
+       return 0;
+}
+
 /*
  * Returns 0 if all link frequencies used by the driver for the given number
  * of MIPI data lanes are mentioned in the device tree, or the value of the
         */
        imx290_entity_init_cfg(&imx290->sd, NULL);
 
-       v4l2_ctrl_handler_init(&imx290->ctrls, 5);
-       imx290->ctrls.lock = &imx290->lock;
-
-       /*
-        * The sensor has an analog gain and a digital gain, both controlled
-        * through a single gain value, expressed in 0.3dB increments. Values
-        * from 0.0dB (0) to 30.0dB (100) apply analog gain only, higher values
-        * up to 72.0dB (240) add further digital gain. Limit the range to
-        * analog gain only, support for digital gain can be added separately
-        * if needed.
-        *
-        * The IMX327 and IMX462 are largely compatible with the IMX290, but
-        * have an analog gain range of 0.0dB to 29.4dB and 42dB of digital
-        * gain. When support for those sensors gets added to the driver, the
-        * gain control should be adjusted accordingly.
-        */
-       v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
-                         V4L2_CID_GAIN, 0, 100, 1, 0);
-
-       v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
-                         V4L2_CID_EXPOSURE, 1, IMX290_VMAX_DEFAULT - 2, 1,
-                         IMX290_VMAX_DEFAULT - 2);
-
-       imx290->link_freq =
-               v4l2_ctrl_new_int_menu(&imx290->ctrls, &imx290_ctrl_ops,
-                                      V4L2_CID_LINK_FREQ,
-                                      imx290_link_freqs_num(imx290) - 1, 0,
-                                      imx290_link_freqs_ptr(imx290));
-       if (imx290->link_freq)
-               imx290->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
-
-       imx290->pixel_rate = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
-                                              V4L2_CID_PIXEL_RATE,
-                                              1, INT_MAX, 1,
-                                              imx290_calc_pixel_rate(imx290));
-
-       v4l2_ctrl_new_std_menu_items(&imx290->ctrls, &imx290_ctrl_ops,
-                                    V4L2_CID_TEST_PATTERN,
-                                    ARRAY_SIZE(imx290_test_pattern_menu) - 1,
-                                    0, 0, imx290_test_pattern_menu);
-
-       imx290->sd.ctrl_handler = &imx290->ctrls;
-
-       if (imx290->ctrls.error) {
-               dev_err(dev, "Control initialization error %d\n",
-                       imx290->ctrls.error);
-               ret = imx290->ctrls.error;
-               goto free_ctrl;
+       ret = imx290_ctrl_init(imx290);
+       if (ret < 0) {
+               dev_err(dev, "Control initialization error %d\n", ret);
+               goto free_mutex;
        }
 
        v4l2_i2c_subdev_init(&imx290->sd, client, &imx290_subdev_ops);
        media_entity_cleanup(&imx290->sd.entity);
 free_ctrl:
        v4l2_ctrl_handler_free(&imx290->ctrls);
+free_mutex:
        mutex_destroy(&imx290->lock);
 free_err:
        v4l2_fwnode_endpoint_free(&ep);