diff mbox series

[v4] media: ov5640: Use runtime PM

Message ID 20220422105327.1918427-1-paul.elder@ideasonboard.com (mailing list archive)
State Superseded
Headers show
Series [v4] media: ov5640: Use runtime PM | expand

Commit Message

Paul Elder April 22, 2022, 10:53 a.m. UTC
Switch to using runtime PM for power management.

Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

---
Changes in v4:
- replace pm_runtime_put* with pm_runtime_put_autosuspend
- remove the manual initial call to ov5640_set_power()

Changes in v3:
- Move v4l2_ctrl_handler_setup() from ov5640_sensor_resume to
  ov5640_s_stream()

Changes in v2:
- replace manual tracking of power status with pm_runtime_get_if_in_use
- power on the sensor before reading the checking the chip id
- add dependency on PM to Kconfig
---
 drivers/media/i2c/Kconfig  |   1 +
 drivers/media/i2c/ov5640.c | 119 ++++++++++++++++++++++---------------
 2 files changed, 71 insertions(+), 49 deletions(-)

Comments

kernel test robot April 22, 2022, 5:35 p.m. UTC | #1
Hi Paul,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on media-tree/master]
[also build test WARNING on v5.18-rc3 next-20220422]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Paul-Elder/media-ov5640-Use-runtime-PM/20220422-185657
base:   git://linuxtv.org/media_tree.git master
config: i386-randconfig-a013 (https://download.01.org/0day-ci/archive/20220423/202204230149.9CdMTZ0P-lkp@intel.com/config)
compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project 5bd87350a5ae429baf8f373cb226a57b62f87280)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/35b46d84ba0a6794350f826f2f70899e266a6064
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Paul-Elder/media-ov5640-Use-runtime-PM/20220422-185657
        git checkout 35b46d84ba0a6794350f826f2f70899e266a6064
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash drivers/media/i2c/

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> drivers/media/i2c/ov5640.c:3171:1: warning: unused label 'free_ctrls' [-Wunused-label]
   free_ctrls:
   ^~~~~~~~~~~
   1 warning generated.


vim +/free_ctrls +3171 drivers/media/i2c/ov5640.c

0f7acb52911066 Hugues Fruchet      2018-01-03  3028  
e671499303e4c0 Kieran Bingham      2019-07-10  3029  static int ov5640_probe(struct i2c_client *client)
19a81c1426c185 Steve Longerbeam    2017-06-07  3030  {
19a81c1426c185 Steve Longerbeam    2017-06-07  3031  	struct device *dev = &client->dev;
19a81c1426c185 Steve Longerbeam    2017-06-07  3032  	struct fwnode_handle *endpoint;
19a81c1426c185 Steve Longerbeam    2017-06-07  3033  	struct ov5640_dev *sensor;
e6441fde848737 Hugues Fruchet      2018-03-06  3034  	struct v4l2_mbus_framefmt *fmt;
c3f3ba3e6f78ea Hugues Fruchet      2018-06-18  3035  	u32 rotation;
19a81c1426c185 Steve Longerbeam    2017-06-07  3036  	int ret;
19a81c1426c185 Steve Longerbeam    2017-06-07  3037  
19a81c1426c185 Steve Longerbeam    2017-06-07  3038  	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
19a81c1426c185 Steve Longerbeam    2017-06-07  3039  	if (!sensor)
19a81c1426c185 Steve Longerbeam    2017-06-07  3040  		return -ENOMEM;
19a81c1426c185 Steve Longerbeam    2017-06-07  3041  
19a81c1426c185 Steve Longerbeam    2017-06-07  3042  	sensor->i2c_client = client;
fb98e29ff1ea5a Hugues Fruchet      2018-08-16  3043  
fb98e29ff1ea5a Hugues Fruchet      2018-08-16  3044  	/*
fb98e29ff1ea5a Hugues Fruchet      2018-08-16  3045  	 * default init sequence initialize sensor to
fb98e29ff1ea5a Hugues Fruchet      2018-08-16  3046  	 * YUV422 UYVY VGA@30fps
fb98e29ff1ea5a Hugues Fruchet      2018-08-16  3047  	 */
e6441fde848737 Hugues Fruchet      2018-03-06  3048  	fmt = &sensor->fmt;
fb98e29ff1ea5a Hugues Fruchet      2018-08-16  3049  	fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
fb98e29ff1ea5a Hugues Fruchet      2018-08-16  3050  	fmt->colorspace = V4L2_COLORSPACE_SRGB;
e6441fde848737 Hugues Fruchet      2018-03-06  3051  	fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
e6441fde848737 Hugues Fruchet      2018-03-06  3052  	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
e6441fde848737 Hugues Fruchet      2018-03-06  3053  	fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
e6441fde848737 Hugues Fruchet      2018-03-06  3054  	fmt->width = 640;
e6441fde848737 Hugues Fruchet      2018-03-06  3055  	fmt->height = 480;
e6441fde848737 Hugues Fruchet      2018-03-06  3056  	fmt->field = V4L2_FIELD_NONE;
19a81c1426c185 Steve Longerbeam    2017-06-07  3057  	sensor->frame_interval.numerator = 1;
19a81c1426c185 Steve Longerbeam    2017-06-07  3058  	sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
19a81c1426c185 Steve Longerbeam    2017-06-07  3059  	sensor->current_fr = OV5640_30_FPS;
19a81c1426c185 Steve Longerbeam    2017-06-07  3060  	sensor->current_mode =
086c25f8fef9ce Maxime Ripard       2018-12-03  3061  		&ov5640_mode_data[OV5640_MODE_VGA_640_480];
985cdcb08a0488 Hugues Fruchet      2018-09-11  3062  	sensor->last_mode = sensor->current_mode;
19a81c1426c185 Steve Longerbeam    2017-06-07  3063  
19a81c1426c185 Steve Longerbeam    2017-06-07  3064  	sensor->ae_target = 52;
19a81c1426c185 Steve Longerbeam    2017-06-07  3065  
c3f3ba3e6f78ea Hugues Fruchet      2018-06-18  3066  	/* optional indication of physical rotation of sensor */
c3f3ba3e6f78ea Hugues Fruchet      2018-06-18  3067  	ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation",
c3f3ba3e6f78ea Hugues Fruchet      2018-06-18  3068  				       &rotation);
c3f3ba3e6f78ea Hugues Fruchet      2018-06-18  3069  	if (!ret) {
c3f3ba3e6f78ea Hugues Fruchet      2018-06-18  3070  		switch (rotation) {
c3f3ba3e6f78ea Hugues Fruchet      2018-06-18  3071  		case 180:
c3f3ba3e6f78ea Hugues Fruchet      2018-06-18  3072  			sensor->upside_down = true;
1771e9fb67e2d6 Gustavo A. R. Silva 2020-07-25  3073  			fallthrough;
c3f3ba3e6f78ea Hugues Fruchet      2018-06-18  3074  		case 0:
c3f3ba3e6f78ea Hugues Fruchet      2018-06-18  3075  			break;
c3f3ba3e6f78ea Hugues Fruchet      2018-06-18  3076  		default:
c3f3ba3e6f78ea Hugues Fruchet      2018-06-18  3077  			dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n",
c3f3ba3e6f78ea Hugues Fruchet      2018-06-18  3078  				 rotation);
c3f3ba3e6f78ea Hugues Fruchet      2018-06-18  3079  		}
c3f3ba3e6f78ea Hugues Fruchet      2018-06-18  3080  	}
c3f3ba3e6f78ea Hugues Fruchet      2018-06-18  3081  
ce96bcf5b4dbd9 Sakari Ailus        2018-04-24  3082  	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
ce96bcf5b4dbd9 Sakari Ailus        2018-04-24  3083  						  NULL);
19a81c1426c185 Steve Longerbeam    2017-06-07  3084  	if (!endpoint) {
19a81c1426c185 Steve Longerbeam    2017-06-07  3085  		dev_err(dev, "endpoint node not found\n");
19a81c1426c185 Steve Longerbeam    2017-06-07  3086  		return -EINVAL;
19a81c1426c185 Steve Longerbeam    2017-06-07  3087  	}
19a81c1426c185 Steve Longerbeam    2017-06-07  3088  
19a81c1426c185 Steve Longerbeam    2017-06-07  3089  	ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
19a81c1426c185 Steve Longerbeam    2017-06-07  3090  	fwnode_handle_put(endpoint);
19a81c1426c185 Steve Longerbeam    2017-06-07  3091  	if (ret) {
19a81c1426c185 Steve Longerbeam    2017-06-07  3092  		dev_err(dev, "Could not parse endpoint\n");
19a81c1426c185 Steve Longerbeam    2017-06-07  3093  		return ret;
19a81c1426c185 Steve Longerbeam    2017-06-07  3094  	}
19a81c1426c185 Steve Longerbeam    2017-06-07  3095  
2c61e48d0a19ef Lad Prabhakar       2020-09-04  3096  	if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL &&
2c61e48d0a19ef Lad Prabhakar       2020-09-04  3097  	    sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY &&
2c61e48d0a19ef Lad Prabhakar       2020-09-04  3098  	    sensor->ep.bus_type != V4L2_MBUS_BT656) {
2c61e48d0a19ef Lad Prabhakar       2020-09-04  3099  		dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type);
2c61e48d0a19ef Lad Prabhakar       2020-09-04  3100  		return -EINVAL;
2c61e48d0a19ef Lad Prabhakar       2020-09-04  3101  	}
2c61e48d0a19ef Lad Prabhakar       2020-09-04  3102  
19a81c1426c185 Steve Longerbeam    2017-06-07  3103  	/* get system clock (xclk) */
19a81c1426c185 Steve Longerbeam    2017-06-07  3104  	sensor->xclk = devm_clk_get(dev, "xclk");
19a81c1426c185 Steve Longerbeam    2017-06-07  3105  	if (IS_ERR(sensor->xclk)) {
19a81c1426c185 Steve Longerbeam    2017-06-07  3106  		dev_err(dev, "failed to get xclk\n");
19a81c1426c185 Steve Longerbeam    2017-06-07  3107  		return PTR_ERR(sensor->xclk);
19a81c1426c185 Steve Longerbeam    2017-06-07  3108  	}
19a81c1426c185 Steve Longerbeam    2017-06-07  3109  
19a81c1426c185 Steve Longerbeam    2017-06-07  3110  	sensor->xclk_freq = clk_get_rate(sensor->xclk);
19a81c1426c185 Steve Longerbeam    2017-06-07  3111  	if (sensor->xclk_freq < OV5640_XCLK_MIN ||
19a81c1426c185 Steve Longerbeam    2017-06-07  3112  	    sensor->xclk_freq > OV5640_XCLK_MAX) {
19a81c1426c185 Steve Longerbeam    2017-06-07  3113  		dev_err(dev, "xclk frequency out of range: %d Hz\n",
19a81c1426c185 Steve Longerbeam    2017-06-07  3114  			sensor->xclk_freq);
19a81c1426c185 Steve Longerbeam    2017-06-07  3115  		return -EINVAL;
19a81c1426c185 Steve Longerbeam    2017-06-07  3116  	}
19a81c1426c185 Steve Longerbeam    2017-06-07  3117  
19a81c1426c185 Steve Longerbeam    2017-06-07  3118  	/* request optional power down pin */
19a81c1426c185 Steve Longerbeam    2017-06-07  3119  	sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
19a81c1426c185 Steve Longerbeam    2017-06-07  3120  						    GPIOD_OUT_HIGH);
8791a102ce5793 Fabio Estevam       2019-06-28  3121  	if (IS_ERR(sensor->pwdn_gpio))
8791a102ce5793 Fabio Estevam       2019-06-28  3122  		return PTR_ERR(sensor->pwdn_gpio);
8791a102ce5793 Fabio Estevam       2019-06-28  3123  
19a81c1426c185 Steve Longerbeam    2017-06-07  3124  	/* request optional reset pin */
19a81c1426c185 Steve Longerbeam    2017-06-07  3125  	sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
19a81c1426c185 Steve Longerbeam    2017-06-07  3126  						     GPIOD_OUT_HIGH);
8791a102ce5793 Fabio Estevam       2019-06-28  3127  	if (IS_ERR(sensor->reset_gpio))
8791a102ce5793 Fabio Estevam       2019-06-28  3128  		return PTR_ERR(sensor->reset_gpio);
19a81c1426c185 Steve Longerbeam    2017-06-07  3129  
19a81c1426c185 Steve Longerbeam    2017-06-07  3130  	v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
19a81c1426c185 Steve Longerbeam    2017-06-07  3131  
2d18fbc5518f05 Akinobu Mita        2018-11-12  3132  	sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2d18fbc5518f05 Akinobu Mita        2018-11-12  3133  			    V4L2_SUBDEV_FL_HAS_EVENTS;
19a81c1426c185 Steve Longerbeam    2017-06-07  3134  	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
19a81c1426c185 Steve Longerbeam    2017-06-07  3135  	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
19a81c1426c185 Steve Longerbeam    2017-06-07  3136  	ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
19a81c1426c185 Steve Longerbeam    2017-06-07  3137  	if (ret)
19a81c1426c185 Steve Longerbeam    2017-06-07  3138  		return ret;
19a81c1426c185 Steve Longerbeam    2017-06-07  3139  
19a81c1426c185 Steve Longerbeam    2017-06-07  3140  	ret = ov5640_get_regulators(sensor);
19a81c1426c185 Steve Longerbeam    2017-06-07  3141  	if (ret)
19a81c1426c185 Steve Longerbeam    2017-06-07  3142  		return ret;
19a81c1426c185 Steve Longerbeam    2017-06-07  3143  
19a81c1426c185 Steve Longerbeam    2017-06-07  3144  	mutex_init(&sensor->lock);
19a81c1426c185 Steve Longerbeam    2017-06-07  3145  
35b46d84ba0a67 Paul Elder          2022-04-22  3146  	ret = ov5640_init_controls(sensor);
0f7acb52911066 Hugues Fruchet      2018-01-03  3147  	if (ret)
0f7acb52911066 Hugues Fruchet      2018-01-03  3148  		goto entity_cleanup;
0f7acb52911066 Hugues Fruchet      2018-01-03  3149  
35b46d84ba0a67 Paul Elder          2022-04-22  3150  	pm_runtime_set_active(dev);
35b46d84ba0a67 Paul Elder          2022-04-22  3151  	pm_runtime_enable(dev);
35b46d84ba0a67 Paul Elder          2022-04-22  3152  	pm_runtime_get(dev);
35b46d84ba0a67 Paul Elder          2022-04-22  3153  
35b46d84ba0a67 Paul Elder          2022-04-22  3154  	ret = ov5640_check_chip_id(sensor);
19a81c1426c185 Steve Longerbeam    2017-06-07  3155  	if (ret)
35b46d84ba0a67 Paul Elder          2022-04-22  3156  		goto err_pm_runtime;
19a81c1426c185 Steve Longerbeam    2017-06-07  3157  
15786f7b564eff Sakari Ailus        2021-03-05  3158  	ret = v4l2_async_register_subdev_sensor(&sensor->sd);
19a81c1426c185 Steve Longerbeam    2017-06-07  3159  	if (ret)
35b46d84ba0a67 Paul Elder          2022-04-22  3160  		goto err_pm_runtime;
35b46d84ba0a67 Paul Elder          2022-04-22  3161  
35b46d84ba0a67 Paul Elder          2022-04-22  3162  	pm_runtime_set_autosuspend_delay(dev, 1000);
35b46d84ba0a67 Paul Elder          2022-04-22  3163  	pm_runtime_use_autosuspend(dev);
35b46d84ba0a67 Paul Elder          2022-04-22  3164  	pm_runtime_put_autosuspend(dev);
19a81c1426c185 Steve Longerbeam    2017-06-07  3165  
19a81c1426c185 Steve Longerbeam    2017-06-07  3166  	return 0;
19a81c1426c185 Steve Longerbeam    2017-06-07  3167  
35b46d84ba0a67 Paul Elder          2022-04-22  3168  err_pm_runtime:
35b46d84ba0a67 Paul Elder          2022-04-22  3169  	pm_runtime_disable(dev);
35b46d84ba0a67 Paul Elder          2022-04-22  3170  	pm_runtime_put_autosuspend(dev);
19a81c1426c185 Steve Longerbeam    2017-06-07 @3171  free_ctrls:
19a81c1426c185 Steve Longerbeam    2017-06-07  3172  	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
19a81c1426c185 Steve Longerbeam    2017-06-07  3173  entity_cleanup:
19a81c1426c185 Steve Longerbeam    2017-06-07  3174  	media_entity_cleanup(&sensor->sd.entity);
bfcba38d95a0ae Tomi Valkeinen      2020-03-25  3175  	mutex_destroy(&sensor->lock);
19a81c1426c185 Steve Longerbeam    2017-06-07  3176  	return ret;
19a81c1426c185 Steve Longerbeam    2017-06-07  3177  }
19a81c1426c185 Steve Longerbeam    2017-06-07  3178
diff mbox series

Patch

diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
index fae2baabb773..0bd53f8198f1 100644
--- a/drivers/media/i2c/Kconfig
+++ b/drivers/media/i2c/Kconfig
@@ -433,6 +433,7 @@  config VIDEO_OV5640
 	tristate "OmniVision OV5640 sensor support"
 	depends on OF
 	depends on GPIOLIB && VIDEO_DEV && I2C
+	depends on PM
 	select MEDIA_CONTROLLER
 	select VIDEO_V4L2_SUBDEV_API
 	select V4L2_FWNODE
diff --git a/drivers/media/i2c/ov5640.c b/drivers/media/i2c/ov5640.c
index db5a19babe67..ff48cd16b735 100644
--- a/drivers/media/i2c/ov5640.c
+++ b/drivers/media/i2c/ov5640.c
@@ -15,6 +15,7 @@ 
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/of_device.h>
+#include <linux/pm_runtime.h>
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>
 #include <linux/types.h>
@@ -240,8 +241,6 @@  struct ov5640_dev {
 	/* lock to protect all members below */
 	struct mutex lock;
 
-	int power_count;
-
 	struct v4l2_mbus_framefmt fmt;
 	bool pending_fmt_change;
 
@@ -2157,37 +2156,6 @@  static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
 
 /* --------------- Subdev Operations --------------- */
 
-static int ov5640_s_power(struct v4l2_subdev *sd, int on)
-{
-	struct ov5640_dev *sensor = to_ov5640_dev(sd);
-	int ret = 0;
-
-	mutex_lock(&sensor->lock);
-
-	/*
-	 * If the power count is modified from 0 to != 0 or from != 0 to 0,
-	 * update the power state.
-	 */
-	if (sensor->power_count == !on) {
-		ret = ov5640_set_power(sensor, !!on);
-		if (ret)
-			goto out;
-	}
-
-	/* Update the power count. */
-	sensor->power_count += on ? 1 : -1;
-	WARN_ON(sensor->power_count < 0);
-out:
-	mutex_unlock(&sensor->lock);
-
-	if (on && !ret && sensor->power_count == 1) {
-		/* restore controls */
-		ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
-	}
-
-	return ret;
-}
-
 static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
 				     struct v4l2_fract *fi,
 				     u32 width, u32 height)
@@ -2663,6 +2631,9 @@  static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
 
 	/* v4l2_ctrl_lock() locks our own mutex */
 
+	if (!pm_runtime_get_if_in_use(&sensor->i2c_client->dev))
+		return 0;
+
 	switch (ctrl->id) {
 	case V4L2_CID_AUTOGAIN:
 		val = ov5640_get_gain(sensor);
@@ -2678,6 +2649,8 @@  static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
 		break;
 	}
 
+	pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
+
 	return 0;
 }
 
@@ -2692,9 +2665,9 @@  static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
 	/*
 	 * If the device is not powered up by the host driver do
 	 * not apply any controls to H/W at this time. Instead
-	 * the controls will be restored right after power-up.
+	 * the controls will be restored at start streaming time.
 	 */
-	if (sensor->power_count == 0)
+	if (!pm_runtime_get_if_in_use(&sensor->i2c_client->dev))
 		return 0;
 
 	switch (ctrl->id) {
@@ -2733,6 +2706,8 @@  static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
 		break;
 	}
 
+	pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
+
 	return ret;
 }
 
@@ -2945,6 +2920,18 @@  static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
 	int ret = 0;
 
+	if (enable) {
+		ret = pm_runtime_resume_and_get(&sensor->i2c_client->dev);
+		if (ret < 0)
+			return ret;
+
+		ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
+		if (ret) {
+			pm_runtime_put(&sensor->i2c_client->dev);
+			return ret;
+		}
+	}
+
 	mutex_lock(&sensor->lock);
 
 	if (sensor->streaming == !enable) {
@@ -2969,13 +2956,17 @@  static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
 		if (!ret)
 			sensor->streaming = enable;
 	}
+
 out:
 	mutex_unlock(&sensor->lock);
+
+	if (!enable || ret)
+		pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
+
 	return ret;
 }
 
 static const struct v4l2_subdev_core_ops ov5640_core_ops = {
-	.s_power = ov5640_s_power,
 	.log_status = v4l2_ctrl_subdev_log_status,
 	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
@@ -3019,26 +3010,20 @@  static int ov5640_check_chip_id(struct ov5640_dev *sensor)
 	int ret = 0;
 	u16 chip_id;
 
-	ret = ov5640_set_power_on(sensor);
-	if (ret)
-		return ret;
-
 	ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
 	if (ret) {
 		dev_err(&client->dev, "%s: failed to read chip identifier\n",
 			__func__);
-		goto power_off;
+		return ret;
 	}
 
 	if (chip_id != 0x5640) {
 		dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
 			__func__, chip_id);
-		ret = -ENXIO;
+		return -ENXIO;
 	}
 
-power_off:
-	ov5640_set_power_off(sensor);
-	return ret;
+	return 0;
 }
 
 static int ov5640_probe(struct i2c_client *client)
@@ -3158,20 +3143,31 @@  static int ov5640_probe(struct i2c_client *client)
 
 	mutex_init(&sensor->lock);
 
-	ret = ov5640_check_chip_id(sensor);
+	ret = ov5640_init_controls(sensor);
 	if (ret)
 		goto entity_cleanup;
 
-	ret = ov5640_init_controls(sensor);
+	pm_runtime_set_active(dev);
+	pm_runtime_enable(dev);
+	pm_runtime_get(dev);
+
+	ret = ov5640_check_chip_id(sensor);
 	if (ret)
-		goto entity_cleanup;
+		goto err_pm_runtime;
 
 	ret = v4l2_async_register_subdev_sensor(&sensor->sd);
 	if (ret)
-		goto free_ctrls;
+		goto err_pm_runtime;
+
+	pm_runtime_set_autosuspend_delay(dev, 1000);
+	pm_runtime_use_autosuspend(dev);
+	pm_runtime_put_autosuspend(dev);
 
 	return 0;
 
+err_pm_runtime:
+	pm_runtime_disable(dev);
+	pm_runtime_put_autosuspend(dev);
 free_ctrls:
 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
 entity_cleanup:
@@ -3184,6 +3180,10 @@  static int ov5640_remove(struct i2c_client *client)
 {
 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
+	struct device *dev = &client->dev;
+
+	pm_runtime_disable(dev);
+	pm_runtime_put_autosuspend(dev);
 
 	v4l2_async_unregister_subdev(&sensor->sd);
 	media_entity_cleanup(&sensor->sd.entity);
@@ -3193,6 +3193,26 @@  static int ov5640_remove(struct i2c_client *client)
 	return 0;
 }
 
+static int __maybe_unused ov5640_sensor_suspend(struct device *dev)
+{
+	struct v4l2_subdev *sd = dev_get_drvdata(dev);
+	struct ov5640_dev *ov5640 = to_ov5640_dev(sd);
+
+	return ov5640_set_power(ov5640, false);
+}
+
+static int __maybe_unused ov5640_sensor_resume(struct device *dev)
+{
+	struct v4l2_subdev *sd = dev_get_drvdata(dev);
+	struct ov5640_dev *ov5640 = to_ov5640_dev(sd);
+
+	return ov5640_set_power(ov5640, true);
+}
+
+static const struct dev_pm_ops ov5640_pm_ops = {
+	SET_RUNTIME_PM_OPS(ov5640_sensor_suspend, ov5640_sensor_resume, NULL)
+};
+
 static const struct i2c_device_id ov5640_id[] = {
 	{"ov5640", 0},
 	{},
@@ -3209,6 +3229,7 @@  static struct i2c_driver ov5640_i2c_driver = {
 	.driver = {
 		.name  = "ov5640",
 		.of_match_table	= ov5640_dt_ids,
+		.pm = &ov5640_pm_ops,
 	},
 	.id_table = ov5640_id,
 	.probe_new = ov5640_probe,