xref: /OK3568_Linux_fs/kernel/drivers/media/i2c/jaguar1_drv/jaguar1_v4l2.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * jaguar1 driver
4  * V0.0X01.0X00 first version.
5  * V0.0X01.0X01 fix kernel5.10 compile error.
6  *
7  */
8 
9 #include <linux/clk.h>
10 #include <linux/device.h>
11 #include <linux/delay.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/sysfs.h>
18 #include <media/media-entity.h>
19 #include <media/v4l2-async.h>
20 #include <media/v4l2-ctrls.h>
21 #include <media/v4l2-subdev.h>
22 #include <media/v4l2-fwnode.h>
23 #include <linux/pinctrl/consumer.h>
24 #include <linux/of.h>
25 #include <linux/of_device.h>
26 #include <linux/of_graph.h>
27 #include <linux/of_platform.h>
28 #include <linux/of_gpio.h>
29 #include <linux/mfd/syscon.h>
30 #include <linux/version.h>
31 #include <linux/rk-camera-module.h>
32 #include <linux/rk-preisp.h>
33 
34 #include "jaguar1_common.h"
35 #include "jaguar1_video.h"
36 #include "jaguar1_coax_protocol.h"
37 #include "jaguar1_motion.h"
38 #include "jaguar1_ioctl.h"
39 #include "jaguar1_video_eq.h"
40 #include "jaguar1_mipi.h"
41 #include "jaguar1_drv.h"
42 #include "jaguar1_v4l2.h"
43 
44 #define DRIVER_VERSION				KERNEL_VERSION(0, 0x01, 0x1)
45 
46 #ifndef V4L2_CID_DIGITAL_GAIN
47 #define V4L2_CID_DIGITAL_GAIN			V4L2_CID_GAIN
48 #endif
49 
50 #define JAGUAR1_XVCLK_FREQ			24000000
51 #define JAGUAR1_LINK_FREQ			320000000
52 #define JAGUAR1_LANES			4
53 #define JAGUAR1_BITS_PER_SAMPLE		8
54 
55 /* pixel rate = link frequency * 2 * lanes / BITS_PER_SAMPLE */
56 #define JAGUAR1_PIXEL_RATE \
57 	(JAGUAR1_LINK_FREQ * 2 / JAGUAR1_BITS_PER_SAMPLE * JAGUAR1_LANES)
58 
59 #define OF_CAMERA_PINCTRL_STATE_DEFAULT		"rockchip,camera_default"
60 #define OF_CAMERA_PINCTRL_STATE_SLEEP		"rockchip,camera_sleep"
61 
62 #define OF_CAMERA_MODULE_REGULATORS		"rockchip,regulator-names"
63 #define OF_CAMERA_MODULE_REGULATOR_VOLTAGES	"rockchip,regulator-voltages"
64 #define RK_CAMERA_MODULE_DEFAULT_RECT		"rockchip,default_rect"
65 
66 #define JAGUAR1_DEFAULT_WIDTH		1920
67 #define JAGUAR1_DEFAULT_HEIGHT		1080
68 
69 #define JAGUAR1_NAME				"jaguar1"
70 
71 /* #define FORCE_720P */
72 
73 struct jaguar1_gpio {
74 	int pltfrm_gpio;
75 	const char *label;
76 	enum of_gpio_flags active_low;
77 };
78 
79 struct jaguar1_regulator {
80 	struct regulator *regulator;
81 	u32 min_uV;
82 	u32 max_uV;
83 };
84 
85 struct jaguar1_regulators {
86 	u32 cnt;
87 	struct jaguar1_regulator *regulator;
88 };
89 
90 struct jaguar1_pixfmt {
91 	u32 code;
92 };
93 
94 struct jaguar1_framesize {
95 	u16 width;
96 	u16 height;
97 	struct v4l2_fract max_fps;
98 	enum NC_VIVO_CH_FORMATDEF fmt_idx;
99 	__u32 field;
100 };
101 
102 struct jaguar1_default_rect {
103 	unsigned int width;
104 	unsigned int height;
105 };
106 
107 struct jaguar1 {
108 	struct i2c_client	*client;
109 	struct clk		*xvclk;
110 	struct gpio_desc	*rst_gpio;
111 	struct gpio_desc	*rst2_gpio;
112 	struct gpio_desc	*pd_gpio;
113 	struct gpio_desc	*pd2_gpio;
114 	struct gpio_desc	*pwd_gpio;
115 	struct gpio_desc	*pwd2_gpio;
116 
117 	struct pinctrl		*pinctrl;
118 	struct pinctrl_state	*pins_default;
119 	struct pinctrl_state	*pins_sleep;
120 
121 	struct v4l2_subdev	subdev;
122 	struct media_pad	pad[PAD_MAX];
123 	struct v4l2_ctrl_handler ctrl_handler;
124 	struct mutex		mutex;
125 	bool			power_on;
126 	struct jaguar1_regulators regulators;
127 
128 	u32			module_index;
129 	const char		*module_facing;
130 	const char		*module_name;
131 	const char		*len_name;
132 
133 	struct v4l2_mbus_framefmt format;
134 	const struct jaguar1_framesize *frame_size;
135 	int streaming;
136 	struct jaguar1_default_rect defrect;
137 };
138 
139 #define to_jaguar1(sd) container_of(sd, struct jaguar1, subdev)
140 
141 static const struct jaguar1_framesize jaguar1_framesizes[] = {
142 #if defined CONFIG_VIDEO_ROCKCHIP_USBACM_CONTROL
143 	{
144 		.width		= 2560,
145 		.height		= 1440,
146 		.max_fps = {
147 			.numerator = 10000,
148 			.denominator = 250000,
149 		},
150 		.fmt_idx	= AHD20_720P_25P,
151 	}
152 #else
153 	{
154 		.width		= 960,
155 		.height		= 480,
156 		.max_fps = {
157 			.numerator = 10000,
158 			.denominator = 250000,
159 		},
160 		.fmt_idx	= AHD20_SD_H960_2EX_Btype_NT,
161 		.field = V4L2_FIELD_INTERLACED,
162 	},
163 	{
164 		.width		= 960,
165 		.height		= 576,
166 		.max_fps = {
167 			.numerator = 10000,
168 			.denominator = 300000,
169 		},
170 		.fmt_idx	= AHD20_SD_H960_2EX_Btype_PAL,
171 		.field = V4L2_FIELD_INTERLACED,
172 	},
173 	{
174 		.width		= 1280,
175 		.height		= 720,
176 		.max_fps = {
177 			.numerator = 10000,
178 			.denominator = 250000,
179 		},
180 		.fmt_idx	= AHD20_720P_25P_EX_Btype,
181 		.field = V4L2_FIELD_NONE
182 	},
183 	{
184 		.width		= 1920,
185 		.height		= 1080,
186 		.max_fps = {
187 			.numerator = 10000,
188 			.denominator = 250000,
189 		},
190 		.fmt_idx	= AHD20_1080P_25P,
191 		.field = V4L2_FIELD_NONE
192 	},
193 	{
194 		.width		= 2560,
195 		.height		= 1440,
196 		.max_fps = {
197 			.numerator = 10000,
198 			.denominator = 250000,
199 		},
200 		.fmt_idx	= AHD20_720P_25P,
201 		.field = V4L2_FIELD_NONE
202 	}
203 #endif
204 };
205 
206 static const struct jaguar1_pixfmt jaguar1_formats[] = {
207 	{
208 		.code = MEDIA_BUS_FMT_UYVY8_2X8
209 	},
210 };
211 
212 static const s64 link_freq_menu_items[] = {
213 	JAGUAR1_LINK_FREQ
214 };
__jaguar1_power_on(struct jaguar1 * jaguar1)215 static int __jaguar1_power_on(struct jaguar1 *jaguar1)
216 {
217 	u32 i;
218 	int ret;
219 	struct jaguar1_regulator *regulator;
220 	struct device *dev = &jaguar1->client->dev;
221 
222 	if (!IS_ERR_OR_NULL(jaguar1->pins_default)) {
223 		ret = pinctrl_select_state(jaguar1->pinctrl,
224 					   jaguar1->pins_default);
225 		if (ret < 0)
226 			dev_err(dev, "could not set pins. ret=%d\n", ret);
227 	}
228 
229 	ret = clk_prepare_enable(jaguar1->xvclk);
230 	if (ret < 0) {
231 		dev_err(dev, "Failed to enable xvclk\n");
232 		return ret;
233 	}
234 
235 	if (jaguar1->regulators.regulator) {
236 		for (i = 0; i < jaguar1->regulators.cnt; i++) {
237 			regulator = jaguar1->regulators.regulator + i;
238 			if (IS_ERR(regulator->regulator))
239 				continue;
240 			regulator_set_voltage(
241 				regulator->regulator,
242 				regulator->min_uV,
243 				regulator->max_uV);
244 			if (regulator_enable(regulator->regulator)) {
245 				dev_err(dev,
246 					"regulator_enable failed!\n");
247 				goto disable_clk;
248 			}
249 		}
250 	}
251 	usleep_range(3000, 5000);
252 
253 	if (!IS_ERR(jaguar1->pwd_gpio)) {
254 		gpiod_direction_output(jaguar1->pwd_gpio, 1);
255 		usleep_range(3000, 5000);
256 	}
257 
258 	if (!IS_ERR(jaguar1->pwd2_gpio)) {
259 		gpiod_direction_output(jaguar1->pwd2_gpio, 1);
260 		usleep_range(3000, 5000);
261 	}
262 
263 	if (!IS_ERR(jaguar1->pd_gpio)) {
264 		gpiod_direction_output(jaguar1->pd_gpio, 1);
265 		usleep_range(1500, 2000);
266 	}
267 
268 	if (!IS_ERR(jaguar1->pd2_gpio)) {
269 		gpiod_direction_output(jaguar1->pd2_gpio, 1);
270 		usleep_range(1500, 2000);
271 	}
272 
273 	if (!IS_ERR(jaguar1->rst_gpio)) {
274 		gpiod_direction_output(jaguar1->rst_gpio, 0);
275 		usleep_range(1500, 2000);
276 		gpiod_direction_output(jaguar1->rst_gpio, 1);
277 	}
278 
279 	if (!IS_ERR(jaguar1->rst2_gpio)) {
280 		gpiod_direction_output(jaguar1->rst2_gpio, 0);
281 		usleep_range(1500, 2000);
282 		gpiod_direction_output(jaguar1->rst2_gpio, 1);
283 	}
284 
285 	return 0;
286 
287 disable_clk:
288 	clk_disable_unprepare(jaguar1->xvclk);
289 
290 	return ret;
291 }
292 
__jaguar1_power_off(struct jaguar1 * jaguar1)293 static void __jaguar1_power_off(struct jaguar1 *jaguar1)
294 {
295 	u32 i;
296 	int ret;
297 	struct jaguar1_regulator *regulator;
298 	struct device *dev = &jaguar1->client->dev;
299 
300 	if (!IS_ERR(jaguar1->pd_gpio))
301 		gpiod_direction_output(jaguar1->pd_gpio, 0);
302 
303 	if (!IS_ERR(jaguar1->pd2_gpio))
304 		gpiod_direction_output(jaguar1->pd2_gpio, 0);
305 
306 	clk_disable_unprepare(jaguar1->xvclk);
307 
308 	if (!IS_ERR(jaguar1->rst_gpio))
309 		gpiod_direction_output(jaguar1->rst_gpio, 0);
310 
311 	if (!IS_ERR(jaguar1->rst2_gpio))
312 		gpiod_direction_output(jaguar1->rst2_gpio, 0);
313 
314 	if (!IS_ERR(jaguar1->pwd_gpio))
315 		gpiod_direction_output(jaguar1->pwd_gpio, 0);
316 
317 	if (!IS_ERR(jaguar1->pwd2_gpio))
318 		gpiod_direction_output(jaguar1->pwd2_gpio, 0);
319 
320 	if (!IS_ERR_OR_NULL(jaguar1->pins_sleep)) {
321 		ret = pinctrl_select_state(jaguar1->pinctrl,
322 					   jaguar1->pins_sleep);
323 		if (ret < 0)
324 			dev_err(dev, "could not set pins\n");
325 	}
326 
327 	if (jaguar1->regulators.regulator) {
328 		for (i = 0; i < jaguar1->regulators.cnt; i++) {
329 			regulator = jaguar1->regulators.regulator + i;
330 			if (IS_ERR(regulator->regulator))
331 				continue;
332 			regulator_disable(regulator->regulator);
333 		}
334 	}
335 }
336 
jaguar1_power(struct v4l2_subdev * sd,int on)337 static int jaguar1_power(struct v4l2_subdev *sd, int on)
338 {
339 	struct i2c_client *client = v4l2_get_subdevdata(sd);
340 	struct jaguar1 *jaguar1 = to_jaguar1(sd);
341 	int ret = 0;
342 
343 	dev_dbg(&client->dev, "%s: on %d\n", __func__, on);
344 	mutex_lock(&jaguar1->mutex);
345 
346 	/* If the power state is not modified - no work to do. */
347 	if (jaguar1->power_on == !!on)
348 		goto exit;
349 
350 	if (on) {
351 		ret = __jaguar1_power_on(jaguar1);
352 		if (ret < 0)
353 			goto exit;
354 
355 		jaguar1->power_on = true;
356 	} else {
357 		__jaguar1_power_off(jaguar1);
358 		jaguar1->power_on = false;
359 	}
360 
361 exit:
362 	mutex_unlock(&jaguar1->mutex);
363 
364 	return ret;
365 }
366 
jaguar1_initialize_controls(struct jaguar1 * jaguar1)367 static int jaguar1_initialize_controls(struct jaguar1 *jaguar1)
368 {
369 	struct v4l2_ctrl_handler *handler;
370 	struct v4l2_ctrl *ctrl;
371 	int ret;
372 
373 	handler = &jaguar1->ctrl_handler;
374 	ret = v4l2_ctrl_handler_init(handler, 2);
375 	if (ret)
376 		return ret;
377 	handler->lock = &jaguar1->mutex;
378 
379 	ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
380 				      0, 0, link_freq_menu_items);
381 	if (ctrl)
382 		ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
383 
384 	v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
385 			  0, JAGUAR1_PIXEL_RATE, 1, JAGUAR1_PIXEL_RATE);
386 
387 	if (handler->error) {
388 		ret = handler->error;
389 		dev_err(&jaguar1->client->dev,
390 			"Failed to init controls(%d)\n", ret);
391 		goto err_free_handler;
392 	}
393 
394 	jaguar1->subdev.ctrl_handler = handler;
395 
396 	return 0;
397 
398 err_free_handler:
399 	v4l2_ctrl_handler_free(handler);
400 
401 	return ret;
402 }
jaguar1_get_default_format(struct jaguar1 * jaguar1)403 static void jaguar1_get_default_format(struct jaguar1 *jaguar1)
404 {
405 	const struct jaguar1_framesize *fsize = &jaguar1_framesizes[0];
406 	const struct jaguar1_framesize *match = NULL;
407 	int i = ARRAY_SIZE(jaguar1_framesizes);
408 	unsigned int min_err = UINT_MAX;
409 	struct v4l2_mbus_framefmt *format = &jaguar1->format;
410 	struct jaguar1_default_rect *rect =  &jaguar1->defrect;
411 
412 	while (i--) {
413 		unsigned int err = abs(fsize->width - rect->width)
414 				+ abs(fsize->height - rect->height);
415 		if (err < min_err) {
416 			min_err = err;
417 			match = fsize;
418 		}
419 		fsize++;
420 	}
421 
422 	if (!match)
423 		match = &jaguar1_framesizes[0];
424 
425 	format->width = match->width;
426 	format->height = match->height;
427 	format->colorspace = V4L2_COLORSPACE_SRGB;
428 	format->code = jaguar1_formats[0].code;
429 	jaguar1->frame_size = match;
430 	format->field = match->field;
431 }
432 
jaguar1_stream(struct v4l2_subdev * sd,int on)433 static int jaguar1_stream(struct v4l2_subdev *sd, int on)
434 {
435 	struct i2c_client *client = v4l2_get_subdevdata(sd);
436 	struct jaguar1 *jaguar1 = to_jaguar1(sd);
437 	video_init_all video_init;
438 	enum NC_VIVO_CH_FORMATDEF fmt_idx;
439 	int ch;
440 
441 	dev_dbg(&client->dev, "%s: on %d\n", __func__, on);
442 	mutex_lock(&jaguar1->mutex);
443 	on = !!on;
444 
445 	if (jaguar1->streaming == on)
446 		goto unlock;
447 
448 	if (on) {
449 		jaguar1_set_mclk(JAGUAR1_MCLK_1242MHZ);
450 		fmt_idx = jaguar1->frame_size->fmt_idx;
451 		for (ch = 0; ch < 4; ch++) {
452 			video_init.ch_param[ch].ch = ch;
453 			video_init.ch_param[ch].format = fmt_idx;
454 			video_init.ch_param[ch].input = SINGLE_ENDED;
455 			video_init.ch_param[ch].interface = YUV_422;
456 		}
457 		jaguar1_start(&video_init);
458 	} else {
459 		jaguar1_stop();
460 	}
461 
462 	jaguar1->streaming = on;
463 
464 unlock:
465 	mutex_unlock(&jaguar1->mutex);
466 
467 	return 0;
468 }
469 
jaguar1_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)470 static int jaguar1_enum_mbus_code(struct v4l2_subdev *sd,
471 				 struct v4l2_subdev_pad_config *cfg,
472 				 struct v4l2_subdev_mbus_code_enum *code)
473 {
474 	if (code->index >= ARRAY_SIZE(jaguar1_formats))
475 		return -EINVAL;
476 
477 	code->code = jaguar1_formats[code->index].code;
478 
479 	return 0;
480 }
481 
jaguar1_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)482 static int jaguar1_enum_frame_sizes(struct v4l2_subdev *sd,
483 				   struct v4l2_subdev_pad_config *cfg,
484 				   struct v4l2_subdev_frame_size_enum *fse)
485 {
486 	struct i2c_client *client = v4l2_get_subdevdata(sd);
487 	int i = ARRAY_SIZE(jaguar1_formats);
488 
489 	dev_dbg(&client->dev, "%s:\n", __func__);
490 
491 	if (fse->index >= ARRAY_SIZE(jaguar1_framesizes))
492 		return -EINVAL;
493 
494 	while (--i)
495 		if (fse->code == jaguar1_formats[i].code)
496 			break;
497 
498 	fse->code = jaguar1_formats[i].code;
499 
500 	fse->min_width  = jaguar1_framesizes[fse->index].width;
501 	fse->max_width  = fse->min_width;
502 	fse->max_height = jaguar1_framesizes[fse->index].height;
503 	fse->min_height = fse->max_height;
504 
505 	return 0;
506 }
507 
jaguar1_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_config * cfg)508 static int jaguar1_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
509 				 struct v4l2_mbus_config *cfg)
510 {
511 	cfg->type = V4L2_MBUS_CSI2_DPHY;
512 	cfg->flags = V4L2_MBUS_CSI2_4_LANE |
513 		     V4L2_MBUS_CSI2_CHANNELS;
514 
515 	return 0;
516 }
517 
jaguar1_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_interval_enum * fie)518 static int jaguar1_enum_frame_interval(struct v4l2_subdev *sd,
519 		struct v4l2_subdev_pad_config *cfg,
520 		struct v4l2_subdev_frame_interval_enum *fie)
521 {
522 	if (fie->index >= ARRAY_SIZE(jaguar1_framesizes))
523 		return -EINVAL;
524 
525 	fie->code = jaguar1_formats[0].code;
526 
527 	fie->width = jaguar1_framesizes[fie->index].width;
528 	fie->height = jaguar1_framesizes[fie->index].height;
529 	fie->interval = jaguar1_framesizes[fie->index].max_fps;
530 
531 	return 0;
532 }
533 
jaguar1_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)534 static int jaguar1_get_fmt(struct v4l2_subdev *sd,
535 			  struct v4l2_subdev_pad_config *cfg,
536 			  struct v4l2_subdev_format *fmt)
537 {
538 	struct i2c_client *client = v4l2_get_subdevdata(sd);
539 	struct jaguar1 *jaguar1 = to_jaguar1(sd);
540 
541 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
542 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
543 		struct v4l2_mbus_framefmt *mf;
544 
545 		mf = v4l2_subdev_get_try_format(sd, cfg, 0);
546 		mutex_lock(&jaguar1->mutex);
547 		fmt->format = *mf;
548 		mutex_unlock(&jaguar1->mutex);
549 		return 0;
550 #else
551 	return -ENOTTY;
552 #endif
553 	}
554 
555 	mutex_lock(&jaguar1->mutex);
556 	fmt->format = jaguar1->format;
557 	mutex_unlock(&jaguar1->mutex);
558 
559 	dev_dbg(&client->dev, "%s: %x %dx%d\n", __func__,
560 		jaguar1->format.code, jaguar1->format.width,
561 		jaguar1->format.height);
562 
563 	return 0;
564 }
565 
__jaguar1_try_frame_size(struct v4l2_mbus_framefmt * mf,const struct jaguar1_framesize ** size)566 static void __jaguar1_try_frame_size(struct v4l2_mbus_framefmt *mf,
567 				    const struct jaguar1_framesize **size)
568 {
569 	const struct jaguar1_framesize *fsize = &jaguar1_framesizes[0];
570 	const struct jaguar1_framesize *match = NULL;
571 	int i = ARRAY_SIZE(jaguar1_framesizes);
572 	unsigned int min_err = UINT_MAX;
573 
574 	while (i--) {
575 		unsigned int err = abs(fsize->width - mf->width)
576 				+ abs(fsize->height - mf->height);
577 		if (err < min_err) {
578 			min_err = err;
579 			match = fsize;
580 		}
581 		fsize++;
582 	}
583 
584 	if (!match)
585 		match = &jaguar1_framesizes[0];
586 
587 	mf->width  = match->width;
588 	mf->height = match->height;
589 
590 	if (size)
591 		*size = match;
592 }
593 
jaguar1_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)594 static int jaguar1_set_fmt(struct v4l2_subdev *sd,
595 			  struct v4l2_subdev_pad_config *cfg,
596 			  struct v4l2_subdev_format *fmt)
597 {
598 	int index = ARRAY_SIZE(jaguar1_formats);
599 	struct v4l2_mbus_framefmt *mf = &fmt->format;
600 	const struct jaguar1_framesize *size = NULL;
601 	struct jaguar1 *jaguar1 = to_jaguar1(sd);
602 	int ret = 0;
603 
604 	__jaguar1_try_frame_size(mf, &size);
605 
606 	while (--index >= 0)
607 		if (jaguar1_formats[index].code == mf->code)
608 			break;
609 
610 	if (index < 0)
611 		return -EINVAL;
612 
613 	mf->colorspace = V4L2_COLORSPACE_SRGB;
614 	mf->code = jaguar1_formats[index].code;
615 	mf->field = V4L2_FIELD_NONE;
616 
617 	mutex_lock(&jaguar1->mutex);
618 
619 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
620 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
621 		mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
622 		*mf = fmt->format;
623 #else
624 		return -ENOTTY;
625 #endif
626 	} else {
627 		if (jaguar1->streaming) {
628 			mutex_unlock(&jaguar1->mutex);
629 			return -EBUSY;
630 		}
631 
632 		jaguar1->frame_size = size;
633 		jaguar1->format = fmt->format;
634 	}
635 
636 	mutex_unlock(&jaguar1->mutex);
637 	return ret;
638 }
639 
jaguar1_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)640 static int jaguar1_g_frame_interval(struct v4l2_subdev *sd,
641 				    struct v4l2_subdev_frame_interval *fi)
642 {
643 	struct jaguar1 *jaguar1 = to_jaguar1(sd);
644 	const struct jaguar1_framesize *size = jaguar1->frame_size;
645 
646 	mutex_lock(&jaguar1->mutex);
647 	fi->interval = size->max_fps;
648 	mutex_unlock(&jaguar1->mutex);
649 
650 	return 0;
651 }
652 
jaguar1_get_module_inf(struct jaguar1 * jaguar1,struct rkmodule_inf * inf)653 static void jaguar1_get_module_inf(struct jaguar1 *jaguar1,
654 				   struct rkmodule_inf *inf)
655 {
656 	memset(inf, 0, sizeof(*inf));
657 	strlcpy(inf->base.sensor, JAGUAR1_NAME, sizeof(inf->base.sensor));
658 	strlcpy(inf->base.module, jaguar1->module_name,
659 		sizeof(inf->base.module));
660 	strlcpy(inf->base.lens, jaguar1->len_name, sizeof(inf->base.lens));
661 }
662 
jaguar1_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)663 static long jaguar1_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
664 {
665 	struct jaguar1 *jaguar1 = to_jaguar1(sd);
666 	long ret = 0;
667 
668 	switch (cmd) {
669 	case RKMODULE_GET_MODULE_INFO:
670 		jaguar1_get_module_inf(jaguar1, (struct rkmodule_inf *)arg);
671 		break;
672 	case RKMODULE_GET_START_STREAM_SEQ:
673 		*(int *)arg = RKMODULE_START_STREAM_FRONT;
674 		break;
675 	default:
676 		ret = -ENOTTY;
677 		break;
678 	}
679 
680 	return ret;
681 }
682 
683 #ifdef CONFIG_COMPAT
jaguar1_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)684 static long jaguar1_compat_ioctl32(struct v4l2_subdev *sd,
685 				   unsigned int cmd, unsigned long arg)
686 {
687 	void __user *up = compat_ptr(arg);
688 	struct rkmodule_inf *inf;
689 	struct rkmodule_awb_cfg *cfg;
690 	long ret;
691 	int *seq;
692 
693 	switch (cmd) {
694 	case RKMODULE_GET_MODULE_INFO:
695 		inf = kzalloc(sizeof(*inf), GFP_KERNEL);
696 		if (!inf) {
697 			ret = -ENOMEM;
698 			return ret;
699 		}
700 
701 		ret = jaguar1_ioctl(sd, cmd, inf);
702 		if (!ret) {
703 			ret = copy_to_user(up, inf, sizeof(*inf));
704 			if (ret)
705 				ret = -EFAULT;
706 		}
707 		kfree(inf);
708 		break;
709 	case RKMODULE_AWB_CFG:
710 		cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
711 		if (!cfg) {
712 			ret = -ENOMEM;
713 			return ret;
714 		}
715 
716 		ret = copy_from_user(cfg, up, sizeof(*cfg));
717 		if (!ret)
718 			ret = jaguar1_ioctl(sd, cmd, cfg);
719 		else
720 			ret = -EFAULT;
721 		kfree(cfg);
722 		break;
723 	case RKMODULE_GET_START_STREAM_SEQ:
724 		seq = kzalloc(sizeof(*seq), GFP_KERNEL);
725 		if (!seq) {
726 			ret = -ENOMEM;
727 			return ret;
728 		}
729 
730 		ret = jaguar1_ioctl(sd, cmd, seq);
731 		if (!ret) {
732 			ret = copy_to_user(up, seq, sizeof(*seq));
733 			if (ret)
734 				ret = -EFAULT;
735 		}
736 		kfree(seq);
737 		break;
738 	default:
739 		ret = -ENOIOCTLCMD;
740 		break;
741 	}
742 
743 	return ret;
744 }
745 #endif
746 
jaguar1_runtime_resume(struct device * dev)747 static int jaguar1_runtime_resume(struct device *dev)
748 {
749 	struct i2c_client *client = to_i2c_client(dev);
750 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
751 	struct jaguar1 *jaguar1 = to_jaguar1(sd);
752 
753 	return __jaguar1_power_on(jaguar1);
754 }
755 
jaguar1_runtime_suspend(struct device * dev)756 static int jaguar1_runtime_suspend(struct device *dev)
757 {
758 	struct i2c_client *client = to_i2c_client(dev);
759 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
760 	struct jaguar1 *jaguar1 = to_jaguar1(sd);
761 
762 	__jaguar1_power_off(jaguar1);
763 
764 	return 0;
765 }
766 
767 static const struct dev_pm_ops jaguar1_pm_ops = {
768 	SET_RUNTIME_PM_OPS(jaguar1_runtime_suspend,
769 			   jaguar1_runtime_resume, NULL)
770 };
771 
772 static const struct v4l2_subdev_video_ops jaguar1_video_ops = {
773 	.s_stream = jaguar1_stream,
774 	.g_frame_interval = jaguar1_g_frame_interval,
775 };
776 
777 static const struct v4l2_subdev_pad_ops jaguar1_subdev_pad_ops = {
778 	.enum_mbus_code = jaguar1_enum_mbus_code,
779 	.enum_frame_size = jaguar1_enum_frame_sizes,
780 	.enum_frame_interval = jaguar1_enum_frame_interval,
781 	.get_fmt = jaguar1_get_fmt,
782 	.set_fmt = jaguar1_set_fmt,
783 	.get_mbus_config = jaguar1_g_mbus_config,
784 };
785 
786 static const struct v4l2_subdev_core_ops jaguar1_core_ops = {
787 	.s_power = jaguar1_power,
788 	.ioctl = jaguar1_ioctl,
789 #ifdef CONFIG_COMPAT
790 	.compat_ioctl32 = jaguar1_compat_ioctl32,
791 #endif
792 };
793 
794 static const struct v4l2_subdev_ops jaguar1_subdev_ops = {
795 	.core = &jaguar1_core_ops,
796 	.video = &jaguar1_video_ops,
797 	.pad   = &jaguar1_subdev_pad_ops,
798 };
799 
jaguar1_analyze_dts(struct jaguar1 * jaguar1)800 static int jaguar1_analyze_dts(struct jaguar1 *jaguar1)
801 {
802 	int ret;
803 	int elem_size, elem_index;
804 	const char *str = "";
805 	struct property *prop;
806 	struct jaguar1_regulator *regulator;
807 	struct device *dev = &jaguar1->client->dev;
808 	struct device_node *np = of_node_get(dev->of_node);
809 
810 	jaguar1->xvclk = devm_clk_get(dev, "xvclk");
811 	if (IS_ERR(jaguar1->xvclk)) {
812 		dev_err(dev, "Failed to get xvclk\n");
813 		return -EINVAL;
814 	}
815 	ret = clk_set_rate(jaguar1->xvclk, JAGUAR1_XVCLK_FREQ);
816 	if (ret < 0) {
817 		dev_err(dev, "Failed to set xvclk rate (24MHz)\n");
818 		return ret;
819 	}
820 	if (clk_get_rate(jaguar1->xvclk) != JAGUAR1_XVCLK_FREQ)
821 		dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
822 
823 	jaguar1->pinctrl = devm_pinctrl_get(dev);
824 	if (!IS_ERR(jaguar1->pinctrl)) {
825 		jaguar1->pins_default =
826 			pinctrl_lookup_state(jaguar1->pinctrl,
827 					     OF_CAMERA_PINCTRL_STATE_DEFAULT);
828 		if (IS_ERR(jaguar1->pins_default))
829 			dev_err(dev, "could not get default pinstate\n");
830 
831 		jaguar1->pins_sleep =
832 			pinctrl_lookup_state(jaguar1->pinctrl,
833 					     OF_CAMERA_PINCTRL_STATE_SLEEP);
834 		if (IS_ERR(jaguar1->pins_sleep))
835 			dev_err(dev, "could not get sleep pinstate\n");
836 	} else {
837 		dev_err(dev, "no pinctrl\n");
838 	}
839 
840 	elem_size = of_property_count_elems_of_size(
841 		np,
842 		OF_CAMERA_MODULE_REGULATOR_VOLTAGES,
843 		sizeof(u32));
844 	prop = of_find_property(
845 		np,
846 		OF_CAMERA_MODULE_REGULATORS,
847 		NULL);
848 	if (elem_size > 0 && !IS_ERR_OR_NULL(prop)) {
849 		jaguar1->regulators.regulator =
850 			devm_kzalloc(&jaguar1->client->dev,
851 				     elem_size * sizeof(struct jaguar1_regulator),
852 				     GFP_KERNEL);
853 		if (!jaguar1->regulators.regulator)
854 			dev_err(dev, "could not malloc jaguar1_regulator\n");
855 
856 		jaguar1->regulators.cnt = elem_size;
857 
858 		str = NULL;
859 		elem_index = 0;
860 		regulator = jaguar1->regulators.regulator;
861 		if (regulator) {
862 			do {
863 				str = of_prop_next_string(prop, str);
864 				if (!str) {
865 					dev_err(dev, "%s is not match %s in dts\n",
866 						OF_CAMERA_MODULE_REGULATORS,
867 						OF_CAMERA_MODULE_REGULATOR_VOLTAGES);
868 					break;
869 				}
870 				regulator->regulator =
871 					devm_regulator_get_optional(dev, str);
872 				if (IS_ERR(regulator->regulator))
873 					dev_err(dev, "devm_regulator_get %s failed\n",
874 						str);
875 				of_property_read_u32_index(
876 					np,
877 					OF_CAMERA_MODULE_REGULATOR_VOLTAGES,
878 					elem_index++,
879 					&regulator->min_uV);
880 				regulator->max_uV = regulator->min_uV;
881 				regulator++;
882 			} while (--elem_size);
883 		}
884 	}
885 	if (of_property_read_u32_array(np,
886 		RK_CAMERA_MODULE_DEFAULT_RECT,
887 		(unsigned int *)&jaguar1->defrect, 2)) {
888 		jaguar1->defrect.width = JAGUAR1_DEFAULT_WIDTH;
889 		jaguar1->defrect.height = JAGUAR1_DEFAULT_HEIGHT;
890 		dev_warn(dev,
891 			"can not get module %s from dts, use default wxh(%dx%d)!\n",
892 			RK_CAMERA_MODULE_DEFAULT_RECT,
893 			JAGUAR1_DEFAULT_WIDTH, JAGUAR1_DEFAULT_HEIGHT);
894 	} else {
895 		dev_info(dev,
896 			"get module %s from dts, wxh(%dx%d)!\n",
897 			RK_CAMERA_MODULE_DEFAULT_RECT,
898 			jaguar1->defrect.width,
899 			jaguar1->defrect.height);
900 	}
901 
902 	jaguar1->pd_gpio = devm_gpiod_get(dev, "pd", GPIOD_OUT_LOW);
903 	if (IS_ERR(jaguar1->pd_gpio))
904 		dev_warn(dev, "can not find pd-gpios, error %ld\n",
905 			 PTR_ERR(jaguar1->pd_gpio));
906 
907 	jaguar1->pd2_gpio = devm_gpiod_get(dev, "pd2", GPIOD_OUT_LOW);
908 	if (IS_ERR(jaguar1->pd2_gpio))
909 		dev_warn(dev, "can not find pd2-gpios, error %ld\n",
910 			 PTR_ERR(jaguar1->pd2_gpio));
911 
912 	jaguar1->rst_gpio = devm_gpiod_get(dev, "rst", GPIOD_OUT_LOW);
913 	if (IS_ERR(jaguar1->rst_gpio))
914 		dev_warn(dev, "can not find rst-gpios, error %ld\n",
915 			 PTR_ERR(jaguar1->rst_gpio));
916 
917 	jaguar1->rst2_gpio = devm_gpiod_get(dev, "rst2", GPIOD_OUT_LOW);
918 	if (IS_ERR(jaguar1->rst2_gpio))
919 		dev_warn(dev, "can not find rst2-gpios, error %ld\n",
920 			 PTR_ERR(jaguar1->rst2_gpio));
921 
922 	jaguar1->pwd_gpio = devm_gpiod_get(dev, "pwd", GPIOD_OUT_HIGH);
923 	if (IS_ERR(jaguar1->pwd_gpio))
924 		dev_warn(dev, "can not find pwd-gpios, error %ld\n",
925 			 PTR_ERR(jaguar1->pwd_gpio));
926 
927 	jaguar1->pwd2_gpio = devm_gpiod_get(dev, "pwd2", GPIOD_OUT_HIGH);
928 	if (IS_ERR(jaguar1->pwd2_gpio))
929 		dev_warn(dev, "can not find pwd2-gpios, error %ld\n",
930 			 PTR_ERR(jaguar1->pwd2_gpio));
931 
932 	return 0;
933 }
934 
jaguar1_probe(struct i2c_client * client,const struct i2c_device_id * id)935 static int jaguar1_probe(struct i2c_client *client,
936 			 const struct i2c_device_id *id)
937 {
938 	struct device *dev = &client->dev;
939 	struct device_node *node = dev->of_node;
940 	struct jaguar1 *jaguar1;
941 	struct v4l2_subdev *sd;
942 	__maybe_unused char facing[2];
943 	int ret, index;
944 
945 	dev_info(dev, "driver version: %02x.%02x.%02x",
946 		 DRIVER_VERSION >> 16,
947 		 (DRIVER_VERSION & 0xff00) >> 8,
948 		 DRIVER_VERSION & 0x00ff);
949 
950 	jaguar1 = devm_kzalloc(dev, sizeof(*jaguar1), GFP_KERNEL);
951 	if (!jaguar1)
952 		return -ENOMEM;
953 
954 	ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
955 				   &jaguar1->module_index);
956 	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
957 				       &jaguar1->module_facing);
958 	ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
959 				       &jaguar1->module_name);
960 	ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
961 				       &jaguar1->len_name);
962 	if (ret) {
963 		dev_err(dev, "could not get %s!\n", RKMODULE_CAMERA_LENS_NAME);
964 		return -EINVAL;
965 	}
966 
967 	jaguar1->client = client;
968 
969 	ret = jaguar1_analyze_dts(jaguar1);
970 	if (ret) {
971 		dev_err(dev, "Failed to analyze dts\n");
972 		return ret;
973 	}
974 
975 	mutex_init(&jaguar1->mutex);
976 	jaguar1_get_default_format(jaguar1);
977 
978 	sd = &jaguar1->subdev;
979 	v4l2_i2c_subdev_init(sd, client, &jaguar1_subdev_ops);
980 	ret = jaguar1_initialize_controls(jaguar1);
981 	if (ret) {
982 		dev_err(dev, "Failed to initialize controls jaguar1\n");
983 		return ret;
984 	}
985 
986 	__jaguar1_power_on(jaguar1);
987 	ret = jaguar1_init(i2c_adapter_id(client->adapter));
988 	if (ret) {
989 		dev_err(dev, "Failed to init jaguar1\n");
990 		__jaguar1_power_off(jaguar1);
991 		mutex_destroy(&jaguar1->mutex);
992 
993 		return ret;
994 	}
995 
996 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
997 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
998 #endif
999 
1000 #if defined(CONFIG_VIDEO_ROCKCHIP_USBACM_CONTROL)
1001 	__jaguar1_power_off(jaguar1);
1002 	mutex_destroy(&jaguar1->mutex);
1003 
1004 	return 0;
1005 #endif
1006 
1007 #if defined(CONFIG_MEDIA_CONTROLLER)
1008 	for (index = 0; index < PAD_MAX; index++)
1009 		jaguar1->pad[index].flags = MEDIA_PAD_FL_SOURCE;
1010 	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1011 	ret = media_entity_pads_init(&sd->entity, PAD_MAX, jaguar1->pad);
1012 	if (ret < 0)
1013 		goto err_power_off;
1014 #endif
1015 
1016 	memset(facing, 0, sizeof(facing));
1017 	if (strcmp(jaguar1->module_facing, "back") == 0)
1018 		facing[0] = 'b';
1019 	else
1020 		facing[0] = 'f';
1021 
1022 	snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
1023 		 jaguar1->module_index, facing,
1024 		 JAGUAR1_NAME, dev_name(sd->dev));
1025 
1026 	ret = v4l2_async_register_subdev_sensor_common(sd);
1027 	if (ret) {
1028 		dev_err(dev, "v4l2 async register subdev failed\n");
1029 		goto err_clean_entity;
1030 	}
1031 	pm_runtime_set_active(dev);
1032 	pm_runtime_enable(dev);
1033 	pm_runtime_idle(dev);
1034 
1035 	v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
1036 			client->addr << 1, client->adapter->name);
1037 	return 0;
1038 
1039 err_power_off:
1040 	__jaguar1_power_off(jaguar1);
1041 err_clean_entity:
1042 #if defined(CONFIG_MEDIA_CONTROLLER)
1043 	media_entity_cleanup(&sd->entity);
1044 #endif
1045 	mutex_destroy(&jaguar1->mutex);
1046 
1047 	return ret;
1048 }
1049 
jaguar1_remove(struct i2c_client * client)1050 static int jaguar1_remove(struct i2c_client *client)
1051 {
1052 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1053 	struct jaguar1 *jaguar1 = to_jaguar1(sd);
1054 
1055 	jaguar1_exit();
1056 	v4l2_ctrl_handler_free(&jaguar1->ctrl_handler);
1057 	mutex_destroy(&jaguar1->mutex);
1058 
1059 	pm_runtime_disable(&client->dev);
1060 	if (!pm_runtime_status_suspended(&client->dev))
1061 		__jaguar1_power_off(jaguar1);
1062 	pm_runtime_set_suspended(&client->dev);
1063 
1064 	return 0;
1065 }
1066 
1067 #if IS_ENABLED(CONFIG_OF)
1068 static const struct of_device_id jaguar1_of_match[] = {
1069 	{ .compatible = "jaguar1-v4l2" },
1070 	{},
1071 };
1072 MODULE_DEVICE_TABLE(of, jaguar1_of_match);
1073 #endif
1074 
1075 static const struct i2c_device_id jaguar1_match_id[] = {
1076 	{ "jaguar1-v4l2", 0 },
1077 	{ },
1078 };
1079 
1080 static struct i2c_driver jaguar1_i2c_driver = {
1081 	.driver = {
1082 		.name = JAGUAR1_NAME,
1083 		.pm = &jaguar1_pm_ops,
1084 		.of_match_table = of_match_ptr(jaguar1_of_match),
1085 	},
1086 	.probe		= &jaguar1_probe,
1087 	.remove		= &jaguar1_remove,
1088 	.id_table	= jaguar1_match_id,
1089 };
1090 
nvp6324_sensor_mod_init(void)1091 int nvp6324_sensor_mod_init(void)
1092 {
1093 	return i2c_add_driver(&jaguar1_i2c_driver);
1094 }
1095 
1096 #ifndef CONFIG_VIDEO_REVERSE_IMAGE
1097 device_initcall_sync(nvp6324_sensor_mod_init);
1098 #endif
1099 
sensor_mod_exit(void)1100 static void __exit sensor_mod_exit(void)
1101 {
1102 	i2c_del_driver(&jaguar1_i2c_driver);
1103 }
1104 
1105 module_exit(sensor_mod_exit);
1106 
1107 MODULE_DESCRIPTION("jaguar1 sensor driver");
1108 MODULE_LICENSE("GPL v2");
1109