1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2015--2017 Intel Corporation.
4 #include <linux/delay.h>
6 #include <linux/module.h>
7 #include <linux/pm_runtime.h>
8 #include <linux/regulator/consumer.h>
9 #include <media/v4l2-ctrls.h>
10 #include <media/v4l2-device.h>
11 #include <media/v4l2-event.h>
13 #define DW9714_NAME "dw9714"
14 #define DW9714_MAX_FOCUS_POS 1023
16 * This sets the minimum granularity for the focus positions.
17 * A value of 1 gives maximum accuracy for a desired focus position
19 #define DW9714_FOCUS_STEPS 1
21 * This acts as the minimum granularity of lens movement.
22 * Keep this value power of 2, so the control steps can be
23 * uniformly adjusted for gradual lens movement, with desired
24 * number of control steps.
26 #define DW9714_CTRL_STEPS 16
27 #define DW9714_CTRL_DELAY_US 1000
29 * S[3:2] = 0x00, codes per step for "Linear Slope Control"
30 * S[1:0] = 0x00, step period
32 #define DW9714_DEFAULT_S 0x0
33 #define DW9714_VAL(data, s) ((data) << 4 | (s))
35 /* dw9714 device structure */
36 struct dw9714_device
{
37 struct v4l2_ctrl_handler ctrls_vcm
;
38 struct v4l2_subdev sd
;
40 struct regulator
*vcc
;
43 static inline struct dw9714_device
*to_dw9714_vcm(struct v4l2_ctrl
*ctrl
)
45 return container_of(ctrl
->handler
, struct dw9714_device
, ctrls_vcm
);
48 static inline struct dw9714_device
*sd_to_dw9714_vcm(struct v4l2_subdev
*subdev
)
50 return container_of(subdev
, struct dw9714_device
, sd
);
53 static int dw9714_i2c_write(struct i2c_client
*client
, u16 data
)
56 __be16 val
= cpu_to_be16(data
);
58 ret
= i2c_master_send(client
, (const char *)&val
, sizeof(val
));
59 if (ret
!= sizeof(val
)) {
60 dev_err(&client
->dev
, "I2C write fail\n");
66 static int dw9714_t_focus_vcm(struct dw9714_device
*dw9714_dev
, u16 val
)
68 struct i2c_client
*client
= v4l2_get_subdevdata(&dw9714_dev
->sd
);
70 dw9714_dev
->current_val
= val
;
72 return dw9714_i2c_write(client
, DW9714_VAL(val
, DW9714_DEFAULT_S
));
75 static int dw9714_set_ctrl(struct v4l2_ctrl
*ctrl
)
77 struct dw9714_device
*dev_vcm
= to_dw9714_vcm(ctrl
);
79 if (ctrl
->id
== V4L2_CID_FOCUS_ABSOLUTE
)
80 return dw9714_t_focus_vcm(dev_vcm
, ctrl
->val
);
85 static const struct v4l2_ctrl_ops dw9714_vcm_ctrl_ops
= {
86 .s_ctrl
= dw9714_set_ctrl
,
89 static int dw9714_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
91 return pm_runtime_resume_and_get(sd
->dev
);
94 static int dw9714_close(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
96 pm_runtime_put(sd
->dev
);
101 static const struct v4l2_subdev_internal_ops dw9714_int_ops
= {
103 .close
= dw9714_close
,
106 static const struct v4l2_subdev_core_ops dw9714_core_ops
= {
107 .log_status
= v4l2_ctrl_subdev_log_status
,
108 .subscribe_event
= v4l2_ctrl_subdev_subscribe_event
,
109 .unsubscribe_event
= v4l2_event_subdev_unsubscribe
,
112 static const struct v4l2_subdev_ops dw9714_ops
= {
113 .core
= &dw9714_core_ops
,
116 static void dw9714_subdev_cleanup(struct dw9714_device
*dw9714_dev
)
118 v4l2_async_unregister_subdev(&dw9714_dev
->sd
);
119 v4l2_ctrl_handler_free(&dw9714_dev
->ctrls_vcm
);
120 media_entity_cleanup(&dw9714_dev
->sd
.entity
);
123 static int dw9714_init_controls(struct dw9714_device
*dev_vcm
)
125 struct v4l2_ctrl_handler
*hdl
= &dev_vcm
->ctrls_vcm
;
126 const struct v4l2_ctrl_ops
*ops
= &dw9714_vcm_ctrl_ops
;
128 v4l2_ctrl_handler_init(hdl
, 1);
130 v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_FOCUS_ABSOLUTE
,
131 0, DW9714_MAX_FOCUS_POS
, DW9714_FOCUS_STEPS
, 0);
134 dev_err(dev_vcm
->sd
.dev
, "%s fail error: 0x%x\n",
135 __func__
, hdl
->error
);
136 dev_vcm
->sd
.ctrl_handler
= hdl
;
140 static int dw9714_probe(struct i2c_client
*client
)
142 struct dw9714_device
*dw9714_dev
;
145 dw9714_dev
= devm_kzalloc(&client
->dev
, sizeof(*dw9714_dev
),
147 if (dw9714_dev
== NULL
)
150 dw9714_dev
->vcc
= devm_regulator_get(&client
->dev
, "vcc");
151 if (IS_ERR(dw9714_dev
->vcc
))
152 return PTR_ERR(dw9714_dev
->vcc
);
154 rval
= regulator_enable(dw9714_dev
->vcc
);
156 dev_err(&client
->dev
, "failed to enable vcc: %d\n", rval
);
160 usleep_range(1000, 2000);
162 v4l2_i2c_subdev_init(&dw9714_dev
->sd
, client
, &dw9714_ops
);
163 dw9714_dev
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
|
164 V4L2_SUBDEV_FL_HAS_EVENTS
;
165 dw9714_dev
->sd
.internal_ops
= &dw9714_int_ops
;
167 rval
= dw9714_init_controls(dw9714_dev
);
171 rval
= media_entity_pads_init(&dw9714_dev
->sd
.entity
, 0, NULL
);
175 dw9714_dev
->sd
.entity
.function
= MEDIA_ENT_F_LENS
;
177 rval
= v4l2_async_register_subdev(&dw9714_dev
->sd
);
181 pm_runtime_set_active(&client
->dev
);
182 pm_runtime_enable(&client
->dev
);
183 pm_runtime_idle(&client
->dev
);
188 regulator_disable(dw9714_dev
->vcc
);
189 v4l2_ctrl_handler_free(&dw9714_dev
->ctrls_vcm
);
190 media_entity_cleanup(&dw9714_dev
->sd
.entity
);
195 static void dw9714_remove(struct i2c_client
*client
)
197 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
198 struct dw9714_device
*dw9714_dev
= sd_to_dw9714_vcm(sd
);
201 pm_runtime_disable(&client
->dev
);
202 if (!pm_runtime_status_suspended(&client
->dev
)) {
203 ret
= regulator_disable(dw9714_dev
->vcc
);
205 dev_err(&client
->dev
,
206 "Failed to disable vcc: %d\n", ret
);
209 pm_runtime_set_suspended(&client
->dev
);
210 dw9714_subdev_cleanup(dw9714_dev
);
214 * This function sets the vcm position, so it consumes least current
215 * The lens position is gradually moved in units of DW9714_CTRL_STEPS,
216 * to make the movements smoothly.
218 static int __maybe_unused
dw9714_vcm_suspend(struct device
*dev
)
220 struct i2c_client
*client
= to_i2c_client(dev
);
221 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
222 struct dw9714_device
*dw9714_dev
= sd_to_dw9714_vcm(sd
);
225 if (pm_runtime_suspended(&client
->dev
))
228 for (val
= dw9714_dev
->current_val
& ~(DW9714_CTRL_STEPS
- 1);
229 val
>= 0; val
-= DW9714_CTRL_STEPS
) {
230 ret
= dw9714_i2c_write(client
,
231 DW9714_VAL(val
, DW9714_DEFAULT_S
));
233 dev_err_once(dev
, "%s I2C failure: %d", __func__
, ret
);
234 usleep_range(DW9714_CTRL_DELAY_US
, DW9714_CTRL_DELAY_US
+ 10);
237 ret
= regulator_disable(dw9714_dev
->vcc
);
239 dev_err(dev
, "Failed to disable vcc: %d\n", ret
);
245 * This function sets the vcm position to the value set by the user
246 * through v4l2_ctrl_ops s_ctrl handler
247 * The lens position is gradually moved in units of DW9714_CTRL_STEPS,
248 * to make the movements smoothly.
250 static int __maybe_unused
dw9714_vcm_resume(struct device
*dev
)
252 struct i2c_client
*client
= to_i2c_client(dev
);
253 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
254 struct dw9714_device
*dw9714_dev
= sd_to_dw9714_vcm(sd
);
257 if (pm_runtime_suspended(&client
->dev
))
260 ret
= regulator_enable(dw9714_dev
->vcc
);
262 dev_err(dev
, "Failed to enable vcc: %d\n", ret
);
265 usleep_range(1000, 2000);
267 for (val
= dw9714_dev
->current_val
% DW9714_CTRL_STEPS
;
268 val
< dw9714_dev
->current_val
+ DW9714_CTRL_STEPS
- 1;
269 val
+= DW9714_CTRL_STEPS
) {
270 ret
= dw9714_i2c_write(client
,
271 DW9714_VAL(val
, DW9714_DEFAULT_S
));
273 dev_err_ratelimited(dev
, "%s I2C failure: %d",
275 usleep_range(DW9714_CTRL_DELAY_US
, DW9714_CTRL_DELAY_US
+ 10);
281 static const struct i2c_device_id dw9714_id_table
[] = {
285 MODULE_DEVICE_TABLE(i2c
, dw9714_id_table
);
287 static const struct of_device_id dw9714_of_table
[] = {
288 { .compatible
= "dongwoon,dw9714" },
291 MODULE_DEVICE_TABLE(of
, dw9714_of_table
);
293 static const struct dev_pm_ops dw9714_pm_ops
= {
294 SET_SYSTEM_SLEEP_PM_OPS(dw9714_vcm_suspend
, dw9714_vcm_resume
)
295 SET_RUNTIME_PM_OPS(dw9714_vcm_suspend
, dw9714_vcm_resume
, NULL
)
298 static struct i2c_driver dw9714_i2c_driver
= {
301 .pm
= &dw9714_pm_ops
,
302 .of_match_table
= dw9714_of_table
,
304 .probe
= dw9714_probe
,
305 .remove
= dw9714_remove
,
306 .id_table
= dw9714_id_table
,
309 module_i2c_driver(dw9714_i2c_driver
);
311 MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>");
312 MODULE_AUTHOR("Jian Xu Zheng");
313 MODULE_AUTHOR("Yuning Pu");
314 MODULE_AUTHOR("Jouni Ukkonen");
315 MODULE_AUTHOR("Tommi Franttila");
316 MODULE_DESCRIPTION("DW9714 VCM driver");
317 MODULE_LICENSE("GPL v2");