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 v4l2_i2c_subdev_init(&dw9714_dev->sd, client, &dw9714_ops);
161 dw9714_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
162 V4L2_SUBDEV_FL_HAS_EVENTS;
163 dw9714_dev->sd.internal_ops = &dw9714_int_ops;
165 rval = dw9714_init_controls(dw9714_dev);
169 rval = media_entity_pads_init(&dw9714_dev->sd.entity, 0, NULL);
173 dw9714_dev->sd.entity.function = MEDIA_ENT_F_LENS;
175 rval = v4l2_async_register_subdev(&dw9714_dev->sd);
179 pm_runtime_set_active(&client->dev);
180 pm_runtime_enable(&client->dev);
181 pm_runtime_idle(&client->dev);
186 regulator_disable(dw9714_dev->vcc);
187 v4l2_ctrl_handler_free(&dw9714_dev->ctrls_vcm);
188 media_entity_cleanup(&dw9714_dev->sd.entity);
193 static void dw9714_remove(struct i2c_client *client)
195 struct v4l2_subdev *sd = i2c_get_clientdata(client);
196 struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd);
199 pm_runtime_disable(&client->dev);
200 if (!pm_runtime_status_suspended(&client->dev)) {
201 ret = regulator_disable(dw9714_dev->vcc);
203 dev_err(&client->dev,
204 "Failed to disable vcc: %d\n", ret);
207 pm_runtime_set_suspended(&client->dev);
208 dw9714_subdev_cleanup(dw9714_dev);
212 * This function sets the vcm position, so it consumes least current
213 * The lens position is gradually moved in units of DW9714_CTRL_STEPS,
214 * to make the movements smoothly.
216 static int __maybe_unused dw9714_vcm_suspend(struct device *dev)
218 struct i2c_client *client = to_i2c_client(dev);
219 struct v4l2_subdev *sd = i2c_get_clientdata(client);
220 struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd);
223 if (pm_runtime_suspended(&client->dev))
226 for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1);
227 val >= 0; val -= DW9714_CTRL_STEPS) {
228 ret = dw9714_i2c_write(client,
229 DW9714_VAL(val, DW9714_DEFAULT_S));
231 dev_err_once(dev, "%s I2C failure: %d", __func__, ret);
232 usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
235 ret = regulator_disable(dw9714_dev->vcc);
237 dev_err(dev, "Failed to disable vcc: %d\n", ret);
243 * This function sets the vcm position to the value set by the user
244 * through v4l2_ctrl_ops s_ctrl handler
245 * The lens position is gradually moved in units of DW9714_CTRL_STEPS,
246 * to make the movements smoothly.
248 static int __maybe_unused dw9714_vcm_resume(struct device *dev)
250 struct i2c_client *client = to_i2c_client(dev);
251 struct v4l2_subdev *sd = i2c_get_clientdata(client);
252 struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd);
255 if (pm_runtime_suspended(&client->dev))
258 ret = regulator_enable(dw9714_dev->vcc);
260 dev_err(dev, "Failed to enable vcc: %d\n", ret);
263 usleep_range(1000, 2000);
265 for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS;
266 val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1;
267 val += DW9714_CTRL_STEPS) {
268 ret = dw9714_i2c_write(client,
269 DW9714_VAL(val, DW9714_DEFAULT_S));
271 dev_err_ratelimited(dev, "%s I2C failure: %d",
273 usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
279 static const struct i2c_device_id dw9714_id_table[] = {
283 MODULE_DEVICE_TABLE(i2c, dw9714_id_table);
285 static const struct of_device_id dw9714_of_table[] = {
286 { .compatible = "dongwoon,dw9714" },
289 MODULE_DEVICE_TABLE(of, dw9714_of_table);
291 static const struct dev_pm_ops dw9714_pm_ops = {
292 SET_SYSTEM_SLEEP_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume)
293 SET_RUNTIME_PM_OPS(dw9714_vcm_suspend, dw9714_vcm_resume, NULL)
296 static struct i2c_driver dw9714_i2c_driver = {
299 .pm = &dw9714_pm_ops,
300 .of_match_table = dw9714_of_table,
302 .probe_new = dw9714_probe,
303 .remove = dw9714_remove,
304 .id_table = dw9714_id_table,
307 module_i2c_driver(dw9714_i2c_driver);
310 MODULE_AUTHOR("Jian Xu Zheng");
314 MODULE_DESCRIPTION("DW9714 VCM driver");
315 MODULE_LICENSE("GPL v2");