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;
68 static inline struct dw9807_device *sd_to_dw9807_vcm(
69 struct v4l2_subdev *subdev)
71 return container_of(subdev, struct dw9807_device, sd);
74 static int dw9807_i2c_check(struct i2c_client *client)
76 const char status_addr = DW9807_STATUS_ADDR;
80 ret = i2c_master_send(client, &status_addr, sizeof(status_addr));
82 dev_err(&client->dev, "I2C write STATUS address fail ret = %d\n",
87 ret = i2c_master_recv(client, &status_result, sizeof(status_result));
89 dev_err(&client->dev, "I2C read STATUS value fail ret = %d\n",
97 static int dw9807_set_dac(struct i2c_client *client, u16 data)
99 const char tx_data[3] = {
100 DW9807_MSB_ADDR, ((data >> 8) & 0x03), (data & 0xff)
105 * According to the datasheet, need to check the bus status before we
106 * write VCM position. This ensure that we really write the value
109 ret = readx_poll_timeout(dw9807_i2c_check, client, val, val <= 0,
110 DW9807_CTRL_DELAY_US, MAX_RETRY * DW9807_CTRL_DELAY_US);
112 if (ret || val < 0) {
114 dev_warn(&client->dev,
115 "Cannot do the write operation because VCM is busy\n");
118 return ret ? -EBUSY : val;
121 /* Write VCM position to registers */
122 ret = i2c_master_send(client, tx_data, sizeof(tx_data));
124 dev_err(&client->dev,
125 "I2C write MSB fail ret=%d\n", ret);
134 * The lens position is gradually moved in units of DW9807_CTRL_STEPS,
135 * to make the movements smoothly. In all cases, even when "start" and
136 * "end" are the same, the lens will be set to the "end" position.
138 * (We don't use hardware slew rate control, because it differs widely
139 * between otherwise-compatible ICs, and may need lens-specific tuning.)
141 static int dw9807_ramp(struct i2c_client *client, int start, int end)
146 step = DW9807_CTRL_STEPS;
148 step = -DW9807_CTRL_STEPS;
153 if (step * (val - end) >= 0)
155 ret = dw9807_set_dac(client, val);
157 dev_err_ratelimited(&client->dev, "%s I2C failure: %d",
161 usleep_range(DW9807_CTRL_DELAY_US, DW9807_CTRL_DELAY_US + 10);
167 static int dw9807_active(struct dw9807_device *dw9807_dev)
169 struct i2c_client *client = v4l2_get_subdevdata(&dw9807_dev->sd);
170 const char tx_data[2] = { DW9807_CTL_ADDR, 0x00 };
174 ret = i2c_master_send(client, tx_data, sizeof(tx_data));
176 dev_err(&client->dev, "I2C write CTL fail ret = %d\n", ret);
180 dw9807_dev->first = true;
182 return dw9807_ramp(client, dw9807_dev->idle_pos, dw9807_dev->current_val);
185 static int dw9807_standby(struct dw9807_device *dw9807_dev)
187 struct i2c_client *client = v4l2_get_subdevdata(&dw9807_dev->sd);
188 const char tx_data[2] = { DW9807_CTL_ADDR, 0x01 };
191 if (abs(dw9807_dev->current_val - dw9807_dev->idle_pos) > DW9807_CTRL_STEPS)
192 dw9807_ramp(client, dw9807_dev->current_val, dw9807_dev->idle_pos);
195 ret = i2c_master_send(client, tx_data, sizeof(tx_data));
197 dev_err(&client->dev, "I2C write CTL fail ret = %d\n", ret);
204 static int dw9807_regulator_event(struct notifier_block *nb,
205 unsigned long action, void *data)
207 struct dw9807_device *dw9807_dev =
208 container_of(nb, struct dw9807_device, notifier);
210 if (action & REGULATOR_EVENT_ENABLE) {
212 * Initialisation delay between VDD low->high and the moment
213 * when the i2c command is available.
214 * From the datasheet, it should be 10ms + 2ms (max power
215 * up sequence duration)
217 usleep_range(DW9807_PW_MIN_DELAY_US,
218 DW9807_PW_MIN_DELAY_US +
219 DW9807_PW_DELAY_RANGE_US);
221 dw9807_active(dw9807_dev);
222 } else if (action & REGULATOR_EVENT_PRE_DISABLE) {
223 dw9807_standby(dw9807_dev);
229 static int dw9807_set_ctrl(struct v4l2_ctrl *ctrl)
231 struct dw9807_device *dev_vcm = container_of(ctrl->handler,
232 struct dw9807_device, ctrls_vcm);
234 if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) {
235 struct i2c_client *client = v4l2_get_subdevdata(&dev_vcm->sd);
236 int start = (dev_vcm->first) ? dev_vcm->current_val : ctrl->val;
238 dev_vcm->first = false;
239 dev_vcm->current_val = ctrl->val;
240 return dw9807_ramp(client, start, ctrl->val);
246 static const struct v4l2_ctrl_ops dw9807_vcm_ctrl_ops = {
247 .s_ctrl = dw9807_set_ctrl,
250 static int dw9807_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
252 return pm_runtime_resume_and_get(sd->dev);
255 static int dw9807_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
257 pm_runtime_put(sd->dev);
262 static const struct v4l2_subdev_internal_ops dw9807_int_ops = {
264 .close = dw9807_close,
267 static const struct v4l2_subdev_ops dw9807_ops = { };
269 static void dw9807_subdev_cleanup(struct dw9807_device *dw9807_dev)
271 v4l2_async_unregister_subdev(&dw9807_dev->sd);
272 v4l2_ctrl_handler_free(&dw9807_dev->ctrls_vcm);
273 media_entity_cleanup(&dw9807_dev->sd.entity);
276 static int dw9807_init_controls(struct dw9807_device *dev_vcm)
278 struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
279 const struct v4l2_ctrl_ops *ops = &dw9807_vcm_ctrl_ops;
280 struct i2c_client *client = v4l2_get_subdevdata(&dev_vcm->sd);
282 v4l2_ctrl_handler_init(hdl, 1);
284 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
285 0, DW9807_MAX_FOCUS_POS, DW9807_FOCUS_STEPS,
286 dev_vcm->current_val);
288 dev_vcm->sd.ctrl_handler = hdl;
290 dev_err(&client->dev, "%s fail error: 0x%x\n",
291 __func__, hdl->error);
298 /* Compatible devices; in fact there are many similar chips.
299 * "data" holds the powered-off (zero current) lens position and a
300 * default/initial control value (which need not be the same as the powered-off
303 static const struct dw9807_cfg dw9807_cfg = {
308 static const struct dw9807_cfg dw9817_cfg = {
313 static const struct of_device_id dw9807_of_table[] = {
314 { .compatible = "dongwoon,dw9807-vcm", .data = &dw9807_cfg },
315 { .compatible = "dongwoon,dw9817-vcm", .data = &dw9817_cfg },
319 static int dw9807_probe(struct i2c_client *client)
321 struct dw9807_device *dw9807_dev;
322 const struct of_device_id *match;
323 const struct dw9807_cfg *cfg;
326 dw9807_dev = devm_kzalloc(&client->dev, sizeof(*dw9807_dev),
328 if (dw9807_dev == NULL)
331 dw9807_dev->vdd = devm_regulator_get_optional(&client->dev, "VDD");
332 if (IS_ERR(dw9807_dev->vdd)) {
333 if (PTR_ERR(dw9807_dev->vdd) != -ENODEV)
334 return PTR_ERR(dw9807_dev->vdd);
336 dw9807_dev->vdd = NULL;
338 dw9807_dev->notifier.notifier_call = dw9807_regulator_event;
340 rval = regulator_register_notifier(dw9807_dev->vdd,
341 &dw9807_dev->notifier);
343 dev_err(&client->dev,
344 "could not register regulator notifier\n");
349 match = i2c_of_match_device(dw9807_of_table, client);
351 cfg = (const struct dw9807_cfg *)match->data;
352 dw9807_dev->idle_pos = cfg->idle_pos;
353 dw9807_dev->current_val = cfg->default_pos;
356 v4l2_i2c_subdev_init(&dw9807_dev->sd, client, &dw9807_ops);
357 dw9807_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
358 dw9807_dev->sd.internal_ops = &dw9807_int_ops;
360 rval = dw9807_init_controls(dw9807_dev);
364 rval = media_entity_pads_init(&dw9807_dev->sd.entity, 0, NULL);
368 dw9807_dev->sd.entity.function = MEDIA_ENT_F_LENS;
370 rval = v4l2_async_register_subdev(&dw9807_dev->sd);
374 pm_runtime_set_active(&client->dev);
375 pm_runtime_enable(&client->dev);
376 pm_runtime_idle(&client->dev);
381 v4l2_ctrl_handler_free(&dw9807_dev->ctrls_vcm);
382 media_entity_cleanup(&dw9807_dev->sd.entity);
387 static int dw9807_remove(struct i2c_client *client)
389 struct v4l2_subdev *sd = i2c_get_clientdata(client);
390 struct dw9807_device *dw9807_dev = sd_to_dw9807_vcm(sd);
393 regulator_unregister_notifier(dw9807_dev->vdd,
394 &dw9807_dev->notifier);
396 pm_runtime_disable(&client->dev);
398 dw9807_subdev_cleanup(dw9807_dev);
404 * This function sets the vcm position, so it consumes least current
405 * The lens position is gradually moved in units of DW9807_CTRL_STEPS,
406 * to make the movements smoothly.
408 static int __maybe_unused dw9807_vcm_suspend(struct device *dev)
410 struct i2c_client *client = to_i2c_client(dev);
411 struct v4l2_subdev *sd = i2c_get_clientdata(client);
412 struct dw9807_device *dw9807_dev = sd_to_dw9807_vcm(sd);
415 return regulator_disable(dw9807_dev->vdd);
417 return dw9807_standby(dw9807_dev);
421 * This function sets the vcm position to the value set by the user
422 * through v4l2_ctrl_ops s_ctrl handler
423 * The lens position is gradually moved in units of DW9807_CTRL_STEPS,
424 * to make the movements smoothly.
426 static int __maybe_unused dw9807_vcm_resume(struct device *dev)
428 struct i2c_client *client = to_i2c_client(dev);
429 struct v4l2_subdev *sd = i2c_get_clientdata(client);
430 struct dw9807_device *dw9807_dev = sd_to_dw9807_vcm(sd);
433 return regulator_enable(dw9807_dev->vdd);
435 return dw9807_active(dw9807_dev);
438 MODULE_DEVICE_TABLE(of, dw9807_of_table);
440 static const struct dev_pm_ops dw9807_pm_ops = {
441 SET_SYSTEM_SLEEP_PM_OPS(dw9807_vcm_suspend, dw9807_vcm_resume)
442 SET_RUNTIME_PM_OPS(dw9807_vcm_suspend, dw9807_vcm_resume, NULL)
445 static struct i2c_driver dw9807_i2c_driver = {
448 .pm = &dw9807_pm_ops,
449 .of_match_table = dw9807_of_table,
451 .probe_new = dw9807_probe,
452 .remove = dw9807_remove,
455 module_i2c_driver(dw9807_i2c_driver);
457 MODULE_AUTHOR("Chiang, Alan");
458 MODULE_DESCRIPTION("DW9807 VCM driver");
459 MODULE_LICENSE("GPL v2");