1// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2015--2017 Intel Corporation.
3
4#include <linux/delay.h>
5#include <linux/i2c.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>
12
13#define DW9714_NAME "dw9714"
14#define DW9714_MAX_FOCUS_POS 1023
15/*
16 * This sets the minimum granularity for the focus positions.
17 * A value of 1 gives maximum accuracy for a desired focus position
18 */
19#define DW9714_FOCUS_STEPS 1
20/*
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.
25 */
26#define DW9714_CTRL_STEPS 16
27#define DW9714_CTRL_DELAY_US 1000
28/*
29 * S[3:2] = 0x00, codes per step for "Linear Slope Control"
30 * S[1:0] = 0x00, step period
31 */
32#define DW9714_DEFAULT_S 0x0
33#define DW9714_VAL(data, s) ((data) << 4 | (s))
34
35/* dw9714 device structure */
36struct dw9714_device {
37 struct v4l2_ctrl_handler ctrls_vcm;
38 struct v4l2_subdev sd;
39 u16 current_val;
40 struct regulator *vcc;
41};
42
43static inline struct dw9714_device *to_dw9714_vcm(struct v4l2_ctrl *ctrl)
44{
45 return container_of(ctrl->handler, struct dw9714_device, ctrls_vcm);
46}
47
48static inline struct dw9714_device *sd_to_dw9714_vcm(struct v4l2_subdev *subdev)
49{
50 return container_of(subdev, struct dw9714_device, sd);
51}
52
53static int dw9714_i2c_write(struct i2c_client *client, u16 data)
54{
55 int ret;
56 __be16 val = cpu_to_be16(data);
57
58 ret = i2c_master_send(client, buf: (const char *)&val, count: sizeof(val));
59 if (ret != sizeof(val)) {
60 dev_err(&client->dev, "I2C write fail\n");
61 return -EIO;
62 }
63 return 0;
64}
65
66static int dw9714_t_focus_vcm(struct dw9714_device *dw9714_dev, u16 val)
67{
68 struct i2c_client *client = v4l2_get_subdevdata(sd: &dw9714_dev->sd);
69
70 dw9714_dev->current_val = val;
71
72 return dw9714_i2c_write(client, DW9714_VAL(val, DW9714_DEFAULT_S));
73}
74
75static int dw9714_set_ctrl(struct v4l2_ctrl *ctrl)
76{
77 struct dw9714_device *dev_vcm = to_dw9714_vcm(ctrl);
78
79 if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
80 return dw9714_t_focus_vcm(dw9714_dev: dev_vcm, val: ctrl->val);
81
82 return -EINVAL;
83}
84
85static const struct v4l2_ctrl_ops dw9714_vcm_ctrl_ops = {
86 .s_ctrl = dw9714_set_ctrl,
87};
88
89static int dw9714_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
90{
91 return pm_runtime_resume_and_get(dev: sd->dev);
92}
93
94static int dw9714_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
95{
96 pm_runtime_put(dev: sd->dev);
97
98 return 0;
99}
100
101static const struct v4l2_subdev_internal_ops dw9714_int_ops = {
102 .open = dw9714_open,
103 .close = dw9714_close,
104};
105
106static 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,
110};
111
112static const struct v4l2_subdev_ops dw9714_ops = {
113 .core = &dw9714_core_ops,
114};
115
116static void dw9714_subdev_cleanup(struct dw9714_device *dw9714_dev)
117{
118 v4l2_async_unregister_subdev(sd: &dw9714_dev->sd);
119 v4l2_ctrl_handler_free(hdl: &dw9714_dev->ctrls_vcm);
120 media_entity_cleanup(entity: &dw9714_dev->sd.entity);
121}
122
123static int dw9714_init_controls(struct dw9714_device *dev_vcm)
124{
125 struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
126 const struct v4l2_ctrl_ops *ops = &dw9714_vcm_ctrl_ops;
127
128 v4l2_ctrl_handler_init(hdl, 1);
129
130 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
131 min: 0, DW9714_MAX_FOCUS_POS, DW9714_FOCUS_STEPS, def: 0);
132
133 if (hdl->error)
134 dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n",
135 __func__, hdl->error);
136 dev_vcm->sd.ctrl_handler = hdl;
137 return hdl->error;
138}
139
140static int dw9714_probe(struct i2c_client *client)
141{
142 struct dw9714_device *dw9714_dev;
143 int rval;
144
145 dw9714_dev = devm_kzalloc(dev: &client->dev, size: sizeof(*dw9714_dev),
146 GFP_KERNEL);
147 if (dw9714_dev == NULL)
148 return -ENOMEM;
149
150 dw9714_dev->vcc = devm_regulator_get(dev: &client->dev, id: "vcc");
151 if (IS_ERR(ptr: dw9714_dev->vcc))
152 return PTR_ERR(ptr: dw9714_dev->vcc);
153
154 rval = regulator_enable(regulator: dw9714_dev->vcc);
155 if (rval < 0) {
156 dev_err(&client->dev, "failed to enable vcc: %d\n", rval);
157 return rval;
158 }
159
160 usleep_range(min: 1000, max: 2000);
161
162 v4l2_i2c_subdev_init(sd: &dw9714_dev->sd, client, ops: &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;
166
167 rval = dw9714_init_controls(dev_vcm: dw9714_dev);
168 if (rval)
169 goto err_cleanup;
170
171 rval = media_entity_pads_init(entity: &dw9714_dev->sd.entity, num_pads: 0, NULL);
172 if (rval < 0)
173 goto err_cleanup;
174
175 dw9714_dev->sd.entity.function = MEDIA_ENT_F_LENS;
176
177 rval = v4l2_async_register_subdev(sd: &dw9714_dev->sd);
178 if (rval < 0)
179 goto err_cleanup;
180
181 pm_runtime_set_active(dev: &client->dev);
182 pm_runtime_enable(dev: &client->dev);
183 pm_runtime_idle(dev: &client->dev);
184
185 return 0;
186
187err_cleanup:
188 regulator_disable(regulator: dw9714_dev->vcc);
189 v4l2_ctrl_handler_free(hdl: &dw9714_dev->ctrls_vcm);
190 media_entity_cleanup(entity: &dw9714_dev->sd.entity);
191
192 return rval;
193}
194
195static void dw9714_remove(struct i2c_client *client)
196{
197 struct v4l2_subdev *sd = i2c_get_clientdata(client);
198 struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(subdev: sd);
199 int ret;
200
201 pm_runtime_disable(dev: &client->dev);
202 if (!pm_runtime_status_suspended(dev: &client->dev)) {
203 ret = regulator_disable(regulator: dw9714_dev->vcc);
204 if (ret) {
205 dev_err(&client->dev,
206 "Failed to disable vcc: %d\n", ret);
207 }
208 }
209 pm_runtime_set_suspended(dev: &client->dev);
210 dw9714_subdev_cleanup(dw9714_dev);
211}
212
213/*
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.
217 */
218static int __maybe_unused dw9714_vcm_suspend(struct device *dev)
219{
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(subdev: sd);
223 int ret, val;
224
225 if (pm_runtime_suspended(dev: &client->dev))
226 return 0;
227
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));
232 if (ret)
233 dev_err_once(dev, "%s I2C failure: %d", __func__, ret);
234 usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
235 }
236
237 ret = regulator_disable(regulator: dw9714_dev->vcc);
238 if (ret)
239 dev_err(dev, "Failed to disable vcc: %d\n", ret);
240
241 return ret;
242}
243
244/*
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.
249 */
250static int __maybe_unused dw9714_vcm_resume(struct device *dev)
251{
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(subdev: sd);
255 int ret, val;
256
257 if (pm_runtime_suspended(dev: &client->dev))
258 return 0;
259
260 ret = regulator_enable(regulator: dw9714_dev->vcc);
261 if (ret) {
262 dev_err(dev, "Failed to enable vcc: %d\n", ret);
263 return ret;
264 }
265 usleep_range(min: 1000, max: 2000);
266
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));
272 if (ret)
273 dev_err_ratelimited(dev, "%s I2C failure: %d",
274 __func__, ret);
275 usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10);
276 }
277
278 return 0;
279}
280
281static const struct i2c_device_id dw9714_id_table[] = {
282 { DW9714_NAME, 0 },
283 { { 0 } }
284};
285MODULE_DEVICE_TABLE(i2c, dw9714_id_table);
286
287static const struct of_device_id dw9714_of_table[] = {
288 { .compatible = "dongwoon,dw9714" },
289 { { 0 } }
290};
291MODULE_DEVICE_TABLE(of, dw9714_of_table);
292
293static 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)
296};
297
298static struct i2c_driver dw9714_i2c_driver = {
299 .driver = {
300 .name = DW9714_NAME,
301 .pm = &dw9714_pm_ops,
302 .of_match_table = dw9714_of_table,
303 },
304 .probe = dw9714_probe,
305 .remove = dw9714_remove,
306 .id_table = dw9714_id_table,
307};
308
309module_i2c_driver(dw9714_i2c_driver);
310
311MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>");
312MODULE_AUTHOR("Jian Xu Zheng");
313MODULE_AUTHOR("Yuning Pu <yuning.pu@intel.com>");
314MODULE_AUTHOR("Jouni Ukkonen <jouni.ukkonen@intel.com>");
315MODULE_AUTHOR("Tommi Franttila <tommi.franttila@intel.com>");
316MODULE_DESCRIPTION("DW9714 VCM driver");
317MODULE_LICENSE("GPL v2");
318

source code of linux/drivers/media/i2c/dw9714.c