• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Rockchip driver for IR-Cuter
4  *
5  * Copyright (C) 2020 Fuzhou Rockchip Electronics Co., Ltd.
6  *
7  * V0.0X01.0X00 first version.
8  */
9 #include <linux/io.h>
10 #include <linux/of_gpio.h>
11 #include <linux/delay.h>
12 #include <linux/module.h>
13 #include <linux/completion.h>
14 #include <linux/mutex.h>
15 #include <linux/workqueue.h>
16 #include <linux/platform_device.h>
17 #include <media/v4l2-subdev.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-device.h>
20 #include <linux/fs.h>
21 #include <linux/version.h>
22 #include <linux/rk-camera-module.h>
23 
24 #define RK_IRCUT_NAME "ircut"
25 #define DRIVER_VERSION	KERNEL_VERSION(0, 0x01, 0x00)
26 
27 //#define USED_SYS_DEBUG
28 
29 enum IRCUT_STATE_e {
30 	IRCUT_STATE_CLOSED,
31 	IRCUT_STATE_CLOSING,
32 	IRCUT_STATE_OPENING,
33 	IRCUT_STATE_OPENED,
34 };
35 
36 struct ircut_op_work {
37 	struct work_struct work;
38 	int op_cmd;
39 	struct ircut_dev *dev;
40 };
41 
42 struct ircut_drv_data {
43 	int	(*parse_dt)(struct ircut_dev *ircut, struct device_node *node);
44 	void	(*ctrl)(struct ircut_dev *ircut, int cmd);
45 };
46 
47 struct ircut_dev {
48 	struct v4l2_subdev sd;
49 	struct v4l2_ctrl_handler ctrl_handler;
50 	struct device *dev;
51 	struct completion complete;
52 	/* state mutex */
53 	struct mutex mut_state;
54 	enum IRCUT_STATE_e state;
55 	struct workqueue_struct *wq;
56 	int pulse_width;
57 	int val;
58 	struct gpio_desc *open_gpio;
59 	struct gpio_desc *close_gpio;
60 	struct gpio_desc *led_gpio;
61 	u32 module_index;
62 	const char *module_facing;
63 	const struct ircut_drv_data *drv_data;
64 };
65 
66 #define IRCUT_STATE_EQ(expected) \
67 	((ircut->state == (expected)) ? true : false)
68 
ap1511a_parse_dt(struct ircut_dev * ircut,struct device_node * node)69 static int ap1511a_parse_dt(struct ircut_dev *ircut, struct device_node *node)
70 {
71 	ircut->open_gpio = devm_gpiod_get(ircut->dev, "ircut-open", GPIOD_OUT_HIGH);
72 	if (IS_ERR(ircut->open_gpio)) {
73 		dev_err(ircut->dev, "Failed to get ircut-open-gpios\n");
74 		return PTR_ERR(ircut->open_gpio);
75 	}
76 
77 	ircut->led_gpio = devm_gpiod_get_optional(ircut->dev, "led", GPIOD_OUT_LOW);
78 	if (IS_ERR(ircut->led_gpio))
79 		dev_err(ircut->dev, "Failed to get led-gpios\n");
80 
81 	return 0;
82 }
83 
ap1511a_ctrl(struct ircut_dev * ircut,int cmd)84 static void ap1511a_ctrl(struct ircut_dev *ircut, int cmd)
85 {
86 	if (cmd > 0) {
87 		gpiod_set_value_cansleep(ircut->open_gpio, 1);
88 		if (!IS_ERR(ircut->led_gpio))
89 			gpiod_set_value_cansleep(ircut->led_gpio, 0);
90 	} else {
91 		gpiod_set_value_cansleep(ircut->open_gpio, 0);
92 		if (!IS_ERR(ircut->led_gpio))
93 			gpiod_set_value_cansleep(ircut->led_gpio, 1);
94 	}
95 }
96 
ba6208_parse_dt(struct ircut_dev * ircut,struct device_node * node)97 static int ba6208_parse_dt(struct ircut_dev *ircut, struct device_node *node)
98 {
99 	int ret;
100 
101 	dev_dbg(ircut->dev, "ircut_gpio_parse_dt");
102 	ret = of_property_read_u32(node,
103 		"rockchip,pulse-width",
104 		&ircut->pulse_width);
105 	if (ret != 0) {
106 		ircut->pulse_width = 100;
107 		dev_err(ircut->dev,
108 			"failed get pulse-width,use dafult value 100\n");
109 	}
110 	if (ircut->pulse_width > 2000) {
111 		ircut->pulse_width = 300;
112 		dev_info(ircut->dev,
113 			"pulse width to long,use default dafult 300");
114 	}
115 	dev_dbg(ircut->dev, "pulse-width value from dts %d\n",
116 		ircut->pulse_width);
117 	/* get ircut open gpio */
118 	ircut->open_gpio = devm_gpiod_get(ircut->dev, "ircut-open", GPIOD_OUT_LOW);
119 	if (IS_ERR(ircut->open_gpio))
120 		dev_err(ircut->dev, "Failed to get ircut-open-gpios\n");
121 
122 	/* get ircut close gpio */
123 	ircut->close_gpio = devm_gpiod_get(ircut->dev,
124 				"ircut-close", GPIOD_OUT_LOW);
125 	if (IS_ERR(ircut->close_gpio))
126 		dev_err(ircut->dev, "Failed to get ircut-close-gpios\n");
127 
128 	/* get led gpio */
129 	ircut->led_gpio = devm_gpiod_get_optional(ircut->dev, "led", GPIOD_OUT_LOW);
130 	if (IS_ERR(ircut->led_gpio))
131 		dev_err(ircut->dev, "Failed to get led-gpios\n");
132 
133 	return 0;
134 }
135 
ba6208_ctrl(struct ircut_dev * ircut,int cmd)136 static void ba6208_ctrl(struct ircut_dev *ircut, int cmd)
137 {
138 	if (cmd > 0) {
139 		if (!IS_ERR(ircut->open_gpio))
140 			gpiod_set_value_cansleep(ircut->open_gpio, 1);
141 		msleep(ircut->pulse_width);
142 		if (!IS_ERR(ircut->open_gpio))
143 			gpiod_set_value_cansleep(ircut->open_gpio, 0);
144 		if (!IS_ERR(ircut->led_gpio))
145 			gpiod_set_value_cansleep(ircut->led_gpio, 0);
146 	} else {
147 		if (!IS_ERR(ircut->close_gpio))
148 			gpiod_set_value_cansleep(ircut->close_gpio, 1);
149 		msleep(ircut->pulse_width);
150 		if (!IS_ERR(ircut->close_gpio))
151 			gpiod_set_value_cansleep(ircut->close_gpio, 0);
152 		if (!IS_ERR(ircut->led_gpio))
153 			gpiod_set_value_cansleep(ircut->led_gpio, 1);
154 	}
155 }
156 
ircut_op_work(struct work_struct * work)157 static void ircut_op_work(struct work_struct *work)
158 {
159 	struct ircut_op_work *wk =
160 		container_of(work, struct ircut_op_work, work);
161 	struct ircut_dev *ircut = wk->dev;
162 	enum IRCUT_STATE_e state;
163 
164 	if (ircut->drv_data->ctrl)
165 		ircut->drv_data->ctrl(ircut, wk->op_cmd);
166 
167 	state = (wk->op_cmd > 0) ? IRCUT_STATE_OPENED : IRCUT_STATE_CLOSED;
168 	mutex_lock(&ircut->mut_state);
169 	complete(&ircut->complete);
170 	ircut->state = state;
171 	mutex_unlock(&ircut->mut_state);
172 	kfree(wk);
173 	wk = NULL;
174 }
175 
ircut_operation(struct ircut_dev * ircut,int op)176 static int ircut_operation(struct ircut_dev *ircut, int op)
177 {
178 	struct ircut_op_work *wk = NULL;
179 	bool should_wait = false;
180 	bool do_nothing = false;
181 	enum IRCUT_STATE_e old_state;
182 
183 	dev_dbg(ircut->dev, "%s, status %d\n", __func__, op);
184 	mutex_lock(&ircut->mut_state);
185 	old_state = ircut->state;
186 	if (op > 0) {
187 		/* check state */
188 		if (IRCUT_STATE_EQ(IRCUT_STATE_OPENING) ||
189 			IRCUT_STATE_EQ(IRCUT_STATE_OPENED)) {
190 			/* already in opening or opened state, do nothing */
191 			do_nothing = true;
192 		} else if (IRCUT_STATE_EQ(IRCUT_STATE_CLOSING)) {
193 			/* in closing state,should wait */
194 			should_wait = true;
195 		} else {
196 			/* in closed state,queue work */
197 		}
198 	} else {
199 		/* check state */
200 		if (IRCUT_STATE_EQ(IRCUT_STATE_CLOSING) ||
201 			IRCUT_STATE_EQ(IRCUT_STATE_CLOSED)) {
202 			/* already in closing or closed state, do nothing */
203 			do_nothing = true;
204 		} else if (IRCUT_STATE_EQ(IRCUT_STATE_OPENING)) {
205 			/* in opening state,should wait */
206 			should_wait = true;
207 		} else {
208 			/* in opened state,queue work */
209 		}
210 	}
211 	mutex_unlock(&ircut->mut_state);
212 	if (do_nothing)
213 		goto op_done;
214 	if (should_wait)
215 		wait_for_completion(&ircut->complete);
216 	wk = kmalloc(sizeof(*wk),
217 		GFP_KERNEL);
218 	if (!wk) {
219 		dev_err(ircut->dev, "failed to alloc ircut work struct\n");
220 		goto err_ircut_operation;
221 	}
222 	wk->op_cmd = op;
223 	wk->dev = ircut;
224 	mutex_lock(&ircut->mut_state);
225 	if (op > 0)
226 		ircut->state = IRCUT_STATE_OPENING;
227 	else
228 		ircut->state = IRCUT_STATE_CLOSING;
229 	reinit_completion(&ircut->complete);
230 	mutex_unlock(&ircut->mut_state);
231 	/* queue work */
232 	INIT_WORK(&wk->work, ircut_op_work);
233 	if (!queue_work(ircut->wq, &wk->work)) {
234 		dev_err(ircut->dev, "queue work failed\n");
235 		kfree(wk);
236 		ircut->state = old_state;
237 		goto err_ircut_operation;
238 	}
239 op_done:
240 	return 0;
241 err_ircut_operation:
242 	return -1;
243 }
244 
245 #ifdef USED_SYS_DEBUG
set_ircut_status(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)246 static ssize_t set_ircut_status(struct device *dev,
247 	struct device_attribute *attr,
248 	const char *buf,
249 	size_t count)
250 {
251 	struct ircut_dev *ircut = dev_get_drvdata(dev);
252 	int status = 0;
253 	int ret = 0;
254 
255 	ret = sscanf(buf, "%d", &status);
256 	if (ret)
257 		ircut_operation(ircut, status);
258 	return count;
259 }
260 
261 static struct device_attribute attributes[] = {
262 	__ATTR(sys_set_ircut, S_IWUSR, NULL, set_ircut_status),
263 };
264 
add_sysfs_interfaces(struct device * dev)265 static int add_sysfs_interfaces(struct device *dev)
266 {
267 	int i;
268 
269 	for (i = 0; i < ARRAY_SIZE(attributes); i++)
270 		if (device_create_file(dev, attributes + i))
271 			goto undo;
272 	return 0;
273 undo:
274 	for (i--; i >= 0 ; i--)
275 		device_remove_file(dev, attributes + i);
276 	dev_err(dev, "%s: failed to create sysfs interface\n", __func__);
277 	return -ENODEV;
278 }
279 #endif
280 
ircut_s_ctrl(struct v4l2_ctrl * ctrl)281 static int ircut_s_ctrl(struct v4l2_ctrl *ctrl)
282 {
283 	int ret = -EINVAL;
284 	struct ircut_dev *ircut = container_of(ctrl->handler,
285 					     struct ircut_dev, ctrl_handler);
286 
287 	if (ctrl->id == V4L2_CID_BAND_STOP_FILTER) {
288 		ret = ircut_operation(ircut, ctrl->val);
289 		if (ret == 0)
290 			ircut->val = ctrl->val;
291 	}
292 	return ret;
293 }
294 
ircut_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)295 static long ircut_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
296 {
297 	return 0;
298 }
299 
300 static const struct v4l2_subdev_core_ops ircut_core_ops = {
301 	.ioctl = ircut_ioctl,
302 };
303 
304 static const struct v4l2_subdev_ops ircut_subdev_ops = {
305 	.core	= &ircut_core_ops,
306 };
307 
308 static const struct v4l2_ctrl_ops ircut_ctrl_ops = {
309 	.s_ctrl = ircut_s_ctrl,
310 };
311 
312 static const struct ircut_drv_data ap1511a_drv_data = {
313 	.parse_dt	= ap1511a_parse_dt,
314 	.ctrl		= ap1511a_ctrl,
315 };
316 
317 static const struct ircut_drv_data ba6208_drv_data = {
318 	.parse_dt	= ba6208_parse_dt,
319 	.ctrl		= ba6208_ctrl,
320 };
321 
322 #if defined(CONFIG_OF)
323 static const struct of_device_id ircut_of_match[] = {
324 	{ .compatible = "rockchip,ircut",
325 		.data = &ba6208_drv_data },
326 	{ .compatible = "ap1511a,ircut",
327 		.data = &ap1511a_drv_data },
328 	{},
329 };
330 MODULE_DEVICE_TABLE(of, ircut_of_match);
331 #endif
332 
ircut_probe(struct platform_device * pdev)333 static int ircut_probe(struct platform_device *pdev)
334 {
335 	struct ircut_dev *ircut = NULL;
336 	struct device_node *node = pdev->dev.of_node;
337 	struct v4l2_ctrl_handler *handler;
338 	const struct of_device_id *match;
339 	int ret = 0;
340 	struct v4l2_subdev *sd;
341 	char facing[2];
342 
343 	dev_info(&pdev->dev, "driver version: %02x.%02x.%02x",
344 		DRIVER_VERSION >> 16,
345 		(DRIVER_VERSION & 0xff00) >> 8,
346 		DRIVER_VERSION & 0x00ff);
347 	ircut = devm_kzalloc(&pdev->dev, sizeof(*ircut), GFP_KERNEL);
348 	if (!ircut) {
349 		dev_err(&pdev->dev, "alloc ircut failed\n");
350 		return -ENOMEM;
351 	}
352 
353 	match = of_match_node(ircut_of_match, pdev->dev.of_node);
354 	if (!match)
355 		return -ENODEV;
356 
357 	ircut->drv_data = match->data;
358 
359 	ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
360 				   &ircut->module_index);
361 	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
362 				       &ircut->module_facing);
363 	if (ret) {
364 		dev_err(&pdev->dev,
365 			"could not get module information!\n");
366 		return -EINVAL;
367 	}
368 	ircut->dev = &pdev->dev;
369 	mutex_init(&ircut->mut_state);
370 	init_completion(&ircut->complete);
371 	ircut->wq = alloc_workqueue("ircut wq",
372 			WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
373 	v4l2_subdev_init(&ircut->sd, &ircut_subdev_ops);
374 	ircut->sd.owner = pdev->dev.driver->owner;
375 	ircut->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
376 	ircut->sd.dev = &pdev->dev;
377 	v4l2_set_subdevdata(&ircut->sd, pdev);
378 	platform_set_drvdata(pdev, &ircut->sd);
379 
380 	handler = &ircut->ctrl_handler;
381 	ret = v4l2_ctrl_handler_init(handler, 1);
382 	if (ret)
383 		return ret;
384 	v4l2_ctrl_new_std(handler, &ircut_ctrl_ops,
385 		V4L2_CID_BAND_STOP_FILTER, 0, 4, 1, 1);
386 	if (handler->error) {
387 		ret = handler->error;
388 		dev_err(&pdev->dev, "Failed to init controls(%d)\n", ret);
389 		goto err_free;
390 	}
391 	ircut->sd.ctrl_handler = handler;
392 	ret = media_entity_pads_init(&ircut->sd.entity, 0, NULL);
393 	if (ret < 0)
394 		goto err_free;
395 
396 	if (ircut->drv_data->parse_dt) {
397 		ret = ircut->drv_data->parse_dt(ircut, node);
398 		if (ret)
399 			goto err_free;
400 	}
401 
402 	sd = &ircut->sd;
403 	sd->entity.function = MEDIA_ENT_F_LENS;
404 	sd->entity.flags = 1;
405 
406 	memset(facing, 0, sizeof(facing));
407 	if (strcmp(ircut->module_facing, "back") == 0)
408 		facing[0] = 'b';
409 	else
410 		facing[0] = 'f';
411 
412 	snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s",
413 		 ircut->module_index, facing,
414 		 RK_IRCUT_NAME);
415 	ret = v4l2_async_register_subdev(sd);
416 	if (ret)
417 		dev_err(&pdev->dev, "v4l2 async register subdev failed\n");
418 
419 	/* set default state to open */
420 	ircut_operation(ircut, 3);
421 	ircut->val = 3;
422 #ifdef USED_SYS_DEBUG
423 	add_sysfs_interfaces(ircut->dev);
424 #endif
425 	dev_info(&pdev->dev, "probe successful!");
426 	return 0;
427 
428 err_free:
429 	v4l2_ctrl_handler_free(handler);
430 	v4l2_device_unregister_subdev(&ircut->sd);
431 	media_entity_cleanup(&ircut->sd.entity);
432 	return ret;
433 }
434 
ircut_drv_remove(struct platform_device * pdev)435 static int ircut_drv_remove(struct platform_device *pdev)
436 {
437 	struct v4l2_subdev *sd = platform_get_drvdata(pdev);
438 	struct ircut_dev *ircut = NULL;
439 
440 	if (sd)
441 		ircut = v4l2_get_subdevdata(sd);
442 	if (ircut && ircut->wq)
443 		drain_workqueue(ircut->wq);
444 	if (sd)
445 		v4l2_device_unregister_subdev(sd);
446 	v4l2_ctrl_handler_free(&ircut->ctrl_handler);
447 	media_entity_cleanup(&ircut->sd.entity);
448 	return 0;
449 }
450 
451 static struct platform_driver ircut_driver = {
452 	.driver = {
453 		.name = RK_IRCUT_NAME,
454 		.of_match_table = of_match_ptr(ircut_of_match),
455 	},
456 	.probe = ircut_probe,
457 	.remove = ircut_drv_remove,
458 };
459 
460 module_platform_driver(ircut_driver);
461 
462 MODULE_LICENSE("GPL");
463 MODULE_ALIAS("platform:rockchip-ircut");
464 MODULE_AUTHOR("ROCKCHIP");
465