xref: /OK3568_Linux_fs/kernel/drivers/media/i2c/nvp6158_drv/nvp6158_v4l2.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * nvp6158_v4l2 interface driver
4  *
5  * Copyright (c) 2021 Rockchip Electronics Co. Ltd.
6  *
7  * V0.0X01.0X00 first version.
8  * V0.0X01.0X01
9  * 1. add workqueue to detect ahd state.
10  * 2. add more resolution support.
11  */
12 
13 #include <linux/clk.h>
14 #include <linux/device.h>
15 #include <linux/delay.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/i2c.h>
18 #include <linux/module.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/sysfs.h>
22 #include <media/media-entity.h>
23 #include <media/v4l2-async.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-subdev.h>
26 #include <media/v4l2-fwnode.h>
27 #include <linux/pinctrl/consumer.h>
28 #include <linux/of.h>
29 #include <linux/of_device.h>
30 #include <linux/of_graph.h>
31 #include <linux/of_platform.h>
32 #include <linux/of_gpio.h>
33 #include <linux/mfd/syscon.h>
34 #include <linux/version.h>
35 #include <linux/rk-camera-module.h>
36 #include <linux/rk-preisp.h>
37 
38 #include "nvp6158_common.h"
39 #include "nvp6158_video.h"
40 #include "nvp6158_coax_protocol.h"
41 #include "nvp6158_motion.h"
42 #include "nvp6158_video_eq.h"
43 #include "nvp6158_drv.h"
44 #include "nvp6158_audio.h"
45 #include "nvp6158_video_auto_detect.h"
46 #include "nvp6158_drv.h"
47 
48 //#define WORK_QUEUE
49 
50 #ifdef WORK_QUEUE
51 #include <linux/workqueue.h>
52 
53 struct sensor_state_check_work {
54 	struct workqueue_struct *state_check_wq;
55 	struct delayed_work d_work;
56 };
57 
58 #endif
59 
60 #define DRIVER_VERSION				KERNEL_VERSION(0, 0x01, 0x1)
61 
62 #ifndef V4L2_CID_DIGITAL_GAIN
63 #define V4L2_CID_DIGITAL_GAIN			V4L2_CID_GAIN
64 #endif
65 
66 #define NVP6158_XVCLK_FREQ			24000000
67 #define NVP6158_BITS_PER_SAMPLE			8
68 
69 /* pixel rate = link frequency * 2 * lanes / BITS_PER_SAMPLE */
70 #define NVP6158_PIXEL_RATE			297000000LL
71 
72 #define OF_CAMERA_PINCTRL_STATE_DEFAULT		"rockchip,camera_default"
73 #define OF_CAMERA_PINCTRL_STATE_SLEEP		"rockchip,camera_sleep"
74 
75 #define OF_CAMERA_MODULE_REGULATORS		"rockchip,regulator-names"
76 #define OF_CAMERA_MODULE_REGULATOR_VOLTAGES	"rockchip,regulator-voltages"
77 
78 /* DVP MODE, BT1120 or BT656 */
79 #define RK_CAMERA_MODULE_DVP_MODE		"rockchip,dvp_mode"
80 #define RK_CAMERA_MODULE_CHANNEL_NUMS		"rockchip,channel_nums"
81 #define RK_CAMERA_MODULE_DUAL_EDGE		"rockchip,dual_edge"
82 #define RK_CAMERA_MODULE_DEFAULT_RECT		"rockchip,default_rect"
83 
84 #define NVP6158_DEFAULT_DVP_MODE		"BT1120"
85 #define NVP6158_DEFAULT_CHANNEL_NUMS		4U
86 #define NVP6158_DEFAULT_DUAL_EDGE		0U
87 #define NVP6158_NAME				"nvp6158"
88 #define NVP6158_DEFAULT_WIDTH			1920
89 #define NVP6158_DEFAULT_HEIGHT			1080
90 
91 struct nvp6158_gpio {
92 	int pltfrm_gpio;
93 	const char *label;
94 	enum of_gpio_flags active_low;
95 };
96 
97 struct nvp6158_regulator {
98 	struct regulator *regulator;
99 	u32 min_uV;
100 	u32 max_uV;
101 };
102 
103 struct nvp6158_regulators {
104 	u32 cnt;
105 	struct nvp6158_regulator *regulator;
106 };
107 
108 struct nvp6158_pixfmt {
109 	u32 code;
110 };
111 
112 struct nvp6158_framesize {
113 	u16 width;
114 	u16 height;
115 	NC_VIVO_CH_FORMATDEF fmt_idx;
116 	struct v4l2_fract max_fps;
117 };
118 
119 struct nvp6158_default_rect {
120 	unsigned int width;
121 	unsigned int height;
122 };
123 
124 #ifdef WORK_QUEUE
125 enum nvp6158_hot_plug_state {
126 	PLUG_IN = 0,
127 	PLUG_OUT,
128 	PLUG_STATE_MAX,
129 };
130 #endif
131 
132 struct nvp6158 {
133 	struct i2c_client	*client;
134 	struct clk		*xvclk;
135 	struct gpio_desc	*pwr_gpio;
136 	struct gpio_desc	*pwr2_gpio;
137 	struct gpio_desc	*rst_gpio;
138 	struct gpio_desc	*rst2_gpio;
139 	struct gpio_desc	*pwdn_gpio;
140 	struct gpio_desc	*pwdn2_gpio;
141 
142 	struct pinctrl		*pinctrl;
143 	struct pinctrl_state	*pins_default;
144 	struct pinctrl_state	*pins_sleep;
145 
146 	struct v4l2_subdev	subdev;
147 	struct media_pad	pad[PAD_MAX];
148 	struct v4l2_ctrl_handler ctrl_handler;
149 	struct mutex		mutex;
150 	bool			power_on;
151 	struct nvp6158_regulators regulators;
152 
153 	u32			module_index;
154 	const char		*module_facing;
155 	const char		*module_name;
156 	const char		*len_name;
157 	const char		*dvp_mode;
158 	NVP6158_DVP_MODE	mode;
159 	u32			ch_nums;
160 	u32			dual_edge;
161 
162 	struct v4l2_mbus_framefmt format;
163 	const struct nvp6158_framesize *frame_size;
164 	int streaming;
165 	struct nvp6158_default_rect defrect;
166 #ifdef WORK_QUEUE
167 	struct sensor_state_check_work plug_state_check;
168 	u8 cur_detect_status;
169 	u8 last_detect_status;
170 #endif
171 	bool hot_plug;
172 	u8 is_reset;
173 
174 };
175 
176 #define to_nvp6158(sd) container_of(sd, struct nvp6158, subdev)
177 
178 static const struct nvp6158_framesize nvp6158_framesizes[] = {
179 	{
180 		.width		= 1280,
181 		.height		= 720,
182 		.fmt_idx	= AHD20_720P_30P,
183 		.max_fps = {
184 			.numerator = 10000,
185 			.denominator = 250000,
186 		},
187 	}, {
188 		.width		= 1920,
189 		.height		= 1080,
190 		.fmt_idx	= AHD20_1080P_25P,
191 		.max_fps = {
192 			.numerator = 10000,
193 			.denominator = 250000,
194 		},
195 	}, {
196 		.width		= 2048,
197 		.height		= 1536,
198 		.fmt_idx	= AHD30_3M_18P,
199 		.max_fps = {
200 			.numerator = 10000,
201 			.denominator = 180000,
202 		},
203 	}, {
204 		.width		= 1280,
205 		.height		= 1440,
206 		.fmt_idx	= AHD30_4M_30P,
207 		.max_fps = {
208 			.numerator = 10000,
209 			.denominator = 300000,
210 		},
211 	}, {
212 		.width		= 2560,
213 		.height		= 1440,
214 		.fmt_idx	= AHD30_4M_15P,
215 		.max_fps = {
216 			.numerator = 10000,
217 			.denominator = 150000,
218 		},
219 	}, {
220 		.width		= 2592,
221 		.height		= 1944,
222 		.fmt_idx	= AHD30_5M_12_5P,
223 		.max_fps = {
224 			.numerator = 10000,
225 			.denominator = 125000,
226 		},
227 	}, {
228 		.width		= 3840,
229 		.height		= 2160,
230 		.fmt_idx	= AHD30_8M_7_5P,
231 		.max_fps = {
232 			.numerator = 10000,
233 			.denominator = 75000,
234 		},
235 	}, {/* test modes, Interlace mode*/
236 		.width		= 720,
237 		.height		= 480,
238 		.fmt_idx	= AHD20_SD_SH720_NT,
239 		.max_fps = {
240 			.numerator = 10000,
241 			.denominator = 250000,
242 		},
243 	}, {
244 		.width		= 720,
245 		.height		= 576,
246 		.fmt_idx	= AHD20_SD_SH720_PAL,
247 		.max_fps = {
248 			.numerator = 10000,
249 			.denominator = 250000,
250 		},
251 	}, {
252 		.width		= 960,
253 		.height		= 576,
254 		.fmt_idx	= AHD20_SD_H960_PAL,
255 		.max_fps = {
256 			.numerator = 10000,
257 			.denominator = 250000,
258 		},
259 	}, {
260 		.width		= 1920,
261 		.height		= 576,
262 		.fmt_idx	= AHD20_SD_H960_EX_PAL,
263 		.max_fps = {
264 			.numerator = 10000,
265 			.denominator = 250000,
266 		},
267 	}
268 };
269 
270 static char *nvp6158_dvp_mode_lists[] = {
271 	[BT601] = "BT601",
272 	[BT656_1MUX] = "BT656_1MUX",
273 	[BT656_2MUX] = "BT656_2MUX",
274 	[BT656_4MUX] = "BT656_4MUX",
275 	[BT1120_1MUX] = "BT1120_1MUX",
276 	[BT1120_2MUX] = "BT1120_2MUX",
277 	[BT1120_4MUX] = "BT1120_4MUX",
278 	[BT656I_TEST_MODES] = "BT656I_TEST_MODES"
279 };
280 
281 static const struct nvp6158_pixfmt nvp6158_formats[] = {
282 	{
283 		.code = MEDIA_BUS_FMT_UYVY8_2X8
284 	},
285 };
286 
nvp6158_querystd(struct v4l2_subdev * sd,v4l2_std_id * std)287 static int nvp6158_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
288 {
289 	struct nvp6158 *nvp6158 = to_nvp6158(sd);
290 
291 	if ((nvp6158->mode > BT656I_TEST_MODES) &&
292 		(nvp6158->mode < NVP6158_DVP_MODES_END)) {
293 		/* for vicap detect bt1120 */
294 		*std = V4L2_STD_ATSC;
295 	} else {
296 		*std = V4L2_STD_PAL;
297 	}
298 	return 0;
299 }
300 
301 /* sensor register write */
nvp6158_write(struct i2c_client * client,u8 reg,u8 val)302 static int nvp6158_write(struct i2c_client *client, u8 reg, u8 val)
303 {
304 	struct i2c_msg msg;
305 	u8 buf[2];
306 	int ret;
307 
308 	dev_info(&client->dev, "write reg(0x%x val:0x%x)!\n", reg, val);
309 	buf[0] = reg & 0xFF;
310 	buf[1] = val;
311 
312 	msg.addr = client->addr;
313 	msg.flags = client->flags;
314 	msg.buf = buf;
315 	msg.len = sizeof(buf);
316 
317 	ret = i2c_transfer(client->adapter, &msg, 1);
318 	if (ret >= 0)
319 		return 0;
320 
321 	dev_err(&client->dev,
322 		"nvp6158 write reg(0x%x val:0x%x) failed !\n", reg, val);
323 
324 	return ret;
325 }
326 
327 /* sensor register read */
nvp6158_read(struct i2c_client * client,u8 reg,u8 * val)328 static int nvp6158_read(struct i2c_client *client, u8 reg, u8 *val)
329 {
330 	struct i2c_msg msg[2];
331 	u8 buf[1];
332 	int ret;
333 
334 	buf[0] = reg & 0xFF;
335 
336 	msg[0].addr = client->addr;
337 	msg[0].flags = client->flags;
338 	msg[0].buf = buf;
339 	msg[0].len = sizeof(buf);
340 
341 	msg[1].addr = client->addr;
342 	msg[1].flags = client->flags | I2C_M_RD;
343 	msg[1].buf = buf;
344 	msg[1].len = 1;
345 
346 	ret = i2c_transfer(client->adapter, msg, 2);
347 	if (ret >= 0) {
348 		*val = buf[0];
349 		return 0;
350 	}
351 
352 	dev_err(&client->dev, "nvp6158 read reg(0x%x) failed !\n", reg);
353 
354 	return ret;
355 }
356 
__nvp6158_power_on(struct nvp6158 * nvp6158)357 static int __nvp6158_power_on(struct nvp6158 *nvp6158)
358 {
359 	u32 i;
360 	int ret;
361 	struct nvp6158_regulator *regulator;
362 	struct device *dev = &nvp6158->client->dev;
363 
364 	dev_info(dev, "%s(%d)\n", __func__, __LINE__);
365 
366 	if (!IS_ERR_OR_NULL(nvp6158->pins_default)) {
367 		ret = pinctrl_select_state(nvp6158->pinctrl,
368 					   nvp6158->pins_default);
369 		if (ret < 0)
370 			dev_err(dev, "could not set pins. ret=%d\n", ret);
371 	}
372 
373 	ret = clk_prepare_enable(nvp6158->xvclk);
374 	if (ret < 0) {
375 		dev_err(dev, "Failed to enable xvclk\n");
376 		return ret;
377 	}
378 
379 	if (nvp6158->regulators.regulator) {
380 		for (i = 0; i < nvp6158->regulators.cnt; i++) {
381 			regulator = nvp6158->regulators.regulator + i;
382 			if (IS_ERR(regulator->regulator))
383 				continue;
384 			regulator_set_voltage(
385 				regulator->regulator,
386 				regulator->min_uV,
387 				regulator->max_uV);
388 			if (regulator_enable(regulator->regulator)) {
389 				dev_err(dev,
390 					"regulator_enable failed!\n");
391 				goto disable_clk;
392 			}
393 		}
394 	}
395 	usleep_range(3000, 5000);
396 
397 	if (!IS_ERR(nvp6158->pwr_gpio)) {
398 		gpiod_direction_output(nvp6158->pwr_gpio, 1);
399 		usleep_range(3000, 5000);
400 	}
401 
402 	if (!IS_ERR(nvp6158->pwr2_gpio)) {
403 		gpiod_direction_output(nvp6158->pwr2_gpio, 1);
404 		usleep_range(3000, 5000);
405 	}
406 
407 	if (!IS_ERR(nvp6158->pwdn_gpio)) {
408 		gpiod_direction_output(nvp6158->pwdn_gpio, 1);
409 		usleep_range(1500, 2000);
410 	}
411 
412 	if (!IS_ERR(nvp6158->pwdn2_gpio)) {
413 		gpiod_direction_output(nvp6158->pwdn2_gpio, 1);
414 		usleep_range(1500, 2000);
415 	}
416 
417 	if (!IS_ERR(nvp6158->rst_gpio)) {
418 		gpiod_direction_output(nvp6158->rst_gpio, 0);
419 		usleep_range(50000, 100000);
420 		gpiod_direction_output(nvp6158->rst_gpio, 1);
421 		usleep_range(3000, 5000);
422 	}
423 
424 	if (!IS_ERR(nvp6158->rst2_gpio)) {
425 		gpiod_direction_output(nvp6158->rst2_gpio, 0);
426 		usleep_range(1500, 2000);
427 		gpiod_direction_output(nvp6158->rst2_gpio, 1);
428 		usleep_range(3000, 5000);
429 	}
430 
431 	return 0;
432 
433 disable_clk:
434 	clk_disable_unprepare(nvp6158->xvclk);
435 
436 	return ret;
437 }
438 
__nvp6158_power_off(struct nvp6158 * nvp6158)439 static void __nvp6158_power_off(struct nvp6158 *nvp6158)
440 {
441 	u32 i;
442 	int ret;
443 	struct nvp6158_regulator *regulator;
444 	struct device *dev = &nvp6158->client->dev;
445 
446 	dev_info(dev, "%s(%d)\n", __func__, __LINE__);
447 	clk_disable_unprepare(nvp6158->xvclk);
448 
449 	if (!IS_ERR(nvp6158->rst_gpio))
450 		gpiod_direction_output(nvp6158->rst_gpio, 0);
451 
452 	if (!IS_ERR(nvp6158->rst2_gpio))
453 		gpiod_direction_output(nvp6158->rst2_gpio, 0);
454 
455 	if (!IS_ERR(nvp6158->pwdn_gpio))
456 		gpiod_direction_output(nvp6158->pwdn_gpio, 0);
457 
458 	if (!IS_ERR(nvp6158->pwdn_gpio))
459 		gpiod_direction_output(nvp6158->pwdn2_gpio, 0);
460 
461 	if (!IS_ERR(nvp6158->pwr_gpio))
462 		gpiod_direction_output(nvp6158->pwr_gpio, 0);
463 
464 	if (!IS_ERR(nvp6158->pwr2_gpio))
465 		gpiod_direction_output(nvp6158->pwr2_gpio, 0);
466 
467 	if (!IS_ERR_OR_NULL(nvp6158->pins_sleep)) {
468 		ret = pinctrl_select_state(nvp6158->pinctrl,
469 					   nvp6158->pins_sleep);
470 		if (ret < 0)
471 			dev_err(dev, "could not set pins\n");
472 	}
473 
474 	if (nvp6158->regulators.regulator) {
475 		for (i = 0; i < nvp6158->regulators.cnt; i++) {
476 			regulator = nvp6158->regulators.regulator + i;
477 			if (IS_ERR(regulator->regulator))
478 				continue;
479 			regulator_disable(regulator->regulator);
480 		}
481 	}
482 }
483 
nvp6158_power(struct v4l2_subdev * sd,int on)484 static int nvp6158_power(struct v4l2_subdev *sd, int on)
485 {
486 	struct i2c_client *client = v4l2_get_subdevdata(sd);
487 	struct nvp6158 *nvp6158 = to_nvp6158(sd);
488 	int ret = 0;
489 
490 	dev_info(&client->dev, "%s: on %d\n", __func__, on);
491 	mutex_lock(&nvp6158->mutex);
492 
493 	/* If the power state is not modified - no work to do. */
494 	if (nvp6158->power_on == !!on)
495 		goto exit;
496 
497 	if (on) {
498 		ret = __nvp6158_power_on(nvp6158);
499 		if (ret < 0)
500 			goto exit;
501 
502 		nvp6158->power_on = true;
503 	} else {
504 		__nvp6158_power_off(nvp6158);
505 		nvp6158->power_on = false;
506 	}
507 
508 exit:
509 	mutex_unlock(&nvp6158->mutex);
510 
511 	return ret;
512 }
513 
514 #define CROP_START(SRC, DST) (((SRC) - (DST)) / 2 / 4 * 4)
515 /*
516  * The resolution of the driver configuration needs to be exactly
517  * the same as the current output resolution of the sensor,
518  * the input width of the isp needs to be 16 aligned,
519  * the input height of the isp needs to be 8 aligned.
520  * Can be cropped to standard resolution by this function,
521  * otherwise it will crop out strange resolution according
522  * to the alignment rules.
523  */
nvp6158_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)524 static int nvp6158_get_selection(struct v4l2_subdev *sd,
525 				 struct v4l2_subdev_pad_config *cfg,
526 				 struct v4l2_subdev_selection *sel)
527 {
528 	struct nvp6158 *nvp6158 = to_nvp6158(sd);
529 
530 	if (sel->target == V4L2_SEL_TGT_CROP_BOUNDS) {
531 		sel->r.left = CROP_START(0, 0);
532 		sel->r.width = nvp6158->frame_size->width;
533 		sel->r.top = CROP_START(0, 0);
534 		sel->r.height = nvp6158->frame_size->height;
535 		return 0;
536 	}
537 	return -EINVAL;
538 }
539 
nvp6158_initialize_controls(struct nvp6158 * nvp6158)540 static int nvp6158_initialize_controls(struct nvp6158 *nvp6158)
541 {
542 	struct v4l2_ctrl_handler *handler;
543 	int ret;
544 
545 	handler = &nvp6158->ctrl_handler;
546 	ret = v4l2_ctrl_handler_init(handler, 2);
547 	if (ret)
548 		return ret;
549 	handler->lock = &nvp6158->mutex;
550 
551 	v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
552 			  0, NVP6158_PIXEL_RATE, 1, NVP6158_PIXEL_RATE);
553 
554 	if (handler->error) {
555 		ret = handler->error;
556 		dev_err(&nvp6158->client->dev,
557 			"Failed to init controls(%d)\n", ret);
558 		goto err_free_handler;
559 	}
560 
561 	nvp6158->subdev.ctrl_handler = handler;
562 
563 	return 0;
564 
565 err_free_handler:
566 	v4l2_ctrl_handler_free(handler);
567 
568 	return ret;
569 }
nvp6158_get_default_format(struct nvp6158 * nvp6158)570 static void nvp6158_get_default_format(struct nvp6158 *nvp6158)
571 {
572 
573 	const struct nvp6158_framesize *fsize = &nvp6158_framesizes[0];
574 	const struct nvp6158_framesize *match = NULL;
575 	int i = ARRAY_SIZE(nvp6158_framesizes);
576 	unsigned int min_err = UINT_MAX;
577 	struct v4l2_mbus_framefmt *format = &nvp6158->format;
578 	struct nvp6158_default_rect *rect =  &nvp6158->defrect;
579 
580 	while (i--) {
581 		unsigned int err = abs(fsize->width - rect->width)
582 				+ abs(fsize->height - rect->height);
583 		if (err < min_err) {
584 			min_err = err;
585 			match = fsize;
586 		}
587 		fsize++;
588 	}
589 
590 	if (!match)
591 		match = &nvp6158_framesizes[0];
592 
593 	format->width = match->width;
594 	format->height = match->height;
595 	format->colorspace = V4L2_COLORSPACE_SRGB;
596 	format->code = nvp6158_formats[0].code;
597 	if (BT656I_TEST_MODES == nvp6158->mode)
598 		format->field = V4L2_FIELD_INTERLACED;
599 	else
600 		format->field = V4L2_FIELD_NONE;
601 	nvp6158->frame_size = match;
602 }
603 
nvp6158_stream(struct v4l2_subdev * sd,int on)604 static int nvp6158_stream(struct v4l2_subdev *sd, int on)
605 {
606 	struct i2c_client *client = v4l2_get_subdevdata(sd);
607 	struct nvp6158 *nvp6158 = to_nvp6158(sd);
608 	video_init_all video_init;
609 	NC_VIVO_CH_FORMATDEF fmt_idx;
610 	int ch;
611 
612 	dev_info(&client->dev, "%s: on: %d, %dx%d\n", __func__, on,
613 				nvp6158->frame_size->width,
614 				nvp6158->frame_size->height);
615 
616 	mutex_lock(&nvp6158->mutex);
617 	on = !!on;
618 
619 	if (nvp6158->streaming == on)
620 		goto unlock;
621 
622 	if (on) {
623 		for (ch = 0; ch < 4; ch++) {
624 			fmt_idx = nvp6158->frame_size->fmt_idx;
625 			video_init.ch_param[ch].ch = ch;
626 			video_init.ch_param[ch].format = fmt_idx;
627 		}
628 		video_init.mode = nvp6158->mode;
629 		nvp6158_start(&video_init, nvp6158->dual_edge ? true : false);
630 #ifdef WORK_QUEUE
631 		if (nvp6158->plug_state_check.state_check_wq) {
632 			dev_info(&client->dev, "%s queue_delayed_work 1000ms", __func__);
633 			queue_delayed_work(nvp6158->plug_state_check.state_check_wq,
634 					   &nvp6158->plug_state_check.d_work,
635 					   msecs_to_jiffies(1000));
636 		}
637 #endif
638 	} else {
639 #ifdef WORK_QUEUE
640 		cancel_delayed_work_sync(&nvp6158->plug_state_check.d_work);
641 		dev_info(&client->dev, "cancle_queue_delayed_work");
642 #endif
643 		nvp6158_stop();
644 	}
645 
646 	nvp6158->streaming = on;
647 
648 unlock:
649 	mutex_unlock(&nvp6158->mutex);
650 
651 	return 0;
652 }
653 
nvp6158_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_interval_enum * fie)654 static int nvp6158_enum_frame_interval(struct v4l2_subdev *sd,
655 				       struct v4l2_subdev_pad_config *cfg,
656 				       struct v4l2_subdev_frame_interval_enum *fie)
657 {
658 	struct nvp6158 *nvp6158 = to_nvp6158(sd);
659 	struct i2c_client *client = nvp6158->client;
660 
661 	dev_dbg(&client->dev, "%s enter.\n", __func__);
662 
663 	if (fie->index >= ARRAY_SIZE(nvp6158_framesizes))
664 		return -EINVAL;
665 
666 	fie->width = nvp6158_framesizes[fie->index].width;
667 	fie->height = nvp6158_framesizes[fie->index].height;
668 	fie->interval = nvp6158_framesizes[fie->index].max_fps;
669 	return 0;
670 }
671 
nvp6158_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)672 static int nvp6158_enum_mbus_code(struct v4l2_subdev *sd,
673 				 struct v4l2_subdev_pad_config *cfg,
674 				 struct v4l2_subdev_mbus_code_enum *code)
675 {
676 	if (code->index >= ARRAY_SIZE(nvp6158_formats))
677 		return -EINVAL;
678 
679 	code->code = nvp6158_formats[code->index].code;
680 
681 	return 0;
682 }
683 
nvp6158_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)684 static int nvp6158_enum_frame_sizes(struct v4l2_subdev *sd,
685 				   struct v4l2_subdev_pad_config *cfg,
686 				   struct v4l2_subdev_frame_size_enum *fse)
687 {
688 	struct i2c_client *client = v4l2_get_subdevdata(sd);
689 	int i = ARRAY_SIZE(nvp6158_formats);
690 
691 	dev_dbg(&client->dev, "%s: enter!\n", __func__);
692 
693 	if (fse->index >= ARRAY_SIZE(nvp6158_framesizes))
694 		return -EINVAL;
695 
696 	while (--i)
697 		if (fse->code == nvp6158_formats[i].code)
698 			break;
699 
700 	fse->code = nvp6158_formats[i].code;
701 
702 	fse->min_width  = nvp6158_framesizes[fse->index].width;
703 	fse->max_width  = fse->min_width;
704 	fse->max_height = nvp6158_framesizes[fse->index].height;
705 	fse->min_height = fse->max_height;
706 
707 	return 0;
708 }
709 
710 /* indicate N4 no signal channel */
nvp6158_no_signal(struct v4l2_subdev * sd,u8 * novid)711 static inline bool nvp6158_no_signal(struct v4l2_subdev *sd, u8 *novid)
712 {
713 	struct nvp6158 *nvp6158 = to_nvp6158(sd);
714 	struct i2c_client *client = nvp6158->client;
715 	u8 videoloss = 0;
716 	int ret;
717 	bool no_signal = false;
718 
719 	nvp6158_write(client, 0xff, 0x00);
720 	ret = nvp6158_read(client, 0xa8, &videoloss);
721 	if (ret < 0)
722 		dev_err(&client->dev, "Failed to read videoloss state!\n");
723 
724 	*novid = videoloss;
725 	dev_info(&client->dev, "%s: video loss status:0x%x.\n", __func__, videoloss);
726 	if (videoloss == 0xf) {
727 		dev_info(&client->dev, "%s: all channels No Video detected.\n", __func__);
728 		no_signal = true;
729 	} else {
730 		dev_info(&client->dev, "%s: channel has some video detection.\n", __func__);
731 		no_signal = false;
732 	}
733 	return no_signal;
734 }
735 
736 /* indicate N4 channel locked status */
nvp6158_sync(struct v4l2_subdev * sd,u8 * lock_st)737 static inline bool nvp6158_sync(struct v4l2_subdev *sd, u8 *lock_st)
738 {
739 	struct i2c_client *client = v4l2_get_subdevdata(sd);
740 	u8 video_lock_status = 0;
741 	int ret;
742 	bool has_sync = false;
743 
744 	nvp6158_write(client, 0xff, 0x00);
745 	ret = nvp6158_read(client, 0xe0, &video_lock_status);
746 	if (ret < 0)
747 		dev_err(&client->dev, "Failed to read sync state!\n");
748 
749 	dev_info(&client->dev, "%s: video AGC LOCK status:0x%x.\n",
750 			__func__, video_lock_status);
751 	*lock_st = video_lock_status;
752 	if (video_lock_status) {
753 		dev_info(&client->dev, "%s: channel has AGC LOCK.\n", __func__);
754 		has_sync = true;
755 	} else {
756 		dev_info(&client->dev, "%s: channel has no AGC LOCK.\n", __func__);
757 		has_sync = false;
758 	}
759 	return has_sync;
760 }
761 
762 #ifdef WORK_QUEUE
nvp6158_plug_state_check_work(struct work_struct * work)763 static void nvp6158_plug_state_check_work(struct work_struct *work)
764 {
765 	struct sensor_state_check_work *params_check =
766 		container_of(work, struct sensor_state_check_work, d_work.work);
767 	struct nvp6158 *nvp6158 =
768 		container_of(params_check, struct nvp6158, plug_state_check);
769 	struct i2c_client *client = nvp6158->client;
770 	struct v4l2_subdev *sd = &nvp6158->subdev;
771 	u8 novid_status = 0x00;
772 	u8 sync_status = 0x00;
773 
774 	nvp6158_no_signal(sd, &novid_status);
775 	nvp6158_sync(sd, &sync_status);
776 	nvp6158->cur_detect_status = novid_status;
777 
778 	/* detect state change to determine is there has plug motion */
779 	novid_status = nvp6158->cur_detect_status ^ nvp6158->last_detect_status;
780 	if (novid_status)
781 		nvp6158->hot_plug = true;
782 	else
783 		nvp6158->hot_plug = false;
784 	nvp6158->last_detect_status = nvp6158->cur_detect_status;
785 
786 	dev_info(&client->dev, "%s has plug motion? (%s)", __func__,
787 			 nvp6158->hot_plug ? "true" : "false");
788 	if (nvp6158->hot_plug) {
789 		dev_info(&client->dev, "queue_delayed_work 1500ms, if has hot plug motion.");
790 		queue_delayed_work(nvp6158->plug_state_check.state_check_wq,
791 				   &nvp6158->plug_state_check.d_work, msecs_to_jiffies(1500));
792 		nvp6158_write(client, 0xFF, 0x20);
793 		nvp6158_write(client, 0x00, (sync_status << 4) | sync_status);
794 		usleep_range(3000, 5000);
795 		nvp6158_write(client, 0x00, 0xFF);
796 	} else {
797 		dev_info(&client->dev, "queue_delayed_work 100ms, if no hot plug motion.");
798 		queue_delayed_work(nvp6158->plug_state_check.state_check_wq,
799 				   &nvp6158->plug_state_check.d_work, msecs_to_jiffies(100));
800 	}
801 }
802 #endif
803 
nvp6158_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_config * cfg)804 static int nvp6158_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
805 				 struct v4l2_mbus_config *cfg)
806 {
807 	struct nvp6158 *nvp6158 = to_nvp6158(sd);
808 
809 	cfg->type = V4L2_MBUS_BT656;
810 	if (nvp6158->dual_edge == 1) {
811 		cfg->flags = RKMODULE_CAMERA_BT656_CHANNELS |
812 			V4L2_MBUS_PCLK_SAMPLE_RISING |
813 			V4L2_MBUS_PCLK_SAMPLE_FALLING;
814 	} else {
815 		cfg->flags = RKMODULE_CAMERA_BT656_CHANNELS |
816 			V4L2_MBUS_PCLK_SAMPLE_RISING;
817 	}
818 	return 0;
819 }
820 
nvp6158_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)821 static int nvp6158_get_fmt(struct v4l2_subdev *sd,
822 			  struct v4l2_subdev_pad_config *cfg,
823 			  struct v4l2_subdev_format *fmt)
824 {
825 	struct i2c_client *client = v4l2_get_subdevdata(sd);
826 	struct nvp6158 *nvp6158 = to_nvp6158(sd);
827 
828 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
829 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
830 		struct v4l2_mbus_framefmt *mf;
831 
832 		mf = v4l2_subdev_get_try_format(sd, cfg, 0);
833 		mutex_lock(&nvp6158->mutex);
834 		fmt->format = *mf;
835 		mutex_unlock(&nvp6158->mutex);
836 		return 0;
837 #else
838 	return -ENOTTY;
839 #endif
840 	}
841 
842 	mutex_lock(&nvp6158->mutex);
843 	fmt->format = nvp6158->format;
844 	mutex_unlock(&nvp6158->mutex);
845 
846 	dev_dbg(&client->dev, "%s: %x %dx%d\n", __func__,
847 		nvp6158->format.code, nvp6158->format.width,
848 		nvp6158->format.height);
849 
850 	return 0;
851 }
852 
__nvp6158_try_frame_size(struct v4l2_mbus_framefmt * mf,const struct nvp6158_framesize ** size)853 static void __nvp6158_try_frame_size(struct v4l2_mbus_framefmt *mf,
854 				    const struct nvp6158_framesize **size)
855 {
856 	const struct nvp6158_framesize *fsize = &nvp6158_framesizes[0];
857 	const struct nvp6158_framesize *match = NULL;
858 	int i = ARRAY_SIZE(nvp6158_framesizes);
859 	unsigned int min_err = UINT_MAX;
860 
861 	while (i--) {
862 		unsigned int err = abs(fsize->width - mf->width)
863 				+ abs(fsize->height - mf->height);
864 		if (err < min_err) {
865 			min_err = err;
866 			match = fsize;
867 		}
868 		fsize++;
869 	}
870 
871 	if (!match)
872 		match = &nvp6158_framesizes[0];
873 
874 	mf->width  = match->width;
875 	mf->height = match->height;
876 
877 	if (size)
878 		*size = match;
879 }
880 
nvp6158_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)881 static int nvp6158_set_fmt(struct v4l2_subdev *sd,
882 			  struct v4l2_subdev_pad_config *cfg,
883 			  struct v4l2_subdev_format *fmt)
884 {
885 	int index = ARRAY_SIZE(nvp6158_formats);
886 	struct v4l2_mbus_framefmt *mf = &fmt->format;
887 	const struct nvp6158_framesize *size = NULL;
888 	struct nvp6158 *nvp6158 = to_nvp6158(sd);
889 	int ret = 0;
890 
891 	__nvp6158_try_frame_size(mf, &size);
892 
893 	while (--index >= 0)
894 		if (nvp6158_formats[index].code == mf->code)
895 			break;
896 
897 	if (index < 0)
898 		return -EINVAL;
899 
900 	mf->colorspace = V4L2_COLORSPACE_SRGB;
901 	mf->code = nvp6158_formats[index].code;
902 	mf->field = nvp6158->format.field;
903 
904 	mutex_lock(&nvp6158->mutex);
905 
906 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
907 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
908 		mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
909 		*mf = fmt->format;
910 #else
911 		return -ENOTTY;
912 #endif
913 	} else {
914 		if (nvp6158->streaming) {
915 			mutex_unlock(&nvp6158->mutex);
916 			return -EBUSY;
917 		}
918 
919 		nvp6158->frame_size = size;
920 		nvp6158->format = fmt->format;
921 	}
922 
923 	mutex_unlock(&nvp6158->mutex);
924 	return ret;
925 }
926 
nvp6158_get_module_inf(struct nvp6158 * nvp6158,struct rkmodule_inf * inf)927 static void nvp6158_get_module_inf(struct nvp6158 *nvp6158,
928 				   struct rkmodule_inf *inf)
929 {
930 	memset(inf, 0, sizeof(*inf));
931 	strlcpy(inf->base.sensor, NVP6158_NAME, sizeof(inf->base.sensor));
932 	strlcpy(inf->base.module, nvp6158->module_name,
933 		sizeof(inf->base.module));
934 	strlcpy(inf->base.lens, nvp6158->len_name, sizeof(inf->base.lens));
935 }
936 
937 static __maybe_unused void
nvp6158_get_bt656_module_inf(struct nvp6158 * nvp6158,struct rkmodule_bt656_mbus_info * inf)938 nvp6158_get_bt656_module_inf(struct nvp6158 *nvp6158,
939 			       struct rkmodule_bt656_mbus_info *inf)
940 {
941 	memset(inf, 0, sizeof(*inf));
942 	inf->flags = RKMODULE_CAMERA_BT656_PARSE_ID_LSB;
943 	switch (nvp6158->ch_nums) {
944 	case 1:
945 		inf->flags |= RKMODULE_CAMERA_BT656_CHANNEL_0;
946 		break;
947 	case 2:
948 		inf->flags |= RKMODULE_CAMERA_BT656_CHANNEL_0 |
949 			      RKMODULE_CAMERA_BT656_CHANNEL_1;
950 		break;
951 	case 4:
952 		inf->flags |= RKMODULE_CAMERA_BT656_CHANNELS;
953 		break;
954 	default:
955 		inf->flags |= RKMODULE_CAMERA_BT656_CHANNELS;
956 	}
957 }
958 
nvp6158_get_vicap_rst_inf(struct nvp6158 * nvp6158,struct rkmodule_vicap_reset_info * rst_info)959 static void nvp6158_get_vicap_rst_inf(struct nvp6158 *nvp6158,
960 				   struct rkmodule_vicap_reset_info *rst_info)
961 {
962 	struct i2c_client *client = nvp6158->client;
963 
964 	rst_info->is_reset = nvp6158->hot_plug;
965 	nvp6158->hot_plug = false;
966 	rst_info->src = RKCIF_RESET_SRC_ERR_HOTPLUG;
967 	dev_info(&client->dev, "%s: rst_info->is_reset:%d.\n", __func__, rst_info->is_reset);
968 }
969 
nvp6158_set_vicap_rst_inf(struct nvp6158 * nvp6158,struct rkmodule_vicap_reset_info rst_info)970 static void nvp6158_set_vicap_rst_inf(struct nvp6158 *nvp6158,
971 				   struct rkmodule_vicap_reset_info rst_info)
972 {
973 	nvp6158->is_reset = rst_info.is_reset;
974 }
975 
nvp6158_set_streaming(struct nvp6158 * nvp6158,int on)976 static void nvp6158_set_streaming(struct nvp6158 *nvp6158, int on)
977 {
978 	struct i2c_client *client = nvp6158->client;
979 
980 
981 	dev_info(&client->dev, "%s: on: %d\n", __func__, on);
982 
983 	if (on) {
984 		//VDO2/VDO1 enabled VCLK_1_EN/VCLK_2_EN
985 		nvp6158_write(client, 0xFF, 0x01);
986 		nvp6158_write(client, 0xCA, 0x66);
987 	} else {
988 		//VDO2/VDO1 disable VCLK_1/VCLK_2_DISABLE
989 		nvp6158_write(client, 0xFF, 0x01);
990 		nvp6158_write(client, 0xCA, 0x00);
991 	}
992 }
993 
nvp6158_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)994 static long nvp6158_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
995 {
996 	struct nvp6158 *nvp6158 = to_nvp6158(sd);
997 	long ret = 0;
998 	u32 stream = 0;
999 
1000 	switch (cmd) {
1001 	case RKMODULE_GET_MODULE_INFO:
1002 		nvp6158_get_module_inf(nvp6158, (struct rkmodule_inf *)arg);
1003 		break;
1004 	case RKMODULE_GET_BT656_MBUS_INFO:
1005 		nvp6158_get_bt656_module_inf(nvp6158,
1006 					       (struct rkmodule_bt656_mbus_info
1007 						*)arg);
1008 		break;
1009 	case RKMODULE_GET_START_STREAM_SEQ:
1010 		if ((nvp6158->mode > BT656_4MUX) &&
1011 			(nvp6158->mode < NVP6158_DVP_MODES_END))
1012 			*(int *)arg = RKMODULE_START_STREAM_FRONT;
1013 		break;
1014 	case RKMODULE_GET_VICAP_RST_INFO:
1015 		nvp6158_get_vicap_rst_inf(nvp6158, (struct rkmodule_vicap_reset_info *)arg);
1016 		break;
1017 	case RKMODULE_SET_VICAP_RST_INFO:
1018 		nvp6158_set_vicap_rst_inf(nvp6158, *(struct rkmodule_vicap_reset_info *)arg);
1019 		break;
1020 	case RKMODULE_SET_QUICK_STREAM:
1021 		stream = *((u32 *)arg);
1022 		nvp6158_set_streaming(nvp6158, !!stream);
1023 		break;
1024 	default:
1025 		ret = -ENOTTY;
1026 		break;
1027 	}
1028 
1029 	return ret;
1030 }
1031 
1032 #ifdef CONFIG_COMPAT
nvp6158_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)1033 static long nvp6158_compat_ioctl32(struct v4l2_subdev *sd,
1034 				   unsigned int cmd, unsigned long arg)
1035 {
1036 	void __user *up = compat_ptr(arg);
1037 	struct rkmodule_inf *inf;
1038 	struct rkmodule_awb_cfg *cfg;
1039 	long ret;
1040 	struct rkmodule_bt656_mbus_info *bt565_inf;
1041 	int *seq;
1042 	struct rkmodule_vicap_reset_info *vicap_rst_inf;
1043 	u32 stream = 0;
1044 
1045 	switch (cmd) {
1046 	case RKMODULE_GET_MODULE_INFO:
1047 		inf = kzalloc(sizeof(*inf), GFP_KERNEL);
1048 		if (!inf) {
1049 			ret = -ENOMEM;
1050 			return ret;
1051 		}
1052 
1053 		ret = nvp6158_ioctl(sd, cmd, inf);
1054 		if (!ret) {
1055 			ret = copy_to_user(up, inf, sizeof(*inf));
1056 			if (ret)
1057 				ret = -EFAULT;
1058 		}
1059 		kfree(inf);
1060 		break;
1061 	case RKMODULE_AWB_CFG:
1062 		cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1063 		if (!cfg) {
1064 			ret = -ENOMEM;
1065 			return ret;
1066 		}
1067 
1068 		ret = copy_from_user(cfg, up, sizeof(*cfg));
1069 		if (!ret)
1070 			ret = nvp6158_ioctl(sd, cmd, cfg);
1071 		else
1072 			ret = -EFAULT;
1073 		kfree(cfg);
1074 		break;
1075 	case RKMODULE_GET_BT656_MBUS_INFO:
1076 		bt565_inf = kzalloc(sizeof(*bt565_inf), GFP_KERNEL);
1077 		if (!bt565_inf) {
1078 			ret = -ENOMEM;
1079 			return ret;
1080 		}
1081 
1082 		ret = nvp6158_ioctl(sd, cmd, bt565_inf);
1083 		if (!ret) {
1084 			ret = copy_to_user(up, bt565_inf, sizeof(*bt565_inf));
1085 			if (ret)
1086 				ret = -EFAULT;
1087 		}
1088 		kfree(bt565_inf);
1089 		break;
1090 	case RKMODULE_GET_START_STREAM_SEQ:
1091 		seq = kzalloc(sizeof(*seq), GFP_KERNEL);
1092 		if (!seq) {
1093 			ret = -ENOMEM;
1094 			return ret;
1095 		}
1096 		ret = nvp6158_ioctl(sd, cmd, seq);
1097 		if (!ret) {
1098 			ret = copy_to_user(up, seq, sizeof(*seq));
1099 			if (ret)
1100 				ret = -EFAULT;
1101 		}
1102 		kfree(seq);
1103 		break;
1104 	case RKMODULE_GET_VICAP_RST_INFO:
1105 		vicap_rst_inf = kzalloc(sizeof(*vicap_rst_inf), GFP_KERNEL);
1106 		if (!vicap_rst_inf) {
1107 			ret = -ENOMEM;
1108 			return ret;
1109 		}
1110 
1111 		ret = nvp6158_ioctl(sd, cmd, vicap_rst_inf);
1112 		if (!ret) {
1113 			ret = copy_to_user(up, vicap_rst_inf, sizeof(*vicap_rst_inf));
1114 			if (ret)
1115 				ret = -EFAULT;
1116 		}
1117 		kfree(vicap_rst_inf);
1118 		break;
1119 	case RKMODULE_SET_VICAP_RST_INFO:
1120 		vicap_rst_inf = kzalloc(sizeof(*vicap_rst_inf), GFP_KERNEL);
1121 		if (!vicap_rst_inf) {
1122 			ret = -ENOMEM;
1123 			return ret;
1124 		}
1125 
1126 		ret = copy_from_user(vicap_rst_inf, up, sizeof(*vicap_rst_inf));
1127 		if (!ret)
1128 			ret = nvp6158_ioctl(sd, cmd, vicap_rst_inf);
1129 		else
1130 			ret = -EFAULT;
1131 		kfree(vicap_rst_inf);
1132 		break;
1133 	case RKMODULE_SET_QUICK_STREAM:
1134 		ret = copy_from_user(&stream, up, sizeof(u32));
1135 		if (!ret)
1136 			ret = nvp6158_ioctl(sd, cmd, &stream);
1137 		else
1138 			ret = -EFAULT;
1139 		break;
1140 	default:
1141 		ret = -ENOIOCTLCMD;
1142 		break;
1143 	}
1144 
1145 	return ret;
1146 }
1147 #endif
1148 
nvp6158_runtime_resume(struct device * dev)1149 static int nvp6158_runtime_resume(struct device *dev)
1150 {
1151 	struct i2c_client *client = to_i2c_client(dev);
1152 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1153 	struct nvp6158 *nvp6158 = to_nvp6158(sd);
1154 
1155 	return __nvp6158_power_on(nvp6158);
1156 }
1157 
nvp6158_runtime_suspend(struct device * dev)1158 static int nvp6158_runtime_suspend(struct device *dev)
1159 {
1160 	struct i2c_client *client = to_i2c_client(dev);
1161 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1162 	struct nvp6158 *nvp6158 = to_nvp6158(sd);
1163 
1164 	__nvp6158_power_off(nvp6158);
1165 
1166 	return 0;
1167 }
1168 
1169 static const struct dev_pm_ops nvp6158_pm_ops = {
1170 	SET_RUNTIME_PM_OPS(nvp6158_runtime_suspend,
1171 			   nvp6158_runtime_resume, NULL)
1172 };
1173 
1174 static const struct v4l2_subdev_video_ops nvp6158_video_ops = {
1175 	.s_stream = nvp6158_stream,
1176 	.querystd = nvp6158_querystd,
1177 };
1178 
1179 static const struct v4l2_subdev_pad_ops nvp6158_subdev_pad_ops = {
1180 	.enum_mbus_code = nvp6158_enum_mbus_code,
1181 	.enum_frame_size = nvp6158_enum_frame_sizes,
1182 	.get_fmt = nvp6158_get_fmt,
1183 	.set_fmt = nvp6158_set_fmt,
1184 	.get_selection = nvp6158_get_selection,
1185 	.enum_frame_interval = nvp6158_enum_frame_interval,
1186 	.get_mbus_config = nvp6158_g_mbus_config,
1187 };
1188 
1189 static const struct v4l2_subdev_core_ops nvp6158_core_ops = {
1190 	.s_power = nvp6158_power,
1191 	.ioctl = nvp6158_ioctl,
1192 #ifdef CONFIG_COMPAT
1193 	.compat_ioctl32 = nvp6158_compat_ioctl32,
1194 #endif
1195 };
1196 
1197 static const struct v4l2_subdev_ops nvp6158_subdev_ops = {
1198 	.core = &nvp6158_core_ops,
1199 	.video = &nvp6158_video_ops,
1200 	.pad   = &nvp6158_subdev_pad_ops,
1201 };
1202 
get_dvp_mode(struct nvp6158 * nvp6158)1203 static void get_dvp_mode(struct nvp6158 *nvp6158)
1204 {
1205 	struct device *dev = &nvp6158->client->dev;
1206 	char mode[128];
1207 	u32 i;
1208 
1209 	sprintf(mode, "%s_%dMUX", nvp6158->dvp_mode, nvp6158->ch_nums);
1210 	dev_info(dev, "combined dvp mode is(%s)\n", mode);
1211 	for (i = 0; i < NVP6158_DVP_MODES_END; i++) {
1212 		if (!strcmp(mode, nvp6158_dvp_mode_lists[i]))
1213 			break;
1214 	}
1215 
1216 	if (i < NVP6158_DVP_MODES_END)
1217 		nvp6158->mode = i;
1218 	else
1219 		nvp6158->mode = BT656I_TEST_MODES;
1220 	dev_info(dev, "get dvp mode (%s)\n", nvp6158_dvp_mode_lists[nvp6158->mode]);
1221 }
1222 
nvp6158_parse_dts(struct nvp6158 * nvp6158)1223 static int nvp6158_parse_dts(struct nvp6158 *nvp6158)
1224 {
1225 	int ret;
1226 	int elem_size, elem_index;
1227 	const char *str = "";
1228 	struct property *prop;
1229 	struct nvp6158_regulator *regulator;
1230 	struct device *dev = &nvp6158->client->dev;
1231 	struct device_node *np = of_node_get(dev->of_node);
1232 
1233 	nvp6158->xvclk = devm_clk_get(dev, "xvclk");
1234 	if (IS_ERR(nvp6158->xvclk)) {
1235 		dev_err(dev, "Failed to get xvclk\n");
1236 		return -EINVAL;
1237 	}
1238 	ret = clk_set_rate(nvp6158->xvclk, NVP6158_XVCLK_FREQ);
1239 	if (ret < 0) {
1240 		dev_err(dev, "Failed to set xvclk rate (24MHz)\n");
1241 		return ret;
1242 	}
1243 	if (clk_get_rate(nvp6158->xvclk) != NVP6158_XVCLK_FREQ)
1244 		dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
1245 
1246 	nvp6158->pinctrl = devm_pinctrl_get(dev);
1247 	if (!IS_ERR(nvp6158->pinctrl)) {
1248 		nvp6158->pins_default =
1249 			pinctrl_lookup_state(nvp6158->pinctrl,
1250 					     OF_CAMERA_PINCTRL_STATE_DEFAULT);
1251 		if (IS_ERR(nvp6158->pins_default))
1252 			dev_err(dev, "could not get default pinstate\n");
1253 
1254 		nvp6158->pins_sleep =
1255 			pinctrl_lookup_state(nvp6158->pinctrl,
1256 					     OF_CAMERA_PINCTRL_STATE_SLEEP);
1257 		if (IS_ERR(nvp6158->pins_sleep))
1258 			dev_err(dev, "could not get sleep pinstate\n");
1259 	} else {
1260 		dev_err(dev, "no pinctrl\n");
1261 	}
1262 
1263 	elem_size = of_property_count_elems_of_size(
1264 		np,
1265 		OF_CAMERA_MODULE_REGULATOR_VOLTAGES,
1266 		sizeof(u32));
1267 	prop = of_find_property(
1268 		np,
1269 		OF_CAMERA_MODULE_REGULATORS,
1270 		NULL);
1271 	if (elem_size > 0 && !IS_ERR_OR_NULL(prop)) {
1272 		nvp6158->regulators.regulator =
1273 			devm_kzalloc(&nvp6158->client->dev,
1274 				     elem_size * sizeof(struct nvp6158_regulator),
1275 				     GFP_KERNEL);
1276 		if (!nvp6158->regulators.regulator)
1277 			dev_err(dev, "could not malloc nvp6158_regulator\n");
1278 
1279 		nvp6158->regulators.cnt = elem_size;
1280 
1281 		str = NULL;
1282 		elem_index = 0;
1283 		regulator = nvp6158->regulators.regulator;
1284 		if (regulator) {
1285 			do {
1286 				str = of_prop_next_string(prop, str);
1287 				if (!str) {
1288 					dev_err(dev, "%s is not match %s in dts\n",
1289 						OF_CAMERA_MODULE_REGULATORS,
1290 						OF_CAMERA_MODULE_REGULATOR_VOLTAGES);
1291 					break;
1292 				}
1293 				regulator->regulator =
1294 					devm_regulator_get_optional(dev, str);
1295 				if (IS_ERR(regulator->regulator))
1296 					dev_err(dev, "devm_regulator_get %s failed\n",
1297 						str);
1298 				of_property_read_u32_index(
1299 					np,
1300 					OF_CAMERA_MODULE_REGULATOR_VOLTAGES,
1301 					elem_index++,
1302 					&regulator->min_uV);
1303 				regulator->max_uV = regulator->min_uV;
1304 				regulator++;
1305 			} while (--elem_size);
1306 		}
1307 	}
1308 
1309 	if (of_property_read_string(np,
1310 		RK_CAMERA_MODULE_DVP_MODE,
1311 		&nvp6158->dvp_mode)) {
1312 		nvp6158->dvp_mode = NVP6158_DEFAULT_DVP_MODE;
1313 		dev_warn(dev,
1314 			"can not get module %s from dts, use default(%s)!\n",
1315 			RK_CAMERA_MODULE_DVP_MODE,
1316 			NVP6158_DEFAULT_DVP_MODE);
1317 	} else {
1318 		dev_info(dev,
1319 			"get module %s from dts, dvp mode(%s)!\n",
1320 			RK_CAMERA_MODULE_DVP_MODE, nvp6158->dvp_mode);
1321 	}
1322 
1323 	if (of_property_read_u32(np,
1324 		RK_CAMERA_MODULE_CHANNEL_NUMS,
1325 		&nvp6158->ch_nums)) {
1326 		nvp6158->ch_nums = NVP6158_DEFAULT_CHANNEL_NUMS;
1327 		dev_warn(dev,
1328 			"can not get module %s from dts, use default(%d)!\n",
1329 			RK_CAMERA_MODULE_CHANNEL_NUMS,
1330 			NVP6158_DEFAULT_CHANNEL_NUMS);
1331 	} else {
1332 		dev_info(dev,
1333 			"get module %s from dts, channel_nums(%d)!\n",
1334 			RK_CAMERA_MODULE_DVP_MODE, nvp6158->ch_nums);
1335 	}
1336 
1337 	if (of_property_read_u32(np,
1338 		RK_CAMERA_MODULE_DUAL_EDGE,
1339 		&nvp6158->dual_edge)) {
1340 		nvp6158->dual_edge = NVP6158_DEFAULT_DUAL_EDGE;
1341 		dev_warn(dev,
1342 			"can not get module %s from dts, use default(%d)!\n",
1343 			RK_CAMERA_MODULE_DUAL_EDGE,
1344 			NVP6158_DEFAULT_DUAL_EDGE);
1345 	} else {
1346 		dev_info(dev,
1347 			"get module %s from dts, dual_edge(%d)!\n",
1348 			RK_CAMERA_MODULE_DUAL_EDGE, nvp6158->dual_edge);
1349 	}
1350 
1351 
1352 
1353 	if (of_property_read_u32_array(np,
1354 		RK_CAMERA_MODULE_DEFAULT_RECT,
1355 		(unsigned int *)&nvp6158->defrect, 2)) {
1356 		nvp6158->defrect.width = NVP6158_DEFAULT_WIDTH;
1357 		nvp6158->defrect.height = NVP6158_DEFAULT_HEIGHT;
1358 		dev_warn(dev,
1359 			"can not get module %s from dts, use default wxh(%dx%d)!\n",
1360 			RK_CAMERA_MODULE_DEFAULT_RECT,
1361 			NVP6158_DEFAULT_WIDTH, NVP6158_DEFAULT_HEIGHT);
1362 	} else {
1363 		dev_info(dev,
1364 			"get module %s from dts, wxh(%dx%d)!\n",
1365 			RK_CAMERA_MODULE_DEFAULT_RECT,
1366 			nvp6158->defrect.width,
1367 			nvp6158->defrect.height);
1368 	}
1369 
1370 	/* AHD_PWR_EN */
1371 	nvp6158->pwr_gpio = devm_gpiod_get(dev, "pwr", GPIOD_OUT_LOW);
1372 	if (IS_ERR(nvp6158->pwr_gpio))
1373 		dev_warn(dev, "can not find pd-gpios, error %ld\n",
1374 			 PTR_ERR(nvp6158->pwr_gpio));
1375 	/* AHD CAM_PWR_EN*/
1376 	nvp6158->pwr2_gpio = devm_gpiod_get(dev, "pwr2", GPIOD_OUT_LOW);
1377 	if (IS_ERR(nvp6158->pwr2_gpio))
1378 		dev_warn(dev, "can not find pd2-gpios, error %ld\n",
1379 			 PTR_ERR(nvp6158->pwr2_gpio));
1380 
1381 	nvp6158->rst_gpio = devm_gpiod_get(dev, "rst", GPIOD_OUT_LOW);
1382 	if (IS_ERR(nvp6158->rst_gpio))
1383 		dev_warn(dev, "can not find rst-gpios, error %ld\n",
1384 			 PTR_ERR(nvp6158->rst_gpio));
1385 
1386 	nvp6158->rst2_gpio = devm_gpiod_get(dev, "rst2", GPIOD_OUT_LOW);
1387 	if (IS_ERR(nvp6158->rst2_gpio))
1388 		dev_warn(dev, "can not find rst2-gpios, error %ld\n",
1389 			 PTR_ERR(nvp6158->rst2_gpio));
1390 
1391 	nvp6158->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_LOW);
1392 	if (IS_ERR(nvp6158->pwdn_gpio))
1393 		dev_warn(dev, "can not find pwd-gpios, error %ld\n",
1394 			 PTR_ERR(nvp6158->pwdn_gpio));
1395 
1396 	nvp6158->pwdn2_gpio = devm_gpiod_get(dev, "pwdn2", GPIOD_OUT_LOW);
1397 	if (IS_ERR(nvp6158->pwdn2_gpio))
1398 		dev_warn(dev, "can not find pwd2-gpios, error %ld\n",
1399 			 PTR_ERR(nvp6158->pwdn2_gpio));
1400 
1401 	return 0;
1402 }
1403 
1404 
nvp6158_probe(struct i2c_client * client,const struct i2c_device_id * id)1405 static int nvp6158_probe(struct i2c_client *client,
1406 			 const struct i2c_device_id *id)
1407 {
1408 	struct device *dev = &client->dev;
1409 	struct device_node *node = dev->of_node;
1410 	struct nvp6158 *nvp6158;
1411 	struct v4l2_subdev *sd;
1412 	__maybe_unused char facing[2];
1413 	int ret, index;
1414 
1415 	dev_info(dev, "driver version: %02x.%02x.%02x",
1416 		 DRIVER_VERSION >> 16,
1417 		 (DRIVER_VERSION & 0xff00) >> 8,
1418 		 DRIVER_VERSION & 0x00ff);
1419 
1420 	nvp6158 = devm_kzalloc(dev, sizeof(*nvp6158), GFP_KERNEL);
1421 	if (!nvp6158)
1422 		return -ENOMEM;
1423 
1424 	ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
1425 				   &nvp6158->module_index);
1426 	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
1427 				       &nvp6158->module_facing);
1428 	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
1429 				       &nvp6158->module_name);
1430 	ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
1431 				       &nvp6158->len_name);
1432 	if (ret) {
1433 		dev_err(dev, "could not get %s!\n", RKMODULE_CAMERA_LENS_NAME);
1434 		return -EINVAL;
1435 	}
1436 
1437 	nvp6158->client = client;
1438 
1439 	ret = nvp6158_parse_dts(nvp6158);
1440 	if (ret) {
1441 		dev_err(dev, "Failed to analyze dts\n");
1442 		return ret;
1443 	}
1444 	get_dvp_mode(nvp6158);
1445 
1446 	mutex_init(&nvp6158->mutex);
1447 	nvp6158_get_default_format(nvp6158);
1448 
1449 	sd = &nvp6158->subdev;
1450 	v4l2_i2c_subdev_init(sd, client, &nvp6158_subdev_ops);
1451 	ret = nvp6158_initialize_controls(nvp6158);
1452 	if (ret)
1453 		goto err_destroy_mutex;
1454 
1455 	__nvp6158_power_on(nvp6158);
1456 	ret = nvp6158_init(i2c_adapter_id(client->adapter));
1457 	if (ret) {
1458 		dev_err(dev, "Failed to init nvp6158\n");
1459 		goto err_power_off;
1460 	}
1461 
1462 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1463 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1464 #endif
1465 
1466 #if defined(CONFIG_MEDIA_CONTROLLER)
1467 	for (index = 0; index < nvp6158->ch_nums; index++)
1468 		nvp6158->pad[index].flags = MEDIA_PAD_FL_SOURCE;
1469 	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1470 	ret = media_entity_pads_init(&sd->entity, nvp6158->ch_nums, nvp6158->pad);
1471 	if (ret < 0)
1472 		goto err_power_off;
1473 #endif
1474 
1475 	memset(facing, 0, sizeof(facing));
1476 	if (strcmp(nvp6158->module_facing, "back") == 0)
1477 		facing[0] = 'b';
1478 	else
1479 		facing[0] = 'f';
1480 
1481 	snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
1482 		 nvp6158->module_index, facing,
1483 		 NVP6158_NAME, dev_name(sd->dev));
1484 
1485 	ret = v4l2_async_register_subdev_sensor_common(sd);
1486 	if (ret) {
1487 		dev_err(dev, "v4l2 async register subdev failed\n");
1488 		goto err_clean_entity;
1489 	}
1490 	pm_runtime_set_active(dev);
1491 	pm_runtime_enable(dev);
1492 	pm_runtime_idle(dev);
1493 #ifdef WORK_QUEUE
1494 	/* init work_queue for state_check */
1495 	INIT_DELAYED_WORK(&nvp6158->plug_state_check.d_work, nvp6158_plug_state_check_work);
1496 	nvp6158->plug_state_check.state_check_wq =
1497 		create_singlethread_workqueue("nvp6158_work_queue");
1498 	if (nvp6158->plug_state_check.state_check_wq == NULL) {
1499 		dev_err(dev, "%s(%d): %s create failed.\n", __func__, __LINE__,
1500 			  "nvp6158_work_queue");
1501 	}
1502 	nvp6158->cur_detect_status = 0x0;
1503 	nvp6158->last_detect_status = 0x0;
1504 	nvp6158->hot_plug = false;
1505 	nvp6158->is_reset = 0;
1506 
1507 #endif
1508 	return 0;
1509 
1510 err_clean_entity:
1511 #if defined(CONFIG_MEDIA_CONTROLLER)
1512 	media_entity_cleanup(&sd->entity);
1513 #endif
1514 err_power_off:
1515 	__nvp6158_power_off(nvp6158);
1516 err_destroy_mutex:
1517 	mutex_destroy(&nvp6158->mutex);
1518 
1519 	return ret;
1520 }
1521 
nvp6158_remove(struct i2c_client * client)1522 static int nvp6158_remove(struct i2c_client *client)
1523 {
1524 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1525 	struct nvp6158 *nvp6158 = to_nvp6158(sd);
1526 
1527 	nvp6158_exit();
1528 	v4l2_ctrl_handler_free(&nvp6158->ctrl_handler);
1529 	mutex_destroy(&nvp6158->mutex);
1530 
1531 	pm_runtime_disable(&client->dev);
1532 	if (!pm_runtime_status_suspended(&client->dev))
1533 		__nvp6158_power_off(nvp6158);
1534 	pm_runtime_set_suspended(&client->dev);
1535 #ifdef WORK_QUEUE
1536 	if (nvp6158->plug_state_check.state_check_wq != NULL)
1537 		destroy_workqueue(nvp6158->plug_state_check.state_check_wq);
1538 #endif
1539 	return 0;
1540 }
1541 
1542 #if IS_ENABLED(CONFIG_OF)
1543 static const struct of_device_id nvp6158_of_match[] = {
1544 	{ .compatible = "nvp6158-v4l2" },
1545 	{},
1546 };
1547 MODULE_DEVICE_TABLE(of, nvp6158_of_match);
1548 #endif
1549 
1550 static const struct i2c_device_id nvp6158_match_id[] = {
1551 	{ "nvp6158-v4l2", 0 },
1552 	{ },
1553 };
1554 
1555 static struct i2c_driver nvp6158_i2c_driver = {
1556 	.driver = {
1557 		.name = NVP6158_NAME,
1558 		.pm = &nvp6158_pm_ops,
1559 		.of_match_table = of_match_ptr(nvp6158_of_match),
1560 	},
1561 	.probe		= &nvp6158_probe,
1562 	.remove		= &nvp6158_remove,
1563 	.id_table	= nvp6158_match_id,
1564 };
1565 
sensor_mod_init(void)1566 static int __init sensor_mod_init(void)
1567 {
1568 	return i2c_add_driver(&nvp6158_i2c_driver);
1569 }
1570 
sensor_mod_exit(void)1571 static void __exit sensor_mod_exit(void)
1572 {
1573 	i2c_del_driver(&nvp6158_i2c_driver);
1574 }
1575 
1576 device_initcall_sync(sensor_mod_init);
1577 module_exit(sensor_mod_exit);
1578 
1579 MODULE_DESCRIPTION("nvp6158 sensor driver");
1580 MODULE_LICENSE("GPL v2");
1581