bcm27xx: add support for linux v5.15
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-5.15 / 950-0724-media-i2c-Add-pm_runtime-support-to-ov7251.patch
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
5
6 Add pm_runtime support to the ov7251 driver.
7
8 Signed-off-by: Daniel Scally <djrscally@gmail.com>
9 ---
10 drivers/media/i2c/ov7251.c | 78 ++++++++++++++++++++++++++++++--------
11 1 file changed, 63 insertions(+), 15 deletions(-)
12
13 --- a/drivers/media/i2c/ov7251.c
14 +++ b/drivers/media/i2c/ov7251.c
15 @@ -15,6 +15,7 @@
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);
25 }
26
27 +static int __maybe_unused ov7251_sensor_suspend(struct device *dev)
28 +{
29 + struct v4l2_subdev *sd = dev_get_drvdata(dev);
30 + struct ov7251 *ov7251 = to_ov7251(sd);
31 +
32 + ov7251_set_power_off(ov7251);
33 +
34 + return 0;
35 +}
36 +
37 +static int __maybe_unused ov7251_sensor_resume(struct device *dev)
38 +{
39 + struct v4l2_subdev *sd = dev_get_drvdata(dev);
40 + struct ov7251 *ov7251 = to_ov7251(sd);
41 +
42 + return ov7251_set_power_on(ov7251);
43 +}
44 +
45 static int ov7251_s_power(struct v4l2_subdev *sd, int on)
46 {
47 struct ov7251 *ov7251 = to_ov7251(sd);
48 @@ -985,7 +1004,7 @@ static int ov7251_s_ctrl(struct v4l2_ctr
49
50 /* v4l2_ctrl_lock() locks our mutex */
51
52 - if (!ov7251->power_on)
53 + if (!pm_runtime_get_if_in_use(ov7251->dev))
54 return 0;
55
56 switch (ctrl->id) {
57 @@ -1009,6 +1028,8 @@ static int ov7251_s_ctrl(struct v4l2_ctr
58 break;
59 }
60
61 + pm_runtime_put(ov7251->dev);
62 +
63 return ret;
64 }
65
66 @@ -1261,10 +1282,15 @@ static int ov7251_s_stream(struct v4l2_s
67 mutex_lock(&ov7251->lock);
68
69 if (enable) {
70 + ret = pm_runtime_get_sync(ov7251->dev);
71 + if (ret < 0)
72 + return ret;
73 +
74 ret = ov7251_pll_configure(ov7251);
75 - if (ret)
76 - return dev_err_probe(ov7251->dev, ret,
77 - "error configuring PLLs\n");
78 + if (ret) {
79 + dev_err(ov7251->dev, "error configuring PLLs\n");
80 + goto err_power_down;
81 + }
82
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);
89 - goto exit;
90 + goto err_power_down;
91 }
92 ret = __v4l2_ctrl_handler_setup(&ov7251->ctrls);
93 if (ret < 0) {
94 dev_err(ov7251->dev, "could not sync v4l2 controls\n");
95 - goto exit;
96 + goto err_power_down;
97 }
98 ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT,
99 OV7251_SC_MODE_SELECT_STREAMING);
100 + if (ret)
101 + goto err_power_down;
102 } else {
103 ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT,
104 OV7251_SC_MODE_SELECT_SW_STANDBY);
105 + pm_runtime_put(ov7251->dev);
106 }
107
108 -exit:
109 mutex_unlock(&ov7251->lock);
110 + return ret;
111
112 +err_power_down:
113 + pm_runtime_put_noidle(ov7251->dev);
114 + mutex_unlock(&ov7251->lock);
115 return ret;
116 }
117
118 @@ -1615,23 +1647,24 @@ static int ov7251_probe(struct i2c_clien
119 goto free_ctrl;
120 }
121
122 - ret = ov7251_s_power(&ov7251->sd, true);
123 - if (ret < 0) {
124 - dev_err(dev, "could not power up OV7251\n");
125 + ret = ov7251_set_power_on(ov7251);
126 + if (ret)
127 goto free_entity;
128 - }
129
130 ret = ov7251_detect_chip(ov7251);
131 if (ret)
132 goto power_down;
133
134 + pm_runtime_set_active(&client->dev);
135 + pm_runtime_get_noresume(&client->dev);
136 + pm_runtime_enable(&client->dev);
137
138 ret = ov7251_read_reg(ov7251, OV7251_PRE_ISP_00,
139 &ov7251->pre_isp_00);
140 if (ret < 0) {
141 dev_err(dev, "could not read test pattern value\n");
142 ret = -ENODEV;
143 - goto power_down;
144 + goto err_pm_runtime;
145 }
146
147 ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT1,
148 @@ -1639,7 +1672,7 @@ static int ov7251_probe(struct i2c_clien
149 if (ret < 0) {
150 dev_err(dev, "could not read vflip value\n");
151 ret = -ENODEV;
152 - goto power_down;
153 + goto err_pm_runtime;
154 }
155
156 ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT2,
157 @@ -1647,10 +1680,12 @@ static int ov7251_probe(struct i2c_clien
158 if (ret < 0) {
159 dev_err(dev, "could not read hflip value\n");
160 ret = -ENODEV;
161 - goto power_down;
162 + goto err_pm_runtime;
163 }
164
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);
169
170 ret = v4l2_async_register_subdev(&ov7251->sd);
171 if (ret < 0) {
172 @@ -1662,6 +1697,9 @@ static int ov7251_probe(struct i2c_clien
173
174 return 0;
175
176 +err_pm_runtime:
177 + pm_runtime_disable(ov7251->dev);
178 + pm_runtime_put_noidle(ov7251->dev);
179 power_down:
180 ov7251_s_power(&ov7251->sd, false);
181 free_entity:
182 @@ -1683,9 +1721,18 @@ static int ov7251_remove(struct i2c_clie
183 v4l2_ctrl_handler_free(&ov7251->ctrls);
184 mutex_destroy(&ov7251->lock);
185
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);
190 +
191 return 0;
192 }
193
194 +static const struct dev_pm_ops ov7251_pm_ops = {
195 + SET_RUNTIME_PM_OPS(ov7251_sensor_suspend, ov7251_sensor_resume, NULL)
196 +};
197 +
198 static const struct of_device_id ov7251_of_match[] = {
199 { .compatible = "ovti,ov7251" },
200 { /* sentinel */ }
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,
204 .name = "ov7251",
205 + .pm = &ov7251_pm_ops,
206 },
207 .probe_new = ov7251_probe,
208 .remove = ov7251_remove,