xref: /linux/drivers/media/test-drivers/vimc/vimc-sensor.c (revision b3f73b21887ecda4c03a61085c7290ce585016d0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * vimc-sensor.c Virtual Media Controller Driver
4  *
5  * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
6  */
7 
8 #include <linux/v4l2-mediabus.h>
9 #include <linux/vmalloc.h>
10 #include <media/v4l2-ctrls.h>
11 #include <media/v4l2-event.h>
12 #include <media/v4l2-subdev.h>
13 #include <media/tpg/v4l2-tpg.h>
14 
15 #include "vimc-common.h"
16 
17 enum vimc_sensor_osd_mode {
18 	VIMC_SENSOR_OSD_SHOW_ALL = 0,
19 	VIMC_SENSOR_OSD_SHOW_COUNTERS = 1,
20 	VIMC_SENSOR_OSD_SHOW_NONE = 2
21 };
22 
23 struct vimc_sensor_device {
24 	struct vimc_ent_device ved;
25 	struct v4l2_subdev sd;
26 	struct tpg_data tpg;
27 	u8 *frame;
28 	enum vimc_sensor_osd_mode osd_value;
29 	u64 start_stream_ts;
30 	/* The active format */
31 	struct v4l2_mbus_framefmt mbus_format;
32 	struct v4l2_ctrl_handler hdl;
33 	struct media_pad pad;
34 };
35 
36 static const struct v4l2_mbus_framefmt fmt_default = {
37 	.width = 640,
38 	.height = 480,
39 	.code = MEDIA_BUS_FMT_RGB888_1X24,
40 	.field = V4L2_FIELD_NONE,
41 	.colorspace = V4L2_COLORSPACE_SRGB,
42 };
43 
44 static int vimc_sensor_init_state(struct v4l2_subdev *sd,
45 				  struct v4l2_subdev_state *sd_state)
46 {
47 	struct v4l2_mbus_framefmt *mf;
48 
49 	mf = v4l2_subdev_state_get_format(sd_state, 0);
50 	*mf = fmt_default;
51 
52 	return 0;
53 }
54 
55 static int vimc_sensor_enum_mbus_code(struct v4l2_subdev *sd,
56 				      struct v4l2_subdev_state *sd_state,
57 				      struct v4l2_subdev_mbus_code_enum *code)
58 {
59 	u32 mbus_code = vimc_mbus_code_by_index(code->index);
60 
61 	if (!mbus_code)
62 		return -EINVAL;
63 
64 	code->code = mbus_code;
65 
66 	return 0;
67 }
68 
69 static int vimc_sensor_enum_frame_size(struct v4l2_subdev *sd,
70 				       struct v4l2_subdev_state *sd_state,
71 				       struct v4l2_subdev_frame_size_enum *fse)
72 {
73 	const struct vimc_pix_map *vpix;
74 
75 	if (fse->index)
76 		return -EINVAL;
77 
78 	/* Only accept code in the pix map table */
79 	vpix = vimc_pix_map_by_code(fse->code);
80 	if (!vpix)
81 		return -EINVAL;
82 
83 	fse->min_width = VIMC_FRAME_MIN_WIDTH;
84 	fse->max_width = VIMC_FRAME_MAX_WIDTH;
85 	fse->min_height = VIMC_FRAME_MIN_HEIGHT;
86 	fse->max_height = VIMC_FRAME_MAX_HEIGHT;
87 
88 	return 0;
89 }
90 
91 static int vimc_sensor_get_fmt(struct v4l2_subdev *sd,
92 			       struct v4l2_subdev_state *sd_state,
93 			       struct v4l2_subdev_format *fmt)
94 {
95 	struct vimc_sensor_device *vsensor =
96 				container_of(sd, struct vimc_sensor_device, sd);
97 
98 	fmt->format = fmt->which == V4L2_SUBDEV_FORMAT_TRY ?
99 		      *v4l2_subdev_state_get_format(sd_state, fmt->pad) :
100 		      vsensor->mbus_format;
101 
102 	return 0;
103 }
104 
105 static void vimc_sensor_tpg_s_format(struct vimc_sensor_device *vsensor)
106 {
107 	const struct vimc_pix_map *vpix =
108 				vimc_pix_map_by_code(vsensor->mbus_format.code);
109 
110 	tpg_reset_source(&vsensor->tpg, vsensor->mbus_format.width,
111 			 vsensor->mbus_format.height, vsensor->mbus_format.field);
112 	tpg_s_bytesperline(&vsensor->tpg, 0, vsensor->mbus_format.width * vpix->bpp);
113 	tpg_s_buf_height(&vsensor->tpg, vsensor->mbus_format.height);
114 	tpg_s_fourcc(&vsensor->tpg, vpix->pixelformat);
115 	/* TODO: add support for V4L2_FIELD_ALTERNATE */
116 	tpg_s_field(&vsensor->tpg, vsensor->mbus_format.field, false);
117 	tpg_s_colorspace(&vsensor->tpg, vsensor->mbus_format.colorspace);
118 	tpg_s_ycbcr_enc(&vsensor->tpg, vsensor->mbus_format.ycbcr_enc);
119 	tpg_s_quantization(&vsensor->tpg, vsensor->mbus_format.quantization);
120 	tpg_s_xfer_func(&vsensor->tpg, vsensor->mbus_format.xfer_func);
121 }
122 
123 static void vimc_sensor_adjust_fmt(struct v4l2_mbus_framefmt *fmt)
124 {
125 	const struct vimc_pix_map *vpix;
126 
127 	/* Only accept code in the pix map table */
128 	vpix = vimc_pix_map_by_code(fmt->code);
129 	if (!vpix)
130 		fmt->code = fmt_default.code;
131 
132 	fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
133 			     VIMC_FRAME_MAX_WIDTH) & ~1;
134 	fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
135 			      VIMC_FRAME_MAX_HEIGHT) & ~1;
136 
137 	/* TODO: add support for V4L2_FIELD_ALTERNATE */
138 	if (fmt->field == V4L2_FIELD_ANY || fmt->field == V4L2_FIELD_ALTERNATE)
139 		fmt->field = fmt_default.field;
140 
141 	vimc_colorimetry_clamp(fmt);
142 }
143 
144 static int vimc_sensor_set_fmt(struct v4l2_subdev *sd,
145 			       struct v4l2_subdev_state *sd_state,
146 			       struct v4l2_subdev_format *fmt)
147 {
148 	struct vimc_sensor_device *vsensor = v4l2_get_subdevdata(sd);
149 	struct v4l2_mbus_framefmt *mf;
150 
151 	if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
152 		/* Do not change the format while stream is on */
153 		if (vsensor->frame)
154 			return -EBUSY;
155 
156 		mf = &vsensor->mbus_format;
157 	} else {
158 		mf = v4l2_subdev_state_get_format(sd_state, fmt->pad);
159 	}
160 
161 	/* Set the new format */
162 	vimc_sensor_adjust_fmt(&fmt->format);
163 
164 	dev_dbg(vsensor->ved.dev, "%s: format update: "
165 		"old:%dx%d (0x%x, %d, %d, %d, %d) "
166 		"new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsensor->sd.name,
167 		/* old */
168 		mf->width, mf->height, mf->code,
169 		mf->colorspace,	mf->quantization,
170 		mf->xfer_func, mf->ycbcr_enc,
171 		/* new */
172 		fmt->format.width, fmt->format.height, fmt->format.code,
173 		fmt->format.colorspace, fmt->format.quantization,
174 		fmt->format.xfer_func, fmt->format.ycbcr_enc);
175 
176 	*mf = fmt->format;
177 
178 	return 0;
179 }
180 
181 static const struct v4l2_subdev_pad_ops vimc_sensor_pad_ops = {
182 	.enum_mbus_code		= vimc_sensor_enum_mbus_code,
183 	.enum_frame_size	= vimc_sensor_enum_frame_size,
184 	.get_fmt		= vimc_sensor_get_fmt,
185 	.set_fmt		= vimc_sensor_set_fmt,
186 };
187 
188 static void *vimc_sensor_process_frame(struct vimc_ent_device *ved,
189 				       const void *sink_frame)
190 {
191 	struct vimc_sensor_device *vsensor =
192 		container_of(ved, struct vimc_sensor_device, ved);
193 
194 	const unsigned int line_height = 16;
195 	u8 *basep[TPG_MAX_PLANES][2];
196 	unsigned int line = 1;
197 	char str[100];
198 
199 	tpg_fill_plane_buffer(&vsensor->tpg, 0, 0, vsensor->frame);
200 	tpg_calc_text_basep(&vsensor->tpg, basep, 0, vsensor->frame);
201 	switch (vsensor->osd_value) {
202 	case VIMC_SENSOR_OSD_SHOW_ALL: {
203 		const char *order = tpg_g_color_order(&vsensor->tpg);
204 
205 		tpg_gen_text(&vsensor->tpg, basep, line++ * line_height,
206 			     16, order);
207 		snprintf(str, sizeof(str),
208 			 "brightness %3d, contrast %3d, saturation %3d, hue %d ",
209 			 vsensor->tpg.brightness,
210 			 vsensor->tpg.contrast,
211 			 vsensor->tpg.saturation,
212 			 vsensor->tpg.hue);
213 		tpg_gen_text(&vsensor->tpg, basep, line++ * line_height, 16, str);
214 		snprintf(str, sizeof(str), "sensor size: %dx%d",
215 			 vsensor->mbus_format.width,
216 			 vsensor->mbus_format.height);
217 		tpg_gen_text(&vsensor->tpg, basep, line++ * line_height, 16, str);
218 		fallthrough;
219 	}
220 	case VIMC_SENSOR_OSD_SHOW_COUNTERS: {
221 		unsigned int ms;
222 
223 		ms = div_u64(ktime_get_ns() - vsensor->start_stream_ts, 1000000);
224 		snprintf(str, sizeof(str), "%02d:%02d:%02d:%03d",
225 			 (ms / (60 * 60 * 1000)) % 24,
226 			 (ms / (60 * 1000)) % 60,
227 			 (ms / 1000) % 60,
228 			 ms % 1000);
229 		tpg_gen_text(&vsensor->tpg, basep, line++ * line_height, 16, str);
230 		break;
231 	}
232 	case VIMC_SENSOR_OSD_SHOW_NONE:
233 	default:
234 		break;
235 	}
236 
237 	return vsensor->frame;
238 }
239 
240 static int vimc_sensor_s_stream(struct v4l2_subdev *sd, int enable)
241 {
242 	struct vimc_sensor_device *vsensor =
243 				container_of(sd, struct vimc_sensor_device, sd);
244 
245 	if (enable) {
246 		const struct vimc_pix_map *vpix;
247 		unsigned int frame_size;
248 
249 		vsensor->start_stream_ts = ktime_get_ns();
250 
251 		/* Calculate the frame size */
252 		vpix = vimc_pix_map_by_code(vsensor->mbus_format.code);
253 		frame_size = vsensor->mbus_format.width * vpix->bpp *
254 			     vsensor->mbus_format.height;
255 
256 		/*
257 		 * Allocate the frame buffer. Use vmalloc to be able to
258 		 * allocate a large amount of memory
259 		 */
260 		vsensor->frame = vmalloc(frame_size);
261 		if (!vsensor->frame)
262 			return -ENOMEM;
263 
264 		/* configure the test pattern generator */
265 		vimc_sensor_tpg_s_format(vsensor);
266 
267 	} else {
268 
269 		vfree(vsensor->frame);
270 		vsensor->frame = NULL;
271 	}
272 
273 	return 0;
274 }
275 
276 static const struct v4l2_subdev_core_ops vimc_sensor_core_ops = {
277 	.log_status = v4l2_ctrl_subdev_log_status,
278 	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
279 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
280 };
281 
282 static const struct v4l2_subdev_video_ops vimc_sensor_video_ops = {
283 	.s_stream = vimc_sensor_s_stream,
284 };
285 
286 static const struct v4l2_subdev_ops vimc_sensor_ops = {
287 	.core = &vimc_sensor_core_ops,
288 	.pad = &vimc_sensor_pad_ops,
289 	.video = &vimc_sensor_video_ops,
290 };
291 
292 static const struct v4l2_subdev_internal_ops vimc_sensor_internal_ops = {
293 	.init_state = vimc_sensor_init_state,
294 };
295 
296 static int vimc_sensor_s_ctrl(struct v4l2_ctrl *ctrl)
297 {
298 	struct vimc_sensor_device *vsensor =
299 		container_of(ctrl->handler, struct vimc_sensor_device, hdl);
300 
301 	switch (ctrl->id) {
302 	case VIMC_CID_TEST_PATTERN:
303 		tpg_s_pattern(&vsensor->tpg, ctrl->val);
304 		break;
305 	case V4L2_CID_HFLIP:
306 		tpg_s_hflip(&vsensor->tpg, ctrl->val);
307 		break;
308 	case V4L2_CID_VFLIP:
309 		tpg_s_vflip(&vsensor->tpg, ctrl->val);
310 		break;
311 	case V4L2_CID_BRIGHTNESS:
312 		tpg_s_brightness(&vsensor->tpg, ctrl->val);
313 		break;
314 	case V4L2_CID_CONTRAST:
315 		tpg_s_contrast(&vsensor->tpg, ctrl->val);
316 		break;
317 	case V4L2_CID_HUE:
318 		tpg_s_hue(&vsensor->tpg, ctrl->val);
319 		break;
320 	case V4L2_CID_SATURATION:
321 		tpg_s_saturation(&vsensor->tpg, ctrl->val);
322 		break;
323 	case VIMC_CID_OSD_TEXT_MODE:
324 		vsensor->osd_value = ctrl->val;
325 		break;
326 	default:
327 		return -EINVAL;
328 	}
329 	return 0;
330 }
331 
332 static const struct v4l2_ctrl_ops vimc_sensor_ctrl_ops = {
333 	.s_ctrl = vimc_sensor_s_ctrl,
334 };
335 
336 static void vimc_sensor_release(struct vimc_ent_device *ved)
337 {
338 	struct vimc_sensor_device *vsensor =
339 		container_of(ved, struct vimc_sensor_device, ved);
340 
341 	v4l2_ctrl_handler_free(&vsensor->hdl);
342 	tpg_free(&vsensor->tpg);
343 	v4l2_subdev_cleanup(&vsensor->sd);
344 	media_entity_cleanup(vsensor->ved.ent);
345 	kfree(vsensor);
346 }
347 
348 /* Image Processing Controls */
349 static const struct v4l2_ctrl_config vimc_sensor_ctrl_class = {
350 	.flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY,
351 	.id = VIMC_CID_VIMC_CLASS,
352 	.name = "VIMC Controls",
353 	.type = V4L2_CTRL_TYPE_CTRL_CLASS,
354 };
355 
356 static const struct v4l2_ctrl_config vimc_sensor_ctrl_test_pattern = {
357 	.ops = &vimc_sensor_ctrl_ops,
358 	.id = VIMC_CID_TEST_PATTERN,
359 	.name = "Test Pattern",
360 	.type = V4L2_CTRL_TYPE_MENU,
361 	.max = TPG_PAT_NOISE,
362 	.qmenu = tpg_pattern_strings,
363 };
364 
365 static const char * const vimc_ctrl_osd_mode_strings[] = {
366 	"All",
367 	"Counters Only",
368 	"None",
369 	NULL,
370 };
371 
372 static const struct v4l2_ctrl_config vimc_sensor_ctrl_osd_mode = {
373 	.ops = &vimc_sensor_ctrl_ops,
374 	.id = VIMC_CID_OSD_TEXT_MODE,
375 	.name = "Show Information",
376 	.type = V4L2_CTRL_TYPE_MENU,
377 	.max = ARRAY_SIZE(vimc_ctrl_osd_mode_strings) - 2,
378 	.qmenu = vimc_ctrl_osd_mode_strings,
379 };
380 
381 static struct vimc_ent_device *vimc_sensor_add(struct vimc_device *vimc,
382 					       const char *vcfg_name)
383 {
384 	struct v4l2_device *v4l2_dev = &vimc->v4l2_dev;
385 	struct vimc_sensor_device *vsensor;
386 	int ret;
387 
388 	/* Allocate the vsensor struct */
389 	vsensor = kzalloc(sizeof(*vsensor), GFP_KERNEL);
390 	if (!vsensor)
391 		return ERR_PTR(-ENOMEM);
392 
393 	v4l2_ctrl_handler_init(&vsensor->hdl, 4);
394 
395 	v4l2_ctrl_new_custom(&vsensor->hdl, &vimc_sensor_ctrl_class, NULL);
396 	v4l2_ctrl_new_custom(&vsensor->hdl, &vimc_sensor_ctrl_test_pattern, NULL);
397 	v4l2_ctrl_new_custom(&vsensor->hdl, &vimc_sensor_ctrl_osd_mode, NULL);
398 	v4l2_ctrl_new_std(&vsensor->hdl, &vimc_sensor_ctrl_ops,
399 			  V4L2_CID_VFLIP, 0, 1, 1, 0);
400 	v4l2_ctrl_new_std(&vsensor->hdl, &vimc_sensor_ctrl_ops,
401 			  V4L2_CID_HFLIP, 0, 1, 1, 0);
402 	v4l2_ctrl_new_std(&vsensor->hdl, &vimc_sensor_ctrl_ops,
403 			  V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
404 	v4l2_ctrl_new_std(&vsensor->hdl, &vimc_sensor_ctrl_ops,
405 			  V4L2_CID_CONTRAST, 0, 255, 1, 128);
406 	v4l2_ctrl_new_std(&vsensor->hdl, &vimc_sensor_ctrl_ops,
407 			  V4L2_CID_HUE, -128, 127, 1, 0);
408 	v4l2_ctrl_new_std(&vsensor->hdl, &vimc_sensor_ctrl_ops,
409 			  V4L2_CID_SATURATION, 0, 255, 1, 128);
410 	vsensor->sd.ctrl_handler = &vsensor->hdl;
411 	if (vsensor->hdl.error) {
412 		ret = vsensor->hdl.error;
413 		goto err_free_vsensor;
414 	}
415 
416 	/* Initialize the test pattern generator */
417 	tpg_init(&vsensor->tpg, vsensor->mbus_format.width,
418 		 vsensor->mbus_format.height);
419 	ret = tpg_alloc(&vsensor->tpg, VIMC_FRAME_MAX_WIDTH);
420 	if (ret)
421 		goto err_free_hdl;
422 
423 	/* Initialize ved and sd */
424 	vsensor->pad.flags = MEDIA_PAD_FL_SOURCE;
425 	ret = vimc_ent_sd_register(&vsensor->ved, &vsensor->sd, v4l2_dev,
426 				   vcfg_name,
427 				   MEDIA_ENT_F_CAM_SENSOR, 1, &vsensor->pad,
428 				   &vimc_sensor_internal_ops, &vimc_sensor_ops);
429 	if (ret)
430 		goto err_free_tpg;
431 
432 	vsensor->ved.process_frame = vimc_sensor_process_frame;
433 	vsensor->ved.dev = vimc->mdev.dev;
434 
435 	/* Initialize the frame format */
436 	vsensor->mbus_format = fmt_default;
437 
438 	return &vsensor->ved;
439 
440 err_free_tpg:
441 	tpg_free(&vsensor->tpg);
442 err_free_hdl:
443 	v4l2_ctrl_handler_free(&vsensor->hdl);
444 err_free_vsensor:
445 	kfree(vsensor);
446 
447 	return ERR_PTR(ret);
448 }
449 
450 const struct vimc_ent_type vimc_sensor_type = {
451 	.add = vimc_sensor_add,
452 	.release = vimc_sensor_release
453 };
454