media: dw9807-vcm: Smooth the first user movement of the lens
[platform/kernel/linux-rpi.git] / drivers / media / i2c / dw9807-vcm.c
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Intel Corporation
3
4 /*
5  * DW9807 is a 10-bit DAC driver, capable of sinking up to 100mA.
6  *
7  * DW9817 is a bidirectional 10-bit driver, driving up to +/- 100mA.
8  * Operationally it is identical to DW9807, except that the idle position is
9  * the mid-point, not 0.
10  */
11
12 #include <linux/acpi.h>
13 #include <linux/delay.h>
14 #include <linux/i2c.h>
15 #include <linux/iopoll.h>
16 #include <linux/module.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regulator/consumer.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-device.h>
21
22 #define DW9807_MAX_FOCUS_POS    1023
23 /*
24  * This sets the minimum granularity for the focus positions.
25  * A value of 1 gives maximum accuracy for a desired focus position.
26  */
27 #define DW9807_FOCUS_STEPS      1
28 /*
29  * This acts as the minimum granularity of lens movement.
30  * Keep this value power of 2, so the control steps can be
31  * uniformly adjusted for gradual lens movement, with desired
32  * number of control steps.
33  */
34 #define DW9807_CTRL_STEPS       16
35 #define DW9807_CTRL_DELAY_US    1000
36
37 #define DW9807_CTL_ADDR         0x02
38 /*
39  * DW9807 separates two registers to control the VCM position.
40  * One for MSB value, another is LSB value.
41  */
42 #define DW9807_MSB_ADDR         0x03
43 #define DW9807_LSB_ADDR         0x04
44 #define DW9807_STATUS_ADDR      0x05
45 #define DW9807_MODE_ADDR        0x06
46 #define DW9807_RESONANCE_ADDR   0x07
47
48 #define MAX_RETRY               10
49
50 #define DW9807_PW_MIN_DELAY_US          100
51 #define DW9807_PW_DELAY_RANGE_US        10
52
53 struct dw9807_cfg {
54         unsigned int idle_pos;
55         unsigned int default_pos;
56 };
57
58 struct dw9807_device {
59         struct v4l2_ctrl_handler ctrls_vcm;
60         struct v4l2_subdev sd;
61         u16 current_val;
62         u16 idle_pos;
63         struct regulator *vdd;
64         struct notifier_block notifier;
65         bool first;
66 };
67
68 static inline struct dw9807_device *sd_to_dw9807_vcm(
69                                         struct v4l2_subdev *subdev)
70 {
71         return container_of(subdev, struct dw9807_device, sd);
72 }
73
74 static int dw9807_i2c_check(struct i2c_client *client)
75 {
76         const char status_addr = DW9807_STATUS_ADDR;
77         char status_result;
78         int ret;
79
80         ret = i2c_master_send(client, &status_addr, sizeof(status_addr));
81         if (ret < 0) {
82                 dev_err(&client->dev, "I2C write STATUS address fail ret = %d\n",
83                         ret);
84                 return ret;
85         }
86
87         ret = i2c_master_recv(client, &status_result, sizeof(status_result));
88         if (ret < 0) {
89                 dev_err(&client->dev, "I2C read STATUS value fail ret = %d\n",
90                         ret);
91                 return ret;
92         }
93
94         return status_result;
95 }
96
97 static int dw9807_set_dac(struct i2c_client *client, u16 data)
98 {
99         const char tx_data[3] = {
100                 DW9807_MSB_ADDR, ((data >> 8) & 0x03), (data & 0xff)
101         };
102         int val, ret;
103
104         /*
105          * According to the datasheet, need to check the bus status before we
106          * write VCM position. This ensure that we really write the value
107          * into the register
108          */
109         ret = readx_poll_timeout(dw9807_i2c_check, client, val, val <= 0,
110                         DW9807_CTRL_DELAY_US, MAX_RETRY * DW9807_CTRL_DELAY_US);
111
112         if (ret || val < 0) {
113                 if (ret) {
114                         dev_warn(&client->dev,
115                                 "Cannot do the write operation because VCM is busy\n");
116                 }
117
118                 return ret ? -EBUSY : val;
119         }
120
121         /* Write VCM position to registers */
122         ret = i2c_master_send(client, tx_data, sizeof(tx_data));
123         if (ret < 0) {
124                 dev_err(&client->dev,
125                         "I2C write MSB fail ret=%d\n", ret);
126
127                 return ret;
128         }
129
130         return 0;
131 }
132
133 /*
134  * The lens position is gradually moved in units of DW9807_CTRL_STEPS,
135  * to make the movements smoothly. In all cases, even when "start" and
136  * "end" are the same, the lens will be set to the "end" position.
137  *
138  * (We don't use hardware slew rate control, because it differs widely
139  * between otherwise-compatible ICs, and may need lens-specific tuning.)
140  */
141 static int dw9807_ramp(struct i2c_client *client, int start, int end)
142 {
143         int step, val, ret;
144
145         if (start < end)
146                 step = DW9807_CTRL_STEPS;
147         else
148                 step = -DW9807_CTRL_STEPS;
149
150         val = start;
151         while (true) {
152                 val += step;
153                 if (step * (val - end) >= 0)
154                         val = end;
155                 ret = dw9807_set_dac(client, val);
156                 if (ret)
157                         dev_err_ratelimited(&client->dev, "%s I2C failure: %d",
158                                             __func__, ret);
159                 if (val == end)
160                         break;
161                 usleep_range(DW9807_CTRL_DELAY_US, DW9807_CTRL_DELAY_US + 10);
162         }
163
164         return ret;
165 }
166
167 static int dw9807_active(struct dw9807_device *dw9807_dev)
168 {
169         struct i2c_client *client = v4l2_get_subdevdata(&dw9807_dev->sd);
170         const char tx_data[2] = { DW9807_CTL_ADDR, 0x00 };
171         int ret;
172
173         /* Power on */
174         ret = i2c_master_send(client, tx_data, sizeof(tx_data));
175         if (ret < 0) {
176                 dev_err(&client->dev, "I2C write CTL fail ret = %d\n", ret);
177                 return ret;
178         }
179
180         dw9807_dev->first = true;
181
182         return dw9807_ramp(client, dw9807_dev->idle_pos, dw9807_dev->current_val);
183 }
184
185 static int dw9807_standby(struct dw9807_device *dw9807_dev)
186 {
187         struct i2c_client *client = v4l2_get_subdevdata(&dw9807_dev->sd);
188         const char tx_data[2] = { DW9807_CTL_ADDR, 0x01 };
189         int ret;
190
191         if (abs(dw9807_dev->current_val - dw9807_dev->idle_pos) > DW9807_CTRL_STEPS)
192                 dw9807_ramp(client, dw9807_dev->current_val, dw9807_dev->idle_pos);
193
194         /* Power down */
195         ret = i2c_master_send(client, tx_data, sizeof(tx_data));
196         if (ret < 0) {
197                 dev_err(&client->dev, "I2C write CTL fail ret = %d\n", ret);
198                 return ret;
199         }
200
201         return 0;
202 }
203
204 static int dw9807_regulator_event(struct notifier_block *nb,
205                                   unsigned long action, void *data)
206 {
207         struct dw9807_device *dw9807_dev =
208                 container_of(nb, struct dw9807_device, notifier);
209
210         if (action & REGULATOR_EVENT_ENABLE) {
211                 /*
212                  * Initialisation delay between VDD low->high and the moment
213                  * when the i2c command is available.
214                  * From the datasheet, it should be 10ms + 2ms (max power
215                  * up sequence duration)
216                  */
217                 usleep_range(DW9807_PW_MIN_DELAY_US,
218                              DW9807_PW_MIN_DELAY_US +
219                              DW9807_PW_DELAY_RANGE_US);
220
221                 dw9807_active(dw9807_dev);
222         } else if (action & REGULATOR_EVENT_PRE_DISABLE) {
223                 dw9807_standby(dw9807_dev);
224         }
225
226         return 0;
227 }
228
229 static int dw9807_set_ctrl(struct v4l2_ctrl *ctrl)
230 {
231         struct dw9807_device *dev_vcm = container_of(ctrl->handler,
232                 struct dw9807_device, ctrls_vcm);
233
234         if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) {
235                 struct i2c_client *client = v4l2_get_subdevdata(&dev_vcm->sd);
236                 int start = (dev_vcm->first) ? dev_vcm->current_val : ctrl->val;
237
238                 dev_vcm->first = false;
239                 dev_vcm->current_val = ctrl->val;
240                 return dw9807_ramp(client, start, ctrl->val);
241         }
242
243         return -EINVAL;
244 }
245
246 static const struct v4l2_ctrl_ops dw9807_vcm_ctrl_ops = {
247         .s_ctrl = dw9807_set_ctrl,
248 };
249
250 static int dw9807_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
251 {
252         return pm_runtime_resume_and_get(sd->dev);
253 }
254
255 static int dw9807_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
256 {
257         pm_runtime_put(sd->dev);
258
259         return 0;
260 }
261
262 static const struct v4l2_subdev_internal_ops dw9807_int_ops = {
263         .open = dw9807_open,
264         .close = dw9807_close,
265 };
266
267 static const struct v4l2_subdev_ops dw9807_ops = { };
268
269 static void dw9807_subdev_cleanup(struct dw9807_device *dw9807_dev)
270 {
271         v4l2_async_unregister_subdev(&dw9807_dev->sd);
272         v4l2_ctrl_handler_free(&dw9807_dev->ctrls_vcm);
273         media_entity_cleanup(&dw9807_dev->sd.entity);
274 }
275
276 static int dw9807_init_controls(struct dw9807_device *dev_vcm)
277 {
278         struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
279         const struct v4l2_ctrl_ops *ops = &dw9807_vcm_ctrl_ops;
280         struct i2c_client *client = v4l2_get_subdevdata(&dev_vcm->sd);
281
282         v4l2_ctrl_handler_init(hdl, 1);
283
284         v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
285                           0, DW9807_MAX_FOCUS_POS, DW9807_FOCUS_STEPS,
286                           dev_vcm->current_val);
287
288         dev_vcm->sd.ctrl_handler = hdl;
289         if (hdl->error) {
290                 dev_err(&client->dev, "%s fail error: 0x%x\n",
291                         __func__, hdl->error);
292                 return hdl->error;
293         }
294
295         return 0;
296 }
297
298 /* Compatible devices; in fact there are many similar chips.
299  * "data" holds the powered-off (zero current) lens position and a
300  * default/initial control value (which need not be the same as the powered-off
301  * value).
302  */
303 static const struct dw9807_cfg dw9807_cfg = {
304         .idle_pos = 0,
305         .default_pos = 0
306 };
307
308 static const struct dw9807_cfg dw9817_cfg = {
309         .idle_pos = 512,
310         .default_pos = 480,
311 };
312
313 static const struct of_device_id dw9807_of_table[] = {
314         { .compatible = "dongwoon,dw9807-vcm", .data = &dw9807_cfg },
315         { .compatible = "dongwoon,dw9817-vcm", .data = &dw9817_cfg },
316         { /* sentinel */ }
317 };
318
319 static int dw9807_probe(struct i2c_client *client)
320 {
321         struct dw9807_device *dw9807_dev;
322         const struct of_device_id *match;
323         const struct dw9807_cfg *cfg;
324         int rval;
325
326         dw9807_dev = devm_kzalloc(&client->dev, sizeof(*dw9807_dev),
327                                   GFP_KERNEL);
328         if (dw9807_dev == NULL)
329                 return -ENOMEM;
330
331         dw9807_dev->vdd = devm_regulator_get_optional(&client->dev, "VDD");
332         if (IS_ERR(dw9807_dev->vdd)) {
333                 if (PTR_ERR(dw9807_dev->vdd) != -ENODEV)
334                         return PTR_ERR(dw9807_dev->vdd);
335
336                 dw9807_dev->vdd = NULL;
337         } else {
338                 dw9807_dev->notifier.notifier_call = dw9807_regulator_event;
339
340                 rval = regulator_register_notifier(dw9807_dev->vdd,
341                                                    &dw9807_dev->notifier);
342                 if (rval) {
343                         dev_err(&client->dev,
344                                 "could not register regulator notifier\n");
345                         return rval;
346                 }
347         }
348
349         match = i2c_of_match_device(dw9807_of_table, client);
350         if (match) {
351                 cfg = (const struct dw9807_cfg *)match->data;
352                 dw9807_dev->idle_pos = cfg->idle_pos;
353                 dw9807_dev->current_val = cfg->default_pos;
354         }
355
356         v4l2_i2c_subdev_init(&dw9807_dev->sd, client, &dw9807_ops);
357         dw9807_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
358         dw9807_dev->sd.internal_ops = &dw9807_int_ops;
359
360         rval = dw9807_init_controls(dw9807_dev);
361         if (rval)
362                 goto err_cleanup;
363
364         rval = media_entity_pads_init(&dw9807_dev->sd.entity, 0, NULL);
365         if (rval < 0)
366                 goto err_cleanup;
367
368         dw9807_dev->sd.entity.function = MEDIA_ENT_F_LENS;
369
370         rval = v4l2_async_register_subdev(&dw9807_dev->sd);
371         if (rval < 0)
372                 goto err_cleanup;
373
374         pm_runtime_set_active(&client->dev);
375         pm_runtime_enable(&client->dev);
376         pm_runtime_idle(&client->dev);
377
378         return 0;
379
380 err_cleanup:
381         v4l2_ctrl_handler_free(&dw9807_dev->ctrls_vcm);
382         media_entity_cleanup(&dw9807_dev->sd.entity);
383
384         return rval;
385 }
386
387 static int dw9807_remove(struct i2c_client *client)
388 {
389         struct v4l2_subdev *sd = i2c_get_clientdata(client);
390         struct dw9807_device *dw9807_dev = sd_to_dw9807_vcm(sd);
391
392         if (dw9807_dev->vdd)
393                 regulator_unregister_notifier(dw9807_dev->vdd,
394                                               &dw9807_dev->notifier);
395
396         pm_runtime_disable(&client->dev);
397
398         dw9807_subdev_cleanup(dw9807_dev);
399
400         return 0;
401 }
402
403 /*
404  * This function sets the vcm position, so it consumes least current
405  * The lens position is gradually moved in units of DW9807_CTRL_STEPS,
406  * to make the movements smoothly.
407  */
408 static int __maybe_unused dw9807_vcm_suspend(struct device *dev)
409 {
410         struct i2c_client *client = to_i2c_client(dev);
411         struct v4l2_subdev *sd = i2c_get_clientdata(client);
412         struct dw9807_device *dw9807_dev = sd_to_dw9807_vcm(sd);
413
414         if (dw9807_dev->vdd)
415                 return regulator_disable(dw9807_dev->vdd);
416
417         return dw9807_standby(dw9807_dev);
418 }
419
420 /*
421  * This function sets the vcm position to the value set by the user
422  * through v4l2_ctrl_ops s_ctrl handler
423  * The lens position is gradually moved in units of DW9807_CTRL_STEPS,
424  * to make the movements smoothly.
425  */
426 static int  __maybe_unused dw9807_vcm_resume(struct device *dev)
427 {
428         struct i2c_client *client = to_i2c_client(dev);
429         struct v4l2_subdev *sd = i2c_get_clientdata(client);
430         struct dw9807_device *dw9807_dev = sd_to_dw9807_vcm(sd);
431
432         if (dw9807_dev->vdd)
433                 return regulator_enable(dw9807_dev->vdd);
434
435         return dw9807_active(dw9807_dev);
436 }
437
438 MODULE_DEVICE_TABLE(of, dw9807_of_table);
439
440 static const struct dev_pm_ops dw9807_pm_ops = {
441         SET_SYSTEM_SLEEP_PM_OPS(dw9807_vcm_suspend, dw9807_vcm_resume)
442         SET_RUNTIME_PM_OPS(dw9807_vcm_suspend, dw9807_vcm_resume, NULL)
443 };
444
445 static struct i2c_driver dw9807_i2c_driver = {
446         .driver = {
447                 .name = "dw9807",
448                 .pm = &dw9807_pm_ops,
449                 .of_match_table = dw9807_of_table,
450         },
451         .probe_new = dw9807_probe,
452         .remove = dw9807_remove,
453 };
454
455 module_i2c_driver(dw9807_i2c_driver);
456
457 MODULE_AUTHOR("Chiang, Alan");
458 MODULE_DESCRIPTION("DW9807 VCM driver");
459 MODULE_LICENSE("GPL v2");