at v5.5 10 kB view raw
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 17struct vimc_sen_device { 18 struct vimc_ent_device ved; 19 struct v4l2_subdev sd; 20 struct tpg_data tpg; 21 u8 *frame; 22 /* The active format */ 23 struct v4l2_mbus_framefmt mbus_format; 24 struct v4l2_ctrl_handler hdl; 25 struct media_pad pad; 26}; 27 28static const struct v4l2_mbus_framefmt fmt_default = { 29 .width = 640, 30 .height = 480, 31 .code = MEDIA_BUS_FMT_RGB888_1X24, 32 .field = V4L2_FIELD_NONE, 33 .colorspace = V4L2_COLORSPACE_DEFAULT, 34}; 35 36static int vimc_sen_init_cfg(struct v4l2_subdev *sd, 37 struct v4l2_subdev_pad_config *cfg) 38{ 39 unsigned int i; 40 41 for (i = 0; i < sd->entity.num_pads; i++) { 42 struct v4l2_mbus_framefmt *mf; 43 44 mf = v4l2_subdev_get_try_format(sd, cfg, i); 45 *mf = fmt_default; 46 } 47 48 return 0; 49} 50 51static int vimc_sen_enum_mbus_code(struct v4l2_subdev *sd, 52 struct v4l2_subdev_pad_config *cfg, 53 struct v4l2_subdev_mbus_code_enum *code) 54{ 55 const struct vimc_pix_map *vpix = vimc_pix_map_by_index(code->index); 56 57 if (!vpix) 58 return -EINVAL; 59 60 code->code = vpix->code; 61 62 return 0; 63} 64 65static int vimc_sen_enum_frame_size(struct v4l2_subdev *sd, 66 struct v4l2_subdev_pad_config *cfg, 67 struct v4l2_subdev_frame_size_enum *fse) 68{ 69 const struct vimc_pix_map *vpix; 70 71 if (fse->index) 72 return -EINVAL; 73 74 /* Only accept code in the pix map table */ 75 vpix = vimc_pix_map_by_code(fse->code); 76 if (!vpix) 77 return -EINVAL; 78 79 fse->min_width = VIMC_FRAME_MIN_WIDTH; 80 fse->max_width = VIMC_FRAME_MAX_WIDTH; 81 fse->min_height = VIMC_FRAME_MIN_HEIGHT; 82 fse->max_height = VIMC_FRAME_MAX_HEIGHT; 83 84 return 0; 85} 86 87static int vimc_sen_get_fmt(struct v4l2_subdev *sd, 88 struct v4l2_subdev_pad_config *cfg, 89 struct v4l2_subdev_format *fmt) 90{ 91 struct vimc_sen_device *vsen = 92 container_of(sd, struct vimc_sen_device, sd); 93 94 fmt->format = fmt->which == V4L2_SUBDEV_FORMAT_TRY ? 95 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) : 96 vsen->mbus_format; 97 98 return 0; 99} 100 101static void vimc_sen_tpg_s_format(struct vimc_sen_device *vsen) 102{ 103 const struct vimc_pix_map *vpix = 104 vimc_pix_map_by_code(vsen->mbus_format.code); 105 106 tpg_reset_source(&vsen->tpg, vsen->mbus_format.width, 107 vsen->mbus_format.height, vsen->mbus_format.field); 108 tpg_s_bytesperline(&vsen->tpg, 0, vsen->mbus_format.width * vpix->bpp); 109 tpg_s_buf_height(&vsen->tpg, vsen->mbus_format.height); 110 tpg_s_fourcc(&vsen->tpg, vpix->pixelformat); 111 /* TODO: add support for V4L2_FIELD_ALTERNATE */ 112 tpg_s_field(&vsen->tpg, vsen->mbus_format.field, false); 113 tpg_s_colorspace(&vsen->tpg, vsen->mbus_format.colorspace); 114 tpg_s_ycbcr_enc(&vsen->tpg, vsen->mbus_format.ycbcr_enc); 115 tpg_s_quantization(&vsen->tpg, vsen->mbus_format.quantization); 116 tpg_s_xfer_func(&vsen->tpg, vsen->mbus_format.xfer_func); 117} 118 119static void vimc_sen_adjust_fmt(struct v4l2_mbus_framefmt *fmt) 120{ 121 const struct vimc_pix_map *vpix; 122 123 /* Only accept code in the pix map table */ 124 vpix = vimc_pix_map_by_code(fmt->code); 125 if (!vpix) 126 fmt->code = fmt_default.code; 127 128 fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH, 129 VIMC_FRAME_MAX_WIDTH) & ~1; 130 fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT, 131 VIMC_FRAME_MAX_HEIGHT) & ~1; 132 133 /* TODO: add support for V4L2_FIELD_ALTERNATE */ 134 if (fmt->field == V4L2_FIELD_ANY || fmt->field == V4L2_FIELD_ALTERNATE) 135 fmt->field = fmt_default.field; 136 137 vimc_colorimetry_clamp(fmt); 138} 139 140static int vimc_sen_set_fmt(struct v4l2_subdev *sd, 141 struct v4l2_subdev_pad_config *cfg, 142 struct v4l2_subdev_format *fmt) 143{ 144 struct vimc_sen_device *vsen = v4l2_get_subdevdata(sd); 145 struct v4l2_mbus_framefmt *mf; 146 147 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) { 148 /* Do not change the format while stream is on */ 149 if (vsen->frame) 150 return -EBUSY; 151 152 mf = &vsen->mbus_format; 153 } else { 154 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad); 155 } 156 157 /* Set the new format */ 158 vimc_sen_adjust_fmt(&fmt->format); 159 160 dev_dbg(vsen->ved.dev, "%s: format update: " 161 "old:%dx%d (0x%x, %d, %d, %d, %d) " 162 "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsen->sd.name, 163 /* old */ 164 mf->width, mf->height, mf->code, 165 mf->colorspace, mf->quantization, 166 mf->xfer_func, mf->ycbcr_enc, 167 /* new */ 168 fmt->format.width, fmt->format.height, fmt->format.code, 169 fmt->format.colorspace, fmt->format.quantization, 170 fmt->format.xfer_func, fmt->format.ycbcr_enc); 171 172 *mf = fmt->format; 173 174 return 0; 175} 176 177static const struct v4l2_subdev_pad_ops vimc_sen_pad_ops = { 178 .init_cfg = vimc_sen_init_cfg, 179 .enum_mbus_code = vimc_sen_enum_mbus_code, 180 .enum_frame_size = vimc_sen_enum_frame_size, 181 .get_fmt = vimc_sen_get_fmt, 182 .set_fmt = vimc_sen_set_fmt, 183}; 184 185static void *vimc_sen_process_frame(struct vimc_ent_device *ved, 186 const void *sink_frame) 187{ 188 struct vimc_sen_device *vsen = container_of(ved, struct vimc_sen_device, 189 ved); 190 191 tpg_fill_plane_buffer(&vsen->tpg, 0, 0, vsen->frame); 192 return vsen->frame; 193} 194 195static int vimc_sen_s_stream(struct v4l2_subdev *sd, int enable) 196{ 197 struct vimc_sen_device *vsen = 198 container_of(sd, struct vimc_sen_device, sd); 199 200 if (enable) { 201 const struct vimc_pix_map *vpix; 202 unsigned int frame_size; 203 204 /* Calculate the frame size */ 205 vpix = vimc_pix_map_by_code(vsen->mbus_format.code); 206 frame_size = vsen->mbus_format.width * vpix->bpp * 207 vsen->mbus_format.height; 208 209 /* 210 * Allocate the frame buffer. Use vmalloc to be able to 211 * allocate a large amount of memory 212 */ 213 vsen->frame = vmalloc(frame_size); 214 if (!vsen->frame) 215 return -ENOMEM; 216 217 /* configure the test pattern generator */ 218 vimc_sen_tpg_s_format(vsen); 219 220 } else { 221 222 vfree(vsen->frame); 223 vsen->frame = NULL; 224 } 225 226 return 0; 227} 228 229static const struct v4l2_subdev_core_ops vimc_sen_core_ops = { 230 .log_status = v4l2_ctrl_subdev_log_status, 231 .subscribe_event = v4l2_ctrl_subdev_subscribe_event, 232 .unsubscribe_event = v4l2_event_subdev_unsubscribe, 233}; 234 235static const struct v4l2_subdev_video_ops vimc_sen_video_ops = { 236 .s_stream = vimc_sen_s_stream, 237}; 238 239static const struct v4l2_subdev_ops vimc_sen_ops = { 240 .core = &vimc_sen_core_ops, 241 .pad = &vimc_sen_pad_ops, 242 .video = &vimc_sen_video_ops, 243}; 244 245static int vimc_sen_s_ctrl(struct v4l2_ctrl *ctrl) 246{ 247 struct vimc_sen_device *vsen = 248 container_of(ctrl->handler, struct vimc_sen_device, hdl); 249 250 switch (ctrl->id) { 251 case VIMC_CID_TEST_PATTERN: 252 tpg_s_pattern(&vsen->tpg, ctrl->val); 253 break; 254 case V4L2_CID_HFLIP: 255 tpg_s_hflip(&vsen->tpg, ctrl->val); 256 break; 257 case V4L2_CID_VFLIP: 258 tpg_s_vflip(&vsen->tpg, ctrl->val); 259 break; 260 case V4L2_CID_BRIGHTNESS: 261 tpg_s_brightness(&vsen->tpg, ctrl->val); 262 break; 263 case V4L2_CID_CONTRAST: 264 tpg_s_contrast(&vsen->tpg, ctrl->val); 265 break; 266 case V4L2_CID_HUE: 267 tpg_s_hue(&vsen->tpg, ctrl->val); 268 break; 269 case V4L2_CID_SATURATION: 270 tpg_s_saturation(&vsen->tpg, ctrl->val); 271 break; 272 default: 273 return -EINVAL; 274 } 275 return 0; 276} 277 278static const struct v4l2_ctrl_ops vimc_sen_ctrl_ops = { 279 .s_ctrl = vimc_sen_s_ctrl, 280}; 281 282static void vimc_sen_release(struct v4l2_subdev *sd) 283{ 284 struct vimc_sen_device *vsen = 285 container_of(sd, struct vimc_sen_device, sd); 286 287 v4l2_ctrl_handler_free(&vsen->hdl); 288 tpg_free(&vsen->tpg); 289 media_entity_cleanup(vsen->ved.ent); 290 kfree(vsen); 291} 292 293static const struct v4l2_subdev_internal_ops vimc_sen_int_ops = { 294 .release = vimc_sen_release, 295}; 296 297void vimc_sen_rm(struct vimc_device *vimc, struct vimc_ent_device *ved) 298{ 299 struct vimc_sen_device *vsen; 300 301 vsen = container_of(ved, struct vimc_sen_device, ved); 302 v4l2_device_unregister_subdev(&vsen->sd); 303} 304 305/* Image Processing Controls */ 306static const struct v4l2_ctrl_config vimc_sen_ctrl_class = { 307 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY, 308 .id = VIMC_CID_VIMC_CLASS, 309 .name = "VIMC Controls", 310 .type = V4L2_CTRL_TYPE_CTRL_CLASS, 311}; 312 313static const struct v4l2_ctrl_config vimc_sen_ctrl_test_pattern = { 314 .ops = &vimc_sen_ctrl_ops, 315 .id = VIMC_CID_TEST_PATTERN, 316 .name = "Test Pattern", 317 .type = V4L2_CTRL_TYPE_MENU, 318 .max = TPG_PAT_NOISE, 319 .qmenu = tpg_pattern_strings, 320}; 321 322struct vimc_ent_device *vimc_sen_add(struct vimc_device *vimc, 323 const char *vcfg_name) 324{ 325 struct v4l2_device *v4l2_dev = &vimc->v4l2_dev; 326 struct vimc_sen_device *vsen; 327 int ret; 328 329 /* Allocate the vsen struct */ 330 vsen = kzalloc(sizeof(*vsen), GFP_KERNEL); 331 if (!vsen) 332 return NULL; 333 334 v4l2_ctrl_handler_init(&vsen->hdl, 4); 335 336 v4l2_ctrl_new_custom(&vsen->hdl, &vimc_sen_ctrl_class, NULL); 337 v4l2_ctrl_new_custom(&vsen->hdl, &vimc_sen_ctrl_test_pattern, NULL); 338 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops, 339 V4L2_CID_VFLIP, 0, 1, 1, 0); 340 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops, 341 V4L2_CID_HFLIP, 0, 1, 1, 0); 342 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops, 343 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128); 344 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops, 345 V4L2_CID_CONTRAST, 0, 255, 1, 128); 346 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops, 347 V4L2_CID_HUE, -128, 127, 1, 0); 348 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops, 349 V4L2_CID_SATURATION, 0, 255, 1, 128); 350 vsen->sd.ctrl_handler = &vsen->hdl; 351 if (vsen->hdl.error) { 352 ret = vsen->hdl.error; 353 goto err_free_vsen; 354 } 355 356 /* Initialize the test pattern generator */ 357 tpg_init(&vsen->tpg, vsen->mbus_format.width, 358 vsen->mbus_format.height); 359 ret = tpg_alloc(&vsen->tpg, VIMC_FRAME_MAX_WIDTH); 360 if (ret) 361 goto err_free_hdl; 362 363 /* Initialize ved and sd */ 364 vsen->pad.flags = MEDIA_PAD_FL_SOURCE; 365 ret = vimc_ent_sd_register(&vsen->ved, &vsen->sd, v4l2_dev, 366 vcfg_name, 367 MEDIA_ENT_F_CAM_SENSOR, 1, &vsen->pad, 368 &vimc_sen_int_ops, &vimc_sen_ops); 369 if (ret) 370 goto err_free_tpg; 371 372 vsen->ved.process_frame = vimc_sen_process_frame; 373 vsen->ved.dev = &vimc->pdev.dev; 374 375 /* Initialize the frame format */ 376 vsen->mbus_format = fmt_default; 377 378 return &vsen->ved; 379 380err_free_tpg: 381 tpg_free(&vsen->tpg); 382err_free_hdl: 383 v4l2_ctrl_handler_free(&vsen->hdl); 384err_free_vsen: 385 kfree(vsen); 386 387 return NULL; 388}