1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * dw9763 vcm driver
4 *
5 * Copyright (C) 2022 Rockchip Electronics Co., Ltd.
6 */
7 // #define DEBUG
8 #include <linux/delay.h>
9 #include <linux/i2c.h>
10 #include <linux/module.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/rk-camera-module.h>
13 #include <linux/version.h>
14 #include <media/v4l2-ctrls.h>
15 #include <media/v4l2-device.h>
16 #include <linux/rk_vcm_head.h>
17 #include <linux/compat.h>
18
19 #define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x0)
20 #define DW9763_NAME "dw9763"
21
22 #define DW9763_MAX_CURRENT 120U
23 #define DW9763_MAX_REG 1023U
24
25 #define DW9763_DEFAULT_START_CURRENT 20
26 #define DW9763_DEFAULT_RATED_CURRENT 90
27 #define DW9763_DEFAULT_STEP_MODE 0x3
28 #define DW9763_DEFAULT_T_SACT 0x20
29 #define DW9763_DEFAULT_T_DIV 0x1
30 #define REG_NULL 0xFF
31
32 #define DW9763_CHIP_ID 0xF9
33 #define DW9763_REG_CHIP_ID 0x00
34
35 enum mode_e {
36 SAC1_MODE,
37 SAC2_MODE,
38 SAC2_5_MODE,
39 SAC3_MODE,
40 SAC4_MODE = 5,
41 DIRECT_MODE,
42 };
43
44 /* dw9763 device structure */
45 struct dw9763_device {
46 struct v4l2_ctrl_handler ctrls_vcm;
47 struct i2c_client *client;
48 struct v4l2_subdev sd;
49 struct v4l2_device vdev;
50 u16 current_val;
51
52 struct gpio_desc *power_gpio;
53 unsigned short current_related_pos;
54 unsigned short current_lens_pos;
55 unsigned int start_current;
56 unsigned int rated_current;
57 unsigned int step;
58 unsigned int step_mode;
59 unsigned int vcm_movefull_t;
60 unsigned int t_src;
61 unsigned int t_div;
62
63 struct __kernel_old_timeval start_move_tv;
64 struct __kernel_old_timeval end_move_tv;
65 unsigned long move_us;
66
67 u32 module_index;
68 const char *module_facing;
69 struct rk_cam_vcm_cfg vcm_cfg;
70 int max_ma;
71 struct mutex lock;
72 };
73
to_dw9763_vcm(struct v4l2_ctrl * ctrl)74 static inline struct dw9763_device *to_dw9763_vcm(struct v4l2_ctrl *ctrl)
75 {
76 return container_of(ctrl->handler, struct dw9763_device, ctrls_vcm);
77 }
78
sd_to_dw9763_vcm(struct v4l2_subdev * subdev)79 static inline struct dw9763_device *sd_to_dw9763_vcm(struct v4l2_subdev *subdev)
80 {
81 return container_of(subdev, struct dw9763_device, sd);
82 }
83
dw9763_write_reg(struct i2c_client * client,u8 reg,u32 len,u32 val)84 static int dw9763_write_reg(struct i2c_client *client, u8 reg,
85 u32 len, u32 val)
86 {
87 u32 buf_i, val_i;
88 u8 buf[5];
89 u8 *val_p;
90 __be32 val_be;
91
92 if (len > 4)
93 return -EINVAL;
94
95 buf[0] = reg;
96
97 val_be = cpu_to_be32(val);
98 val_p = (u8 *)&val_be;
99 buf_i = 1;
100 val_i = 4 - len;
101
102 while (val_i < 4)
103 buf[buf_i++] = val_p[val_i++];
104
105 if (i2c_master_send(client, buf, len + 1) != len + 1) {
106 dev_err(&client->dev, "Failed to write 0x%04x,0x%x\n", reg, val);
107 return -EIO;
108 }
109 dev_dbg(&client->dev, "succeed to write 0x%04x,0x%x\n", reg, val);
110
111 return 0;
112 }
113
dw9763_read_reg(struct i2c_client * client,u8 reg,unsigned int len,u32 * val)114 static int dw9763_read_reg(struct i2c_client *client,
115 u8 reg,
116 unsigned int len,
117 u32 *val)
118 {
119 struct i2c_msg msgs[2];
120 u8 *data_be_p;
121 __be32 data_be = 0;
122 int ret;
123
124 if (len > 4 || !len)
125 return -EINVAL;
126
127 data_be_p = (u8 *)&data_be;
128 /* Write register address */
129 msgs[0].addr = client->addr;
130 msgs[0].flags = 0;
131 msgs[0].len = 1;
132 msgs[0].buf = (u8 *)®
133
134 /* Read data from register */
135 msgs[1].addr = client->addr;
136 msgs[1].flags = I2C_M_RD;
137 msgs[1].len = len;
138 msgs[1].buf = &data_be_p[4 - len];
139
140 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
141 if (ret != ARRAY_SIZE(msgs))
142 return -EIO;
143
144 *val = be32_to_cpu(data_be);
145
146 return 0;
147 }
148
dw9763_move_time_div(struct dw9763_device * dev_vcm,unsigned int move_time_us)149 static unsigned int dw9763_move_time_div(struct dw9763_device *dev_vcm,
150 unsigned int move_time_us)
151 {
152 struct i2c_client *client = dev_vcm->client;
153 unsigned int move_time = 0;
154
155 switch (dev_vcm->t_div) {
156 case 0:
157 move_time = move_time_us * 2;
158 break;
159 case 1:
160 move_time = move_time_us;
161 break;
162 case 2:
163 move_time = move_time_us / 2;
164 break;
165 case 3:
166 move_time = move_time_us / 4;
167 break;
168 case 4:
169 move_time = move_time_us * 8;
170 break;
171 case 5:
172 move_time = move_time_us * 4;
173 break;
174 default:
175 dev_err(&client->dev, "%s: t_div parameter err %d\n", __func__, dev_vcm->t_div);
176 move_time = move_time_us;
177 break;
178 }
179 return move_time;
180 }
181
dw9763_move_time(struct dw9763_device * dev_vcm,unsigned int move_pos)182 static unsigned int dw9763_move_time(struct dw9763_device *dev_vcm,
183 unsigned int move_pos)
184 {
185 struct i2c_client *client = dev_vcm->client;
186 unsigned int move_time_us = 0;
187
188 switch (dev_vcm->step_mode) {
189 case SAC1_MODE:
190 case SAC2_MODE:
191 case SAC2_5_MODE:
192 case SAC3_MODE:
193 case SAC4_MODE:
194 move_time_us = 6300 + dev_vcm->t_src * 100;
195 move_time_us = dw9763_move_time_div(dev_vcm, move_time_us);
196 break;
197 case DIRECT_MODE:
198 move_time_us = 30000;
199 break;
200 default:
201 dev_err(&client->dev,
202 "%s: step_mode is error %d\n",
203 __func__, dev_vcm->step_mode);
204 break;
205 }
206
207 dev_dbg(&client->dev,
208 "%s: vcm_movefull_t is: %d us\n",
209 __func__, move_time_us);
210
211 return move_time_us;
212 }
213
dw9763_set_dac(struct dw9763_device * dev_vcm,unsigned int dest_dac)214 static int dw9763_set_dac(struct dw9763_device *dev_vcm,
215 unsigned int dest_dac)
216 {
217 struct i2c_client *client = v4l2_get_subdevdata(&dev_vcm->sd);
218 int ret;
219 u32 is_busy, i;
220
221 for (i = 0; i < 10; i++) {
222 ret = dw9763_read_reg(client, 0x05, 1, &is_busy);
223 if (!ret && !(is_busy & 0x01))
224 break;
225 usleep_range(100, 200);
226 }
227
228 ret = dw9763_write_reg(client, 0x03, 2, dest_dac);
229 if (ret != 0)
230 goto err;
231 dev_dbg(&client->dev,
232 "%s: set reg val %d\n", __func__, dest_dac);
233
234 return ret;
235 err:
236 dev_err(&client->dev,
237 "%s: failed with error %d\n", __func__, ret);
238 return ret;
239 }
240
dw9763_get_dac(struct dw9763_device * dev_vcm,unsigned int * cur_dac)241 static int dw9763_get_dac(struct dw9763_device *dev_vcm, unsigned int *cur_dac)
242 {
243 struct i2c_client *client = v4l2_get_subdevdata(&dev_vcm->sd);
244 int ret;
245 unsigned int abs_step;
246
247 ret = dw9763_read_reg(client, 0x03, 2, &abs_step);
248 if (ret != 0)
249 goto err;
250
251 *cur_dac = abs_step;
252 dev_dbg(&client->dev, "%s: get dac %d\n", __func__, *cur_dac);
253
254 return 0;
255
256 err:
257 dev_err(&client->dev,
258 "%s: failed with error %d\n", __func__, ret);
259 return ret;
260 }
261
dw9763_get_pos(struct dw9763_device * dev_vcm,unsigned int * cur_pos)262 static int dw9763_get_pos(struct dw9763_device *dev_vcm,
263 unsigned int *cur_pos)
264 {
265 struct i2c_client *client = dev_vcm->client;
266 int ret;
267 unsigned int abs_step;
268
269 ret = dw9763_read_reg(client, 0x03, 2, &abs_step);
270 if (ret != 0)
271 goto err;
272
273 if (abs_step <= dev_vcm->start_current)
274 abs_step = VCMDRV_MAX_LOG;
275 else if ((abs_step > dev_vcm->start_current) &&
276 (abs_step <= dev_vcm->rated_current))
277 abs_step = (dev_vcm->rated_current - abs_step) / dev_vcm->step;
278 else
279 abs_step = 0;
280
281 *cur_pos = abs_step;
282 dev_dbg(&client->dev, "%s: get position %d\n", __func__, *cur_pos);
283 return 0;
284
285 err:
286 dev_err(&client->dev,
287 "%s: failed with error %d\n", __func__, ret);
288 return ret;
289 }
290
dw9763_set_pos(struct dw9763_device * dev_vcm,unsigned int dest_pos)291 static int dw9763_set_pos(struct dw9763_device *dev_vcm,
292 unsigned int dest_pos)
293 {
294 int ret;
295 unsigned int position = 0;
296 struct i2c_client *client = dev_vcm->client;
297
298 if (dest_pos >= VCMDRV_MAX_LOG)
299 position = dev_vcm->start_current;
300 else
301 position = dev_vcm->start_current +
302 (dev_vcm->step * (VCMDRV_MAX_LOG - dest_pos));
303
304 if (position > DW9763_MAX_REG)
305 position = DW9763_MAX_REG;
306
307 dev_vcm->current_lens_pos = position;
308 dev_vcm->current_related_pos = dest_pos;
309
310 ret = dw9763_set_dac(dev_vcm, position);
311 dev_dbg(&client->dev, "%s: set position %d, dac %d\n", __func__, dest_pos, position);
312
313 return ret;
314 }
315
dw9763_get_ctrl(struct v4l2_ctrl * ctrl)316 static int dw9763_get_ctrl(struct v4l2_ctrl *ctrl)
317 {
318 struct dw9763_device *dev_vcm = to_dw9763_vcm(ctrl);
319
320 if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
321 return dw9763_get_pos(dev_vcm, &ctrl->val);
322
323 return -EINVAL;
324 }
325
dw9763_set_ctrl(struct v4l2_ctrl * ctrl)326 static int dw9763_set_ctrl(struct v4l2_ctrl *ctrl)
327 {
328 struct dw9763_device *dev_vcm = to_dw9763_vcm(ctrl);
329 struct i2c_client *client = dev_vcm->client;
330 unsigned int dest_pos = ctrl->val;
331 long mv_us;
332 int ret = 0;
333
334 dev_dbg(&client->dev, "ctrl->id: 0x%x, ctrl->val: 0x%x\n",
335 ctrl->id, ctrl->val);
336
337 if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) {
338
339 if (dest_pos > VCMDRV_MAX_LOG) {
340 dev_info(&client->dev,
341 "%s dest_pos is error. %d > %d\n",
342 __func__, dest_pos, VCMDRV_MAX_LOG);
343 return -EINVAL;
344 }
345
346 ret = dw9763_set_pos(dev_vcm, dest_pos);
347
348 dev_vcm->move_us = dev_vcm->vcm_movefull_t;
349
350 dev_dbg(&client->dev,
351 "dest_pos %d, move_us %ld\n",
352 dest_pos, dev_vcm->move_us);
353
354 dev_vcm->start_move_tv = ns_to_kernel_old_timeval(ktime_get_ns());
355 mv_us = dev_vcm->start_move_tv.tv_usec +
356 dev_vcm->move_us;
357 if (mv_us >= 1000000) {
358 dev_vcm->end_move_tv.tv_sec =
359 dev_vcm->start_move_tv.tv_sec + 1;
360 dev_vcm->end_move_tv.tv_usec = mv_us - 1000000;
361 } else {
362 dev_vcm->end_move_tv.tv_sec =
363 dev_vcm->start_move_tv.tv_sec;
364 dev_vcm->end_move_tv.tv_usec = mv_us;
365 }
366 }
367
368 return ret;
369 }
370
371 static const struct v4l2_ctrl_ops dw9763_vcm_ctrl_ops = {
372 .g_volatile_ctrl = dw9763_get_ctrl,
373 .s_ctrl = dw9763_set_ctrl,
374 };
375
dw9763_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)376 static int dw9763_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
377 {
378 int rval;
379
380 rval = pm_runtime_get_sync(sd->dev);
381 if (rval < 0) {
382 pm_runtime_put_noidle(sd->dev);
383 return rval;
384 }
385
386 return 0;
387 }
388
dw9763_close(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)389 static int dw9763_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
390 {
391 pm_runtime_put(sd->dev);
392
393 return 0;
394 }
395
396 static const struct v4l2_subdev_internal_ops dw9763_int_ops = {
397 .open = dw9763_open,
398 .close = dw9763_close,
399 };
400
dw9763_update_vcm_cfg(struct dw9763_device * dev_vcm)401 static void dw9763_update_vcm_cfg(struct dw9763_device *dev_vcm)
402 {
403 struct i2c_client *client = dev_vcm->client;
404 int cur_dist;
405
406 if (dev_vcm->max_ma == 0) {
407 dev_err(&client->dev, "max current is zero");
408 return;
409 }
410
411 cur_dist = dev_vcm->vcm_cfg.rated_ma - dev_vcm->vcm_cfg.start_ma;
412 cur_dist = cur_dist * DW9763_MAX_REG / dev_vcm->max_ma;
413 dev_vcm->step = (cur_dist + (VCMDRV_MAX_LOG - 1)) / VCMDRV_MAX_LOG;
414 dev_vcm->start_current = dev_vcm->vcm_cfg.start_ma *
415 DW9763_MAX_REG / dev_vcm->max_ma;
416 dev_vcm->rated_current = dev_vcm->vcm_cfg.rated_ma *
417 DW9763_MAX_REG / dev_vcm->max_ma;
418 dev_vcm->step_mode = dev_vcm->vcm_cfg.step_mode;
419
420 dev_info(&client->dev,
421 "vcm_cfg: %d, %d, %d, max_ma %d\n",
422 dev_vcm->vcm_cfg.start_ma,
423 dev_vcm->vcm_cfg.rated_ma,
424 dev_vcm->vcm_cfg.step_mode,
425 dev_vcm->max_ma);
426 }
427
dw9763_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)428 static long dw9763_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
429 {
430 struct dw9763_device *dev_vcm = sd_to_dw9763_vcm(sd);
431 struct i2c_client *client = dev_vcm->client;
432 struct rk_cam_vcm_tim *vcm_tim;
433 struct rk_cam_vcm_cfg *vcm_cfg;
434 int ret = 0;
435
436 if (cmd == RK_VIDIOC_VCM_TIMEINFO) {
437 vcm_tim = (struct rk_cam_vcm_tim *)arg;
438
439 vcm_tim->vcm_start_t.tv_sec = dev_vcm->start_move_tv.tv_sec;
440 vcm_tim->vcm_start_t.tv_usec =
441 dev_vcm->start_move_tv.tv_usec;
442 vcm_tim->vcm_end_t.tv_sec = dev_vcm->end_move_tv.tv_sec;
443 vcm_tim->vcm_end_t.tv_usec = dev_vcm->end_move_tv.tv_usec;
444
445 dev_dbg(&client->dev, "dw9763_get_move_res 0x%lx, 0x%lx, 0x%lx, 0x%lx\n",
446 vcm_tim->vcm_start_t.tv_sec,
447 vcm_tim->vcm_start_t.tv_usec,
448 vcm_tim->vcm_end_t.tv_sec,
449 vcm_tim->vcm_end_t.tv_usec);
450 } else if (cmd == RK_VIDIOC_GET_VCM_CFG) {
451 vcm_cfg = (struct rk_cam_vcm_cfg *)arg;
452
453 vcm_cfg->start_ma = dev_vcm->vcm_cfg.start_ma;
454 vcm_cfg->rated_ma = dev_vcm->vcm_cfg.rated_ma;
455 vcm_cfg->step_mode = dev_vcm->vcm_cfg.step_mode;
456 } else if (cmd == RK_VIDIOC_SET_VCM_CFG) {
457 vcm_cfg = (struct rk_cam_vcm_cfg *)arg;
458
459 if (vcm_cfg->start_ma == 0 && vcm_cfg->rated_ma == 0) {
460 dev_err(&client->dev,
461 "vcm_cfg err, start_ma %d, rated_ma %d\n",
462 vcm_cfg->start_ma, vcm_cfg->rated_ma);
463 return -EINVAL;
464 }
465 dev_vcm->vcm_cfg.start_ma = vcm_cfg->start_ma;
466 dev_vcm->vcm_cfg.rated_ma = vcm_cfg->rated_ma;
467 dev_vcm->vcm_cfg.step_mode = vcm_cfg->step_mode;
468 dw9763_update_vcm_cfg(dev_vcm);
469 } else {
470 dev_err(&client->dev,
471 "cmd 0x%x not supported\n", cmd);
472 return -EINVAL;
473 }
474
475 return ret;
476 }
477
478 #ifdef CONFIG_COMPAT
dw9763_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)479 static long dw9763_compat_ioctl32(struct v4l2_subdev *sd,
480 unsigned int cmd, unsigned long arg)
481 {
482 struct dw9763_device *dev_vcm = sd_to_dw9763_vcm(sd);
483 struct i2c_client *client = dev_vcm->client;
484 void __user *up = compat_ptr(arg);
485 struct rk_cam_compat_vcm_tim compat_vcm_tim;
486 struct rk_cam_vcm_tim vcm_tim;
487 struct rk_cam_vcm_cfg vcm_cfg;
488 long ret;
489
490 if (cmd == RK_VIDIOC_COMPAT_VCM_TIMEINFO) {
491 struct rk_cam_compat_vcm_tim __user *p32 = up;
492
493 ret = dw9763_ioctl(sd, RK_VIDIOC_VCM_TIMEINFO, &vcm_tim);
494 compat_vcm_tim.vcm_start_t.tv_sec = vcm_tim.vcm_start_t.tv_sec;
495 compat_vcm_tim.vcm_start_t.tv_usec = vcm_tim.vcm_start_t.tv_usec;
496 compat_vcm_tim.vcm_end_t.tv_sec = vcm_tim.vcm_end_t.tv_sec;
497 compat_vcm_tim.vcm_end_t.tv_usec = vcm_tim.vcm_end_t.tv_usec;
498
499 put_user(compat_vcm_tim.vcm_start_t.tv_sec,
500 &p32->vcm_start_t.tv_sec);
501 put_user(compat_vcm_tim.vcm_start_t.tv_usec,
502 &p32->vcm_start_t.tv_usec);
503 put_user(compat_vcm_tim.vcm_end_t.tv_sec,
504 &p32->vcm_end_t.tv_sec);
505 put_user(compat_vcm_tim.vcm_end_t.tv_usec,
506 &p32->vcm_end_t.tv_usec);
507 } else if (cmd == RK_VIDIOC_GET_VCM_CFG) {
508 ret = dw9763_ioctl(sd, RK_VIDIOC_GET_VCM_CFG, &vcm_cfg);
509 if (!ret) {
510 ret = copy_to_user(up, &vcm_cfg, sizeof(vcm_cfg));
511 if (ret)
512 ret = -EFAULT;
513 }
514 } else if (cmd == RK_VIDIOC_SET_VCM_CFG) {
515 ret = copy_from_user(&vcm_cfg, up, sizeof(vcm_cfg));
516 if (!ret)
517 ret = dw9763_ioctl(sd, cmd, &vcm_cfg);
518 else
519 ret = -EFAULT;
520 } else {
521 dev_err(&client->dev,
522 "cmd 0x%x not supported\n", cmd);
523 return -EINVAL;
524 }
525
526 return ret;
527 }
528 #endif
529
530 static const struct v4l2_subdev_core_ops dw9763_core_ops = {
531 .ioctl = dw9763_ioctl,
532 #ifdef CONFIG_COMPAT
533 .compat_ioctl32 = dw9763_compat_ioctl32
534 #endif
535 };
536
537 static const struct v4l2_subdev_ops dw9763_ops = {
538 .core = &dw9763_core_ops,
539 };
540
dw9763_subdev_cleanup(struct dw9763_device * dw9763_dev)541 static void dw9763_subdev_cleanup(struct dw9763_device *dw9763_dev)
542 {
543 v4l2_device_unregister_subdev(&dw9763_dev->sd);
544 v4l2_device_unregister(&dw9763_dev->vdev);
545 v4l2_ctrl_handler_free(&dw9763_dev->ctrls_vcm);
546 media_entity_cleanup(&dw9763_dev->sd.entity);
547 }
548
dw9763_init_controls(struct dw9763_device * dev_vcm)549 static int dw9763_init_controls(struct dw9763_device *dev_vcm)
550 {
551 struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
552 const struct v4l2_ctrl_ops *ops = &dw9763_vcm_ctrl_ops;
553
554 v4l2_ctrl_handler_init(hdl, 1);
555
556 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
557 0, VCMDRV_MAX_LOG, 1, 32);
558
559 if (hdl->error)
560 dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n",
561 __func__, hdl->error);
562 dev_vcm->sd.ctrl_handler = hdl;
563 return hdl->error;
564 }
565
566 #define USED_SYS_DEBUG
567 #ifdef USED_SYS_DEBUG
set_dacval(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)568 static ssize_t set_dacval(struct device *dev,
569 struct device_attribute *attr,
570 const char *buf,
571 size_t count)
572 {
573 struct i2c_client *client = to_i2c_client(dev);
574 struct v4l2_subdev *sd = i2c_get_clientdata(client);
575 struct dw9763_device *dev_vcm = sd_to_dw9763_vcm(sd);
576 int val = 0;
577 int ret = 0;
578
579 ret = kstrtoint(buf, 0, &val);
580 if (!ret)
581 dw9763_set_dac(dev_vcm, val);
582
583 return count;
584 }
585
get_dacval(struct device * dev,struct device_attribute * attr,char * buf)586 static ssize_t get_dacval(struct device *dev,
587 struct device_attribute *attr, char *buf)
588 {
589 struct i2c_client *client = to_i2c_client(dev);
590 struct v4l2_subdev *sd = i2c_get_clientdata(client);
591 struct dw9763_device *dev_vcm = sd_to_dw9763_vcm(sd);
592 unsigned int dac = 0;
593
594 dw9763_get_dac(dev_vcm, &dac);
595 return sprintf(buf, "%u\n", dac);
596 }
597
598 static struct device_attribute attributes[] = {
599 __ATTR(dacval, 0600, get_dacval, set_dacval),
600 };
601
add_sysfs_interfaces(struct device * dev)602 static int add_sysfs_interfaces(struct device *dev)
603 {
604 int i;
605
606 for (i = 0; i < ARRAY_SIZE(attributes); i++)
607 if (device_create_file(dev, attributes + i))
608 goto undo;
609 return 0;
610 undo:
611 for (i--; i >= 0 ; i--)
612 device_remove_file(dev, attributes + i);
613 dev_err(dev, "%s: failed to create sysfs interface\n", __func__);
614 return -ENODEV;
615 }
616
remove_sysfs_interfaces(struct device * dev)617 static int remove_sysfs_interfaces(struct device *dev)
618 {
619 int i;
620
621 for (i = 0; i < ARRAY_SIZE(attributes); i++)
622 device_remove_file(dev, attributes + i);
623 return 0;
624 }
625 #else
add_sysfs_interfaces(struct device * dev)626 static inline int add_sysfs_interfaces(struct device *dev)
627 {
628 return 0;
629 }
630
remove_sysfs_interfaces(struct device * dev)631 static inline int remove_sysfs_interfaces(struct device *dev)
632 {
633 return 0;
634 }
635 #endif
636
__dw9763_set_power(struct dw9763_device * dw9763_dev,bool on)637 static int __dw9763_set_power(struct dw9763_device *dw9763_dev, bool on)
638 {
639 if (dw9763_dev->power_gpio)
640 gpiod_direction_output(dw9763_dev->power_gpio, on);
641 usleep_range(10000, 11000);
642
643 return 0;
644 }
645
dw9763_check_id(struct dw9763_device * dw9763_dev)646 static int __maybe_unused dw9763_check_id(struct dw9763_device *dw9763_dev)
647 {
648 int ret = 0;
649 unsigned int pid = 0x00;
650 struct i2c_client *client = dw9763_dev->client;
651 struct device *dev = &client->dev;
652
653 __dw9763_set_power(dw9763_dev, true);
654 ret = dw9763_read_reg(client, DW9763_REG_CHIP_ID, 1, &pid);
655
656 if (pid != DW9763_CHIP_ID) {
657 dev_err(dev, "Unexpected sensor id(%06x), ret(%d)\n", pid, ret);
658 return -ENODEV;
659 }
660
661 dev_info(&dw9763_dev->client->dev,
662 "Detected dw9763 vcm id:0x%x\n", DW9763_CHIP_ID);
663 return 0;
664 }
dw9763_probe_init(struct i2c_client * client)665 static int dw9763_probe_init(struct i2c_client *client)
666 {
667 int ret = 0;
668
669 /* Default goto power down mode when finished probe */
670 ret = dw9763_write_reg(client, 0x02, 1, 0x01);
671 if (ret)
672 goto err;
673
674 return 0;
675 err:
676 dev_err(&client->dev, "probe init failed with error %d\n", ret);
677 return -1;
678 }
679
dw9763_probe(struct i2c_client * client,const struct i2c_device_id * id)680 static int dw9763_probe(struct i2c_client *client,
681 const struct i2c_device_id *id)
682 {
683 struct device_node *np = of_node_get(client->dev.of_node);
684 struct dw9763_device *dw9763_dev;
685 unsigned int max_ma, start_ma, rated_ma, step_mode;
686 unsigned int t_src, t_div;
687 struct v4l2_subdev *sd;
688 char facing[2];
689 int ret;
690
691 dev_info(&client->dev, "probing...\n");
692 if (of_property_read_u32(np,
693 OF_CAMERA_VCMDRV_MAX_CURRENT,
694 (unsigned int *)&max_ma)) {
695 max_ma = DW9763_MAX_CURRENT;
696 dev_info(&client->dev,
697 "could not get module %s from dts!\n",
698 OF_CAMERA_VCMDRV_MAX_CURRENT);
699 }
700 if (max_ma == 0)
701 max_ma = DW9763_MAX_CURRENT;
702
703 if (of_property_read_u32(np,
704 OF_CAMERA_VCMDRV_START_CURRENT,
705 (unsigned int *)&start_ma)) {
706 start_ma = DW9763_DEFAULT_START_CURRENT;
707 dev_info(&client->dev,
708 "could not get module %s from dts!\n",
709 OF_CAMERA_VCMDRV_START_CURRENT);
710 }
711 if (of_property_read_u32(np,
712 OF_CAMERA_VCMDRV_RATED_CURRENT,
713 (unsigned int *)&rated_ma)) {
714 rated_ma = DW9763_DEFAULT_RATED_CURRENT;
715 dev_info(&client->dev,
716 "could not get module %s from dts!\n",
717 OF_CAMERA_VCMDRV_RATED_CURRENT);
718 }
719 if (of_property_read_u32(np,
720 OF_CAMERA_VCMDRV_STEP_MODE,
721 (unsigned int *)&step_mode)) {
722 step_mode = DW9763_DEFAULT_STEP_MODE;
723 dev_info(&client->dev,
724 "could not get module %s from dts!\n",
725 OF_CAMERA_VCMDRV_STEP_MODE);
726 }
727
728 if (of_property_read_u32(np,
729 OF_CAMERA_VCMDRV_T_SRC,
730 (unsigned int *)&t_src)) {
731 t_src = DW9763_DEFAULT_T_SACT;
732 dev_info(&client->dev,
733 "could not get module %s from dts!\n",
734 OF_CAMERA_VCMDRV_T_SRC);
735 }
736
737 if (of_property_read_u32(np,
738 OF_CAMERA_VCMDRV_T_DIV,
739 (unsigned int *)&t_div)) {
740 t_div = DW9763_DEFAULT_T_DIV;
741 dev_info(&client->dev,
742 "could not get module %s from dts!\n",
743 OF_CAMERA_VCMDRV_T_DIV);
744 }
745
746 dw9763_dev = devm_kzalloc(&client->dev, sizeof(*dw9763_dev),
747 GFP_KERNEL);
748 if (dw9763_dev == NULL)
749 return -ENOMEM;
750
751 ret = of_property_read_u32(np, RKMODULE_CAMERA_MODULE_INDEX,
752 &dw9763_dev->module_index);
753 ret |= of_property_read_string(np, RKMODULE_CAMERA_MODULE_FACING,
754 &dw9763_dev->module_facing);
755 if (ret) {
756 dev_err(&client->dev,
757 "could not get module information!\n");
758 return -EINVAL;
759 }
760 dw9763_dev->client = client;
761 dw9763_dev->power_gpio = devm_gpiod_get(&client->dev,
762 "power", GPIOD_OUT_LOW);
763 if (IS_ERR(dw9763_dev->power_gpio)) {
764 dw9763_dev->power_gpio = NULL;
765 dev_warn(&client->dev,
766 "Failed to get power-gpios, maybe no use\n");
767 }
768
769 /* enter power down mode */
770 dw9763_probe_init(client);
771
772 v4l2_i2c_subdev_init(&dw9763_dev->sd, client, &dw9763_ops);
773 dw9763_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
774 dw9763_dev->sd.internal_ops = &dw9763_int_ops;
775
776 ret = dw9763_init_controls(dw9763_dev);
777 if (ret)
778 goto err_cleanup;
779
780 ret = media_entity_pads_init(&dw9763_dev->sd.entity, 0, NULL);
781 if (ret < 0)
782 goto err_cleanup;
783
784 sd = &dw9763_dev->sd;
785 sd->entity.function = MEDIA_ENT_F_LENS;
786
787 memset(facing, 0, sizeof(facing));
788 if (strcmp(dw9763_dev->module_facing, "back") == 0)
789 facing[0] = 'b';
790 else
791 facing[0] = 'f';
792
793 snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
794 dw9763_dev->module_index, facing,
795 DW9763_NAME, dev_name(sd->dev));
796 ret = v4l2_async_register_subdev(sd);
797 if (ret)
798 dev_err(&client->dev, "v4l2 async register subdev failed\n");
799
800 dw9763_dev->max_ma = max_ma;
801 dw9763_dev->vcm_cfg.start_ma = start_ma;
802 dw9763_dev->vcm_cfg.rated_ma = rated_ma;
803 dw9763_dev->vcm_cfg.step_mode = step_mode;
804 dw9763_update_vcm_cfg(dw9763_dev);
805 dw9763_dev->move_us = 0;
806 dw9763_dev->current_related_pos = VCMDRV_MAX_LOG;
807 dw9763_dev->start_move_tv = ns_to_kernel_old_timeval(ktime_get_ns());
808 dw9763_dev->end_move_tv = ns_to_kernel_old_timeval(ktime_get_ns());
809
810 dw9763_dev->t_src = t_src;
811 dw9763_dev->t_div = t_div;
812
813 i2c_set_clientdata(client, dw9763_dev);
814 mutex_init(&dw9763_dev->lock);
815
816 dw9763_dev->vcm_movefull_t =
817 dw9763_move_time(dw9763_dev, DW9763_MAX_REG);
818 pm_runtime_set_active(&client->dev);
819 pm_runtime_enable(&client->dev);
820 pm_runtime_idle(&client->dev);
821
822 add_sysfs_interfaces(&client->dev);
823 dev_info(&client->dev, "probing successful\n");
824
825 return 0;
826 err_cleanup:
827 dw9763_subdev_cleanup(dw9763_dev);
828
829 dev_err(&client->dev, "Probe failed: %d\n", ret);
830
831 return ret;
832 }
833
dw9763_remove(struct i2c_client * client)834 static int dw9763_remove(struct i2c_client *client)
835 {
836 struct dw9763_device *dw9763_dev = i2c_get_clientdata(client);
837
838 remove_sysfs_interfaces(&client->dev);
839 mutex_destroy(&dw9763_dev->lock);
840 pm_runtime_disable(&client->dev);
841 dw9763_subdev_cleanup(dw9763_dev);
842
843 return 0;
844 }
845
dw9763_init(struct i2c_client * client)846 static int dw9763_init(struct i2c_client *client)
847 {
848 struct dw9763_device *dev_vcm = i2c_get_clientdata(client);
849 int ret = 0;
850 u32 ring = 0;
851 u32 mode_val = 0;
852 u32 algo_time = 0;
853
854
855 /* Delay 200us~300us */
856 usleep_range(200, 300);
857 ret = dw9763_write_reg(client, 0x02, 1, 0x00);
858 if (ret)
859 goto err;
860 usleep_range(100, 200);
861
862 if (dev_vcm->step_mode != DIRECT_MODE)
863 ring = 0x02;
864 ret = dw9763_write_reg(client, 0x02, 1, ring);
865 if (ret)
866 goto err;
867 switch (dev_vcm->step_mode) {
868 case SAC1_MODE:
869 case SAC2_MODE:
870 case SAC2_5_MODE:
871 case SAC3_MODE:
872 case SAC4_MODE:
873 mode_val |= dev_vcm->step_mode << 5;
874 break;
875 default:
876 break;
877 }
878
879 mode_val |= (dev_vcm->t_div & 0x07);
880 algo_time = dev_vcm->t_src;
881 ret = dw9763_write_reg(client, 0x06, 1, mode_val);
882 if (ret)
883 goto err;
884 ret = dw9763_write_reg(client, 0x07, 1, algo_time);
885 if (ret)
886 goto err;
887 usleep_range(100, 200);
888
889 return 0;
890 err:
891 dev_err(&client->dev, "init failed with error %d\n", ret);
892 return -1;
893 }
894
dw9763_vcm_suspend(struct device * dev)895 static int __maybe_unused dw9763_vcm_suspend(struct device *dev)
896 {
897 struct i2c_client *client = to_i2c_client(dev);
898 int ret = 0;
899
900 /* set to power down mode */
901 ret = dw9763_write_reg(client, 0x02, 1, 0x01);
902 if (ret)
903 dev_err(&client->dev, "failed to set power down mode!\n");
904
905 return 0;
906 }
907
dw9763_vcm_resume(struct device * dev)908 static int __maybe_unused dw9763_vcm_resume(struct device *dev)
909 {
910 struct i2c_client *client = to_i2c_client(dev);
911 struct dw9763_device *dev_vcm = i2c_get_clientdata(client);
912
913 dw9763_init(client);
914 dw9763_set_pos(dev_vcm, dev_vcm->current_related_pos);
915 return 0;
916 }
917
918 static const struct i2c_device_id dw9763_id_table[] = {
919 { DW9763_NAME, 0 },
920 { { 0 } }
921 };
922 MODULE_DEVICE_TABLE(i2c, dw9763_id_table);
923
924 static const struct of_device_id dw9763_of_table[] = {
925 { .compatible = "dongwoon,dw9763" },
926 { { 0 } }
927 };
928 MODULE_DEVICE_TABLE(of, dw9763_of_table);
929
930 static const struct dev_pm_ops dw9763_pm_ops = {
931 SET_SYSTEM_SLEEP_PM_OPS(dw9763_vcm_suspend, dw9763_vcm_resume)
932 SET_RUNTIME_PM_OPS(dw9763_vcm_suspend, dw9763_vcm_resume, NULL)
933 };
934
935 static struct i2c_driver dw9763_i2c_driver = {
936 .driver = {
937 .name = DW9763_NAME,
938 .pm = &dw9763_pm_ops,
939 .of_match_table = dw9763_of_table,
940 },
941 .probe = &dw9763_probe,
942 .remove = &dw9763_remove,
943 .id_table = dw9763_id_table,
944 };
945
946 module_i2c_driver(dw9763_i2c_driver);
947
948 MODULE_DESCRIPTION("DW9763 VCM driver");
949 MODULE_LICENSE("GPL");
950