xref: /OK3568_Linux_fs/kernel/drivers/media/i2c/mp6507.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * motor  driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2020 Rockchip Electronics Co., Ltd.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun //#define DEBUG
9*4882a593Smuzhiyun #include <linux/io.h>
10*4882a593Smuzhiyun #include <linux/of_gpio.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/init.h>
13*4882a593Smuzhiyun #include <linux/device.h>
14*4882a593Smuzhiyun #include <linux/fb.h>
15*4882a593Smuzhiyun #include <linux/interrupt.h>
16*4882a593Smuzhiyun #include <linux/kernel.h>
17*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
18*4882a593Smuzhiyun #include <linux/of_irq.h>
19*4882a593Smuzhiyun #include <linux/platform_device.h>
20*4882a593Smuzhiyun #include <linux/wakelock.h>
21*4882a593Smuzhiyun #include <linux/hrtimer.h>
22*4882a593Smuzhiyun #include <linux/pwm.h>
23*4882a593Smuzhiyun #include <linux/delay.h>
24*4882a593Smuzhiyun #include <media/v4l2-subdev.h>
25*4882a593Smuzhiyun #include <media/v4l2-ctrls.h>
26*4882a593Smuzhiyun #include <media/v4l2-device.h>
27*4882a593Smuzhiyun #include <linux/mutex.h>
28*4882a593Smuzhiyun #include <linux/version.h>
29*4882a593Smuzhiyun #include <linux/rk-camera-module.h>
30*4882a593Smuzhiyun #include <linux/completion.h>
31*4882a593Smuzhiyun #include <linux/rk_vcm_head.h>
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #define DRIVER_VERSION	KERNEL_VERSION(0, 0x01, 0x00)
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #define DRIVER_NAME "mp6507"
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #define MAX_START_UP_HZ			(1200)
38*4882a593Smuzhiyun #define MOTOR_MAX_HZ			(2500)
39*4882a593Smuzhiyun #define SPEED_QUEUE_MAX			(71)
40*4882a593Smuzhiyun #define THRESHOLD_TO_SPEEDED_UP_DEF	(500)
41*4882a593Smuzhiyun #define STEP_PER_SPEED_DEF		(8)
42*4882a593Smuzhiyun #define SPEED_QUEUE_NUM_DEF		(71)
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #define IRIS_MAX_STEP_DEF		80
45*4882a593Smuzhiyun #define FOCUS_MAX_STEP_DEF		7500
46*4882a593Smuzhiyun #define ZOOM_MAX_STEP_DEF		7500
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #define IRIS_MAX_LOG			80
49*4882a593Smuzhiyun #define FOCUS_MAX_LOG			7500
50*4882a593Smuzhiyun #define ZOOM_MAX_LOG			7500
51*4882a593Smuzhiyun #define IRIS_LOG_STEP			4
52*4882a593Smuzhiyun #define FOCUS_LOG_STEP			4
53*4882a593Smuzhiyun #define ZOOM_LOG_STEP			4
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun enum {
56*4882a593Smuzhiyun 	MOTOR_STATUS_STOPPED = 0,
57*4882a593Smuzhiyun 	MOTOR_STATUS_CW = 1,
58*4882a593Smuzhiyun 	MOTOR_STATUS_CCW = 2,
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun enum ext_dev_type {
62*4882a593Smuzhiyun 	TYPE_IRIS = 0,
63*4882a593Smuzhiyun 	TYPE_FOCUS = 1,
64*4882a593Smuzhiyun 	TYPE_ZOOM = 2,
65*4882a593Smuzhiyun };
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun struct speed_s {
68*4882a593Smuzhiyun 	u32 count;
69*4882a593Smuzhiyun 	u64 phase_interval_ns;
70*4882a593Smuzhiyun };
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun struct speed_queue_s {
73*4882a593Smuzhiyun 	int count;
74*4882a593Smuzhiyun 	struct speed_s *speed_p;
75*4882a593Smuzhiyun };
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun struct ext_dev {
78*4882a593Smuzhiyun 	u8 type;
79*4882a593Smuzhiyun 	u32 step_max;
80*4882a593Smuzhiyun 	u32 cur_pos;
81*4882a593Smuzhiyun 	u32 step_per_pos;
82*4882a593Smuzhiyun 	u32 start_up_speed;
83*4882a593Smuzhiyun 	u32 max_speed;
84*4882a593Smuzhiyun 	u32 speed_queue_num;
85*4882a593Smuzhiyun 	u32 first_speed_step;
86*4882a593Smuzhiyun 	u32 ths_speeded_up;
87*4882a593Smuzhiyun 	u32 speed_up_step_cnt;
88*4882a593Smuzhiyun 	u32 *speed_up_table;
89*4882a593Smuzhiyun 	u32 *speed_down_table;
90*4882a593Smuzhiyun 	u32 length_up;//speed_up_table length
91*4882a593Smuzhiyun 	u32 length_down;
92*4882a593Smuzhiyun 	struct gpio_desc *en_gpio;
93*4882a593Smuzhiyun 	struct rk_cam_vcm_tim mv_tim;
94*4882a593Smuzhiyun 	struct speed_queue_s speed_que;
95*4882a593Smuzhiyun 	struct speed_queue_s one_speed_que;
96*4882a593Smuzhiyun };
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun struct motor_dev {
99*4882a593Smuzhiyun 	struct v4l2_subdev sd;
100*4882a593Smuzhiyun 	struct v4l2_ctrl_handler ctrl_handler;
101*4882a593Smuzhiyun 	struct pwm_device *pwm_a1;
102*4882a593Smuzhiyun 	struct pwm_device *pwm_a2;
103*4882a593Smuzhiyun 	struct pwm_device *pwm_b1;
104*4882a593Smuzhiyun 	struct pwm_device *pwm_b2;
105*4882a593Smuzhiyun 	struct v4l2_ctrl *iris_ctrl;
106*4882a593Smuzhiyun 	struct v4l2_ctrl *focus_ctrl;
107*4882a593Smuzhiyun 	struct v4l2_ctrl *zoom_ctrl;
108*4882a593Smuzhiyun 	struct device *dev;
109*4882a593Smuzhiyun 	struct hrtimer timer;
110*4882a593Smuzhiyun 	struct mutex mutex;
111*4882a593Smuzhiyun 	u32 move_status;
112*4882a593Smuzhiyun 	u32 move_cnt;
113*4882a593Smuzhiyun 	u32 module_index;
114*4882a593Smuzhiyun 	const char *module_facing;
115*4882a593Smuzhiyun 	bool resched;
116*4882a593Smuzhiyun 	struct completion complete;
117*4882a593Smuzhiyun 	struct ext_dev iris;
118*4882a593Smuzhiyun 	struct ext_dev focus;
119*4882a593Smuzhiyun 	struct ext_dev zoom;
120*4882a593Smuzhiyun 	struct ext_dev *cur_ext_dev;
121*4882a593Smuzhiyun 	struct speed_queue_s *run_queue;
122*4882a593Smuzhiyun 	struct pwm_state pwm_state;
123*4882a593Smuzhiyun };
124*4882a593Smuzhiyun 
set_motor_running_status(struct motor_dev * motor,struct ext_dev * cur_ext_dev,int status,u32 pos)125*4882a593Smuzhiyun static int set_motor_running_status(struct motor_dev *motor,
126*4882a593Smuzhiyun 				    struct ext_dev *cur_ext_dev,
127*4882a593Smuzhiyun 				    int status, u32 pos)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	int ret = 0;
130*4882a593Smuzhiyun 	u64 mv_us = 0;
131*4882a593Smuzhiyun 	u64 mv_s = 0;
132*4882a593Smuzhiyun 	u64 move_time = 0;
133*4882a593Smuzhiyun 	u32 step_cnt = 0;
134*4882a593Smuzhiyun 	int i = 0;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	if (motor->move_status != MOTOR_STATUS_STOPPED)
137*4882a593Smuzhiyun 		wait_for_completion(&motor->complete);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	motor->cur_ext_dev = cur_ext_dev;
140*4882a593Smuzhiyun 	if (!IS_ERR(cur_ext_dev->en_gpio))
141*4882a593Smuzhiyun 		gpiod_set_value_cansleep(cur_ext_dev->en_gpio, 1);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	motor->move_status = status;
144*4882a593Smuzhiyun 	step_cnt = pos * cur_ext_dev->step_per_pos;
145*4882a593Smuzhiyun 	if (cur_ext_dev->speed_queue_num > 1 &&
146*4882a593Smuzhiyun 	    step_cnt >= cur_ext_dev->ths_speeded_up) {
147*4882a593Smuzhiyun 		motor->run_queue = &cur_ext_dev->speed_que;
148*4882a593Smuzhiyun 		motor->run_queue->speed_p[cur_ext_dev->length_up - 1].count =
149*4882a593Smuzhiyun 			step_cnt - cur_ext_dev->speed_up_step_cnt;
150*4882a593Smuzhiyun 	} else {
151*4882a593Smuzhiyun 		motor->run_queue = &cur_ext_dev->one_speed_que;
152*4882a593Smuzhiyun 		motor->run_queue->speed_p[0].count = step_cnt;
153*4882a593Smuzhiyun 	}
154*4882a593Smuzhiyun 	motor->move_cnt = motor->run_queue->count;
155*4882a593Smuzhiyun 	reinit_completion(&motor->complete);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	cur_ext_dev->mv_tim.vcm_start_t = ns_to_timeval(ktime_get_ns());
158*4882a593Smuzhiyun 	for (i = 0; i < motor->run_queue->count; i++) {
159*4882a593Smuzhiyun 		move_time += (u64)motor->run_queue->speed_p[i].count *
160*4882a593Smuzhiyun 			     (u64)motor->run_queue->speed_p[i].phase_interval_ns;
161*4882a593Smuzhiyun 		dev_dbg(motor->dev, "speed_que.speed[%d], count %d, phase_interval_ns %llu\n",
162*4882a593Smuzhiyun 			i,
163*4882a593Smuzhiyun 			motor->run_queue->speed_p[i].count,
164*4882a593Smuzhiyun 			motor->run_queue->speed_p[i].phase_interval_ns);
165*4882a593Smuzhiyun 	}
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	mv_us = div_u64(move_time, 1000);
168*4882a593Smuzhiyun 	dev_dbg(motor->dev, "motor move needs %lld us\n", mv_us);
169*4882a593Smuzhiyun 	mv_us += cur_ext_dev->mv_tim.vcm_start_t.tv_usec;
170*4882a593Smuzhiyun 	if (mv_us >= 1000000) {
171*4882a593Smuzhiyun 		mv_s = div_u64(mv_us, 1000000);
172*4882a593Smuzhiyun 		cur_ext_dev->mv_tim.vcm_end_t.tv_sec =
173*4882a593Smuzhiyun 			cur_ext_dev->mv_tim.vcm_start_t.tv_sec + mv_s;
174*4882a593Smuzhiyun 		cur_ext_dev->mv_tim.vcm_end_t.tv_usec = mv_us - (mv_s * 1000000);
175*4882a593Smuzhiyun 	} else {
176*4882a593Smuzhiyun 		cur_ext_dev->mv_tim.vcm_end_t.tv_sec =
177*4882a593Smuzhiyun 				cur_ext_dev->mv_tim.vcm_start_t.tv_sec;
178*4882a593Smuzhiyun 		cur_ext_dev->mv_tim.vcm_end_t.tv_usec = mv_us;
179*4882a593Smuzhiyun 	}
180*4882a593Smuzhiyun 	hrtimer_start(&motor->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
181*4882a593Smuzhiyun 	return ret;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun 
fill_speed_squeue(struct device * dev,struct ext_dev * ext_dev)184*4882a593Smuzhiyun static int fill_speed_squeue(struct device *dev, struct ext_dev *ext_dev)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	struct device_node *node = dev->of_node;
187*4882a593Smuzhiyun 	struct property *prop = NULL;
188*4882a593Smuzhiyun 	u32 length_up = 0;
189*4882a593Smuzhiyun 	u32 length_down = 0;
190*4882a593Smuzhiyun 	u32 *speed_up_table = NULL;
191*4882a593Smuzhiyun 	u32 *speed_down_table = NULL;
192*4882a593Smuzhiyun 	int i = 0;
193*4882a593Smuzhiyun 	size_t size;
194*4882a593Smuzhiyun 	u32 step_cnt = 0;
195*4882a593Smuzhiyun 	u32 step_total = 0;
196*4882a593Smuzhiyun 	int ret = 0;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	ext_dev->ths_speeded_up = 0;
199*4882a593Smuzhiyun 	size = sizeof(*ext_dev->one_speed_que.speed_p);
200*4882a593Smuzhiyun 	ext_dev->one_speed_que.speed_p = devm_kzalloc(dev, size, GFP_KERNEL);
201*4882a593Smuzhiyun 	if (!ext_dev->one_speed_que.speed_p)
202*4882a593Smuzhiyun 		return -ENOMEM;
203*4882a593Smuzhiyun 	ext_dev->one_speed_que.count = 1;
204*4882a593Smuzhiyun 	ext_dev->one_speed_que.speed_p[0].count = ext_dev->first_speed_step;
205*4882a593Smuzhiyun 	ext_dev->one_speed_que.speed_p[0].phase_interval_ns =
206*4882a593Smuzhiyun 		div_u64(NSEC_PER_SEC, ext_dev->start_up_speed);
207*4882a593Smuzhiyun 	switch (ext_dev->type) {
208*4882a593Smuzhiyun 	case TYPE_IRIS:
209*4882a593Smuzhiyun 		//max step is 80, needn't speed-up
210*4882a593Smuzhiyun 		return 0;
211*4882a593Smuzhiyun 	case TYPE_FOCUS:
212*4882a593Smuzhiyun 		prop = of_find_property(node, "focus-speed-up-table", &length_up);
213*4882a593Smuzhiyun 		if (prop)
214*4882a593Smuzhiyun 			length_up /= sizeof(u32);
215*4882a593Smuzhiyun 		if (length_up > 0) {
216*4882a593Smuzhiyun 			size = sizeof(*speed_up_table) * length_up;
217*4882a593Smuzhiyun 			speed_up_table = devm_kzalloc(dev, size, GFP_KERNEL);
218*4882a593Smuzhiyun 			if (!speed_up_table)
219*4882a593Smuzhiyun 				return -ENOMEM;
220*4882a593Smuzhiyun 			ret = of_property_read_u32_array(node, "focus-speed-up-table",
221*4882a593Smuzhiyun 							speed_up_table,
222*4882a593Smuzhiyun 							length_up);
223*4882a593Smuzhiyun 			if (ret < 0) {
224*4882a593Smuzhiyun 				dev_info(dev,
225*4882a593Smuzhiyun 					"fail to get speed table, used default speed!\n");
226*4882a593Smuzhiyun 				devm_kfree(dev, speed_up_table);
227*4882a593Smuzhiyun 				speed_up_table = NULL;
228*4882a593Smuzhiyun 				ext_dev->speed_queue_num = 1;
229*4882a593Smuzhiyun 				return 0;
230*4882a593Smuzhiyun 			}
231*4882a593Smuzhiyun 			dev_dbg(dev,
232*4882a593Smuzhiyun 				"dev tpype %d, speed-up table length %d, buf size %u\n",
233*4882a593Smuzhiyun 				ext_dev->type,
234*4882a593Smuzhiyun 				length_up,
235*4882a593Smuzhiyun 				size);
236*4882a593Smuzhiyun 		}
237*4882a593Smuzhiyun 		prop = of_find_property(node, "focus-speed-down-table", &length_down);
238*4882a593Smuzhiyun 		if (prop)
239*4882a593Smuzhiyun 			length_down /= sizeof(u32);
240*4882a593Smuzhiyun 		if (length_down > 0) {
241*4882a593Smuzhiyun 			size = sizeof(*speed_down_table) * length_down;
242*4882a593Smuzhiyun 			speed_down_table = devm_kzalloc(dev, size, GFP_KERNEL);
243*4882a593Smuzhiyun 			if (!speed_down_table)
244*4882a593Smuzhiyun 				return -ENOMEM;
245*4882a593Smuzhiyun 			ret = of_property_read_u32_array(node, "focus-speed-down-table",
246*4882a593Smuzhiyun 							speed_down_table,
247*4882a593Smuzhiyun 							length_down);
248*4882a593Smuzhiyun 			if (ret < 0) {
249*4882a593Smuzhiyun 				dev_info(dev,
250*4882a593Smuzhiyun 					"fail to get speed table, used default speed!\n");
251*4882a593Smuzhiyun 				devm_kfree(dev, speed_down_table);
252*4882a593Smuzhiyun 				speed_down_table = NULL;
253*4882a593Smuzhiyun 			} else {
254*4882a593Smuzhiyun 				dev_dbg(dev,
255*4882a593Smuzhiyun 					"dev tpype %d, speed-down table length %d, buf size %u\n",
256*4882a593Smuzhiyun 					ext_dev->type,
257*4882a593Smuzhiyun 					length_up,
258*4882a593Smuzhiyun 					size);
259*4882a593Smuzhiyun 			}
260*4882a593Smuzhiyun 		}
261*4882a593Smuzhiyun 		break;
262*4882a593Smuzhiyun 	case TYPE_ZOOM:
263*4882a593Smuzhiyun 		prop = of_find_property(node, "zoom-speed-up-table", &length_up);
264*4882a593Smuzhiyun 		if (prop)
265*4882a593Smuzhiyun 			length_up /= sizeof(u32);
266*4882a593Smuzhiyun 		if (length_up > 0) {
267*4882a593Smuzhiyun 			size = sizeof(*speed_up_table) * length_up;
268*4882a593Smuzhiyun 			speed_up_table = devm_kzalloc(dev, size, GFP_KERNEL);
269*4882a593Smuzhiyun 			if (!speed_up_table)
270*4882a593Smuzhiyun 				return -ENOMEM;
271*4882a593Smuzhiyun 			ret = of_property_read_u32_array(node, "zoom-speed-up-table",
272*4882a593Smuzhiyun 							speed_up_table,
273*4882a593Smuzhiyun 							length_up);
274*4882a593Smuzhiyun 			if (ret < 0) {
275*4882a593Smuzhiyun 				dev_info(dev,
276*4882a593Smuzhiyun 					"fail to get speed table, used default speed!\n");
277*4882a593Smuzhiyun 				ext_dev->speed_queue_num = 1;
278*4882a593Smuzhiyun 				devm_kfree(dev, speed_up_table);
279*4882a593Smuzhiyun 				speed_up_table = NULL;
280*4882a593Smuzhiyun 				return 0;
281*4882a593Smuzhiyun 			}
282*4882a593Smuzhiyun 			dev_dbg(dev,
283*4882a593Smuzhiyun 				"dev tpype %d, speed-up table length %d, buf size %u\n",
284*4882a593Smuzhiyun 				ext_dev->type,
285*4882a593Smuzhiyun 				length_up,
286*4882a593Smuzhiyun 				size);
287*4882a593Smuzhiyun 		}
288*4882a593Smuzhiyun 		prop = of_find_property(node, "zoom-speed-down-table", &length_down);
289*4882a593Smuzhiyun 		if (prop)
290*4882a593Smuzhiyun 			length_down /= sizeof(u32);
291*4882a593Smuzhiyun 		if (length_down > 0) {
292*4882a593Smuzhiyun 			size = sizeof(*speed_down_table) * length_down;
293*4882a593Smuzhiyun 			speed_down_table = devm_kzalloc(dev, size, GFP_KERNEL);
294*4882a593Smuzhiyun 			if (!speed_down_table)
295*4882a593Smuzhiyun 				return -ENOMEM;
296*4882a593Smuzhiyun 			ret = of_property_read_u32_array(node, "zoom-speed-down-table",
297*4882a593Smuzhiyun 							speed_down_table,
298*4882a593Smuzhiyun 							length_down);
299*4882a593Smuzhiyun 			if (ret < 0) {
300*4882a593Smuzhiyun 				dev_info(dev,
301*4882a593Smuzhiyun 					"fail to get speed table, used default speed!\n");
302*4882a593Smuzhiyun 				devm_kfree(dev, speed_down_table);
303*4882a593Smuzhiyun 				speed_down_table = NULL;
304*4882a593Smuzhiyun 			} else {
305*4882a593Smuzhiyun 				dev_dbg(dev,
306*4882a593Smuzhiyun 					"dev tpype %d, speed-down table length %d, buf size %u\n",
307*4882a593Smuzhiyun 					ext_dev->type,
308*4882a593Smuzhiyun 					length_up,
309*4882a593Smuzhiyun 					size);
310*4882a593Smuzhiyun 			}
311*4882a593Smuzhiyun 		}
312*4882a593Smuzhiyun 		break;
313*4882a593Smuzhiyun 	default:
314*4882a593Smuzhiyun 		return -EINVAL;
315*4882a593Smuzhiyun 	}
316*4882a593Smuzhiyun 	if (speed_up_table == NULL || speed_up_table[0] > ext_dev->start_up_speed ||
317*4882a593Smuzhiyun 	    speed_up_table[length_up - 1] > ext_dev->max_speed) {
318*4882a593Smuzhiyun 		dev_info(dev,
319*4882a593Smuzhiyun 			"speed_up_table data error, not to used it!\n");
320*4882a593Smuzhiyun 		ext_dev->speed_queue_num = 1;
321*4882a593Smuzhiyun 	} else {
322*4882a593Smuzhiyun 		ext_dev->length_up = length_up;
323*4882a593Smuzhiyun 		if (speed_down_table != NULL)
324*4882a593Smuzhiyun 			ext_dev->speed_queue_num = length_up + length_down;
325*4882a593Smuzhiyun 		else
326*4882a593Smuzhiyun 			ext_dev->speed_queue_num = length_up * 2 - 1;
327*4882a593Smuzhiyun 		size = sizeof(*ext_dev->speed_que.speed_p) * ext_dev->speed_queue_num;
328*4882a593Smuzhiyun 		ext_dev->speed_que.speed_p = devm_kzalloc(dev, size, GFP_KERNEL);
329*4882a593Smuzhiyun 		if (!ext_dev->speed_que.speed_p)
330*4882a593Smuzhiyun 			return -ENOMEM;
331*4882a593Smuzhiyun 		for (i = 0; i < length_up - 1; i++) {
332*4882a593Smuzhiyun 			if (i == 0)
333*4882a593Smuzhiyun 				step_cnt = ext_dev->first_speed_step;
334*4882a593Smuzhiyun 			else
335*4882a593Smuzhiyun 				step_cnt =
336*4882a593Smuzhiyun 					ext_dev->first_speed_step *
337*4882a593Smuzhiyun 					speed_up_table[i] / speed_up_table[0];
338*4882a593Smuzhiyun 			step_cnt = (step_cnt + 3) / 4 * 4;
339*4882a593Smuzhiyun 			ext_dev->speed_que.speed_p[i].count = step_cnt;
340*4882a593Smuzhiyun 			ext_dev->speed_que.speed_p[i].phase_interval_ns =
341*4882a593Smuzhiyun 				div_u64(NSEC_PER_SEC, speed_up_table[i]);
342*4882a593Smuzhiyun 			step_total += step_cnt;
343*4882a593Smuzhiyun 			if (speed_down_table == NULL ||
344*4882a593Smuzhiyun 			    speed_down_table[0] > speed_up_table[length_up - 1]) {
345*4882a593Smuzhiyun 				dev_info(dev,
346*4882a593Smuzhiyun 					"speed_down_table data error, used speed_up_table\n");
347*4882a593Smuzhiyun 				ext_dev->speed_que.speed_p[ext_dev->speed_queue_num - i - 1].count =
348*4882a593Smuzhiyun 					step_cnt;
349*4882a593Smuzhiyun 				ext_dev->speed_que.speed_p[ext_dev->speed_queue_num - i - 1].phase_interval_ns =
350*4882a593Smuzhiyun 					div_u64(NSEC_PER_SEC, speed_up_table[i]);
351*4882a593Smuzhiyun 				step_total += step_cnt;
352*4882a593Smuzhiyun 			}
353*4882a593Smuzhiyun 			dev_info(dev,
354*4882a593Smuzhiyun 				"index %d, speed %d, count %d\n",
355*4882a593Smuzhiyun 				i, speed_up_table[i], ext_dev->speed_que.speed_p[i].count);
356*4882a593Smuzhiyun 		}
357*4882a593Smuzhiyun 		ext_dev->speed_up_table = speed_up_table;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 		if (speed_down_table != NULL &&
360*4882a593Smuzhiyun 		    speed_down_table[0] <= speed_up_table[length_up - 1]) {
361*4882a593Smuzhiyun 			for (i = 0; i < length_down; i++) {
362*4882a593Smuzhiyun 				step_cnt =
363*4882a593Smuzhiyun 					ext_dev->first_speed_step *
364*4882a593Smuzhiyun 					speed_down_table[i] / speed_up_table[0];
365*4882a593Smuzhiyun 				step_cnt = (step_cnt + 3) / 4 * 4;
366*4882a593Smuzhiyun 				ext_dev->speed_que.speed_p[length_up + i].count =
367*4882a593Smuzhiyun 					step_cnt;
368*4882a593Smuzhiyun 				ext_dev->speed_que.speed_p[length_up + i].phase_interval_ns =
369*4882a593Smuzhiyun 					div_u64(NSEC_PER_SEC, speed_down_table[i]);
370*4882a593Smuzhiyun 				step_total += step_cnt;
371*4882a593Smuzhiyun 			}
372*4882a593Smuzhiyun 			ext_dev->speed_down_table = speed_down_table;
373*4882a593Smuzhiyun 			ext_dev->length_down = length_down;
374*4882a593Smuzhiyun 		}
375*4882a593Smuzhiyun 		ext_dev->speed_up_step_cnt = step_total;
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 		step_cnt =
378*4882a593Smuzhiyun 			ext_dev->first_speed_step *
379*4882a593Smuzhiyun 			speed_up_table[length_up - 1] / speed_up_table[0];
380*4882a593Smuzhiyun 		step_cnt = (step_cnt + 3) / 4 * 4;
381*4882a593Smuzhiyun 		ext_dev->speed_que.speed_p[length_up - 1].count = step_cnt;
382*4882a593Smuzhiyun 		ext_dev->speed_que.speed_p[length_up - 1].phase_interval_ns =
383*4882a593Smuzhiyun 			div_u64(NSEC_PER_SEC, speed_up_table[length_up - 1]);
384*4882a593Smuzhiyun 		step_total += step_cnt;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 		ext_dev->ths_speeded_up = step_total;
387*4882a593Smuzhiyun 		ext_dev->speed_que.count = ext_dev->speed_queue_num;
388*4882a593Smuzhiyun 	}
389*4882a593Smuzhiyun 	return 0;
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun 
motor_dev_parse_dt(struct motor_dev * motor)392*4882a593Smuzhiyun static int motor_dev_parse_dt(struct motor_dev *motor)
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun 	struct device_node *node = motor->dev->of_node;
395*4882a593Smuzhiyun 	int ret = 0;
396*4882a593Smuzhiyun 	int error = 0;
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	motor->pwm_a1 = devm_pwm_get(motor->dev, "ain1");
399*4882a593Smuzhiyun 	motor->pwm_a2 = devm_pwm_get(motor->dev, "ain2");
400*4882a593Smuzhiyun 	motor->pwm_b1 = devm_pwm_get(motor->dev, "bin1");
401*4882a593Smuzhiyun 	motor->pwm_b2 = devm_pwm_get(motor->dev, "bin2");
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	if (IS_ERR(motor->pwm_a1)) {
404*4882a593Smuzhiyun 		error = PTR_ERR(motor->pwm_a1);
405*4882a593Smuzhiyun 		if (error != -EPROBE_DEFER)
406*4882a593Smuzhiyun 			dev_err(motor->dev, "Failed to request PWM a1 device: %d\n", error);
407*4882a593Smuzhiyun 		return error;
408*4882a593Smuzhiyun 	}
409*4882a593Smuzhiyun 	if (IS_ERR(motor->pwm_a2)) {
410*4882a593Smuzhiyun 		error = PTR_ERR(motor->pwm_a2);
411*4882a593Smuzhiyun 		if (error != -EPROBE_DEFER)
412*4882a593Smuzhiyun 			dev_err(motor->dev, "Failed to request PWM a2 device: %d\n", error);
413*4882a593Smuzhiyun 		return error;
414*4882a593Smuzhiyun 	}
415*4882a593Smuzhiyun 	if (IS_ERR(motor->pwm_b1)) {
416*4882a593Smuzhiyun 		error = PTR_ERR(motor->pwm_b1);
417*4882a593Smuzhiyun 		if (error != -EPROBE_DEFER)
418*4882a593Smuzhiyun 			dev_err(motor->dev, "Failed to request PWM b1 device: %d\n", error);
419*4882a593Smuzhiyun 		return error;
420*4882a593Smuzhiyun 	}
421*4882a593Smuzhiyun 	if (IS_ERR(motor->pwm_b2)) {
422*4882a593Smuzhiyun 		error = PTR_ERR(motor->pwm_b2);
423*4882a593Smuzhiyun 		if (error != -EPROBE_DEFER)
424*4882a593Smuzhiyun 			dev_err(motor->dev, "Failed to request PWM b2 device: %d\n", error);
425*4882a593Smuzhiyun 		return error;
426*4882a593Smuzhiyun 	}
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	/* get iris_en gpio */
429*4882a593Smuzhiyun 	motor->iris.en_gpio = devm_gpiod_get(motor->dev,
430*4882a593Smuzhiyun 					     "iris_en", GPIOD_OUT_LOW);
431*4882a593Smuzhiyun 	if (IS_ERR(motor->iris.en_gpio))
432*4882a593Smuzhiyun 		dev_err(motor->dev, "Failed to get iris_en-gpios\n");
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	/* get focus_en gpio */
435*4882a593Smuzhiyun 	motor->focus.en_gpio = devm_gpiod_get(motor->dev,
436*4882a593Smuzhiyun 					      "focus_en", GPIOD_OUT_LOW);
437*4882a593Smuzhiyun 	if (IS_ERR(motor->focus.en_gpio))
438*4882a593Smuzhiyun 		dev_err(motor->dev, "Failed to get focus_en-gpios\n");
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	/* get zoom_en gpio */
441*4882a593Smuzhiyun 	motor->zoom.en_gpio = devm_gpiod_get(motor->dev,
442*4882a593Smuzhiyun 					     "zoom_en", GPIOD_OUT_LOW);
443*4882a593Smuzhiyun 	if (IS_ERR(motor->zoom.en_gpio))
444*4882a593Smuzhiyun 		dev_err(motor->dev, "Failed to get zoom_en-gpios\n");
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	ret = of_property_read_u32(node,
447*4882a593Smuzhiyun 				   "iris-step-max",
448*4882a593Smuzhiyun 				   &motor->iris.step_max);
449*4882a593Smuzhiyun 	if (ret != 0) {
450*4882a593Smuzhiyun 		motor->iris.step_max = IRIS_MAX_STEP_DEF;
451*4882a593Smuzhiyun 		dev_err(motor->dev,
452*4882a593Smuzhiyun 			"failed get iris iris_pos_max,use dafult value 80\n");
453*4882a593Smuzhiyun 	}
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	ret = of_property_read_u32(node,
456*4882a593Smuzhiyun 				   "focus-step-max",
457*4882a593Smuzhiyun 				   &motor->focus.step_max);
458*4882a593Smuzhiyun 	if (ret != 0) {
459*4882a593Smuzhiyun 		motor->focus.step_max = FOCUS_MAX_STEP_DEF;
460*4882a593Smuzhiyun 		dev_err(motor->dev,
461*4882a593Smuzhiyun 			"failed get iris focus_pos_max,use dafult value 7500\n");
462*4882a593Smuzhiyun 	}
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	ret = of_property_read_u32(node,
465*4882a593Smuzhiyun 				   "zoom-step-max",
466*4882a593Smuzhiyun 				   &motor->zoom.step_max);
467*4882a593Smuzhiyun 	if (ret != 0) {
468*4882a593Smuzhiyun 		motor->zoom.step_max = ZOOM_MAX_STEP_DEF;
469*4882a593Smuzhiyun 		dev_err(motor->dev,
470*4882a593Smuzhiyun 			"failed get iris zoom_pos_max,use dafult value 7500\n");
471*4882a593Smuzhiyun 	}
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	ret = of_property_read_u32(node,
474*4882a593Smuzhiyun 				   "iris-start-up-speed",
475*4882a593Smuzhiyun 				   &motor->iris.start_up_speed);
476*4882a593Smuzhiyun 	if (ret != 0) {
477*4882a593Smuzhiyun 		motor->iris.start_up_speed = MAX_START_UP_HZ;
478*4882a593Smuzhiyun 		dev_err(motor->dev,
479*4882a593Smuzhiyun 			"failed get motor start up speed,use dafult value\n");
480*4882a593Smuzhiyun 	}
481*4882a593Smuzhiyun 	ret = of_property_read_u32(node,
482*4882a593Smuzhiyun 				   "iris-max-speed",
483*4882a593Smuzhiyun 				   &motor->iris.max_speed);
484*4882a593Smuzhiyun 	if (ret != 0) {
485*4882a593Smuzhiyun 		motor->iris.max_speed = MOTOR_MAX_HZ;
486*4882a593Smuzhiyun 		dev_err(motor->dev,
487*4882a593Smuzhiyun 			"failed get motor max speed,use dafult value\n");
488*4882a593Smuzhiyun 	}
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	ret = of_property_read_u32(node,
491*4882a593Smuzhiyun 				   "focus-start-up-speed",
492*4882a593Smuzhiyun 				   &motor->focus.start_up_speed);
493*4882a593Smuzhiyun 	if (ret != 0) {
494*4882a593Smuzhiyun 		motor->focus.start_up_speed = MAX_START_UP_HZ;
495*4882a593Smuzhiyun 		dev_err(motor->dev,
496*4882a593Smuzhiyun 			"failed get motor start up speed,use dafult value\n");
497*4882a593Smuzhiyun 	}
498*4882a593Smuzhiyun 	ret = of_property_read_u32(node,
499*4882a593Smuzhiyun 				   "focus-max-speed",
500*4882a593Smuzhiyun 				   &motor->focus.max_speed);
501*4882a593Smuzhiyun 	if (ret != 0) {
502*4882a593Smuzhiyun 		motor->focus.max_speed = MOTOR_MAX_HZ;
503*4882a593Smuzhiyun 		dev_err(motor->dev,
504*4882a593Smuzhiyun 			"failed get motor max speed,use dafult value\n");
505*4882a593Smuzhiyun 	}
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	ret = of_property_read_u32(node,
508*4882a593Smuzhiyun 				   "zoom-start-up-speed",
509*4882a593Smuzhiyun 				   &motor->zoom.start_up_speed);
510*4882a593Smuzhiyun 	if (ret != 0) {
511*4882a593Smuzhiyun 		motor->zoom.start_up_speed = MAX_START_UP_HZ;
512*4882a593Smuzhiyun 		dev_err(motor->dev,
513*4882a593Smuzhiyun 			"failed get motor start up speed,use dafult value\n");
514*4882a593Smuzhiyun 	}
515*4882a593Smuzhiyun 	ret = of_property_read_u32(node,
516*4882a593Smuzhiyun 				   "zoom-max-speed",
517*4882a593Smuzhiyun 				   &motor->zoom.max_speed);
518*4882a593Smuzhiyun 	if (ret != 0) {
519*4882a593Smuzhiyun 		motor->zoom.max_speed = MOTOR_MAX_HZ;
520*4882a593Smuzhiyun 		dev_err(motor->dev,
521*4882a593Smuzhiyun 			"failed get motor max speed,use dafult value\n");
522*4882a593Smuzhiyun 	}
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	ret = of_property_read_u32(node,
525*4882a593Smuzhiyun 				   "focus-first-speed-step",
526*4882a593Smuzhiyun 				   &motor->focus.first_speed_step);
527*4882a593Smuzhiyun 	if (ret != 0) {
528*4882a593Smuzhiyun 		motor->focus.first_speed_step = STEP_PER_SPEED_DEF;
529*4882a593Smuzhiyun 		dev_err(motor->dev,
530*4882a593Smuzhiyun 			"failed get motor step of first speed,use dafult value\n");
531*4882a593Smuzhiyun 	}
532*4882a593Smuzhiyun 	ret = of_property_read_u32(node,
533*4882a593Smuzhiyun 				   "zoom-first-speed-step",
534*4882a593Smuzhiyun 				   &motor->zoom.first_speed_step);
535*4882a593Smuzhiyun 	if (ret != 0) {
536*4882a593Smuzhiyun 		motor->zoom.first_speed_step = STEP_PER_SPEED_DEF;
537*4882a593Smuzhiyun 		dev_err(motor->dev,
538*4882a593Smuzhiyun 			"failed get motor step of first speed,use dafult value\n");
539*4882a593Smuzhiyun 	}
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	motor->iris.type = TYPE_IRIS;
542*4882a593Smuzhiyun 	ret = fill_speed_squeue(motor->dev, &motor->iris);
543*4882a593Smuzhiyun 	motor->focus.type = TYPE_FOCUS;
544*4882a593Smuzhiyun 	ret |= fill_speed_squeue(motor->dev, &motor->focus);
545*4882a593Smuzhiyun 	motor->zoom.type = TYPE_ZOOM;
546*4882a593Smuzhiyun 	ret |= fill_speed_squeue(motor->dev, &motor->zoom);
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
549*4882a593Smuzhiyun 				   &motor->module_index);
550*4882a593Smuzhiyun 	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
551*4882a593Smuzhiyun 				       &motor->module_facing);
552*4882a593Smuzhiyun 	if (ret) {
553*4882a593Smuzhiyun 		dev_err(motor->dev,
554*4882a593Smuzhiyun 			"could not get module information!\n");
555*4882a593Smuzhiyun 		return -EINVAL;
556*4882a593Smuzhiyun 	}
557*4882a593Smuzhiyun 	return 0;
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun 
motor_timer_func(struct hrtimer * timer)560*4882a593Smuzhiyun static enum hrtimer_restart motor_timer_func(struct hrtimer *timer)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun 	struct motor_dev *motor;
563*4882a593Smuzhiyun 	struct pwm_state *pwm_state;
564*4882a593Smuzhiyun 	int idx = 0;
565*4882a593Smuzhiyun 	u64 time_cnt = 0;
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	motor = container_of(timer, struct motor_dev, timer);
568*4882a593Smuzhiyun 	pwm_state = &motor->pwm_state;
569*4882a593Smuzhiyun 	if (motor->move_cnt < 1 || motor->move_status == MOTOR_STATUS_STOPPED) {
570*4882a593Smuzhiyun 		pwm_state->enabled = false;
571*4882a593Smuzhiyun 		pwm_apply_state(motor->pwm_b1, pwm_state);
572*4882a593Smuzhiyun 		pwm_apply_state(motor->pwm_b2, pwm_state);
573*4882a593Smuzhiyun 		pwm_apply_state(motor->pwm_a1, pwm_state);
574*4882a593Smuzhiyun 		pwm_apply_state(motor->pwm_a2, pwm_state);
575*4882a593Smuzhiyun 		if (!IS_ERR(motor->cur_ext_dev->en_gpio))
576*4882a593Smuzhiyun 			gpiod_set_value(motor->cur_ext_dev->en_gpio, 0);
577*4882a593Smuzhiyun 		motor->move_status = MOTOR_STATUS_STOPPED;
578*4882a593Smuzhiyun 		motor->resched = false;
579*4882a593Smuzhiyun 		complete(&motor->complete);
580*4882a593Smuzhiyun 		dev_dbg(motor->dev, "motor stop\n");
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	} else {
583*4882a593Smuzhiyun 		/* do phase change */
584*4882a593Smuzhiyun 		switch (motor->move_status) {
585*4882a593Smuzhiyun 		case MOTOR_STATUS_CW:
586*4882a593Smuzhiyun 			if (motor->resched == true) {
587*4882a593Smuzhiyun 				pwm_state->enabled = false;
588*4882a593Smuzhiyun 				pwm_apply_state(motor->pwm_b1, pwm_state);
589*4882a593Smuzhiyun 				pwm_apply_state(motor->pwm_b2, pwm_state);
590*4882a593Smuzhiyun 				pwm_apply_state(motor->pwm_a1, pwm_state);
591*4882a593Smuzhiyun 				pwm_apply_state(motor->pwm_a2, pwm_state);
592*4882a593Smuzhiyun 			}
593*4882a593Smuzhiyun 			idx = motor->run_queue->count - motor->move_cnt;
594*4882a593Smuzhiyun 			pwm_state->polarity = PWM_POLARITY_INVERSED;
595*4882a593Smuzhiyun 			pwm_state->enabled = true;
596*4882a593Smuzhiyun 			pwm_state->period =
597*4882a593Smuzhiyun 				motor->run_queue->speed_p[idx].phase_interval_ns * 4;
598*4882a593Smuzhiyun 			pwm_state->duty_cycle =
599*4882a593Smuzhiyun 				motor->run_queue->speed_p[idx].phase_interval_ns * 2;
600*4882a593Smuzhiyun 			pwm_apply_state(motor->pwm_b1, pwm_state);
601*4882a593Smuzhiyun 			pwm_state->enabled = true;
602*4882a593Smuzhiyun 			pwm_state->polarity = PWM_POLARITY_NORMAL;
603*4882a593Smuzhiyun 			pwm_apply_state(motor->pwm_b2, pwm_state);
604*4882a593Smuzhiyun 			pwm_state->polarity = PWM_POLARITY_NORMAL;
605*4882a593Smuzhiyun 			pwm_state->enabled = true;
606*4882a593Smuzhiyun 			pwm_apply_state(motor->pwm_a1, pwm_state);
607*4882a593Smuzhiyun 			pwm_state->polarity = PWM_POLARITY_INVERSED;
608*4882a593Smuzhiyun 			pwm_state->enabled = true;
609*4882a593Smuzhiyun 			pwm_apply_state(motor->pwm_a2, pwm_state);
610*4882a593Smuzhiyun 			break;
611*4882a593Smuzhiyun 		case MOTOR_STATUS_CCW:
612*4882a593Smuzhiyun 			if (motor->resched == true) {
613*4882a593Smuzhiyun 				pwm_state->enabled = false;
614*4882a593Smuzhiyun 				pwm_apply_state(motor->pwm_b1, pwm_state);
615*4882a593Smuzhiyun 				pwm_apply_state(motor->pwm_b2, pwm_state);
616*4882a593Smuzhiyun 				pwm_apply_state(motor->pwm_a1, pwm_state);
617*4882a593Smuzhiyun 				pwm_apply_state(motor->pwm_a2, pwm_state);
618*4882a593Smuzhiyun 			}
619*4882a593Smuzhiyun 			idx = motor->run_queue->count - motor->move_cnt;
620*4882a593Smuzhiyun 			pwm_state->polarity = PWM_POLARITY_INVERSED;
621*4882a593Smuzhiyun 			pwm_state->enabled = true;
622*4882a593Smuzhiyun 			pwm_state->period =
623*4882a593Smuzhiyun 				motor->run_queue->speed_p[idx].phase_interval_ns * 4;
624*4882a593Smuzhiyun 			pwm_state->duty_cycle =
625*4882a593Smuzhiyun 				motor->run_queue->speed_p[idx].phase_interval_ns * 2;
626*4882a593Smuzhiyun 			pwm_apply_state(motor->pwm_b1, pwm_state);
627*4882a593Smuzhiyun 			pwm_state->polarity = PWM_POLARITY_NORMAL;
628*4882a593Smuzhiyun 			pwm_state->enabled = true;
629*4882a593Smuzhiyun 			pwm_apply_state(motor->pwm_b2, pwm_state);
630*4882a593Smuzhiyun 			pwm_state->polarity = PWM_POLARITY_INVERSED;
631*4882a593Smuzhiyun 			pwm_state->enabled = true;
632*4882a593Smuzhiyun 			pwm_apply_state(motor->pwm_a1, pwm_state);
633*4882a593Smuzhiyun 			pwm_state->polarity = PWM_POLARITY_NORMAL;
634*4882a593Smuzhiyun 			pwm_state->enabled = true;
635*4882a593Smuzhiyun 			pwm_apply_state(motor->pwm_a2, pwm_state);
636*4882a593Smuzhiyun 			break;
637*4882a593Smuzhiyun 		default:
638*4882a593Smuzhiyun 			break;
639*4882a593Smuzhiyun 		}
640*4882a593Smuzhiyun 		if (motor->resched == false)
641*4882a593Smuzhiyun 			motor->resched = true;
642*4882a593Smuzhiyun 		motor->move_cnt--;
643*4882a593Smuzhiyun 	}
644*4882a593Smuzhiyun 	if (motor->resched) {
645*4882a593Smuzhiyun 		time_cnt = ((u64)motor->run_queue->speed_p[idx].phase_interval_ns *
646*4882a593Smuzhiyun 			motor->run_queue->speed_p[idx].count);
647*4882a593Smuzhiyun 		hrtimer_forward_now(timer,
648*4882a593Smuzhiyun 			ns_to_ktime(time_cnt - 80000));
649*4882a593Smuzhiyun 		return HRTIMER_RESTART;
650*4882a593Smuzhiyun 	}
651*4882a593Smuzhiyun 	return HRTIMER_NORESTART;
652*4882a593Smuzhiyun }
653*4882a593Smuzhiyun 
motor_s_ctrl(struct v4l2_ctrl * ctrl)654*4882a593Smuzhiyun static int motor_s_ctrl(struct v4l2_ctrl *ctrl)
655*4882a593Smuzhiyun {
656*4882a593Smuzhiyun 	int ret = 0;
657*4882a593Smuzhiyun 	struct motor_dev *motor = container_of(ctrl->handler,
658*4882a593Smuzhiyun 					     struct motor_dev, ctrl_handler);
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 	switch (ctrl->id) {
661*4882a593Smuzhiyun 	case V4L2_CID_IRIS_ABSOLUTE:
662*4882a593Smuzhiyun 		if (ctrl->val > motor->iris.cur_pos)
663*4882a593Smuzhiyun 			ret = set_motor_running_status(motor,
664*4882a593Smuzhiyun 				&motor->iris,
665*4882a593Smuzhiyun 				MOTOR_STATUS_CCW,
666*4882a593Smuzhiyun 				abs(ctrl->val - motor->iris.cur_pos));
667*4882a593Smuzhiyun 		else
668*4882a593Smuzhiyun 			ret = set_motor_running_status(motor,
669*4882a593Smuzhiyun 				&motor->iris,
670*4882a593Smuzhiyun 				MOTOR_STATUS_CW,
671*4882a593Smuzhiyun 				abs(ctrl->val - motor->iris.cur_pos));
672*4882a593Smuzhiyun 		motor->iris.cur_pos = ctrl->val;
673*4882a593Smuzhiyun 		dev_dbg(motor->dev, "set iris pos %d\n", ctrl->val);
674*4882a593Smuzhiyun 		break;
675*4882a593Smuzhiyun 	case V4L2_CID_FOCUS_ABSOLUTE:
676*4882a593Smuzhiyun 		if (ctrl->val > motor->focus.cur_pos)
677*4882a593Smuzhiyun 			ret = set_motor_running_status(motor,
678*4882a593Smuzhiyun 				&motor->focus,
679*4882a593Smuzhiyun 				MOTOR_STATUS_CCW,
680*4882a593Smuzhiyun 				abs(ctrl->val - motor->focus.cur_pos));
681*4882a593Smuzhiyun 		else
682*4882a593Smuzhiyun 			ret = set_motor_running_status(motor,
683*4882a593Smuzhiyun 				&motor->focus,
684*4882a593Smuzhiyun 				MOTOR_STATUS_CW,
685*4882a593Smuzhiyun 				abs(ctrl->val - motor->focus.cur_pos));
686*4882a593Smuzhiyun 		motor->focus.cur_pos = ctrl->val;
687*4882a593Smuzhiyun 		dev_dbg(motor->dev, "set focus pos %d\n", ctrl->val);
688*4882a593Smuzhiyun 		break;
689*4882a593Smuzhiyun 	case V4L2_CID_ZOOM_ABSOLUTE:
690*4882a593Smuzhiyun 		if (ctrl->val > motor->zoom.cur_pos)
691*4882a593Smuzhiyun 			ret = set_motor_running_status(motor,
692*4882a593Smuzhiyun 				&motor->zoom,
693*4882a593Smuzhiyun 				MOTOR_STATUS_CCW,
694*4882a593Smuzhiyun 				abs(ctrl->val - motor->zoom.cur_pos));
695*4882a593Smuzhiyun 		else
696*4882a593Smuzhiyun 			ret = set_motor_running_status(motor,
697*4882a593Smuzhiyun 				&motor->zoom,
698*4882a593Smuzhiyun 				MOTOR_STATUS_CW,
699*4882a593Smuzhiyun 				abs(ctrl->val - motor->zoom.cur_pos));
700*4882a593Smuzhiyun 		motor->zoom.cur_pos = ctrl->val;
701*4882a593Smuzhiyun 		dev_dbg(motor->dev, "set zoom pos %d\n", ctrl->val);
702*4882a593Smuzhiyun 		break;
703*4882a593Smuzhiyun 	default:
704*4882a593Smuzhiyun 		dev_err(motor->dev, "not support cmd %d\n", ctrl->id);
705*4882a593Smuzhiyun 		break;
706*4882a593Smuzhiyun 	}
707*4882a593Smuzhiyun 	return ret;
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun 
motor_init_iris_status(struct motor_dev * motor)710*4882a593Smuzhiyun static int motor_init_iris_status(struct motor_dev *motor)
711*4882a593Smuzhiyun {
712*4882a593Smuzhiyun 	int ret = 0;
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	ret = set_motor_running_status(motor, &motor->iris,
715*4882a593Smuzhiyun 				       MOTOR_STATUS_CCW, IRIS_MAX_LOG);
716*4882a593Smuzhiyun 	motor->iris.cur_pos = IRIS_MAX_LOG;
717*4882a593Smuzhiyun 	__v4l2_ctrl_modify_range(motor->iris_ctrl,
718*4882a593Smuzhiyun 				 0,
719*4882a593Smuzhiyun 				 IRIS_MAX_LOG,
720*4882a593Smuzhiyun 				 IRIS_LOG_STEP,
721*4882a593Smuzhiyun 				 motor->iris.cur_pos);
722*4882a593Smuzhiyun 	return ret;
723*4882a593Smuzhiyun }
724*4882a593Smuzhiyun 
motor_init_focus_status(struct motor_dev * motor)725*4882a593Smuzhiyun static int motor_init_focus_status(struct motor_dev *motor)
726*4882a593Smuzhiyun {
727*4882a593Smuzhiyun 	int ret = 0;
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 	ret = set_motor_running_status(motor, &motor->focus,
730*4882a593Smuzhiyun 				       MOTOR_STATUS_CW, FOCUS_MAX_LOG);
731*4882a593Smuzhiyun 	motor->focus.cur_pos = 0;
732*4882a593Smuzhiyun 	__v4l2_ctrl_modify_range(motor->focus_ctrl,
733*4882a593Smuzhiyun 				 0,
734*4882a593Smuzhiyun 				 FOCUS_MAX_LOG,
735*4882a593Smuzhiyun 				 FOCUS_LOG_STEP,
736*4882a593Smuzhiyun 				 motor->focus.cur_pos);
737*4882a593Smuzhiyun 	return ret;
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun 
motor_init_zoom_status(struct motor_dev * motor)740*4882a593Smuzhiyun static int motor_init_zoom_status(struct motor_dev *motor)
741*4882a593Smuzhiyun {
742*4882a593Smuzhiyun 	int ret = 0;
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	ret = set_motor_running_status(motor, &motor->zoom,
745*4882a593Smuzhiyun 				       MOTOR_STATUS_CW, ZOOM_MAX_LOG);
746*4882a593Smuzhiyun 	motor->zoom.cur_pos = 0;
747*4882a593Smuzhiyun 	__v4l2_ctrl_modify_range(motor->zoom_ctrl,
748*4882a593Smuzhiyun 				 0,
749*4882a593Smuzhiyun 				 ZOOM_MAX_LOG,
750*4882a593Smuzhiyun 				 ZOOM_LOG_STEP,
751*4882a593Smuzhiyun 				 motor->zoom.cur_pos);
752*4882a593Smuzhiyun 	return ret;
753*4882a593Smuzhiyun }
754*4882a593Smuzhiyun 
motor_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)755*4882a593Smuzhiyun static long motor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
756*4882a593Smuzhiyun {
757*4882a593Smuzhiyun 	struct rk_cam_vcm_tim *mv_tim;
758*4882a593Smuzhiyun 	struct motor_dev *motor = container_of(sd, struct motor_dev, sd);
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	switch (cmd) {
761*4882a593Smuzhiyun 	case RK_VIDIOC_VCM_TIMEINFO:
762*4882a593Smuzhiyun 		mv_tim = (struct rk_cam_vcm_tim *)arg;
763*4882a593Smuzhiyun 		memcpy(mv_tim, &motor->focus.mv_tim, sizeof(*mv_tim));
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 		dev_dbg(motor->dev, "get_focus_move_tim 0x%lx, 0x%lx, 0x%lx, 0x%lx\n",
766*4882a593Smuzhiyun 			mv_tim->vcm_start_t.tv_sec,
767*4882a593Smuzhiyun 			mv_tim->vcm_start_t.tv_usec,
768*4882a593Smuzhiyun 			mv_tim->vcm_end_t.tv_sec,
769*4882a593Smuzhiyun 			mv_tim->vcm_end_t.tv_usec);
770*4882a593Smuzhiyun 		break;
771*4882a593Smuzhiyun 	case RK_VIDIOC_IRIS_TIMEINFO:
772*4882a593Smuzhiyun 		mv_tim = (struct rk_cam_vcm_tim *)arg;
773*4882a593Smuzhiyun 		memcpy(mv_tim, &motor->iris.mv_tim, sizeof(*mv_tim));
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun 		dev_dbg(motor->dev, "get_iris_move_tim 0x%lx, 0x%lx, 0x%lx, 0x%lx\n",
776*4882a593Smuzhiyun 			mv_tim->vcm_start_t.tv_sec,
777*4882a593Smuzhiyun 			mv_tim->vcm_start_t.tv_usec,
778*4882a593Smuzhiyun 			mv_tim->vcm_end_t.tv_sec,
779*4882a593Smuzhiyun 			mv_tim->vcm_end_t.tv_usec);
780*4882a593Smuzhiyun 		break;
781*4882a593Smuzhiyun 	case RK_VIDIOC_ZOOM_TIMEINFO:
782*4882a593Smuzhiyun 		mv_tim = (struct rk_cam_vcm_tim *)arg;
783*4882a593Smuzhiyun 		memcpy(mv_tim, &motor->zoom.mv_tim, sizeof(*mv_tim));
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 		dev_dbg(motor->dev, "get_zoom_move_tim 0x%lx, 0x%lx, 0x%lx, 0x%lx\n",
786*4882a593Smuzhiyun 			mv_tim->vcm_start_t.tv_sec,
787*4882a593Smuzhiyun 			mv_tim->vcm_start_t.tv_usec,
788*4882a593Smuzhiyun 			mv_tim->vcm_end_t.tv_sec,
789*4882a593Smuzhiyun 			mv_tim->vcm_end_t.tv_usec);
790*4882a593Smuzhiyun 		break;
791*4882a593Smuzhiyun 	case RK_VIDIOC_IRIS_CORRECTION:
792*4882a593Smuzhiyun 		motor_init_iris_status(motor);
793*4882a593Smuzhiyun 		break;
794*4882a593Smuzhiyun 	case RK_VIDIOC_FOCUS_CORRECTION:
795*4882a593Smuzhiyun 		motor_init_focus_status(motor);
796*4882a593Smuzhiyun 		break;
797*4882a593Smuzhiyun 	case RK_VIDIOC_ZOOM_CORRECTION:
798*4882a593Smuzhiyun 		motor_init_zoom_status(motor);
799*4882a593Smuzhiyun 		break;
800*4882a593Smuzhiyun 	default:
801*4882a593Smuzhiyun 		break;
802*4882a593Smuzhiyun 	}
803*4882a593Smuzhiyun 	return 0;
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun static const struct v4l2_subdev_core_ops motor_core_ops = {
807*4882a593Smuzhiyun 	.ioctl = motor_ioctl,
808*4882a593Smuzhiyun };
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun static const struct v4l2_subdev_ops motor_subdev_ops = {
811*4882a593Smuzhiyun 	.core	= &motor_core_ops,
812*4882a593Smuzhiyun };
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun static const struct v4l2_ctrl_ops motor_ctrl_ops = {
815*4882a593Smuzhiyun 	.s_ctrl = motor_s_ctrl,
816*4882a593Smuzhiyun };
817*4882a593Smuzhiyun 
motor_initialize_controls(struct motor_dev * motor)818*4882a593Smuzhiyun static int motor_initialize_controls(struct motor_dev *motor)
819*4882a593Smuzhiyun {
820*4882a593Smuzhiyun 	struct v4l2_ctrl_handler *handler;
821*4882a593Smuzhiyun 	int ret = 0;
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	handler = &motor->ctrl_handler;
824*4882a593Smuzhiyun 	ret = v4l2_ctrl_handler_init(handler, 3);
825*4882a593Smuzhiyun 	if (ret)
826*4882a593Smuzhiyun 		return ret;
827*4882a593Smuzhiyun 	handler->lock = &motor->mutex;
828*4882a593Smuzhiyun 	if (!IS_ERR(motor->iris.en_gpio)) {
829*4882a593Smuzhiyun 		motor->iris_ctrl = v4l2_ctrl_new_std(handler, &motor_ctrl_ops,
830*4882a593Smuzhiyun 			V4L2_CID_IRIS_ABSOLUTE, 0, IRIS_MAX_LOG, IRIS_LOG_STEP, 0);
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 		ret = motor_init_iris_status(motor);
833*4882a593Smuzhiyun 	}
834*4882a593Smuzhiyun 	if (!IS_ERR(motor->focus.en_gpio)) {
835*4882a593Smuzhiyun 		motor->focus_ctrl = v4l2_ctrl_new_std(handler, &motor_ctrl_ops,
836*4882a593Smuzhiyun 			V4L2_CID_FOCUS_ABSOLUTE, 0, FOCUS_MAX_LOG,
837*4882a593Smuzhiyun 			FOCUS_LOG_STEP, 0);
838*4882a593Smuzhiyun 	}
839*4882a593Smuzhiyun 	if (!IS_ERR(motor->zoom.en_gpio)) {
840*4882a593Smuzhiyun 		motor->zoom_ctrl = v4l2_ctrl_new_std(handler, &motor_ctrl_ops,
841*4882a593Smuzhiyun 			V4L2_CID_ZOOM_ABSOLUTE, 0, ZOOM_MAX_LOG,
842*4882a593Smuzhiyun 			ZOOM_LOG_STEP, 0);
843*4882a593Smuzhiyun 	}
844*4882a593Smuzhiyun 	if (handler->error) {
845*4882a593Smuzhiyun 		ret = handler->error;
846*4882a593Smuzhiyun 		dev_err(motor->dev,
847*4882a593Smuzhiyun 			"Failed to init controls(%d)\n", ret);
848*4882a593Smuzhiyun 		goto err_free_handler;
849*4882a593Smuzhiyun 	}
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 	motor->sd.ctrl_handler = handler;
852*4882a593Smuzhiyun 	return ret;
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun err_free_handler:
855*4882a593Smuzhiyun 	v4l2_ctrl_handler_free(handler);
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	return ret;
858*4882a593Smuzhiyun }
859*4882a593Smuzhiyun #define USED_SYS_DEBUG
860*4882a593Smuzhiyun #ifdef USED_SYS_DEBUG
861*4882a593Smuzhiyun 
set_iris_correction(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)862*4882a593Smuzhiyun static ssize_t set_iris_correction(struct device *dev,
863*4882a593Smuzhiyun 	struct device_attribute *attr,
864*4882a593Smuzhiyun 	const char *buf,
865*4882a593Smuzhiyun 	size_t count)
866*4882a593Smuzhiyun {
867*4882a593Smuzhiyun 	struct motor_dev *motor = dev_get_drvdata(dev);
868*4882a593Smuzhiyun 	int status = 0;
869*4882a593Smuzhiyun 	int ret = 0;
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	ret = kstrtoint(buf, 0, &status);
872*4882a593Smuzhiyun 	if (!ret && status >= 0 && status < 2) {
873*4882a593Smuzhiyun 		if (status)
874*4882a593Smuzhiyun 			motor_init_iris_status(motor);
875*4882a593Smuzhiyun 		dev_info(dev, "camera iris position correction\n");
876*4882a593Smuzhiyun 	}
877*4882a593Smuzhiyun 	return count;
878*4882a593Smuzhiyun }
879*4882a593Smuzhiyun 
set_focus_correction(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)880*4882a593Smuzhiyun static ssize_t set_focus_correction(struct device *dev,
881*4882a593Smuzhiyun 	struct device_attribute *attr,
882*4882a593Smuzhiyun 	const char *buf,
883*4882a593Smuzhiyun 	size_t count)
884*4882a593Smuzhiyun {
885*4882a593Smuzhiyun 	struct motor_dev *motor = dev_get_drvdata(dev);
886*4882a593Smuzhiyun 	int status = 0;
887*4882a593Smuzhiyun 	int ret = 0;
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	ret = kstrtoint(buf, 0, &status);
890*4882a593Smuzhiyun 	if (!ret && status >= 0 && status < 2) {
891*4882a593Smuzhiyun 		if (status)
892*4882a593Smuzhiyun 			motor_init_focus_status(motor);
893*4882a593Smuzhiyun 		dev_info(dev, "camera focus position correction\n");
894*4882a593Smuzhiyun 	}
895*4882a593Smuzhiyun 	return count;
896*4882a593Smuzhiyun }
897*4882a593Smuzhiyun 
set_zoom_correction(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)898*4882a593Smuzhiyun static ssize_t set_zoom_correction(struct device *dev,
899*4882a593Smuzhiyun 	struct device_attribute *attr,
900*4882a593Smuzhiyun 	const char *buf,
901*4882a593Smuzhiyun 	size_t count)
902*4882a593Smuzhiyun {
903*4882a593Smuzhiyun 	struct motor_dev *motor = dev_get_drvdata(dev);
904*4882a593Smuzhiyun 	int status = 0;
905*4882a593Smuzhiyun 	int ret = 0;
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	ret = kstrtoint(buf, 0, &status);
908*4882a593Smuzhiyun 	if (!ret && status >= 0 && status < 2) {
909*4882a593Smuzhiyun 		if (status)
910*4882a593Smuzhiyun 			motor_init_zoom_status(motor);
911*4882a593Smuzhiyun 		dev_info(dev, "camera zoom position correction\n");
912*4882a593Smuzhiyun 	}
913*4882a593Smuzhiyun 	return count;
914*4882a593Smuzhiyun }
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun static struct device_attribute attributes[] = {
917*4882a593Smuzhiyun 	__ATTR(is_iris_correction, S_IWUSR, NULL, set_iris_correction),
918*4882a593Smuzhiyun 	__ATTR(is_focus_correction, S_IWUSR, NULL, set_focus_correction),
919*4882a593Smuzhiyun 	__ATTR(is_zoom_correction, S_IWUSR, NULL, set_zoom_correction),
920*4882a593Smuzhiyun };
921*4882a593Smuzhiyun 
add_sysfs_interfaces(struct device * dev)922*4882a593Smuzhiyun static int add_sysfs_interfaces(struct device *dev)
923*4882a593Smuzhiyun {
924*4882a593Smuzhiyun 	int i;
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(attributes); i++)
927*4882a593Smuzhiyun 		if (device_create_file(dev, attributes + i))
928*4882a593Smuzhiyun 			goto undo;
929*4882a593Smuzhiyun 	return 0;
930*4882a593Smuzhiyun undo:
931*4882a593Smuzhiyun 	for (i--; i >= 0 ; i--)
932*4882a593Smuzhiyun 		device_remove_file(dev, attributes + i);
933*4882a593Smuzhiyun 	dev_err(dev, "%s: failed to create sysfs interface\n", __func__);
934*4882a593Smuzhiyun 	return -ENODEV;
935*4882a593Smuzhiyun }
936*4882a593Smuzhiyun #endif
937*4882a593Smuzhiyun 
dev_param_init(struct motor_dev * motor)938*4882a593Smuzhiyun static void dev_param_init(struct motor_dev *motor)
939*4882a593Smuzhiyun {
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 	motor->iris.step_per_pos = motor->iris.step_max / IRIS_MAX_LOG;
942*4882a593Smuzhiyun 	motor->iris.mv_tim.vcm_start_t = ns_to_timeval(ktime_get_ns());
943*4882a593Smuzhiyun 	motor->iris.mv_tim.vcm_end_t = ns_to_timeval(ktime_get_ns());
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	motor->focus.step_per_pos = motor->focus.step_max / FOCUS_MAX_LOG;
946*4882a593Smuzhiyun 	motor->focus.mv_tim.vcm_start_t = ns_to_timeval(ktime_get_ns());
947*4882a593Smuzhiyun 	motor->focus.mv_tim.vcm_end_t = ns_to_timeval(ktime_get_ns());
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 	motor->zoom.step_per_pos = motor->zoom.step_max / ZOOM_MAX_LOG;
950*4882a593Smuzhiyun 	motor->zoom.mv_tim.vcm_start_t = ns_to_timeval(ktime_get_ns());
951*4882a593Smuzhiyun 	motor->zoom.mv_tim.vcm_end_t = ns_to_timeval(ktime_get_ns());
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 	motor->move_status = MOTOR_STATUS_STOPPED;
954*4882a593Smuzhiyun 	motor->resched = false;
955*4882a593Smuzhiyun }
956*4882a593Smuzhiyun 
motor_dev_probe(struct platform_device * pdev)957*4882a593Smuzhiyun static int motor_dev_probe(struct platform_device *pdev)
958*4882a593Smuzhiyun {
959*4882a593Smuzhiyun 	int ret = 0;
960*4882a593Smuzhiyun 	struct motor_dev *motor;
961*4882a593Smuzhiyun 	struct v4l2_subdev *sd;
962*4882a593Smuzhiyun 	char facing[2];
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 	dev_info(&pdev->dev, "driver version: %02x.%02x.%02x",
965*4882a593Smuzhiyun 		DRIVER_VERSION >> 16,
966*4882a593Smuzhiyun 		(DRIVER_VERSION & 0xff00) >> 8,
967*4882a593Smuzhiyun 		DRIVER_VERSION & 0x00ff);
968*4882a593Smuzhiyun 	motor = devm_kzalloc(&pdev->dev, sizeof(*motor), GFP_KERNEL);
969*4882a593Smuzhiyun 	if (!motor)
970*4882a593Smuzhiyun 		return -ENOMEM;
971*4882a593Smuzhiyun 	motor->dev = &pdev->dev;
972*4882a593Smuzhiyun 	dev_set_name(motor->dev, "motor");
973*4882a593Smuzhiyun 	dev_set_drvdata(motor->dev, motor);
974*4882a593Smuzhiyun 	if (motor_dev_parse_dt(motor)) {
975*4882a593Smuzhiyun 		dev_err(motor->dev, "parse dt error\n");
976*4882a593Smuzhiyun 		return -EINVAL;
977*4882a593Smuzhiyun 	}
978*4882a593Smuzhiyun 	dev_param_init(motor);
979*4882a593Smuzhiyun 	mutex_init(&motor->mutex);
980*4882a593Smuzhiyun 	hrtimer_init(&motor->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
981*4882a593Smuzhiyun 	motor->timer.function = motor_timer_func;
982*4882a593Smuzhiyun 	init_completion(&motor->complete);
983*4882a593Smuzhiyun 	v4l2_subdev_init(&motor->sd, &motor_subdev_ops);
984*4882a593Smuzhiyun 	motor->sd.owner = pdev->dev.driver->owner;
985*4882a593Smuzhiyun 	motor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
986*4882a593Smuzhiyun 	motor->sd.dev = &pdev->dev;
987*4882a593Smuzhiyun 	v4l2_set_subdevdata(&motor->sd, pdev);
988*4882a593Smuzhiyun 	platform_set_drvdata(pdev, &motor->sd);
989*4882a593Smuzhiyun 	motor_initialize_controls(motor);
990*4882a593Smuzhiyun 	if (ret)
991*4882a593Smuzhiyun 		goto err_free;
992*4882a593Smuzhiyun 	ret = media_entity_pads_init(&motor->sd.entity, 0, NULL);
993*4882a593Smuzhiyun 	if (ret < 0)
994*4882a593Smuzhiyun 		goto err_free;
995*4882a593Smuzhiyun 	sd = &motor->sd;
996*4882a593Smuzhiyun 	sd->entity.function = MEDIA_ENT_F_LENS;
997*4882a593Smuzhiyun 	sd->entity.flags = 0;
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun 	memset(facing, 0, sizeof(facing));
1000*4882a593Smuzhiyun 	if (strcmp(motor->module_facing, "back") == 0)
1001*4882a593Smuzhiyun 		facing[0] = 'b';
1002*4882a593Smuzhiyun 	else
1003*4882a593Smuzhiyun 		facing[0] = 'f';
1004*4882a593Smuzhiyun 	snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s",
1005*4882a593Smuzhiyun 		 motor->module_index, facing,
1006*4882a593Smuzhiyun 		 DRIVER_NAME);
1007*4882a593Smuzhiyun 	ret = v4l2_async_register_subdev(sd);
1008*4882a593Smuzhiyun 	if (ret)
1009*4882a593Smuzhiyun 		dev_err(&pdev->dev, "v4l2 async register subdev failed\n");
1010*4882a593Smuzhiyun #ifdef USED_SYS_DEBUG
1011*4882a593Smuzhiyun 	add_sysfs_interfaces(&pdev->dev);
1012*4882a593Smuzhiyun #endif
1013*4882a593Smuzhiyun 	dev_info(motor->dev, "gpio motor driver probe success\n");
1014*4882a593Smuzhiyun 	return 0;
1015*4882a593Smuzhiyun err_free:
1016*4882a593Smuzhiyun 	v4l2_ctrl_handler_free(&motor->ctrl_handler);
1017*4882a593Smuzhiyun 	v4l2_device_unregister_subdev(&motor->sd);
1018*4882a593Smuzhiyun 	media_entity_cleanup(&motor->sd.entity);
1019*4882a593Smuzhiyun 	return ret;
1020*4882a593Smuzhiyun }
1021*4882a593Smuzhiyun 
motor_dev_remove(struct platform_device * pdev)1022*4882a593Smuzhiyun static int motor_dev_remove(struct platform_device *pdev)
1023*4882a593Smuzhiyun {
1024*4882a593Smuzhiyun 	struct v4l2_subdev *sd = platform_get_drvdata(pdev);
1025*4882a593Smuzhiyun 	struct motor_dev *motor;
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun 	if (sd)
1028*4882a593Smuzhiyun 		motor = v4l2_get_subdevdata(sd);
1029*4882a593Smuzhiyun 	else
1030*4882a593Smuzhiyun 		return -ENODEV;
1031*4882a593Smuzhiyun 	hrtimer_cancel(&motor->timer);
1032*4882a593Smuzhiyun 	if (sd)
1033*4882a593Smuzhiyun 		v4l2_device_unregister_subdev(sd);
1034*4882a593Smuzhiyun 	v4l2_ctrl_handler_free(&motor->ctrl_handler);
1035*4882a593Smuzhiyun 	media_entity_cleanup(&motor->sd.entity);
1036*4882a593Smuzhiyun 	return 0;
1037*4882a593Smuzhiyun }
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun #if defined(CONFIG_OF)
1040*4882a593Smuzhiyun static const struct of_device_id motor_dev_of_match[] = {
1041*4882a593Smuzhiyun 	{ .compatible = "monolithicpower,mp6507", },
1042*4882a593Smuzhiyun 	{},
1043*4882a593Smuzhiyun };
1044*4882a593Smuzhiyun #endif
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun static struct platform_driver motor_dev_driver = {
1047*4882a593Smuzhiyun 	.driver = {
1048*4882a593Smuzhiyun 		.name = DRIVER_NAME,
1049*4882a593Smuzhiyun 		.owner = THIS_MODULE,
1050*4882a593Smuzhiyun 		.of_match_table = of_match_ptr(motor_dev_of_match),
1051*4882a593Smuzhiyun 	},
1052*4882a593Smuzhiyun 	.probe = motor_dev_probe,
1053*4882a593Smuzhiyun 	.remove = motor_dev_remove,
1054*4882a593Smuzhiyun };
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun module_platform_driver(motor_dev_driver);
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1059*4882a593Smuzhiyun MODULE_ALIAS("platform:motor");
1060*4882a593Smuzhiyun MODULE_AUTHOR("ROCKCHIP");
1061