aa814c7e1c7a06d7d7ad336f004b4c0c46b5826d
[openwrt/staging/jow.git] /
1 From 441e1fbef6c33549520b52ff357682b6fe7e8646 Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.com>
3 Date: Tue, 3 Jan 2023 16:35:59 +0000
4 Subject: [PATCH] media: dw9807-vcm: Add regulator support to the
5 driver
6
7 Uses the regulator notifier framework so that the current
8 focus position will be restored whenever any user of the
9 regulator powers it up. This means that should the VCM
10 and sensor share a common regulator then starting the sensor
11 will automatically restore the default position. If they
12 have independent regulators then it will behave be powered
13 up when the VCM subdev is opened.
14
15 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
16 ---
17 drivers/media/i2c/dw9807-vcm.c | 113 ++++++++++++++++++++++++++-------
18 1 file changed, 90 insertions(+), 23 deletions(-)
19
20 --- a/drivers/media/i2c/dw9807-vcm.c
21 +++ b/drivers/media/i2c/dw9807-vcm.c
22 @@ -15,6 +15,7 @@
23 #include <linux/iopoll.h>
24 #include <linux/module.h>
25 #include <linux/pm_runtime.h>
26 +#include <linux/regulator/consumer.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-device.h>
29
30 @@ -46,6 +47,9 @@
31
32 #define MAX_RETRY 10
33
34 +#define DW9807_PW_MIN_DELAY_US 100
35 +#define DW9807_PW_DELAY_RANGE_US 10
36 +
37 struct dw9807_cfg {
38 unsigned int idle_pos;
39 unsigned int default_pos;
40 @@ -56,6 +60,8 @@ struct dw9807_device {
41 struct v4l2_subdev sd;
42 u16 current_val;
43 u16 idle_pos;
44 + struct regulator *vdd;
45 + struct notifier_block notifier;
46 };
47
48 static inline struct dw9807_device *sd_to_dw9807_vcm(
49 @@ -157,6 +163,66 @@ static int dw9807_ramp(struct i2c_client
50 return ret;
51 }
52
53 +static int dw9807_active(struct dw9807_device *dw9807_dev)
54 +{
55 + struct i2c_client *client = v4l2_get_subdevdata(&dw9807_dev->sd);
56 + const char tx_data[2] = { DW9807_CTL_ADDR, 0x00 };
57 + int ret;
58 +
59 + /* Power on */
60 + ret = i2c_master_send(client, tx_data, sizeof(tx_data));
61 + if (ret < 0) {
62 + dev_err(&client->dev, "I2C write CTL fail ret = %d\n", ret);
63 + return ret;
64 + }
65 +
66 + return dw9807_ramp(client, dw9807_dev->idle_pos, dw9807_dev->current_val);
67 +}
68 +
69 +static int dw9807_standby(struct dw9807_device *dw9807_dev)
70 +{
71 + struct i2c_client *client = v4l2_get_subdevdata(&dw9807_dev->sd);
72 + const char tx_data[2] = { DW9807_CTL_ADDR, 0x01 };
73 + int ret;
74 +
75 + if (abs(dw9807_dev->current_val - dw9807_dev->idle_pos) > DW9807_CTRL_STEPS)
76 + dw9807_ramp(client, dw9807_dev->current_val, dw9807_dev->idle_pos);
77 +
78 + /* Power down */
79 + ret = i2c_master_send(client, tx_data, sizeof(tx_data));
80 + if (ret < 0) {
81 + dev_err(&client->dev, "I2C write CTL fail ret = %d\n", ret);
82 + return ret;
83 + }
84 +
85 + return 0;
86 +}
87 +
88 +static int dw9807_regulator_event(struct notifier_block *nb,
89 + unsigned long action, void *data)
90 +{
91 + struct dw9807_device *dw9807_dev =
92 + container_of(nb, struct dw9807_device, notifier);
93 +
94 + if (action & REGULATOR_EVENT_ENABLE) {
95 + /*
96 + * Initialisation delay between VDD low->high and the moment
97 + * when the i2c command is available.
98 + * From the datasheet, it should be 10ms + 2ms (max power
99 + * up sequence duration)
100 + */
101 + usleep_range(DW9807_PW_MIN_DELAY_US,
102 + DW9807_PW_MIN_DELAY_US +
103 + DW9807_PW_DELAY_RANGE_US);
104 +
105 + dw9807_active(dw9807_dev);
106 + } else if (action & REGULATOR_EVENT_PRE_DISABLE) {
107 + dw9807_standby(dw9807_dev);
108 + }
109 +
110 + return 0;
111 +}
112 +
113 static int dw9807_set_ctrl(struct v4l2_ctrl *ctrl)
114 {
115 struct dw9807_device *dev_vcm = container_of(ctrl->handler,
116 @@ -257,6 +323,24 @@ static int dw9807_probe(struct i2c_clien
117 if (dw9807_dev == NULL)
118 return -ENOMEM;
119
120 + dw9807_dev->vdd = devm_regulator_get_optional(&client->dev, "VDD");
121 + if (IS_ERR(dw9807_dev->vdd)) {
122 + if (PTR_ERR(dw9807_dev->vdd) != -ENODEV)
123 + return PTR_ERR(dw9807_dev->vdd);
124 +
125 + dw9807_dev->vdd = NULL;
126 + } else {
127 + dw9807_dev->notifier.notifier_call = dw9807_regulator_event;
128 +
129 + rval = regulator_register_notifier(dw9807_dev->vdd,
130 + &dw9807_dev->notifier);
131 + if (rval) {
132 + dev_err(&client->dev,
133 + "could not register regulator notifier\n");
134 + return rval;
135 + }
136 + }
137 +
138 match = i2c_of_match_device(dw9807_of_table, client);
139 if (match) {
140 cfg = (const struct dw9807_cfg *)match->data;
141 @@ -320,20 +404,11 @@ static int __maybe_unused dw9807_vcm_sus
142 struct i2c_client *client = to_i2c_client(dev);
143 struct v4l2_subdev *sd = i2c_get_clientdata(client);
144 struct dw9807_device *dw9807_dev = sd_to_dw9807_vcm(sd);
145 - const char tx_data[2] = { DW9807_CTL_ADDR, 0x01 };
146 - int ret;
147 -
148 - if (abs(dw9807_dev->current_val - dw9807_dev->idle_pos) > DW9807_CTRL_STEPS)
149 - dw9807_ramp(client, dw9807_dev->current_val, dw9807_dev->idle_pos);
150
151 - /* Power down */
152 - ret = i2c_master_send(client, tx_data, sizeof(tx_data));
153 - if (ret < 0) {
154 - dev_err(&client->dev, "I2C write CTL fail ret = %d\n", ret);
155 - return ret;
156 - }
157 + if (dw9807_dev->vdd)
158 + return regulator_disable(dw9807_dev->vdd);
159
160 - return 0;
161 + return dw9807_standby(dw9807_dev);
162 }
163
164 /*
165 @@ -347,19 +422,11 @@ static int __maybe_unused dw9807_vcm_re
166 struct i2c_client *client = to_i2c_client(dev);
167 struct v4l2_subdev *sd = i2c_get_clientdata(client);
168 struct dw9807_device *dw9807_dev = sd_to_dw9807_vcm(sd);
169 - const char tx_data[2] = { DW9807_CTL_ADDR, 0x00 };
170 - int ret;
171 -
172 - /* Power on */
173 - ret = i2c_master_send(client, tx_data, sizeof(tx_data));
174 - if (ret < 0) {
175 - dev_err(&client->dev, "I2C write CTL fail ret = %d\n", ret);
176 - return ret;
177 - }
178
179 - dw9807_ramp(client, dw9807_dev->idle_pos, dw9807_dev->current_val);
180 + if (dw9807_dev->vdd)
181 + return regulator_enable(dw9807_dev->vdd);
182
183 - return 0;
184 + return dw9807_active(dw9807_dev);
185 }
186
187 MODULE_DEVICE_TABLE(of, dw9807_of_table);