1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Intel Corporation
5 * DW9807 is a 10-bit DAC driver, capable of sinking up to 100mA.
7 * DW9817 is a bidirectional 10-bit driver, driving up to +/- 100mA.
8 * Operationally it is identical to DW9807, except that the idle position is
9 * the mid-point, not 0.
12 #include <linux/acpi.h>
13 #include <linux/delay.h>
14 #include <linux/i2c.h>
15 #include <linux/iopoll.h>
16 #include <linux/module.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regulator/consumer.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-device.h>
22 #define DW9807_MAX_FOCUS_POS 1023
24 * This sets the minimum granularity for the focus positions.
25 * A value of 1 gives maximum accuracy for a desired focus position.
27 #define DW9807_FOCUS_STEPS 1
29 * This acts as the minimum granularity of lens movement.
30 * Keep this value power of 2, so the control steps can be
31 * uniformly adjusted for gradual lens movement, with desired
32 * number of control steps.
34 #define DW9807_CTRL_STEPS 16
35 #define DW9807_CTRL_DELAY_US 1000
37 #define DW9807_CTL_ADDR 0x02
39 * DW9807 separates two registers to control the VCM position.
40 * One for MSB value, another is LSB value.
42 #define DW9807_MSB_ADDR 0x03
43 #define DW9807_LSB_ADDR 0x04
44 #define DW9807_STATUS_ADDR 0x05
45 #define DW9807_MODE_ADDR 0x06
46 #define DW9807_RESONANCE_ADDR 0x07
50 #define DW9807_PW_MIN_DELAY_US 100
51 #define DW9807_PW_DELAY_RANGE_US 10
54 unsigned int idle_pos;
55 unsigned int default_pos;
58 struct dw9807_device {
59 struct v4l2_ctrl_handler ctrls_vcm;
60 struct v4l2_subdev sd;
63 struct regulator *vdd;
64 struct notifier_block notifier;
67 static inline struct dw9807_device *sd_to_dw9807_vcm(
68 struct v4l2_subdev *subdev)
70 return container_of(subdev, struct dw9807_device, sd);
73 static int dw9807_i2c_check(struct i2c_client *client)
75 const char status_addr = DW9807_STATUS_ADDR;
79 ret = i2c_master_send(client, &status_addr, sizeof(status_addr));
81 dev_err(&client->dev, "I2C write STATUS address fail ret = %d\n",
86 ret = i2c_master_recv(client, &status_result, sizeof(status_result));
88 dev_err(&client->dev, "I2C read STATUS value fail ret = %d\n",
96 static int dw9807_set_dac(struct i2c_client *client, u16 data)
98 const char tx_data[3] = {
99 DW9807_MSB_ADDR, ((data >> 8) & 0x03), (data & 0xff)
104 * According to the datasheet, need to check the bus status before we
105 * write VCM position. This ensure that we really write the value
108 ret = readx_poll_timeout(dw9807_i2c_check, client, val, val <= 0,
109 DW9807_CTRL_DELAY_US, MAX_RETRY * DW9807_CTRL_DELAY_US);
111 if (ret || val < 0) {
113 dev_warn(&client->dev,
114 "Cannot do the write operation because VCM is busy\n");
117 return ret ? -EBUSY : val;
120 /* Write VCM position to registers */
121 ret = i2c_master_send(client, tx_data, sizeof(tx_data));
123 dev_err(&client->dev,
124 "I2C write MSB fail ret=%d\n", ret);
133 * The lens position is gradually moved in units of DW9807_CTRL_STEPS,
134 * to make the movements smoothly. In all cases, even when "start" and
135 * "end" are the same, the lens will be set to the "end" position.
137 * (We don't use hardware slew rate control, because it differs widely
138 * between otherwise-compatible ICs, and may need lens-specific tuning.)
140 static int dw9807_ramp(struct i2c_client *client, int start, int end)
145 step = DW9807_CTRL_STEPS;
147 step = -DW9807_CTRL_STEPS;
152 if (step * (val - end) >= 0)
154 ret = dw9807_set_dac(client, val);
156 dev_err_ratelimited(&client->dev, "%s I2C failure: %d",
160 usleep_range(DW9807_CTRL_DELAY_US, DW9807_CTRL_DELAY_US + 10);
166 static int dw9807_active(struct dw9807_device *dw9807_dev)
168 struct i2c_client *client = v4l2_get_subdevdata(&dw9807_dev->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 return dw9807_ramp(client, dw9807_dev->idle_pos, dw9807_dev->current_val);
182 static int dw9807_standby(struct dw9807_device *dw9807_dev)
184 struct i2c_client *client = v4l2_get_subdevdata(&dw9807_dev->sd);
185 const char tx_data[2] = { DW9807_CTL_ADDR, 0x01 };
188 if (abs(dw9807_dev->current_val - dw9807_dev->idle_pos) > DW9807_CTRL_STEPS)
189 dw9807_ramp(client, dw9807_dev->current_val, dw9807_dev->idle_pos);
192 ret = i2c_master_send(client, tx_data, sizeof(tx_data));
194 dev_err(&client->dev, "I2C write CTL fail ret = %d\n", ret);
201 static int dw9807_regulator_event(struct notifier_block *nb,
202 unsigned long action, void *data)
204 struct dw9807_device *dw9807_dev =
205 container_of(nb, struct dw9807_device, notifier);
207 if (action & REGULATOR_EVENT_ENABLE) {
209 * Initialisation delay between VDD low->high and the moment
210 * when the i2c command is available.
211 * From the datasheet, it should be 10ms + 2ms (max power
212 * up sequence duration)
214 usleep_range(DW9807_PW_MIN_DELAY_US,
215 DW9807_PW_MIN_DELAY_US +
216 DW9807_PW_DELAY_RANGE_US);
218 dw9807_active(dw9807_dev);
219 } else if (action & REGULATOR_EVENT_PRE_DISABLE) {
220 dw9807_standby(dw9807_dev);
226 static int dw9807_set_ctrl(struct v4l2_ctrl *ctrl)
228 struct dw9807_device *dev_vcm = container_of(ctrl->handler,
229 struct dw9807_device, ctrls_vcm);
231 if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) {
232 struct i2c_client *client = v4l2_get_subdevdata(&dev_vcm->sd);
234 dev_vcm->current_val = ctrl->val;
235 return dw9807_ramp(client, ctrl->val, ctrl->val);
241 static const struct v4l2_ctrl_ops dw9807_vcm_ctrl_ops = {
242 .s_ctrl = dw9807_set_ctrl,
245 static int dw9807_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
247 return pm_runtime_resume_and_get(sd->dev);
250 static int dw9807_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
252 pm_runtime_put(sd->dev);
257 static const struct v4l2_subdev_internal_ops dw9807_int_ops = {
259 .close = dw9807_close,
262 static const struct v4l2_subdev_ops dw9807_ops = { };
264 static void dw9807_subdev_cleanup(struct dw9807_device *dw9807_dev)
266 v4l2_async_unregister_subdev(&dw9807_dev->sd);
267 v4l2_ctrl_handler_free(&dw9807_dev->ctrls_vcm);
268 media_entity_cleanup(&dw9807_dev->sd.entity);
271 static int dw9807_init_controls(struct dw9807_device *dev_vcm)
273 struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
274 const struct v4l2_ctrl_ops *ops = &dw9807_vcm_ctrl_ops;
275 struct i2c_client *client = v4l2_get_subdevdata(&dev_vcm->sd);
277 v4l2_ctrl_handler_init(hdl, 1);
279 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
280 0, DW9807_MAX_FOCUS_POS, DW9807_FOCUS_STEPS,
281 dev_vcm->current_val);
283 dev_vcm->sd.ctrl_handler = hdl;
285 dev_err(&client->dev, "%s fail error: 0x%x\n",
286 __func__, hdl->error);
293 /* Compatible devices; in fact there are many similar chips.
294 * "data" holds the powered-off (zero current) lens position and a
295 * default/initial control value (which need not be the same as the powered-off
298 static const struct dw9807_cfg dw9807_cfg = {
303 static const struct dw9807_cfg dw9817_cfg = {
308 static const struct of_device_id dw9807_of_table[] = {
309 { .compatible = "dongwoon,dw9807-vcm", .data = &dw9807_cfg },
310 { .compatible = "dongwoon,dw9817-vcm", .data = &dw9817_cfg },
314 static int dw9807_probe(struct i2c_client *client)
316 struct dw9807_device *dw9807_dev;
317 const struct of_device_id *match;
318 const struct dw9807_cfg *cfg;
321 dw9807_dev = devm_kzalloc(&client->dev, sizeof(*dw9807_dev),
323 if (dw9807_dev == NULL)
326 dw9807_dev->vdd = devm_regulator_get_optional(&client->dev, "VDD");
327 if (IS_ERR(dw9807_dev->vdd)) {
328 if (PTR_ERR(dw9807_dev->vdd) != -ENODEV)
329 return PTR_ERR(dw9807_dev->vdd);
331 dw9807_dev->vdd = NULL;
333 dw9807_dev->notifier.notifier_call = dw9807_regulator_event;
335 rval = regulator_register_notifier(dw9807_dev->vdd,
336 &dw9807_dev->notifier);
338 dev_err(&client->dev,
339 "could not register regulator notifier\n");
344 match = i2c_of_match_device(dw9807_of_table, client);
346 cfg = (const struct dw9807_cfg *)match->data;
347 dw9807_dev->idle_pos = cfg->idle_pos;
348 dw9807_dev->current_val = cfg->default_pos;
351 v4l2_i2c_subdev_init(&dw9807_dev->sd, client, &dw9807_ops);
352 dw9807_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
353 dw9807_dev->sd.internal_ops = &dw9807_int_ops;
355 rval = dw9807_init_controls(dw9807_dev);
359 rval = media_entity_pads_init(&dw9807_dev->sd.entity, 0, NULL);
363 dw9807_dev->sd.entity.function = MEDIA_ENT_F_LENS;
365 rval = v4l2_async_register_subdev(&dw9807_dev->sd);
369 pm_runtime_set_active(&client->dev);
370 pm_runtime_enable(&client->dev);
371 pm_runtime_idle(&client->dev);
376 v4l2_ctrl_handler_free(&dw9807_dev->ctrls_vcm);
377 media_entity_cleanup(&dw9807_dev->sd.entity);
382 static int dw9807_remove(struct i2c_client *client)
384 struct v4l2_subdev *sd = i2c_get_clientdata(client);
385 struct dw9807_device *dw9807_dev = sd_to_dw9807_vcm(sd);
388 regulator_unregister_notifier(dw9807_dev->vdd,
389 &dw9807_dev->notifier);
391 pm_runtime_disable(&client->dev);
393 dw9807_subdev_cleanup(dw9807_dev);
399 * This function sets the vcm position, so it consumes least current
400 * The lens position is gradually moved in units of DW9807_CTRL_STEPS,
401 * to make the movements smoothly.
403 static int __maybe_unused dw9807_vcm_suspend(struct device *dev)
405 struct i2c_client *client = to_i2c_client(dev);
406 struct v4l2_subdev *sd = i2c_get_clientdata(client);
407 struct dw9807_device *dw9807_dev = sd_to_dw9807_vcm(sd);
410 return regulator_disable(dw9807_dev->vdd);
412 return dw9807_standby(dw9807_dev);
416 * This function sets the vcm position to the value set by the user
417 * through v4l2_ctrl_ops s_ctrl handler
418 * The lens position is gradually moved in units of DW9807_CTRL_STEPS,
419 * to make the movements smoothly.
421 static int __maybe_unused dw9807_vcm_resume(struct device *dev)
423 struct i2c_client *client = to_i2c_client(dev);
424 struct v4l2_subdev *sd = i2c_get_clientdata(client);
425 struct dw9807_device *dw9807_dev = sd_to_dw9807_vcm(sd);
428 return regulator_enable(dw9807_dev->vdd);
430 return dw9807_active(dw9807_dev);
433 MODULE_DEVICE_TABLE(of, dw9807_of_table);
435 static const struct dev_pm_ops dw9807_pm_ops = {
436 SET_SYSTEM_SLEEP_PM_OPS(dw9807_vcm_suspend, dw9807_vcm_resume)
437 SET_RUNTIME_PM_OPS(dw9807_vcm_suspend, dw9807_vcm_resume, NULL)
440 static struct i2c_driver dw9807_i2c_driver = {
443 .pm = &dw9807_pm_ops,
444 .of_match_table = dw9807_of_table,
446 .probe_new = dw9807_probe,
447 .remove = dw9807_remove,
450 module_i2c_driver(dw9807_i2c_driver);
452 MODULE_AUTHOR("Chiang, Alan");
453 MODULE_DESCRIPTION("DW9807 VCM driver");
454 MODULE_LICENSE("GPL v2");