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