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 <media/v4l2-ctrls.h>
9 #include <media/v4l2-device.h>
11 #define DW9714_NAME "dw9714"
12 #define DW9714_MAX_FOCUS_POS 1023
14 * This sets the minimum granularity for the focus positions.
15 * A value of 1 gives maximum accuracy for a desired focus position
17 #define DW9714_FOCUS_STEPS 1
19 * This acts as the minimum granularity of lens movement.
20 * Keep this value power of 2, so the control steps can be
21 * uniformly adjusted for gradual lens movement, with desired
22 * number of control steps.
24 #define DW9714_CTRL_STEPS 16
25 #define DW9714_CTRL_DELAY_US 1000
27 * S[3:2] = 0x00, codes per step for "Linear Slope Control"
28 * S[1:0] = 0x00, step period
30 #define DW9714_DEFAULT_S 0x0
31 #define DW9714_VAL(data, s) ((data) << 4 | (s))
33 /* dw9714 device structure */
34 struct dw9714_device
{
35 struct v4l2_ctrl_handler ctrls_vcm
;
36 struct v4l2_subdev sd
;
40 static inline struct dw9714_device
*to_dw9714_vcm(struct v4l2_ctrl
*ctrl
)
42 return container_of(ctrl
->handler
, struct dw9714_device
, ctrls_vcm
);
45 static inline struct dw9714_device
*sd_to_dw9714_vcm(struct v4l2_subdev
*subdev
)
47 return container_of(subdev
, struct dw9714_device
, sd
);
50 static int dw9714_i2c_write(struct i2c_client
*client
, u16 data
)
53 __be16 val
= cpu_to_be16(data
);
55 ret
= i2c_master_send(client
, (const char *)&val
, sizeof(val
));
56 if (ret
!= sizeof(val
)) {
57 dev_err(&client
->dev
, "I2C write fail\n");
63 static int dw9714_t_focus_vcm(struct dw9714_device
*dw9714_dev
, u16 val
)
65 struct i2c_client
*client
= v4l2_get_subdevdata(&dw9714_dev
->sd
);
67 dw9714_dev
->current_val
= val
;
69 return dw9714_i2c_write(client
, DW9714_VAL(val
, DW9714_DEFAULT_S
));
72 static int dw9714_set_ctrl(struct v4l2_ctrl
*ctrl
)
74 struct dw9714_device
*dev_vcm
= to_dw9714_vcm(ctrl
);
76 if (ctrl
->id
== V4L2_CID_FOCUS_ABSOLUTE
)
77 return dw9714_t_focus_vcm(dev_vcm
, ctrl
->val
);
82 static const struct v4l2_ctrl_ops dw9714_vcm_ctrl_ops
= {
83 .s_ctrl
= dw9714_set_ctrl
,
86 static int dw9714_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
90 rval
= pm_runtime_get_sync(sd
->dev
);
92 pm_runtime_put_noidle(sd
->dev
);
99 static int dw9714_close(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
101 pm_runtime_put(sd
->dev
);
106 static const struct v4l2_subdev_internal_ops dw9714_int_ops
= {
108 .close
= dw9714_close
,
111 static const struct v4l2_subdev_ops dw9714_ops
= { };
113 static void dw9714_subdev_cleanup(struct dw9714_device
*dw9714_dev
)
115 v4l2_async_unregister_subdev(&dw9714_dev
->sd
);
116 v4l2_ctrl_handler_free(&dw9714_dev
->ctrls_vcm
);
117 media_entity_cleanup(&dw9714_dev
->sd
.entity
);
120 static int dw9714_init_controls(struct dw9714_device
*dev_vcm
)
122 struct v4l2_ctrl_handler
*hdl
= &dev_vcm
->ctrls_vcm
;
123 const struct v4l2_ctrl_ops
*ops
= &dw9714_vcm_ctrl_ops
;
125 v4l2_ctrl_handler_init(hdl
, 1);
127 v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_FOCUS_ABSOLUTE
,
128 0, DW9714_MAX_FOCUS_POS
, DW9714_FOCUS_STEPS
, 0);
131 dev_err(dev_vcm
->sd
.dev
, "%s fail error: 0x%x\n",
132 __func__
, hdl
->error
);
133 dev_vcm
->sd
.ctrl_handler
= hdl
;
137 static int dw9714_probe(struct i2c_client
*client
)
139 struct dw9714_device
*dw9714_dev
;
142 dw9714_dev
= devm_kzalloc(&client
->dev
, sizeof(*dw9714_dev
),
144 if (dw9714_dev
== NULL
)
147 v4l2_i2c_subdev_init(&dw9714_dev
->sd
, client
, &dw9714_ops
);
148 dw9714_dev
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
149 dw9714_dev
->sd
.internal_ops
= &dw9714_int_ops
;
151 rval
= dw9714_init_controls(dw9714_dev
);
155 rval
= media_entity_pads_init(&dw9714_dev
->sd
.entity
, 0, NULL
);
159 dw9714_dev
->sd
.entity
.function
= MEDIA_ENT_F_LENS
;
161 rval
= v4l2_async_register_subdev(&dw9714_dev
->sd
);
165 pm_runtime_set_active(&client
->dev
);
166 pm_runtime_enable(&client
->dev
);
167 pm_runtime_idle(&client
->dev
);
172 v4l2_ctrl_handler_free(&dw9714_dev
->ctrls_vcm
);
173 media_entity_cleanup(&dw9714_dev
->sd
.entity
);
178 static int dw9714_remove(struct i2c_client
*client
)
180 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
181 struct dw9714_device
*dw9714_dev
= sd_to_dw9714_vcm(sd
);
183 pm_runtime_disable(&client
->dev
);
184 dw9714_subdev_cleanup(dw9714_dev
);
190 * This function sets the vcm position, so it consumes least current
191 * The lens position is gradually moved in units of DW9714_CTRL_STEPS,
192 * to make the movements smoothly.
194 static int __maybe_unused
dw9714_vcm_suspend(struct device
*dev
)
196 struct i2c_client
*client
= to_i2c_client(dev
);
197 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
198 struct dw9714_device
*dw9714_dev
= sd_to_dw9714_vcm(sd
);
201 for (val
= dw9714_dev
->current_val
& ~(DW9714_CTRL_STEPS
- 1);
202 val
>= 0; val
-= DW9714_CTRL_STEPS
) {
203 ret
= dw9714_i2c_write(client
,
204 DW9714_VAL(val
, DW9714_DEFAULT_S
));
206 dev_err_once(dev
, "%s I2C failure: %d", __func__
, ret
);
207 usleep_range(DW9714_CTRL_DELAY_US
, DW9714_CTRL_DELAY_US
+ 10);
213 * This function sets the vcm position to the value set by the user
214 * through v4l2_ctrl_ops s_ctrl handler
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_resume(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 for (val
= dw9714_dev
->current_val
% DW9714_CTRL_STEPS
;
226 val
< dw9714_dev
->current_val
+ DW9714_CTRL_STEPS
- 1;
227 val
+= DW9714_CTRL_STEPS
) {
228 ret
= dw9714_i2c_write(client
,
229 DW9714_VAL(val
, DW9714_DEFAULT_S
));
231 dev_err_ratelimited(dev
, "%s I2C failure: %d",
233 usleep_range(DW9714_CTRL_DELAY_US
, DW9714_CTRL_DELAY_US
+ 10);
239 static const struct i2c_device_id dw9714_id_table
[] = {
243 MODULE_DEVICE_TABLE(i2c
, dw9714_id_table
);
245 static const struct of_device_id dw9714_of_table
[] = {
246 { .compatible
= "dongwoon,dw9714" },
249 MODULE_DEVICE_TABLE(of
, dw9714_of_table
);
251 static const struct dev_pm_ops dw9714_pm_ops
= {
252 SET_SYSTEM_SLEEP_PM_OPS(dw9714_vcm_suspend
, dw9714_vcm_resume
)
253 SET_RUNTIME_PM_OPS(dw9714_vcm_suspend
, dw9714_vcm_resume
, NULL
)
256 static struct i2c_driver dw9714_i2c_driver
= {
259 .pm
= &dw9714_pm_ops
,
260 .of_match_table
= dw9714_of_table
,
262 .probe_new
= dw9714_probe
,
263 .remove
= dw9714_remove
,
264 .id_table
= dw9714_id_table
,
267 module_i2c_driver(dw9714_i2c_driver
);
269 MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>");
270 MODULE_AUTHOR("Jian Xu Zheng");
271 MODULE_AUTHOR("Yuning Pu <yuning.pu@intel.com>");
272 MODULE_AUTHOR("Jouni Ukkonen <jouni.ukkonen@intel.com>");
273 MODULE_AUTHOR("Tommi Franttila <tommi.franttila@intel.com>");
274 MODULE_DESCRIPTION("DW9714 VCM driver");
275 MODULE_LICENSE("GPL v2");