1 From 1768a6f48030e8b670ca3aad08e078bd4bd3ef64 Mon Sep 17 00:00:00 2001
2 From: Daniel Scally <djrscally@gmail.com>
3 Date: Tue, 15 Feb 2022 23:07:35 +0000
4 Subject: [PATCH] media: i2c: Add pm_runtime support to ov7251
6 Add pm_runtime support to the ov7251 driver.
8 Signed-off-by: Daniel Scally <djrscally@gmail.com>
10 drivers/media/i2c/ov7251.c | 78 ++++++++++++++++++++++++++++++--------
11 1 file changed, 63 insertions(+), 15 deletions(-)
13 --- a/drivers/media/i2c/ov7251.c
14 +++ b/drivers/media/i2c/ov7251.c
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/mod_devicetable.h>
19 +#include <linux/pm_runtime.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/slab.h>
22 #include <linux/types.h>
23 @@ -884,6 +885,24 @@ static void ov7251_set_power_off(struct
24 ov7251_regulators_disable(ov7251);
27 +static int __maybe_unused ov7251_sensor_suspend(struct device *dev)
29 + struct v4l2_subdev *sd = dev_get_drvdata(dev);
30 + struct ov7251 *ov7251 = to_ov7251(sd);
32 + ov7251_set_power_off(ov7251);
37 +static int __maybe_unused ov7251_sensor_resume(struct device *dev)
39 + struct v4l2_subdev *sd = dev_get_drvdata(dev);
40 + struct ov7251 *ov7251 = to_ov7251(sd);
42 + return ov7251_set_power_on(ov7251);
45 static int ov7251_s_power(struct v4l2_subdev *sd, int on)
47 struct ov7251 *ov7251 = to_ov7251(sd);
48 @@ -985,7 +1004,7 @@ static int ov7251_s_ctrl(struct v4l2_ctr
50 /* v4l2_ctrl_lock() locks our mutex */
52 - if (!ov7251->power_on)
53 + if (!pm_runtime_get_if_in_use(ov7251->dev))
57 @@ -1009,6 +1028,8 @@ static int ov7251_s_ctrl(struct v4l2_ctr
61 + pm_runtime_put(ov7251->dev);
66 @@ -1261,10 +1282,15 @@ static int ov7251_s_stream(struct v4l2_s
67 mutex_lock(&ov7251->lock);
70 + ret = pm_runtime_get_sync(ov7251->dev);
74 ret = ov7251_pll_configure(ov7251);
76 - return dev_err_probe(ov7251->dev, ret,
77 - "error configuring PLLs\n");
79 + dev_err(ov7251->dev, "error configuring PLLs\n");
80 + goto err_power_down;
83 ret = ov7251_set_register_array(ov7251,
84 ov7251->current_mode->data,
85 @@ -1273,23 +1299,29 @@ static int ov7251_s_stream(struct v4l2_s
86 dev_err(ov7251->dev, "could not set mode %dx%d\n",
87 ov7251->current_mode->width,
88 ov7251->current_mode->height);
90 + goto err_power_down;
92 ret = __v4l2_ctrl_handler_setup(&ov7251->ctrls);
94 dev_err(ov7251->dev, "could not sync v4l2 controls\n");
96 + goto err_power_down;
98 ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT,
99 OV7251_SC_MODE_SELECT_STREAMING);
101 + goto err_power_down;
103 ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT,
104 OV7251_SC_MODE_SELECT_SW_STANDBY);
105 + pm_runtime_put(ov7251->dev);
109 mutex_unlock(&ov7251->lock);
113 + pm_runtime_put_noidle(ov7251->dev);
114 + mutex_unlock(&ov7251->lock);
118 @@ -1615,23 +1647,24 @@ static int ov7251_probe(struct i2c_clien
122 - ret = ov7251_s_power(&ov7251->sd, true);
124 - dev_err(dev, "could not power up OV7251\n");
125 + ret = ov7251_set_power_on(ov7251);
130 ret = ov7251_detect_chip(ov7251);
134 + pm_runtime_set_active(&client->dev);
135 + pm_runtime_get_noresume(&client->dev);
136 + pm_runtime_enable(&client->dev);
138 ret = ov7251_read_reg(ov7251, OV7251_PRE_ISP_00,
139 &ov7251->pre_isp_00);
141 dev_err(dev, "could not read test pattern value\n");
144 + goto err_pm_runtime;
147 ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT1,
148 @@ -1639,7 +1672,7 @@ static int ov7251_probe(struct i2c_clien
150 dev_err(dev, "could not read vflip value\n");
153 + goto err_pm_runtime;
156 ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT2,
157 @@ -1647,10 +1680,12 @@ static int ov7251_probe(struct i2c_clien
159 dev_err(dev, "could not read hflip value\n");
162 + goto err_pm_runtime;
165 - ov7251_s_power(&ov7251->sd, false);
166 + pm_runtime_set_autosuspend_delay(&client->dev, 1000);
167 + pm_runtime_use_autosuspend(&client->dev);
168 + pm_runtime_put_autosuspend(&client->dev);
170 ret = v4l2_async_register_subdev(&ov7251->sd);
172 @@ -1662,6 +1697,9 @@ static int ov7251_probe(struct i2c_clien
177 + pm_runtime_disable(ov7251->dev);
178 + pm_runtime_put_noidle(ov7251->dev);
180 ov7251_s_power(&ov7251->sd, false);
182 @@ -1683,9 +1721,18 @@ static int ov7251_remove(struct i2c_clie
183 v4l2_ctrl_handler_free(&ov7251->ctrls);
184 mutex_destroy(&ov7251->lock);
186 + pm_runtime_disable(ov7251->dev);
187 + if (!pm_runtime_status_suspended(ov7251->dev))
188 + ov7251_set_power_off(ov7251);
189 + pm_runtime_set_suspended(ov7251->dev);
194 +static const struct dev_pm_ops ov7251_pm_ops = {
195 + SET_RUNTIME_PM_OPS(ov7251_sensor_suspend, ov7251_sensor_resume, NULL)
198 static const struct of_device_id ov7251_of_match[] = {
199 { .compatible = "ovti,ov7251" },
201 @@ -1703,6 +1750,7 @@ static struct i2c_driver ov7251_i2c_driv
202 .of_match_table = ov7251_of_match,
203 .acpi_match_table = ov7251_acpi_match,
205 + .pm = &ov7251_pm_ops,
207 .probe_new = ov7251_probe,
208 .remove = ov7251_remove,