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