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