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