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
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.
15 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
17 drivers/media/i2c/dw9807-vcm.c | 113 ++++++++++++++++++++++++++-------
18 1 file changed, 90 insertions(+), 23 deletions(-)
20 --- a/drivers/media/i2c/dw9807-vcm.c
21 +++ b/drivers/media/i2c/dw9807-vcm.c
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>
34 +#define DW9807_PW_MIN_DELAY_US 100
35 +#define DW9807_PW_DELAY_RANGE_US 10
38 unsigned int idle_pos;
39 unsigned int default_pos;
40 @@ -56,6 +60,8 @@ struct dw9807_device {
41 struct v4l2_subdev sd;
44 + struct regulator *vdd;
45 + struct notifier_block notifier;
48 static inline struct dw9807_device *sd_to_dw9807_vcm(
49 @@ -157,6 +163,66 @@ static int dw9807_ramp(struct i2c_client
53 +static int dw9807_active(struct dw9807_device *dw9807_dev)
55 + struct i2c_client *client = v4l2_get_subdevdata(&dw9807_dev->sd);
56 + const char tx_data[2] = { DW9807_CTL_ADDR, 0x00 };
60 + ret = i2c_master_send(client, tx_data, sizeof(tx_data));
62 + dev_err(&client->dev, "I2C write CTL fail ret = %d\n", ret);
66 + return dw9807_ramp(client, dw9807_dev->idle_pos, dw9807_dev->current_val);
69 +static int dw9807_standby(struct dw9807_device *dw9807_dev)
71 + struct i2c_client *client = v4l2_get_subdevdata(&dw9807_dev->sd);
72 + const char tx_data[2] = { DW9807_CTL_ADDR, 0x01 };
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);
79 + ret = i2c_master_send(client, tx_data, sizeof(tx_data));
81 + dev_err(&client->dev, "I2C write CTL fail ret = %d\n", ret);
88 +static int dw9807_regulator_event(struct notifier_block *nb,
89 + unsigned long action, void *data)
91 + struct dw9807_device *dw9807_dev =
92 + container_of(nb, struct dw9807_device, notifier);
94 + if (action & REGULATOR_EVENT_ENABLE) {
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)
101 + usleep_range(DW9807_PW_MIN_DELAY_US,
102 + DW9807_PW_MIN_DELAY_US +
103 + DW9807_PW_DELAY_RANGE_US);
105 + dw9807_active(dw9807_dev);
106 + } else if (action & REGULATOR_EVENT_PRE_DISABLE) {
107 + dw9807_standby(dw9807_dev);
113 static int dw9807_set_ctrl(struct v4l2_ctrl *ctrl)
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)
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);
125 + dw9807_dev->vdd = NULL;
127 + dw9807_dev->notifier.notifier_call = dw9807_regulator_event;
129 + rval = regulator_register_notifier(dw9807_dev->vdd,
130 + &dw9807_dev->notifier);
132 + dev_err(&client->dev,
133 + "could not register regulator notifier\n");
138 match = i2c_of_match_device(dw9807_of_table, client);
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 };
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);
152 - ret = i2c_master_send(client, tx_data, sizeof(tx_data));
154 - dev_err(&client->dev, "I2C write CTL fail ret = %d\n", ret);
157 + if (dw9807_dev->vdd)
158 + return regulator_disable(dw9807_dev->vdd);
161 + return dw9807_standby(dw9807_dev);
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 };
173 - ret = i2c_master_send(client, tx_data, sizeof(tx_data));
175 - dev_err(&client->dev, "I2C write CTL fail ret = %d\n", ret);
179 - dw9807_ramp(client, dw9807_dev->idle_pos, dw9807_dev->current_val);
180 + if (dw9807_dev->vdd)
181 + return regulator_enable(dw9807_dev->vdd);
184 + return dw9807_active(dw9807_dev);
187 MODULE_DEVICE_TABLE(of, dw9807_of_table);