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 <media/v4l2-ctrls.h>
19 #include <media/v4l2-device.h>
21 #define DW9807_MAX_FOCUS_POS 1023
23 * This sets the minimum granularity for the focus positions.
24 * A value of 1 gives maximum accuracy for a desired focus position.
26 #define DW9807_FOCUS_STEPS 1
28 * This acts as the minimum granularity of lens movement.
29 * Keep this value power of 2, so the control steps can be
30 * uniformly adjusted for gradual lens movement, with desired
31 * number of control steps.
33 #define DW9807_CTRL_STEPS 16
34 #define DW9807_CTRL_DELAY_US 1000
36 #define DW9807_CTL_ADDR 0x02
38 * DW9807 separates two registers to control the VCM position.
39 * One for MSB value, another is LSB value.
41 #define DW9807_MSB_ADDR 0x03
42 #define DW9807_LSB_ADDR 0x04
43 #define DW9807_STATUS_ADDR 0x05
44 #define DW9807_MODE_ADDR 0x06
45 #define DW9807_RESONANCE_ADDR 0x07
50 unsigned int idle_pos;
51 unsigned int default_pos;
54 struct dw9807_device {
55 struct v4l2_ctrl_handler ctrls_vcm;
56 struct v4l2_subdev sd;
61 static inline struct dw9807_device *sd_to_dw9807_vcm(
62 struct v4l2_subdev *subdev)
64 return container_of(subdev, struct dw9807_device, sd);
67 static int dw9807_i2c_check(struct i2c_client *client)
69 const char status_addr = DW9807_STATUS_ADDR;
73 ret = i2c_master_send(client, &status_addr, sizeof(status_addr));
75 dev_err(&client->dev, "I2C write STATUS address fail ret = %d\n",
80 ret = i2c_master_recv(client, &status_result, sizeof(status_result));
82 dev_err(&client->dev, "I2C read STATUS value fail ret = %d\n",
90 static int dw9807_set_dac(struct i2c_client *client, u16 data)
92 const char tx_data[3] = {
93 DW9807_MSB_ADDR, ((data >> 8) & 0x03), (data & 0xff)
98 * According to the datasheet, need to check the bus status before we
99 * write VCM position. This ensure that we really write the value
102 ret = readx_poll_timeout(dw9807_i2c_check, client, val, val <= 0,
103 DW9807_CTRL_DELAY_US, MAX_RETRY * DW9807_CTRL_DELAY_US);
105 if (ret || val < 0) {
107 dev_warn(&client->dev,
108 "Cannot do the write operation because VCM is busy\n");
111 return ret ? -EBUSY : val;
114 /* Write VCM position to registers */
115 ret = i2c_master_send(client, tx_data, sizeof(tx_data));
117 dev_err(&client->dev,
118 "I2C write MSB fail ret=%d\n", ret);
127 * The lens position is gradually moved in units of DW9807_CTRL_STEPS,
128 * to make the movements smoothly. In all cases, even when "start" and
129 * "end" are the same, the lens will be set to the "end" position.
131 * (We don't use hardware slew rate control, because it differs widely
132 * between otherwise-compatible ICs, and may need lens-specific tuning.)
134 static int dw9807_ramp(struct i2c_client *client, int start, int end)
139 step = DW9807_CTRL_STEPS;
141 step = -DW9807_CTRL_STEPS;
146 if (step * (val - end) >= 0)
148 ret = dw9807_set_dac(client, val);
150 dev_err_ratelimited(&client->dev, "%s I2C failure: %d",
154 usleep_range(DW9807_CTRL_DELAY_US, DW9807_CTRL_DELAY_US + 10);
160 static int dw9807_set_ctrl(struct v4l2_ctrl *ctrl)
162 struct dw9807_device *dev_vcm = container_of(ctrl->handler,
163 struct dw9807_device, ctrls_vcm);
165 if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) {
166 struct i2c_client *client = v4l2_get_subdevdata(&dev_vcm->sd);
168 dev_vcm->current_val = ctrl->val;
169 return dw9807_ramp(client, ctrl->val, ctrl->val);
175 static const struct v4l2_ctrl_ops dw9807_vcm_ctrl_ops = {
176 .s_ctrl = dw9807_set_ctrl,
179 static int dw9807_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
181 return pm_runtime_resume_and_get(sd->dev);
184 static int dw9807_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
186 pm_runtime_put(sd->dev);
191 static const struct v4l2_subdev_internal_ops dw9807_int_ops = {
193 .close = dw9807_close,
196 static const struct v4l2_subdev_ops dw9807_ops = { };
198 static void dw9807_subdev_cleanup(struct dw9807_device *dw9807_dev)
200 v4l2_async_unregister_subdev(&dw9807_dev->sd);
201 v4l2_ctrl_handler_free(&dw9807_dev->ctrls_vcm);
202 media_entity_cleanup(&dw9807_dev->sd.entity);
205 static int dw9807_init_controls(struct dw9807_device *dev_vcm)
207 struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
208 const struct v4l2_ctrl_ops *ops = &dw9807_vcm_ctrl_ops;
209 struct i2c_client *client = v4l2_get_subdevdata(&dev_vcm->sd);
211 v4l2_ctrl_handler_init(hdl, 1);
213 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
214 0, DW9807_MAX_FOCUS_POS, DW9807_FOCUS_STEPS,
215 dev_vcm->current_val);
217 dev_vcm->sd.ctrl_handler = hdl;
219 dev_err(&client->dev, "%s fail error: 0x%x\n",
220 __func__, hdl->error);
227 /* Compatible devices; in fact there are many similar chips.
228 * "data" holds the powered-off (zero current) lens position and a
229 * default/initial control value (which need not be the same as the powered-off
232 static const struct dw9807_cfg dw9807_cfg = {
237 static const struct dw9807_cfg dw9817_cfg = {
242 static const struct of_device_id dw9807_of_table[] = {
243 { .compatible = "dongwoon,dw9807-vcm", .data = &dw9807_cfg },
244 { .compatible = "dongwoon,dw9817-vcm", .data = &dw9817_cfg },
248 static int dw9807_probe(struct i2c_client *client)
250 struct dw9807_device *dw9807_dev;
251 const struct of_device_id *match;
252 const struct dw9807_cfg *cfg;
255 dw9807_dev = devm_kzalloc(&client->dev, sizeof(*dw9807_dev),
257 if (dw9807_dev == NULL)
260 match = i2c_of_match_device(dw9807_of_table, client);
262 cfg = (const struct dw9807_cfg *)match->data;
263 dw9807_dev->idle_pos = cfg->idle_pos;
264 dw9807_dev->current_val = cfg->default_pos;
267 v4l2_i2c_subdev_init(&dw9807_dev->sd, client, &dw9807_ops);
268 dw9807_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
269 dw9807_dev->sd.internal_ops = &dw9807_int_ops;
271 rval = dw9807_init_controls(dw9807_dev);
275 rval = media_entity_pads_init(&dw9807_dev->sd.entity, 0, NULL);
279 dw9807_dev->sd.entity.function = MEDIA_ENT_F_LENS;
281 rval = v4l2_async_register_subdev(&dw9807_dev->sd);
285 pm_runtime_set_active(&client->dev);
286 pm_runtime_enable(&client->dev);
287 pm_runtime_idle(&client->dev);
292 v4l2_ctrl_handler_free(&dw9807_dev->ctrls_vcm);
293 media_entity_cleanup(&dw9807_dev->sd.entity);
298 static int dw9807_remove(struct i2c_client *client)
300 struct v4l2_subdev *sd = i2c_get_clientdata(client);
301 struct dw9807_device *dw9807_dev = sd_to_dw9807_vcm(sd);
304 regulator_unregister_notifier(dw9807_dev->vdd,
305 &dw9807_dev->notifier);
307 pm_runtime_disable(&client->dev);
309 dw9807_subdev_cleanup(dw9807_dev);
315 * This function sets the vcm position, so it consumes least current
316 * The lens position is gradually moved in units of DW9807_CTRL_STEPS,
317 * to make the movements smoothly.
319 static int __maybe_unused dw9807_vcm_suspend(struct device *dev)
321 struct i2c_client *client = to_i2c_client(dev);
322 struct v4l2_subdev *sd = i2c_get_clientdata(client);
323 struct dw9807_device *dw9807_dev = sd_to_dw9807_vcm(sd);
324 const char tx_data[2] = { DW9807_CTL_ADDR, 0x01 };
327 if (abs(dw9807_dev->current_val - dw9807_dev->idle_pos) > DW9807_CTRL_STEPS)
328 dw9807_ramp(client, dw9807_dev->current_val, dw9807_dev->idle_pos);
331 ret = i2c_master_send(client, tx_data, sizeof(tx_data));
333 dev_err(&client->dev, "I2C write CTL fail ret = %d\n", ret);
341 * This function sets the vcm position to the value set by the user
342 * through v4l2_ctrl_ops s_ctrl handler
343 * The lens position is gradually moved in units of DW9807_CTRL_STEPS,
344 * to make the movements smoothly.
346 static int __maybe_unused dw9807_vcm_resume(struct device *dev)
348 struct i2c_client *client = to_i2c_client(dev);
349 struct v4l2_subdev *sd = i2c_get_clientdata(client);
350 struct dw9807_device *dw9807_dev = sd_to_dw9807_vcm(sd);
351 const char tx_data[2] = { DW9807_CTL_ADDR, 0x00 };
355 ret = i2c_master_send(client, tx_data, sizeof(tx_data));
357 dev_err(&client->dev, "I2C write CTL fail ret = %d\n", ret);
361 dw9807_ramp(client, dw9807_dev->idle_pos, dw9807_dev->current_val);
366 MODULE_DEVICE_TABLE(of, dw9807_of_table);
368 static const struct dev_pm_ops dw9807_pm_ops = {
369 SET_SYSTEM_SLEEP_PM_OPS(dw9807_vcm_suspend, dw9807_vcm_resume)
370 SET_RUNTIME_PM_OPS(dw9807_vcm_suspend, dw9807_vcm_resume, NULL)
373 static struct i2c_driver dw9807_i2c_driver = {
376 .pm = &dw9807_pm_ops,
377 .of_match_table = dw9807_of_table,
379 .probe_new = dw9807_probe,
380 .remove = dw9807_remove,
383 module_i2c_driver(dw9807_i2c_driver);
385 MODULE_AUTHOR("Chiang, Alan");
386 MODULE_DESCRIPTION("DW9807 VCM driver");
387 MODULE_LICENSE("GPL v2");