0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/v4l2-mediabus.h>
0009 #include <linux/vmalloc.h>
0010 #include <media/v4l2-ctrls.h>
0011 #include <media/v4l2-event.h>
0012 #include <media/v4l2-subdev.h>
0013 #include <media/tpg/v4l2-tpg.h>
0014
0015 #include "vimc-common.h"
0016
0017 enum vimc_sensor_osd_mode {
0018 VIMC_SENSOR_OSD_SHOW_ALL = 0,
0019 VIMC_SENSOR_OSD_SHOW_COUNTERS = 1,
0020 VIMC_SENSOR_OSD_SHOW_NONE = 2
0021 };
0022
0023 struct vimc_sensor_device {
0024 struct vimc_ent_device ved;
0025 struct v4l2_subdev sd;
0026 struct tpg_data tpg;
0027 u8 *frame;
0028 enum vimc_sensor_osd_mode osd_value;
0029 u64 start_stream_ts;
0030
0031 struct v4l2_mbus_framefmt mbus_format;
0032 struct v4l2_ctrl_handler hdl;
0033 struct media_pad pad;
0034 };
0035
0036 static const struct v4l2_mbus_framefmt fmt_default = {
0037 .width = 640,
0038 .height = 480,
0039 .code = MEDIA_BUS_FMT_RGB888_1X24,
0040 .field = V4L2_FIELD_NONE,
0041 .colorspace = V4L2_COLORSPACE_SRGB,
0042 };
0043
0044 static int vimc_sensor_init_cfg(struct v4l2_subdev *sd,
0045 struct v4l2_subdev_state *sd_state)
0046 {
0047 unsigned int i;
0048
0049 for (i = 0; i < sd->entity.num_pads; i++) {
0050 struct v4l2_mbus_framefmt *mf;
0051
0052 mf = v4l2_subdev_get_try_format(sd, sd_state, i);
0053 *mf = fmt_default;
0054 }
0055
0056 return 0;
0057 }
0058
0059 static int vimc_sensor_enum_mbus_code(struct v4l2_subdev *sd,
0060 struct v4l2_subdev_state *sd_state,
0061 struct v4l2_subdev_mbus_code_enum *code)
0062 {
0063 u32 mbus_code = vimc_mbus_code_by_index(code->index);
0064
0065 if (!mbus_code)
0066 return -EINVAL;
0067
0068 code->code = mbus_code;
0069
0070 return 0;
0071 }
0072
0073 static int vimc_sensor_enum_frame_size(struct v4l2_subdev *sd,
0074 struct v4l2_subdev_state *sd_state,
0075 struct v4l2_subdev_frame_size_enum *fse)
0076 {
0077 const struct vimc_pix_map *vpix;
0078
0079 if (fse->index)
0080 return -EINVAL;
0081
0082
0083 vpix = vimc_pix_map_by_code(fse->code);
0084 if (!vpix)
0085 return -EINVAL;
0086
0087 fse->min_width = VIMC_FRAME_MIN_WIDTH;
0088 fse->max_width = VIMC_FRAME_MAX_WIDTH;
0089 fse->min_height = VIMC_FRAME_MIN_HEIGHT;
0090 fse->max_height = VIMC_FRAME_MAX_HEIGHT;
0091
0092 return 0;
0093 }
0094
0095 static int vimc_sensor_get_fmt(struct v4l2_subdev *sd,
0096 struct v4l2_subdev_state *sd_state,
0097 struct v4l2_subdev_format *fmt)
0098 {
0099 struct vimc_sensor_device *vsensor =
0100 container_of(sd, struct vimc_sensor_device, sd);
0101
0102 fmt->format = fmt->which == V4L2_SUBDEV_FORMAT_TRY ?
0103 *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad) :
0104 vsensor->mbus_format;
0105
0106 return 0;
0107 }
0108
0109 static void vimc_sensor_tpg_s_format(struct vimc_sensor_device *vsensor)
0110 {
0111 const struct vimc_pix_map *vpix =
0112 vimc_pix_map_by_code(vsensor->mbus_format.code);
0113
0114 tpg_reset_source(&vsensor->tpg, vsensor->mbus_format.width,
0115 vsensor->mbus_format.height, vsensor->mbus_format.field);
0116 tpg_s_bytesperline(&vsensor->tpg, 0, vsensor->mbus_format.width * vpix->bpp);
0117 tpg_s_buf_height(&vsensor->tpg, vsensor->mbus_format.height);
0118 tpg_s_fourcc(&vsensor->tpg, vpix->pixelformat);
0119
0120 tpg_s_field(&vsensor->tpg, vsensor->mbus_format.field, false);
0121 tpg_s_colorspace(&vsensor->tpg, vsensor->mbus_format.colorspace);
0122 tpg_s_ycbcr_enc(&vsensor->tpg, vsensor->mbus_format.ycbcr_enc);
0123 tpg_s_quantization(&vsensor->tpg, vsensor->mbus_format.quantization);
0124 tpg_s_xfer_func(&vsensor->tpg, vsensor->mbus_format.xfer_func);
0125 }
0126
0127 static void vimc_sensor_adjust_fmt(struct v4l2_mbus_framefmt *fmt)
0128 {
0129 const struct vimc_pix_map *vpix;
0130
0131
0132 vpix = vimc_pix_map_by_code(fmt->code);
0133 if (!vpix)
0134 fmt->code = fmt_default.code;
0135
0136 fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
0137 VIMC_FRAME_MAX_WIDTH) & ~1;
0138 fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
0139 VIMC_FRAME_MAX_HEIGHT) & ~1;
0140
0141
0142 if (fmt->field == V4L2_FIELD_ANY || fmt->field == V4L2_FIELD_ALTERNATE)
0143 fmt->field = fmt_default.field;
0144
0145 vimc_colorimetry_clamp(fmt);
0146 }
0147
0148 static int vimc_sensor_set_fmt(struct v4l2_subdev *sd,
0149 struct v4l2_subdev_state *sd_state,
0150 struct v4l2_subdev_format *fmt)
0151 {
0152 struct vimc_sensor_device *vsensor = v4l2_get_subdevdata(sd);
0153 struct v4l2_mbus_framefmt *mf;
0154
0155 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
0156
0157 if (vsensor->frame)
0158 return -EBUSY;
0159
0160 mf = &vsensor->mbus_format;
0161 } else {
0162 mf = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
0163 }
0164
0165
0166 vimc_sensor_adjust_fmt(&fmt->format);
0167
0168 dev_dbg(vsensor->ved.dev, "%s: format update: "
0169 "old:%dx%d (0x%x, %d, %d, %d, %d) "
0170 "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsensor->sd.name,
0171
0172 mf->width, mf->height, mf->code,
0173 mf->colorspace, mf->quantization,
0174 mf->xfer_func, mf->ycbcr_enc,
0175
0176 fmt->format.width, fmt->format.height, fmt->format.code,
0177 fmt->format.colorspace, fmt->format.quantization,
0178 fmt->format.xfer_func, fmt->format.ycbcr_enc);
0179
0180 *mf = fmt->format;
0181
0182 return 0;
0183 }
0184
0185 static const struct v4l2_subdev_pad_ops vimc_sensor_pad_ops = {
0186 .init_cfg = vimc_sensor_init_cfg,
0187 .enum_mbus_code = vimc_sensor_enum_mbus_code,
0188 .enum_frame_size = vimc_sensor_enum_frame_size,
0189 .get_fmt = vimc_sensor_get_fmt,
0190 .set_fmt = vimc_sensor_set_fmt,
0191 };
0192
0193 static void *vimc_sensor_process_frame(struct vimc_ent_device *ved,
0194 const void *sink_frame)
0195 {
0196 struct vimc_sensor_device *vsensor =
0197 container_of(ved, struct vimc_sensor_device, ved);
0198
0199 const unsigned int line_height = 16;
0200 u8 *basep[TPG_MAX_PLANES][2];
0201 unsigned int line = 1;
0202 char str[100];
0203
0204 tpg_fill_plane_buffer(&vsensor->tpg, 0, 0, vsensor->frame);
0205 tpg_calc_text_basep(&vsensor->tpg, basep, 0, vsensor->frame);
0206 switch (vsensor->osd_value) {
0207 case VIMC_SENSOR_OSD_SHOW_ALL: {
0208 const char *order = tpg_g_color_order(&vsensor->tpg);
0209
0210 tpg_gen_text(&vsensor->tpg, basep, line++ * line_height,
0211 16, order);
0212 snprintf(str, sizeof(str),
0213 "brightness %3d, contrast %3d, saturation %3d, hue %d ",
0214 vsensor->tpg.brightness,
0215 vsensor->tpg.contrast,
0216 vsensor->tpg.saturation,
0217 vsensor->tpg.hue);
0218 tpg_gen_text(&vsensor->tpg, basep, line++ * line_height, 16, str);
0219 snprintf(str, sizeof(str), "sensor size: %dx%d",
0220 vsensor->mbus_format.width,
0221 vsensor->mbus_format.height);
0222 tpg_gen_text(&vsensor->tpg, basep, line++ * line_height, 16, str);
0223 fallthrough;
0224 }
0225 case VIMC_SENSOR_OSD_SHOW_COUNTERS: {
0226 unsigned int ms;
0227
0228 ms = div_u64(ktime_get_ns() - vsensor->start_stream_ts, 1000000);
0229 snprintf(str, sizeof(str), "%02d:%02d:%02d:%03d",
0230 (ms / (60 * 60 * 1000)) % 24,
0231 (ms / (60 * 1000)) % 60,
0232 (ms / 1000) % 60,
0233 ms % 1000);
0234 tpg_gen_text(&vsensor->tpg, basep, line++ * line_height, 16, str);
0235 break;
0236 }
0237 case VIMC_SENSOR_OSD_SHOW_NONE:
0238 default:
0239 break;
0240 }
0241
0242 return vsensor->frame;
0243 }
0244
0245 static int vimc_sensor_s_stream(struct v4l2_subdev *sd, int enable)
0246 {
0247 struct vimc_sensor_device *vsensor =
0248 container_of(sd, struct vimc_sensor_device, sd);
0249
0250 if (enable) {
0251 const struct vimc_pix_map *vpix;
0252 unsigned int frame_size;
0253
0254 vsensor->start_stream_ts = ktime_get_ns();
0255
0256
0257 vpix = vimc_pix_map_by_code(vsensor->mbus_format.code);
0258 frame_size = vsensor->mbus_format.width * vpix->bpp *
0259 vsensor->mbus_format.height;
0260
0261
0262
0263
0264
0265 vsensor->frame = vmalloc(frame_size);
0266 if (!vsensor->frame)
0267 return -ENOMEM;
0268
0269
0270 vimc_sensor_tpg_s_format(vsensor);
0271
0272 } else {
0273
0274 vfree(vsensor->frame);
0275 vsensor->frame = NULL;
0276 }
0277
0278 return 0;
0279 }
0280
0281 static const struct v4l2_subdev_core_ops vimc_sensor_core_ops = {
0282 .log_status = v4l2_ctrl_subdev_log_status,
0283 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
0284 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
0285 };
0286
0287 static const struct v4l2_subdev_video_ops vimc_sensor_video_ops = {
0288 .s_stream = vimc_sensor_s_stream,
0289 };
0290
0291 static const struct v4l2_subdev_ops vimc_sensor_ops = {
0292 .core = &vimc_sensor_core_ops,
0293 .pad = &vimc_sensor_pad_ops,
0294 .video = &vimc_sensor_video_ops,
0295 };
0296
0297 static int vimc_sensor_s_ctrl(struct v4l2_ctrl *ctrl)
0298 {
0299 struct vimc_sensor_device *vsensor =
0300 container_of(ctrl->handler, struct vimc_sensor_device, hdl);
0301
0302 switch (ctrl->id) {
0303 case VIMC_CID_TEST_PATTERN:
0304 tpg_s_pattern(&vsensor->tpg, ctrl->val);
0305 break;
0306 case V4L2_CID_HFLIP:
0307 tpg_s_hflip(&vsensor->tpg, ctrl->val);
0308 break;
0309 case V4L2_CID_VFLIP:
0310 tpg_s_vflip(&vsensor->tpg, ctrl->val);
0311 break;
0312 case V4L2_CID_BRIGHTNESS:
0313 tpg_s_brightness(&vsensor->tpg, ctrl->val);
0314 break;
0315 case V4L2_CID_CONTRAST:
0316 tpg_s_contrast(&vsensor->tpg, ctrl->val);
0317 break;
0318 case V4L2_CID_HUE:
0319 tpg_s_hue(&vsensor->tpg, ctrl->val);
0320 break;
0321 case V4L2_CID_SATURATION:
0322 tpg_s_saturation(&vsensor->tpg, ctrl->val);
0323 break;
0324 case VIMC_CID_OSD_TEXT_MODE:
0325 vsensor->osd_value = ctrl->val;
0326 break;
0327 default:
0328 return -EINVAL;
0329 }
0330 return 0;
0331 }
0332
0333 static const struct v4l2_ctrl_ops vimc_sensor_ctrl_ops = {
0334 .s_ctrl = vimc_sensor_s_ctrl,
0335 };
0336
0337 static void vimc_sensor_release(struct vimc_ent_device *ved)
0338 {
0339 struct vimc_sensor_device *vsensor =
0340 container_of(ved, struct vimc_sensor_device, ved);
0341
0342 v4l2_ctrl_handler_free(&vsensor->hdl);
0343 tpg_free(&vsensor->tpg);
0344 media_entity_cleanup(vsensor->ved.ent);
0345 kfree(vsensor);
0346 }
0347
0348
0349 static const struct v4l2_ctrl_config vimc_sensor_ctrl_class = {
0350 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY,
0351 .id = VIMC_CID_VIMC_CLASS,
0352 .name = "VIMC Controls",
0353 .type = V4L2_CTRL_TYPE_CTRL_CLASS,
0354 };
0355
0356 static const struct v4l2_ctrl_config vimc_sensor_ctrl_test_pattern = {
0357 .ops = &vimc_sensor_ctrl_ops,
0358 .id = VIMC_CID_TEST_PATTERN,
0359 .name = "Test Pattern",
0360 .type = V4L2_CTRL_TYPE_MENU,
0361 .max = TPG_PAT_NOISE,
0362 .qmenu = tpg_pattern_strings,
0363 };
0364
0365 static const char * const vimc_ctrl_osd_mode_strings[] = {
0366 "All",
0367 "Counters Only",
0368 "None",
0369 NULL,
0370 };
0371
0372 static const struct v4l2_ctrl_config vimc_sensor_ctrl_osd_mode = {
0373 .ops = &vimc_sensor_ctrl_ops,
0374 .id = VIMC_CID_OSD_TEXT_MODE,
0375 .name = "Show Information",
0376 .type = V4L2_CTRL_TYPE_MENU,
0377 .max = ARRAY_SIZE(vimc_ctrl_osd_mode_strings) - 2,
0378 .qmenu = vimc_ctrl_osd_mode_strings,
0379 };
0380
0381 static struct vimc_ent_device *vimc_sensor_add(struct vimc_device *vimc,
0382 const char *vcfg_name)
0383 {
0384 struct v4l2_device *v4l2_dev = &vimc->v4l2_dev;
0385 struct vimc_sensor_device *vsensor;
0386 int ret;
0387
0388
0389 vsensor = kzalloc(sizeof(*vsensor), GFP_KERNEL);
0390 if (!vsensor)
0391 return ERR_PTR(-ENOMEM);
0392
0393 v4l2_ctrl_handler_init(&vsensor->hdl, 4);
0394
0395 v4l2_ctrl_new_custom(&vsensor->hdl, &vimc_sensor_ctrl_class, NULL);
0396 v4l2_ctrl_new_custom(&vsensor->hdl, &vimc_sensor_ctrl_test_pattern, NULL);
0397 v4l2_ctrl_new_custom(&vsensor->hdl, &vimc_sensor_ctrl_osd_mode, NULL);
0398 v4l2_ctrl_new_std(&vsensor->hdl, &vimc_sensor_ctrl_ops,
0399 V4L2_CID_VFLIP, 0, 1, 1, 0);
0400 v4l2_ctrl_new_std(&vsensor->hdl, &vimc_sensor_ctrl_ops,
0401 V4L2_CID_HFLIP, 0, 1, 1, 0);
0402 v4l2_ctrl_new_std(&vsensor->hdl, &vimc_sensor_ctrl_ops,
0403 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
0404 v4l2_ctrl_new_std(&vsensor->hdl, &vimc_sensor_ctrl_ops,
0405 V4L2_CID_CONTRAST, 0, 255, 1, 128);
0406 v4l2_ctrl_new_std(&vsensor->hdl, &vimc_sensor_ctrl_ops,
0407 V4L2_CID_HUE, -128, 127, 1, 0);
0408 v4l2_ctrl_new_std(&vsensor->hdl, &vimc_sensor_ctrl_ops,
0409 V4L2_CID_SATURATION, 0, 255, 1, 128);
0410 vsensor->sd.ctrl_handler = &vsensor->hdl;
0411 if (vsensor->hdl.error) {
0412 ret = vsensor->hdl.error;
0413 goto err_free_vsensor;
0414 }
0415
0416
0417 tpg_init(&vsensor->tpg, vsensor->mbus_format.width,
0418 vsensor->mbus_format.height);
0419 ret = tpg_alloc(&vsensor->tpg, VIMC_FRAME_MAX_WIDTH);
0420 if (ret)
0421 goto err_free_hdl;
0422
0423
0424 vsensor->pad.flags = MEDIA_PAD_FL_SOURCE;
0425 ret = vimc_ent_sd_register(&vsensor->ved, &vsensor->sd, v4l2_dev,
0426 vcfg_name,
0427 MEDIA_ENT_F_CAM_SENSOR, 1, &vsensor->pad,
0428 &vimc_sensor_ops);
0429 if (ret)
0430 goto err_free_tpg;
0431
0432 vsensor->ved.process_frame = vimc_sensor_process_frame;
0433 vsensor->ved.dev = vimc->mdev.dev;
0434
0435
0436 vsensor->mbus_format = fmt_default;
0437
0438 return &vsensor->ved;
0439
0440 err_free_tpg:
0441 tpg_free(&vsensor->tpg);
0442 err_free_hdl:
0443 v4l2_ctrl_handler_free(&vsensor->hdl);
0444 err_free_vsensor:
0445 kfree(vsensor);
0446
0447 return ERR_PTR(ret);
0448 }
0449
0450 struct vimc_ent_type vimc_sensor_type = {
0451 .add = vimc_sensor_add,
0452 .release = vimc_sensor_release
0453 };