1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Intel Corporation
4 #include <linux/acpi.h>
5 #include <linux/delay.h>
7 #include <linux/iopoll.h>
8 #include <linux/module.h>
9 #include <linux/pm_runtime.h>
10 #include <media/v4l2-ctrls.h>
11 #include <media/v4l2-device.h>
13 #define DW9807_MAX_FOCUS_POS 1023
15 * This sets the minimum granularity for the focus positions.
16 * A value of 1 gives maximum accuracy for a desired focus position.
18 #define DW9807_FOCUS_STEPS 1
20 * This acts as the minimum granularity of lens movement.
21 * Keep this value power of 2, so the control steps can be
22 * uniformly adjusted for gradual lens movement, with desired
23 * number of control steps.
25 #define DW9807_CTRL_STEPS 16
26 #define DW9807_CTRL_DELAY_US 1000
28 #define DW9807_CTL_ADDR 0x02
30 * DW9807 separates two registers to control the VCM position.
31 * One for MSB value, another is LSB value.
33 #define DW9807_MSB_ADDR 0x03
34 #define DW9807_LSB_ADDR 0x04
35 #define DW9807_STATUS_ADDR 0x05
36 #define DW9807_MODE_ADDR 0x06
37 #define DW9807_RESONANCE_ADDR 0x07
41 struct dw9807_device
{
42 struct v4l2_ctrl_handler ctrls_vcm
;
43 struct v4l2_subdev sd
;
47 static inline struct dw9807_device
*sd_to_dw9807_vcm(
48 struct v4l2_subdev
*subdev
)
50 return container_of(subdev
, struct dw9807_device
, sd
);
53 static int dw9807_i2c_check(struct i2c_client
*client
)
55 const char status_addr
= DW9807_STATUS_ADDR
;
59 ret
= i2c_master_send(client
, &status_addr
, sizeof(status_addr
));
61 dev_err(&client
->dev
, "I2C write STATUS address fail ret = %d\n",
66 ret
= i2c_master_recv(client
, &status_result
, sizeof(status_result
));
68 dev_err(&client
->dev
, "I2C read STATUS value fail ret = %d\n",
76 static int dw9807_set_dac(struct i2c_client
*client
, u16 data
)
78 const char tx_data
[3] = {
79 DW9807_MSB_ADDR
, ((data
>> 8) & 0x03), (data
& 0xff)
84 * According to the datasheet, need to check the bus status before we
85 * write VCM position. This ensure that we really write the value
88 ret
= readx_poll_timeout(dw9807_i2c_check
, client
, val
, val
<= 0,
89 DW9807_CTRL_DELAY_US
, MAX_RETRY
* DW9807_CTRL_DELAY_US
);
93 dev_warn(&client
->dev
,
94 "Cannot do the write operation because VCM is busy\n");
97 return ret
? -EBUSY
: val
;
100 /* Write VCM position to registers */
101 ret
= i2c_master_send(client
, tx_data
, sizeof(tx_data
));
103 dev_err(&client
->dev
,
104 "I2C write MSB fail ret=%d\n", ret
);
112 static int dw9807_set_ctrl(struct v4l2_ctrl
*ctrl
)
114 struct dw9807_device
*dev_vcm
= container_of(ctrl
->handler
,
115 struct dw9807_device
, ctrls_vcm
);
117 if (ctrl
->id
== V4L2_CID_FOCUS_ABSOLUTE
) {
118 struct i2c_client
*client
= v4l2_get_subdevdata(&dev_vcm
->sd
);
120 dev_vcm
->current_val
= ctrl
->val
;
121 return dw9807_set_dac(client
, ctrl
->val
);
127 static const struct v4l2_ctrl_ops dw9807_vcm_ctrl_ops
= {
128 .s_ctrl
= dw9807_set_ctrl
,
131 static int dw9807_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
133 return pm_runtime_resume_and_get(sd
->dev
);
136 static int dw9807_close(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
138 pm_runtime_put(sd
->dev
);
143 static const struct v4l2_subdev_internal_ops dw9807_int_ops
= {
145 .close
= dw9807_close
,
148 static const struct v4l2_subdev_ops dw9807_ops
= { };
150 static void dw9807_subdev_cleanup(struct dw9807_device
*dw9807_dev
)
152 v4l2_async_unregister_subdev(&dw9807_dev
->sd
);
153 v4l2_ctrl_handler_free(&dw9807_dev
->ctrls_vcm
);
154 media_entity_cleanup(&dw9807_dev
->sd
.entity
);
157 static int dw9807_init_controls(struct dw9807_device
*dev_vcm
)
159 struct v4l2_ctrl_handler
*hdl
= &dev_vcm
->ctrls_vcm
;
160 const struct v4l2_ctrl_ops
*ops
= &dw9807_vcm_ctrl_ops
;
161 struct i2c_client
*client
= v4l2_get_subdevdata(&dev_vcm
->sd
);
163 v4l2_ctrl_handler_init(hdl
, 1);
165 v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_FOCUS_ABSOLUTE
,
166 0, DW9807_MAX_FOCUS_POS
, DW9807_FOCUS_STEPS
, 0);
168 dev_vcm
->sd
.ctrl_handler
= hdl
;
170 dev_err(&client
->dev
, "%s fail error: 0x%x\n",
171 __func__
, hdl
->error
);
178 static int dw9807_probe(struct i2c_client
*client
)
180 struct dw9807_device
*dw9807_dev
;
183 dw9807_dev
= devm_kzalloc(&client
->dev
, sizeof(*dw9807_dev
),
185 if (dw9807_dev
== NULL
)
188 v4l2_i2c_subdev_init(&dw9807_dev
->sd
, client
, &dw9807_ops
);
189 dw9807_dev
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
190 dw9807_dev
->sd
.internal_ops
= &dw9807_int_ops
;
192 rval
= dw9807_init_controls(dw9807_dev
);
196 rval
= media_entity_pads_init(&dw9807_dev
->sd
.entity
, 0, NULL
);
200 dw9807_dev
->sd
.entity
.function
= MEDIA_ENT_F_LENS
;
202 rval
= v4l2_async_register_subdev(&dw9807_dev
->sd
);
206 pm_runtime_set_active(&client
->dev
);
207 pm_runtime_enable(&client
->dev
);
208 pm_runtime_idle(&client
->dev
);
213 v4l2_ctrl_handler_free(&dw9807_dev
->ctrls_vcm
);
214 media_entity_cleanup(&dw9807_dev
->sd
.entity
);
219 static void dw9807_remove(struct i2c_client
*client
)
221 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
222 struct dw9807_device
*dw9807_dev
= sd_to_dw9807_vcm(sd
);
224 pm_runtime_disable(&client
->dev
);
226 dw9807_subdev_cleanup(dw9807_dev
);
230 * This function sets the vcm position, so it consumes least current
231 * The lens position is gradually moved in units of DW9807_CTRL_STEPS,
232 * to make the movements smoothly.
234 static int __maybe_unused
dw9807_vcm_suspend(struct device
*dev
)
236 struct i2c_client
*client
= to_i2c_client(dev
);
237 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
238 struct dw9807_device
*dw9807_dev
= sd_to_dw9807_vcm(sd
);
239 const char tx_data
[2] = { DW9807_CTL_ADDR
, 0x01 };
242 for (val
= dw9807_dev
->current_val
& ~(DW9807_CTRL_STEPS
- 1);
243 val
>= 0; val
-= DW9807_CTRL_STEPS
) {
244 ret
= dw9807_set_dac(client
, val
);
246 dev_err_once(dev
, "%s I2C failure: %d", __func__
, ret
);
247 usleep_range(DW9807_CTRL_DELAY_US
, DW9807_CTRL_DELAY_US
+ 10);
251 ret
= i2c_master_send(client
, tx_data
, sizeof(tx_data
));
253 dev_err(&client
->dev
, "I2C write CTL fail ret = %d\n", ret
);
261 * This function sets the vcm position to the value set by the user
262 * through v4l2_ctrl_ops s_ctrl handler
263 * The lens position is gradually moved in units of DW9807_CTRL_STEPS,
264 * to make the movements smoothly.
266 static int __maybe_unused
dw9807_vcm_resume(struct device
*dev
)
268 struct i2c_client
*client
= to_i2c_client(dev
);
269 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
270 struct dw9807_device
*dw9807_dev
= sd_to_dw9807_vcm(sd
);
271 const char tx_data
[2] = { DW9807_CTL_ADDR
, 0x00 };
275 ret
= i2c_master_send(client
, tx_data
, sizeof(tx_data
));
277 dev_err(&client
->dev
, "I2C write CTL fail ret = %d\n", ret
);
281 for (val
= dw9807_dev
->current_val
% DW9807_CTRL_STEPS
;
282 val
< dw9807_dev
->current_val
+ DW9807_CTRL_STEPS
- 1;
283 val
+= DW9807_CTRL_STEPS
) {
284 ret
= dw9807_set_dac(client
, val
);
286 dev_err_ratelimited(dev
, "%s I2C failure: %d",
288 usleep_range(DW9807_CTRL_DELAY_US
, DW9807_CTRL_DELAY_US
+ 10);
294 static const struct of_device_id dw9807_of_table
[] = {
295 { .compatible
= "dongwoon,dw9807-vcm" },
296 /* Compatibility for older firmware, NEVER USE THIS IN FIRMWARE! */
297 { .compatible
= "dongwoon,dw9807" },
300 MODULE_DEVICE_TABLE(of
, dw9807_of_table
);
302 static const struct dev_pm_ops dw9807_pm_ops
= {
303 SET_SYSTEM_SLEEP_PM_OPS(dw9807_vcm_suspend
, dw9807_vcm_resume
)
304 SET_RUNTIME_PM_OPS(dw9807_vcm_suspend
, dw9807_vcm_resume
, NULL
)
307 static struct i2c_driver dw9807_i2c_driver
= {
310 .pm
= &dw9807_pm_ops
,
311 .of_match_table
= dw9807_of_table
,
313 .probe
= dw9807_probe
,
314 .remove
= dw9807_remove
,
317 module_i2c_driver(dw9807_i2c_driver
);
319 MODULE_AUTHOR("Chiang, Alan");
320 MODULE_DESCRIPTION("DW9807 VCM driver");
321 MODULE_LICENSE("GPL v2");