projects/linux·blob master · refs

509 lines · 14.2 KiB

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