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