Commit b5045039 authored by Lad Prabhakar's avatar Lad Prabhakar Committed by Mauro Carvalho Chehab

media: i2c: ov5645: Use runtime PM

Switch to using runtime PM for power management.
Signed-off-by: default avatarLad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Reviewed-by: default avatarLaurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: default avatarSakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@kernel.org>
parent 105c3bc0
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <linux/module.h> #include <linux/module.h>
#include <linux/of.h> #include <linux/of.h>
#include <linux/of_graph.h> #include <linux/of_graph.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h> #include <linux/regulator/consumer.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/types.h> #include <linux/types.h>
...@@ -108,7 +109,6 @@ struct ov5645 { ...@@ -108,7 +109,6 @@ struct ov5645 {
u8 timing_tc_reg21; u8 timing_tc_reg21;
struct mutex power_lock; /* lock to protect power state */ struct mutex power_lock; /* lock to protect power state */
int power_count;
struct gpio_desc *enable_gpio; struct gpio_desc *enable_gpio;
struct gpio_desc *rst_gpio; struct gpio_desc *rst_gpio;
...@@ -635,8 +635,24 @@ static int ov5645_set_register_array(struct ov5645 *ov5645, ...@@ -635,8 +635,24 @@ static int ov5645_set_register_array(struct ov5645 *ov5645,
return 0; return 0;
} }
static int ov5645_set_power_on(struct ov5645 *ov5645) static int ov5645_set_power_off(struct device *dev)
{ {
struct v4l2_subdev *sd = dev_get_drvdata(dev);
struct ov5645 *ov5645 = to_ov5645(sd);
ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x58);
gpiod_set_value_cansleep(ov5645->rst_gpio, 1);
gpiod_set_value_cansleep(ov5645->enable_gpio, 0);
clk_disable_unprepare(ov5645->xclk);
regulator_bulk_disable(OV5645_NUM_SUPPLIES, ov5645->supplies);
return 0;
}
static int ov5645_set_power_on(struct device *dev)
{
struct v4l2_subdev *sd = dev_get_drvdata(dev);
struct ov5645 *ov5645 = to_ov5645(sd);
int ret; int ret;
ret = regulator_bulk_enable(OV5645_NUM_SUPPLIES, ov5645->supplies); ret = regulator_bulk_enable(OV5645_NUM_SUPPLIES, ov5645->supplies);
...@@ -658,57 +674,19 @@ static int ov5645_set_power_on(struct ov5645 *ov5645) ...@@ -658,57 +674,19 @@ static int ov5645_set_power_on(struct ov5645 *ov5645)
msleep(20); msleep(20);
return 0; ret = ov5645_set_register_array(ov5645, ov5645_global_init_setting,
}
static void ov5645_set_power_off(struct ov5645 *ov5645)
{
gpiod_set_value_cansleep(ov5645->rst_gpio, 1);
gpiod_set_value_cansleep(ov5645->enable_gpio, 0);
clk_disable_unprepare(ov5645->xclk);
regulator_bulk_disable(OV5645_NUM_SUPPLIES, ov5645->supplies);
}
static int ov5645_s_power(struct v4l2_subdev *sd, int on)
{
struct ov5645 *ov5645 = to_ov5645(sd);
int ret = 0;
mutex_lock(&ov5645->power_lock);
/* If the power count is modified from 0 to != 0 or from != 0 to 0,
* update the power state.
*/
if (ov5645->power_count == !on) {
if (on) {
ret = ov5645_set_power_on(ov5645);
if (ret < 0)
goto exit;
ret = ov5645_set_register_array(ov5645,
ov5645_global_init_setting,
ARRAY_SIZE(ov5645_global_init_setting)); ARRAY_SIZE(ov5645_global_init_setting));
if (ret < 0) { if (ret < 0) {
dev_err(ov5645->dev, dev_err(ov5645->dev, "could not set init registers\n");
"could not set init registers\n");
ov5645_set_power_off(ov5645);
goto exit; goto exit;
} }
usleep_range(500, 1000); usleep_range(500, 1000);
} else {
ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x58);
ov5645_set_power_off(ov5645);
}
}
/* Update the power count. */ return 0;
ov5645->power_count += on ? 1 : -1;
WARN_ON(ov5645->power_count < 0);
exit: exit:
mutex_unlock(&ov5645->power_lock); ov5645_set_power_off(dev);
return ret; return ret;
} }
...@@ -795,7 +773,7 @@ static int ov5645_s_ctrl(struct v4l2_ctrl *ctrl) ...@@ -795,7 +773,7 @@ static int ov5645_s_ctrl(struct v4l2_ctrl *ctrl)
int ret; int ret;
mutex_lock(&ov5645->power_lock); mutex_lock(&ov5645->power_lock);
if (!ov5645->power_count) { if (!pm_runtime_get_if_in_use(ov5645->dev)) {
mutex_unlock(&ov5645->power_lock); mutex_unlock(&ov5645->power_lock);
return 0; return 0;
} }
...@@ -827,6 +805,8 @@ static int ov5645_s_ctrl(struct v4l2_ctrl *ctrl) ...@@ -827,6 +805,8 @@ static int ov5645_s_ctrl(struct v4l2_ctrl *ctrl)
break; break;
} }
pm_runtime_mark_last_busy(ov5645->dev);
pm_runtime_put_autosuspend(ov5645->dev);
mutex_unlock(&ov5645->power_lock); mutex_unlock(&ov5645->power_lock);
return ret; return ret;
...@@ -991,6 +971,10 @@ static int ov5645_s_stream(struct v4l2_subdev *subdev, int enable) ...@@ -991,6 +971,10 @@ static int ov5645_s_stream(struct v4l2_subdev *subdev, int enable)
int ret; int ret;
if (enable) { if (enable) {
ret = pm_runtime_resume_and_get(ov5645->dev);
if (ret < 0)
return ret;
ret = ov5645_set_register_array(ov5645, ret = ov5645_set_register_array(ov5645,
ov5645->current_mode->data, ov5645->current_mode->data,
ov5645->current_mode->data_size); ov5645->current_mode->data_size);
...@@ -998,22 +982,22 @@ static int ov5645_s_stream(struct v4l2_subdev *subdev, int enable) ...@@ -998,22 +982,22 @@ static int ov5645_s_stream(struct v4l2_subdev *subdev, int enable)
dev_err(ov5645->dev, "could not set mode %dx%d\n", dev_err(ov5645->dev, "could not set mode %dx%d\n",
ov5645->current_mode->width, ov5645->current_mode->width,
ov5645->current_mode->height); ov5645->current_mode->height);
return ret; goto err_rpm_put;
} }
ret = v4l2_ctrl_handler_setup(&ov5645->ctrls); ret = v4l2_ctrl_handler_setup(&ov5645->ctrls);
if (ret < 0) { if (ret < 0) {
dev_err(ov5645->dev, "could not sync v4l2 controls\n"); dev_err(ov5645->dev, "could not sync v4l2 controls\n");
return ret; goto err_rpm_put;
} }
ret = ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x45); ret = ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x45);
if (ret < 0) if (ret < 0)
return ret; goto err_rpm_put;
ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0, ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
OV5645_SYSTEM_CTRL0_START); OV5645_SYSTEM_CTRL0_START);
if (ret < 0) if (ret < 0)
return ret; goto err_rpm_put;
} else { } else {
ret = ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x40); ret = ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x40);
if (ret < 0) if (ret < 0)
...@@ -1023,14 +1007,17 @@ static int ov5645_s_stream(struct v4l2_subdev *subdev, int enable) ...@@ -1023,14 +1007,17 @@ static int ov5645_s_stream(struct v4l2_subdev *subdev, int enable)
OV5645_SYSTEM_CTRL0_STOP); OV5645_SYSTEM_CTRL0_STOP);
if (ret < 0) if (ret < 0)
return ret; return ret;
pm_runtime_mark_last_busy(ov5645->dev);
pm_runtime_put_autosuspend(ov5645->dev);
} }
return 0; return 0;
}
static const struct v4l2_subdev_core_ops ov5645_core_ops = { err_rpm_put:
.s_power = ov5645_s_power, pm_runtime_put_sync(ov5645->dev);
}; return ret;
}
static const struct v4l2_subdev_video_ops ov5645_video_ops = { static const struct v4l2_subdev_video_ops ov5645_video_ops = {
.s_stream = ov5645_s_stream, .s_stream = ov5645_s_stream,
...@@ -1046,7 +1033,6 @@ static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = { ...@@ -1046,7 +1033,6 @@ static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
}; };
static const struct v4l2_subdev_ops ov5645_subdev_ops = { static const struct v4l2_subdev_ops ov5645_subdev_ops = {
.core = &ov5645_core_ops,
.video = &ov5645_video_ops, .video = &ov5645_video_ops,
.pad = &ov5645_subdev_pad_ops, .pad = &ov5645_subdev_pad_ops,
}; };
...@@ -1188,11 +1174,9 @@ static int ov5645_probe(struct i2c_client *client) ...@@ -1188,11 +1174,9 @@ static int ov5645_probe(struct i2c_client *client)
goto free_ctrl; goto free_ctrl;
} }
ret = ov5645_s_power(&ov5645->sd, true); ret = ov5645_set_power_on(dev);
if (ret < 0) { if (ret)
dev_err(dev, "could not power up OV5645\n");
goto free_entity; goto free_entity;
}
ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_HIGH, &chip_id_high); ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_HIGH, &chip_id_high);
if (ret < 0 || chip_id_high != OV5645_CHIP_ID_HIGH_BYTE) { if (ret < 0 || chip_id_high != OV5645_CHIP_ID_HIGH_BYTE) {
...@@ -1233,20 +1217,30 @@ static int ov5645_probe(struct i2c_client *client) ...@@ -1233,20 +1217,30 @@ static int ov5645_probe(struct i2c_client *client)
goto power_down; goto power_down;
} }
ov5645_s_power(&ov5645->sd, false); pm_runtime_set_active(dev);
pm_runtime_get_noresume(dev);
pm_runtime_enable(dev);
ret = v4l2_async_register_subdev(&ov5645->sd); ret = v4l2_async_register_subdev(&ov5645->sd);
if (ret < 0) { if (ret < 0) {
dev_err(dev, "could not register v4l2 device\n"); dev_err(dev, "could not register v4l2 device\n");
goto free_entity; goto err_pm_runtime;
} }
pm_runtime_set_autosuspend_delay(dev, 1000);
pm_runtime_use_autosuspend(dev);
pm_runtime_mark_last_busy(dev);
pm_runtime_put_autosuspend(dev);
ov5645_entity_init_cfg(&ov5645->sd, NULL); ov5645_entity_init_cfg(&ov5645->sd, NULL);
return 0; return 0;
err_pm_runtime:
pm_runtime_disable(dev);
pm_runtime_put_noidle(dev);
power_down: power_down:
ov5645_s_power(&ov5645->sd, false); ov5645_set_power_off(dev);
free_entity: free_entity:
media_entity_cleanup(&ov5645->sd.entity); media_entity_cleanup(&ov5645->sd.entity);
free_ctrl: free_ctrl:
...@@ -1264,6 +1258,10 @@ static void ov5645_remove(struct i2c_client *client) ...@@ -1264,6 +1258,10 @@ static void ov5645_remove(struct i2c_client *client)
v4l2_async_unregister_subdev(&ov5645->sd); v4l2_async_unregister_subdev(&ov5645->sd);
media_entity_cleanup(&ov5645->sd.entity); media_entity_cleanup(&ov5645->sd.entity);
v4l2_ctrl_handler_free(&ov5645->ctrls); v4l2_ctrl_handler_free(&ov5645->ctrls);
pm_runtime_disable(ov5645->dev);
if (!pm_runtime_status_suspended(ov5645->dev))
ov5645_set_power_off(ov5645->dev);
pm_runtime_set_suspended(ov5645->dev);
mutex_destroy(&ov5645->power_lock); mutex_destroy(&ov5645->power_lock);
} }
...@@ -1279,10 +1277,15 @@ static const struct of_device_id ov5645_of_match[] = { ...@@ -1279,10 +1277,15 @@ static const struct of_device_id ov5645_of_match[] = {
}; };
MODULE_DEVICE_TABLE(of, ov5645_of_match); MODULE_DEVICE_TABLE(of, ov5645_of_match);
static const struct dev_pm_ops ov5645_pm_ops = {
SET_RUNTIME_PM_OPS(ov5645_set_power_off, ov5645_set_power_on, NULL)
};
static struct i2c_driver ov5645_i2c_driver = { static struct i2c_driver ov5645_i2c_driver = {
.driver = { .driver = {
.of_match_table = ov5645_of_match, .of_match_table = ov5645_of_match,
.name = "ov5645", .name = "ov5645",
.pm = &ov5645_pm_ops,
}, },
.probe_new = ov5645_probe, .probe_new = ov5645_probe,
.remove = ov5645_remove, .remove = ov5645_remove,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment