• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * dw9763 vcm driver
4  *
5  * Copyright (C) 2019 Fuzhou Rockchip Electronics Co., Ltd.
6  */
7 
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_RING_PD_CONTROL_REG		0x02
23 #define DW9763_DATAM_REG 0X03
24 #define DW9763_DATAL_REG 0X04
25 
26 #define DW9763_STATUS_ADDR			0x05
27 #define DW9763_SAC_PRESC_REG			0x06
28 #define DW9763_SAC_TIME_REG	0x07
29 #define DW9763_FLAG_REG 0X10
30 
31 #define DW9763_MAX_CURRENT		100U
32 #define DW9763_MAX_REG			1023U
33 
34 #define DW9763_DEFAULT_START_CURRENT	0
35 #define DW9763_DEFAULT_RATED_CURRENT	100
36 #define DW9763_DEFAULT_STEP_MODE	0xd
37 #define REG_NULL			0xFF
38 
39 #define OF_CAMERA_VCMDRV_CONTROL_MODE     "rockchip,vcm-control-mode"
40 #define OF_CAMERA_VCMDRV_SACDIV_MODE      "rockchip,vcm-sacdiv-mode"
41 #define VCMDRV_DEFAULT_CONTROL_MODE       4
42 #define VCMDRV_DEFAULT_SACDIV_MODE        1
43 
44 /* dw9763 device structure */
45 struct dw9763_device {
46 	struct v4l2_ctrl_handler ctrls_vcm;
47 	struct v4l2_subdev sd;
48 	struct v4l2_device vdev;
49 	u16 current_val;
50 
51 	unsigned short current_related_pos;
52 	unsigned short current_lens_pos;
53 	unsigned int start_current;
54 	unsigned int rated_current;
55 	unsigned int step;
56 	unsigned int step_mode;
57 	unsigned int control_mode;
58 	unsigned int sacdiv_mode;
59 	unsigned long mv_time_per_pos;
60 
61 	struct __kernel_old_timeval start_move_tv;
62 	struct __kernel_old_timeval end_move_tv;
63 	unsigned long move_us;
64 
65 	u32 module_index;
66 	const char *module_facing;
67 };
68 
to_dw9763_vcm(struct v4l2_ctrl * ctrl)69 static inline struct dw9763_device *to_dw9763_vcm(struct v4l2_ctrl *ctrl)
70 {
71 	return container_of(ctrl->handler, struct dw9763_device, ctrls_vcm);
72 }
73 
sd_to_dw9763_vcm(struct v4l2_subdev * subdev)74 static inline struct dw9763_device *sd_to_dw9763_vcm(struct v4l2_subdev *subdev)
75 {
76 	return container_of(subdev, struct dw9763_device, sd);
77 }
78 
dw9763_read_reg(struct i2c_client * client,u8 addr,u32 * val,u8 len)79 static int dw9763_read_reg(struct i2c_client *client,
80 	u8 addr, u32 *val, u8 len)
81 {
82 	struct i2c_msg msgs[2];
83 	u8 *data_be_p;
84 	__be32 data_be = 0;
85 	int ret;
86 
87 	if (len > 4 || !len)
88 		return -EINVAL;
89 
90 	data_be_p = (u8 *)&data_be;
91 	/* Write register address */
92 	msgs[0].addr = client->addr;
93 	msgs[0].flags = 0;
94 	msgs[0].len = 1;
95 	msgs[0].buf = (u8 *)&addr;
96 
97 	/* Read data from register */
98 	msgs[1].addr = client->addr;
99 	msgs[1].flags = I2C_M_RD;
100 	msgs[1].len = len;
101 	msgs[1].buf = &data_be_p[4 - len];
102 
103 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
104 	if (ret != ARRAY_SIZE(msgs))
105 		return -EIO;
106 
107 	*val = be32_to_cpu(data_be);
108 
109 	return 0;
110 }
111 
dw9763_write_reg(struct i2c_client * client,u8 addr,u32 val,u8 len)112 static int dw9763_write_reg(struct i2c_client *client,
113 	u8 addr, u32 val, u8 len)
114 {
115 	u32 buf_i, val_i;
116 	u8 buf[5];
117 	u8 *val_p;
118 	__be32 val_be;
119 
120 	if (len > 4)
121 		return -EINVAL;
122 
123 	buf[0] = addr & 0xff;
124 
125 	val_be = cpu_to_be32(val);
126 	val_p = (u8 *)&val_be;
127 	buf_i = 1;
128 	val_i = 4 - len;
129 
130 	while (val_i < 4)
131 		buf[buf_i++] = val_p[val_i++];
132 
133 	if (i2c_master_send(client, buf, len + 1) != len + 1)
134 		return -EIO;
135 
136 	return 0;
137 }
138 
dw9763_get_pos(struct dw9763_device * dev_vcm,unsigned int * cur_pos)139 static int dw9763_get_pos(struct dw9763_device *dev_vcm,
140 	unsigned int *cur_pos)
141 {
142 	struct i2c_client *client = v4l2_get_subdevdata(&dev_vcm->sd);
143 	int ret;
144 	u32 val;
145 	unsigned int abs_step;
146 
147 	ret = dw9763_read_reg(client, DW9763_DATAM_REG, &val, 2);
148 	if (ret != 0)
149 		goto err;
150 
151 	abs_step = val & 0x3ff;
152 	if (abs_step <= dev_vcm->start_current)
153 		abs_step = VCMDRV_MAX_LOG;
154 	else if ((abs_step > dev_vcm->start_current) &&
155 		 (abs_step <= dev_vcm->rated_current))
156 		abs_step = (dev_vcm->rated_current - abs_step) / dev_vcm->step;
157 	else
158 		abs_step = 0;
159 
160 	*cur_pos = abs_step;
161 	dev_dbg(&client->dev, "%s: get position %d\n", __func__, *cur_pos);
162 	return 0;
163 
164 err:
165 	dev_err(&client->dev,
166 		"%s: failed with error %d\n", __func__, ret);
167 	return ret;
168 }
169 
dw9763_set_pos(struct dw9763_device * dev_vcm,unsigned int dest_pos)170 static int dw9763_set_pos(struct dw9763_device *dev_vcm,
171 	unsigned int dest_pos)
172 {
173 	int ret;
174 	unsigned int position = 0;
175 	struct i2c_client *client = v4l2_get_subdevdata(&dev_vcm->sd);
176 
177 	if (dest_pos >= VCMDRV_MAX_LOG)
178 		position = dev_vcm->start_current;
179 	else
180 		position = dev_vcm->start_current +
181 			   (dev_vcm->step * (VCMDRV_MAX_LOG - dest_pos));
182 
183 	if (position > DW9763_MAX_REG)
184 		position = DW9763_MAX_REG;
185 
186 	dev_vcm->current_lens_pos = position;
187 	dev_vcm->current_related_pos = dest_pos;
188 	ret = dw9763_write_reg(client, DW9763_DATAM_REG, position & 0x3ff, 2);
189 	if (ret != 0)
190 		goto err;
191 
192 	dev_dbg(&client->dev, "@@@@@@@@ %s: get position %d\n", __func__, position);
193 	return ret;
194 err:
195 	dev_err(&client->dev,
196 		"%s: failed with error %d\n", __func__, ret);
197 	return ret;
198 }
199 
dw9763_get_ctrl(struct v4l2_ctrl * ctrl)200 static int dw9763_get_ctrl(struct v4l2_ctrl *ctrl)
201 {
202 	struct dw9763_device *dev_vcm = to_dw9763_vcm(ctrl);
203 
204 	if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE)
205 		return dw9763_get_pos(dev_vcm, &ctrl->val);
206 
207 	return -EINVAL;
208 }
209 
dw9763_set_ctrl(struct v4l2_ctrl * ctrl)210 static int dw9763_set_ctrl(struct v4l2_ctrl *ctrl)
211 {
212 	struct dw9763_device *dev_vcm = to_dw9763_vcm(ctrl);
213 	struct i2c_client *client = v4l2_get_subdevdata(&dev_vcm->sd);
214 	unsigned int dest_pos = ctrl->val;
215 	int move_pos;
216 	long mv_us;
217 	int ret = 0;
218 
219 	if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) {
220 		if (dest_pos > VCMDRV_MAX_LOG) {
221 			dev_info(&client->dev,
222 				"%s dest_pos is error. %d > %d\n",
223 				__func__, dest_pos, VCMDRV_MAX_LOG);
224 			return -EINVAL;
225 		}
226 		/* calculate move time */
227 		move_pos = dev_vcm->current_related_pos - dest_pos;
228 		if (move_pos < 0)
229 			move_pos = -move_pos;
230 
231 		ret = dw9763_set_pos(dev_vcm, dest_pos);
232 
233 		if (dev_vcm->control_mode == 1)
234 			dev_vcm->move_us = dev_vcm->mv_time_per_pos * move_pos;
235 		else
236 			dev_vcm->move_us = dev_vcm->mv_time_per_pos;
237 		dev_dbg(&client->dev,
238 			"dest_pos %d, move_us %ld\n",
239 			dest_pos, dev_vcm->move_us);
240 
241 		dev_vcm->start_move_tv = ns_to_kernel_old_timeval(ktime_get_ns());
242 		mv_us = dev_vcm->start_move_tv.tv_usec + dev_vcm->move_us;
243 		if (mv_us >= 1000000) {
244 			dev_vcm->end_move_tv.tv_sec =
245 				dev_vcm->start_move_tv.tv_sec + 1;
246 			dev_vcm->end_move_tv.tv_usec = mv_us - 1000000;
247 		} else {
248 			dev_vcm->end_move_tv.tv_sec =
249 					dev_vcm->start_move_tv.tv_sec;
250 			dev_vcm->end_move_tv.tv_usec = mv_us;
251 		}
252 	}
253 
254 	return ret;
255 }
256 
257 static const struct v4l2_ctrl_ops dw9763_vcm_ctrl_ops = {
258 	.g_volatile_ctrl = dw9763_get_ctrl,
259 	.s_ctrl = dw9763_set_ctrl,
260 };
261 
dw9763t_init(struct dw9763_device * dev)262 static int dw9763t_init(struct dw9763_device *dev)
263 {
264 	int ret = 0;
265 	struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
266 	u32 control_mode = 0;
267 	u32 step_mode = 0;
268 
269 	ret = dw9763_write_reg(client, DW9763_RING_PD_CONTROL_REG, 0x01, 1);
270 	if (ret < 0)
271 		goto err;
272 
273 	ret = dw9763_write_reg(client, DW9763_RING_PD_CONTROL_REG, 0x00, 1);
274 	if (ret < 0)
275 		goto err;
276 
277 	/*There is need a sleep after power on for write i2c*/
278 	usleep_range(10000, 11000);
279 
280 	ret = dw9763_write_reg(client, DW9763_RING_PD_CONTROL_REG, 0x02, 1);
281 	if (ret < 0)
282 		goto err;
283 
284 
285 	ret = dw9763_write_reg(client, DW9763_SAC_PRESC_REG, 0x61, 2);
286 	if (ret < 0)
287 		goto err;
288 
289 	ret = dw9763_write_reg(client, DW9763_SAC_TIME_REG, 0x39, 2);
290 	if (ret < 0)
291 		goto err;
292 
293 	dev_info(&client->dev, "enter vcm driver init\n");
294 	/*There is need a sleep after out of standby status*/
295 	msleep(100);
296 
297 	/*step_mode = (dev->sacdiv_mode << 6) | (dev->step_mode & 0x3f);
298 	ret = dw9763_write_reg(client, DW9763_SACT_REG, step_mode, 1);
299 	if (ret < 0)
300 		goto err;
301 	control_mode = 0x31;
302 	control_mode |= (dev->control_mode & 0x07) << 1;
303 	ret = dw9763_write_reg(client, DW9763_CONTROL, control_mode, 1);
304 	if (ret < 0)
305 		goto err;
306 */
307 	dev_info(&client->dev, "dw9763t_init OK!!!\n");
308 	return 0;
309 err:
310 	dev_err(&client->dev, "failed with error %d\n", ret);
311 	return ret;
312 }
313 
dw9763_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)314 static int dw9763_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
315 {
316 	int rval;
317 	struct dw9763_device *dev_vcm = sd_to_dw9763_vcm(sd);
318 
319 	rval = pm_runtime_get_sync(sd->dev);
320 	if (rval < 0) {
321 		pm_runtime_put_noidle(sd->dev);
322 		return rval;
323 	}
324 
325 	rval = dw9763t_init(dev_vcm);
326 	if (rval < 0) {
327 		pm_runtime_put_noidle(sd->dev);
328 		return rval;
329 	}
330 	return 0;
331 }
332 
dw9763_close(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)333 static int dw9763_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
334 {
335 	pm_runtime_put(sd->dev);
336 
337 	return 0;
338 }
339 
340 static const struct v4l2_subdev_internal_ops dw9763_int_ops = {
341 	.open = dw9763_open,
342 	.close = dw9763_close,
343 };
344 
dw9763_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)345 static long dw9763_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
346 {
347 	int ret = 0;
348 	struct rk_cam_vcm_tim *vcm_tim;
349 	struct i2c_client *client = v4l2_get_subdevdata(sd);
350 	struct dw9763_device *dw9763_dev = sd_to_dw9763_vcm(sd);
351 
352 //	if (cmd == RK_VIDIOC_VCM_TIMEINFO) {
353 		vcm_tim = (struct rk_cam_vcm_tim *)arg;
354 
355 		vcm_tim->vcm_start_t.tv_sec = dw9763_dev->start_move_tv.tv_sec;
356 		vcm_tim->vcm_start_t.tv_usec =
357 				dw9763_dev->start_move_tv.tv_usec;
358 		vcm_tim->vcm_end_t.tv_sec = dw9763_dev->end_move_tv.tv_sec;
359 		vcm_tim->vcm_end_t.tv_usec = dw9763_dev->end_move_tv.tv_usec;
360 
361 		dev_dbg(&client->dev, "dw9763_get_move_res 0x%lx, 0x%lx, 0x%lx, 0x%lx\n",
362 			vcm_tim->vcm_start_t.tv_sec,
363 			vcm_tim->vcm_start_t.tv_usec,
364 			vcm_tim->vcm_end_t.tv_sec,
365 			vcm_tim->vcm_end_t.tv_usec);
366 	//} else {
367 	//	dev_err(&client->dev,
368 	//		"cmd 0x%x not supported\n", cmd);
369 	//	return -EINVAL;
370 	//}
371 
372 	return ret;
373 }
374 
375 #ifdef CONFIG_COMPAT
dw9763_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)376 static long dw9763_compat_ioctl32(struct v4l2_subdev *sd,
377 	unsigned int cmd, unsigned long arg)
378 {
379 	struct rk_cam_vcm_tim vcm_tim;
380 	struct rk_cam_compat_vcm_tim compat_vcm_tim;
381 	void __user *up = compat_ptr(arg);
382 	struct i2c_client *client = v4l2_get_subdevdata(sd);
383 	long ret;
384 
385 	if (cmd == RK_VIDIOC_COMPAT_VCM_TIMEINFO) {
386 		struct rk_cam_compat_vcm_tim __user *p32 = up;
387 
388 		ret = dw9763_ioctl(sd, RK_VIDIOC_VCM_TIMEINFO, &vcm_tim);
389 		compat_vcm_tim.vcm_start_t.tv_sec = vcm_tim.vcm_start_t.tv_sec;
390 		compat_vcm_tim.vcm_start_t.tv_usec =
391 				vcm_tim.vcm_start_t.tv_usec;
392 		compat_vcm_tim.vcm_end_t.tv_sec = vcm_tim.vcm_end_t.tv_sec;
393 		compat_vcm_tim.vcm_end_t.tv_usec = vcm_tim.vcm_end_t.tv_usec;
394 
395 		put_user(compat_vcm_tim.vcm_start_t.tv_sec,
396 			&p32->vcm_start_t.tv_sec);
397 		put_user(compat_vcm_tim.vcm_start_t.tv_usec,
398 			&p32->vcm_start_t.tv_usec);
399 		put_user(compat_vcm_tim.vcm_end_t.tv_sec,
400 			&p32->vcm_end_t.tv_sec);
401 		put_user(compat_vcm_tim.vcm_end_t.tv_usec,
402 			&p32->vcm_end_t.tv_usec);
403 	} else {
404 		dev_err(&client->dev,
405 			"cmd 0x%x not supported\n", cmd);
406 		return -EINVAL;
407 	}
408 
409 	return ret;
410 }
411 #endif
412 
413 static const struct v4l2_subdev_core_ops dw9763_core_ops = {
414 	.ioctl = dw9763_ioctl,
415 #ifdef CONFIG_COMPAT
416 	.compat_ioctl32 = dw9763_compat_ioctl32
417 #endif
418 };
419 
420 static const struct v4l2_subdev_ops dw9763_ops = {
421 	.core = &dw9763_core_ops,
422 };
423 
dw9763_subdev_cleanup(struct dw9763_device * dw9763_dev)424 static void dw9763_subdev_cleanup(struct dw9763_device *dw9763_dev)
425 {
426 	v4l2_device_unregister_subdev(&dw9763_dev->sd);
427 	v4l2_device_unregister(&dw9763_dev->vdev);
428 	v4l2_ctrl_handler_free(&dw9763_dev->ctrls_vcm);
429 	media_entity_cleanup(&dw9763_dev->sd.entity);
430 }
431 
dw9763_init_controls(struct dw9763_device * dev_vcm)432 static int dw9763_init_controls(struct dw9763_device *dev_vcm)
433 {
434 	struct v4l2_ctrl_handler *hdl = &dev_vcm->ctrls_vcm;
435 	const struct v4l2_ctrl_ops *ops = &dw9763_vcm_ctrl_ops;
436 
437 	v4l2_ctrl_handler_init(hdl, 1);
438 
439 	v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FOCUS_ABSOLUTE,
440 			  0, VCMDRV_MAX_LOG, 1, VCMDRV_MAX_LOG);
441 
442 	if (hdl->error)
443 		dev_err(dev_vcm->sd.dev, "%s fail error: 0x%x\n",
444 			__func__, hdl->error);
445 	dev_vcm->sd.ctrl_handler = hdl;
446 	return hdl->error;
447 }
448 
dw9763_probe(struct i2c_client * client,const struct i2c_device_id * id)449 static int dw9763_probe(struct i2c_client *client,
450 			const struct i2c_device_id *id)
451 {
452 	struct device_node *np = of_node_get(client->dev.of_node);
453 	struct dw9763_device *dw9763_dev;
454 	int ret;
455 	int current_distance;
456 	unsigned int start_current;
457 	unsigned int rated_current;
458 	unsigned int step_mode;
459 	unsigned int control_mode;
460 	unsigned int sacdiv_mode;
461 	struct v4l2_subdev *sd;
462 	char facing[2];
463 
464 	printk("XXXXXXXXXXXXXXX dw9763_probe\n");
465 
466 	dev_info(&client->dev, "probing...\n");
467 	if (of_property_read_u32(np,
468 		OF_CAMERA_VCMDRV_START_CURRENT,
469 		(unsigned int *)&start_current)) {
470 		start_current = DW9763_DEFAULT_START_CURRENT;
471 		dev_info(&client->dev,
472 			"could not get module %s from dts!\n",
473 			OF_CAMERA_VCMDRV_START_CURRENT);
474 	}
475 	if (of_property_read_u32(np,
476 		OF_CAMERA_VCMDRV_RATED_CURRENT,
477 		(unsigned int *)&rated_current)) {
478 		rated_current = DW9763_DEFAULT_RATED_CURRENT;
479 		dev_info(&client->dev,
480 			"could not get module %s from dts!\n",
481 			OF_CAMERA_VCMDRV_RATED_CURRENT);
482 	}
483 	if (of_property_read_u32(np,
484 		OF_CAMERA_VCMDRV_STEP_MODE,
485 		(unsigned int *)&step_mode)) {
486 		step_mode = DW9763_DEFAULT_STEP_MODE;
487 		dev_info(&client->dev,
488 			"could not get module %s from dts!\n",
489 			OF_CAMERA_VCMDRV_STEP_MODE);
490 	}
491 	if (of_property_read_u32(np,
492 		OF_CAMERA_VCMDRV_CONTROL_MODE,
493 		(unsigned int *)&control_mode)) {
494 		control_mode = VCMDRV_DEFAULT_CONTROL_MODE;
495 		dev_info(&client->dev,
496 			"could not get module %s from dts!\n",
497 			OF_CAMERA_VCMDRV_CONTROL_MODE);
498 	}
499 	if (of_property_read_u32(np,
500 		OF_CAMERA_VCMDRV_SACDIV_MODE,
501 		(unsigned int *)&sacdiv_mode)) {
502 		sacdiv_mode = VCMDRV_DEFAULT_SACDIV_MODE;
503 		dev_info(&client->dev,
504 			"could not get module %s from dts!\n",
505 			OF_CAMERA_VCMDRV_SACDIV_MODE);
506 	}
507 
508 	dw9763_dev = devm_kzalloc(&client->dev, sizeof(*dw9763_dev),
509 				  GFP_KERNEL);
510 	if (dw9763_dev == NULL)
511 		return -ENOMEM;
512 
513 	ret = of_property_read_u32(np, RKMODULE_CAMERA_MODULE_INDEX,
514 				   &dw9763_dev->module_index);
515 	ret |= of_property_read_string(np, RKMODULE_CAMERA_MODULE_FACING,
516 				       &dw9763_dev->module_facing);
517 	if (ret) {
518 		dev_err(&client->dev,
519 			"could not get module information!\n");
520 		return -EINVAL;
521 	}
522 
523 	v4l2_i2c_subdev_init(&dw9763_dev->sd, client, &dw9763_ops);
524 	dw9763_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
525 	dw9763_dev->sd.internal_ops = &dw9763_int_ops;
526 
527 	ret = dw9763_init_controls(dw9763_dev);
528 	if (ret)
529 		goto err_cleanup;
530 
531 	ret = media_entity_pads_init(&dw9763_dev->sd.entity, 0, NULL);
532 	if (ret < 0)
533 		goto err_cleanup;
534 
535 	sd = &dw9763_dev->sd;
536 	sd->entity.function = MEDIA_ENT_F_LENS;
537 
538 	memset(facing, 0, sizeof(facing));
539 	if (strcmp(dw9763_dev->module_facing, "back") == 0)
540 		facing[0] = 'b';
541 	else
542 		facing[0] = 'f';
543 
544 	snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
545 		 dw9763_dev->module_index, facing,
546 		 DW9763_NAME, dev_name(sd->dev));
547 	ret = v4l2_async_register_subdev(sd);
548 	if (ret)
549 		dev_err(&client->dev, "v4l2 async register subdev failed\n");
550 
551 	current_distance = rated_current - start_current;
552 	current_distance = current_distance * DW9763_MAX_REG /
553 						DW9763_MAX_CURRENT;
554 	dw9763_dev->step = (current_distance + (VCMDRV_MAX_LOG - 1)) /
555 						VCMDRV_MAX_LOG;
556 	dw9763_dev->start_current = start_current * DW9763_MAX_REG /
557 						DW9763_MAX_CURRENT;
558 	dw9763_dev->rated_current = dw9763_dev->start_current +
559 						VCMDRV_MAX_LOG *
560 						dw9763_dev->step;
561 	dw9763_dev->step_mode     = step_mode;
562 	dw9763_dev->move_us       = 0;
563 	dw9763_dev->current_related_pos = VCMDRV_MAX_LOG;
564 	dw9763_dev->start_move_tv = ns_to_kernel_old_timeval(ktime_get_ns());
565 	dw9763_dev->end_move_tv = ns_to_kernel_old_timeval(ktime_get_ns());
566 
567 	switch (control_mode) {
568 	case 0:
569 		dev_err(&client->dev, "control_mode is derect mode, not support\n");
570 		return -EINVAL;
571 	case 1:
572 		dw9763_dev->mv_time_per_pos = (126 + step_mode * 2) * dw9763_dev->step;
573 		dev_dbg(&client->dev, "control_mode is LSC mode\n");
574 		break;
575 	case 2:
576 	case 3:
577 	case 4:
578 	case 5:
579 	case 6:
580 	case 7:
581 		dw9763_dev->mv_time_per_pos = (6300 + step_mode * 100);
582 		dev_dbg(&client->dev, "control_mode is LSC mode\n");
583 		break;
584 	default:
585 		dev_err(&client->dev, "set unknown control_mode\n");
586 		return -EINVAL;
587 	}
588 
589 	switch (sacdiv_mode) {
590 	case 0:
591 		dw9763_dev->mv_time_per_pos *= 2;
592 		dev_dbg(&client->dev, "sacdiv_mode is %d\n", sacdiv_mode);
593 		break;
594 	case 1:
595 		dev_dbg(&client->dev, "sacdiv_mode is %d\n", sacdiv_mode);
596 		break;
597 	case 2:
598 		dw9763_dev->mv_time_per_pos /= 2;
599 		dev_dbg(&client->dev, "sacdiv_mode is %d\n", sacdiv_mode);
600 		break;
601 	case 3:
602 		dw9763_dev->mv_time_per_pos /= 4;
603 		dev_dbg(&client->dev, "sacdiv_mode is %d\n", sacdiv_mode);
604 		break;
605 	default:
606 		dev_err(&client->dev, "set unknown control_mode\n");
607 		return -EINVAL;
608 	}
609 
610 	pm_runtime_set_active(&client->dev);
611 	pm_runtime_enable(&client->dev);
612 	pm_runtime_idle(&client->dev);
613 
614 	dev_info(&client->dev, "probing successful\n");
615 
616 	printk("XXXXXXXXXXXXXXX dw9763_probe OK\n");
617 
618 	return 0;
619 
620 err_cleanup:
621 	dw9763_subdev_cleanup(dw9763_dev);
622 	dev_err(&client->dev, "Probe failed: %d\n", ret);
623 	return ret;
624 }
625 
dw9763_remove(struct i2c_client * client)626 static int dw9763_remove(struct i2c_client *client)
627 {
628 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
629 	struct dw9763_device *dw9763_dev = sd_to_dw9763_vcm(sd);
630 
631 	pm_runtime_disable(&client->dev);
632 	dw9763_subdev_cleanup(dw9763_dev);
633 
634 	return 0;
635 }
636 
dw9763_vcm_suspend(struct device * dev)637 static int __maybe_unused dw9763_vcm_suspend(struct device *dev)
638 {
639 	return 0;
640 }
641 
dw9763_vcm_resume(struct device * dev)642 static int __maybe_unused dw9763_vcm_resume(struct device *dev)
643 {
644 	return 0;
645 }
646 
647 static const struct i2c_device_id dw9763_id_table[] = {
648 	{ DW9763_NAME, 0 },
649 	{ { 0 } }
650 };
651 MODULE_DEVICE_TABLE(i2c, dw9763_id_table);
652 
653 static const struct of_device_id dw9763_of_table[] = {
654 	{ .compatible = "dongwoon,dw9763" },
655 	{ { 0 } }
656 };
657 MODULE_DEVICE_TABLE(of, dw9763_of_table);
658 
659 static const struct dev_pm_ops dw9763_pm_ops = {
660 	SET_SYSTEM_SLEEP_PM_OPS(dw9763_vcm_suspend, dw9763_vcm_resume)
661 	SET_RUNTIME_PM_OPS(dw9763_vcm_suspend, dw9763_vcm_resume, NULL)
662 };
663 
664 static struct i2c_driver dw9763_i2c_driver = {
665 	.driver = {
666 		.name = DW9763_NAME,
667 		.pm = &dw9763_pm_ops,
668 		.of_match_table = dw9763_of_table,
669 	},
670 	.probe = &dw9763_probe,
671 	.remove = &dw9763_remove,
672 	.id_table = dw9763_id_table,
673 };
674 
675 module_i2c_driver(dw9763_i2c_driver);
676 
677 MODULE_DESCRIPTION("DW9763 VCM driver");
678 MODULE_LICENSE("GPL v2");
679