1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * vsp1_histo.c -- R-Car VSP1 Histogram API 4 * 5 * Copyright (C) 2016 Renesas Electronics Corporation 6 * Copyright (C) 2016 Laurent Pinchart 7 * 8 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com) 9 */ 10 11 #include <linux/device.h> 12 #include <linux/gfp.h> 13 14 #include <media/v4l2-ioctl.h> 15 #include <media/v4l2-subdev.h> 16 #include <media/videobuf2-vmalloc.h> 17 18 #include "vsp1.h" 19 #include "vsp1_histo.h" 20 #include "vsp1_pipe.h" 21 22 #define HISTO_MIN_SIZE 4U 23 #define HISTO_MAX_SIZE 8192U 24 25 /* ----------------------------------------------------------------------------- 26 * Buffer Operations 27 */ 28 29 static inline struct vsp1_histogram_buffer * 30 to_vsp1_histogram_buffer(struct vb2_v4l2_buffer *vbuf) 31 { 32 return container_of(vbuf, struct vsp1_histogram_buffer, buf); 33 } 34 35 struct vsp1_histogram_buffer * 36 vsp1_histogram_buffer_get(struct vsp1_histogram *histo) 37 { 38 struct vsp1_histogram_buffer *buf = NULL; 39 40 spin_lock(&histo->irqlock); 41 42 if (list_empty(&histo->irqqueue)) 43 goto done; 44 45 buf = list_first_entry(&histo->irqqueue, struct vsp1_histogram_buffer, 46 queue); 47 list_del(&buf->queue); 48 histo->readout = true; 49 50 done: 51 spin_unlock(&histo->irqlock); 52 return buf; 53 } 54 55 void vsp1_histogram_buffer_complete(struct vsp1_histogram *histo, 56 struct vsp1_histogram_buffer *buf, 57 size_t size) 58 { 59 struct vsp1_pipeline *pipe = histo->entity.pipe; 60 61 /* 62 * The pipeline pointer is guaranteed to be valid as this function is 63 * called from the frame completion interrupt handler, which can only 64 * occur when video streaming is active. 65 */ 66 buf->buf.sequence = pipe->sequence; 67 buf->buf.vb2_buf.timestamp = ktime_get_ns(); 68 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, size); 69 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE); 70 71 spin_lock(&histo->irqlock); 72 histo->readout = false; 73 wake_up(&histo->wait_queue); 74 spin_unlock(&histo->irqlock); 75 } 76 77 /* ----------------------------------------------------------------------------- 78 * videobuf2 Queue Operations 79 */ 80 81 static int histo_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers, 82 unsigned int *nplanes, unsigned int sizes[], 83 struct device *alloc_devs[]) 84 { 85 struct vsp1_histogram *histo = vb2_get_drv_priv(vq); 86 87 if (*nplanes) { 88 if (*nplanes != 1) 89 return -EINVAL; 90 91 if (sizes[0] < histo->data_size) 92 return -EINVAL; 93 94 return 0; 95 } 96 97 *nplanes = 1; 98 sizes[0] = histo->data_size; 99 100 return 0; 101 } 102 103 static int histo_buffer_prepare(struct vb2_buffer *vb) 104 { 105 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 106 struct vsp1_histogram *histo = vb2_get_drv_priv(vb->vb2_queue); 107 struct vsp1_histogram_buffer *buf = to_vsp1_histogram_buffer(vbuf); 108 109 if (vb->num_planes != 1) 110 return -EINVAL; 111 112 if (vb2_plane_size(vb, 0) < histo->data_size) 113 return -EINVAL; 114 115 buf->addr = vb2_plane_vaddr(vb, 0); 116 117 return 0; 118 } 119 120 static void histo_buffer_queue(struct vb2_buffer *vb) 121 { 122 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 123 struct vsp1_histogram *histo = vb2_get_drv_priv(vb->vb2_queue); 124 struct vsp1_histogram_buffer *buf = to_vsp1_histogram_buffer(vbuf); 125 126 spin_lock_irq(&histo->irqlock); 127 list_add_tail(&buf->queue, &histo->irqqueue); 128 spin_unlock_irq(&histo->irqlock); 129 } 130 131 static int histo_start_streaming(struct vb2_queue *vq, unsigned int count) 132 { 133 return 0; 134 } 135 136 static void histo_stop_streaming(struct vb2_queue *vq) 137 { 138 struct vsp1_histogram *histo = vb2_get_drv_priv(vq); 139 struct vsp1_histogram_buffer *buffer; 140 141 spin_lock_irq(&histo->irqlock); 142 143 /* Remove all buffers from the IRQ queue. */ 144 list_for_each_entry(buffer, &histo->irqqueue, queue) 145 vb2_buffer_done(&buffer->buf.vb2_buf, VB2_BUF_STATE_ERROR); 146 INIT_LIST_HEAD(&histo->irqqueue); 147 148 /* Wait for the buffer being read out (if any) to complete. */ 149 wait_event_lock_irq(histo->wait_queue, !histo->readout, histo->irqlock); 150 151 spin_unlock_irq(&histo->irqlock); 152 } 153 154 static const struct vb2_ops histo_video_queue_qops = { 155 .queue_setup = histo_queue_setup, 156 .buf_prepare = histo_buffer_prepare, 157 .buf_queue = histo_buffer_queue, 158 .start_streaming = histo_start_streaming, 159 .stop_streaming = histo_stop_streaming, 160 }; 161 162 /* ----------------------------------------------------------------------------- 163 * V4L2 Subdevice Operations 164 */ 165 166 static int histo_enum_mbus_code(struct v4l2_subdev *subdev, 167 struct v4l2_subdev_state *sd_state, 168 struct v4l2_subdev_mbus_code_enum *code) 169 { 170 struct vsp1_histogram *histo = subdev_to_histo(subdev); 171 172 if (code->pad == HISTO_PAD_SOURCE) { 173 code->code = MEDIA_BUS_FMT_FIXED; 174 return 0; 175 } 176 177 return vsp1_subdev_enum_mbus_code(subdev, sd_state, code, 178 histo->formats, 179 histo->num_formats); 180 } 181 182 static int histo_enum_frame_size(struct v4l2_subdev *subdev, 183 struct v4l2_subdev_state *sd_state, 184 struct v4l2_subdev_frame_size_enum *fse) 185 { 186 if (fse->pad != HISTO_PAD_SINK) 187 return -EINVAL; 188 189 return vsp1_subdev_enum_frame_size(subdev, sd_state, fse, 190 HISTO_MIN_SIZE, 191 HISTO_MIN_SIZE, HISTO_MAX_SIZE, 192 HISTO_MAX_SIZE); 193 } 194 195 static int histo_get_selection(struct v4l2_subdev *subdev, 196 struct v4l2_subdev_state *sd_state, 197 struct v4l2_subdev_selection *sel) 198 { 199 struct vsp1_histogram *histo = subdev_to_histo(subdev); 200 struct v4l2_subdev_state *state; 201 struct v4l2_mbus_framefmt *format; 202 struct v4l2_rect *crop; 203 int ret = 0; 204 205 if (sel->pad != HISTO_PAD_SINK) 206 return -EINVAL; 207 208 mutex_lock(&histo->entity.lock); 209 210 state = vsp1_entity_get_state(&histo->entity, sd_state, sel->which); 211 if (!state) { 212 ret = -EINVAL; 213 goto done; 214 } 215 216 switch (sel->target) { 217 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 218 case V4L2_SEL_TGT_COMPOSE_DEFAULT: 219 crop = v4l2_subdev_state_get_crop(state, HISTO_PAD_SINK); 220 sel->r.left = 0; 221 sel->r.top = 0; 222 sel->r.width = crop->width; 223 sel->r.height = crop->height; 224 break; 225 226 case V4L2_SEL_TGT_CROP_BOUNDS: 227 case V4L2_SEL_TGT_CROP_DEFAULT: 228 format = v4l2_subdev_state_get_format(state, HISTO_PAD_SINK); 229 sel->r.left = 0; 230 sel->r.top = 0; 231 sel->r.width = format->width; 232 sel->r.height = format->height; 233 break; 234 235 case V4L2_SEL_TGT_COMPOSE: 236 sel->r = *v4l2_subdev_state_get_compose(state, sel->pad); 237 break; 238 239 case V4L2_SEL_TGT_CROP: 240 sel->r = *v4l2_subdev_state_get_crop(state, sel->pad); 241 break; 242 243 default: 244 ret = -EINVAL; 245 break; 246 } 247 248 done: 249 mutex_unlock(&histo->entity.lock); 250 return ret; 251 } 252 253 static int histo_set_crop(struct v4l2_subdev *subdev, 254 struct v4l2_subdev_state *sd_state, 255 struct v4l2_subdev_selection *sel) 256 { 257 struct v4l2_mbus_framefmt *format; 258 259 /* The crop rectangle must be inside the input frame. */ 260 format = v4l2_subdev_state_get_format(sd_state, HISTO_PAD_SINK); 261 sel->r.left = clamp_t(unsigned int, sel->r.left, 0, format->width - 1); 262 sel->r.top = clamp_t(unsigned int, sel->r.top, 0, format->height - 1); 263 sel->r.width = clamp_t(unsigned int, sel->r.width, HISTO_MIN_SIZE, 264 format->width - sel->r.left); 265 sel->r.height = clamp_t(unsigned int, sel->r.height, HISTO_MIN_SIZE, 266 format->height - sel->r.top); 267 268 /* Set the crop rectangle and reset the compose rectangle. */ 269 *v4l2_subdev_state_get_crop(sd_state, sel->pad) = sel->r; 270 *v4l2_subdev_state_get_compose(sd_state, sel->pad) = sel->r; 271 272 return 0; 273 } 274 275 static int histo_set_compose(struct v4l2_subdev *subdev, 276 struct v4l2_subdev_state *sd_state, 277 struct v4l2_subdev_selection *sel) 278 { 279 struct v4l2_rect *compose; 280 struct v4l2_rect *crop; 281 unsigned int ratio; 282 283 /* 284 * The compose rectangle is used to configure downscaling, the top left 285 * corner is fixed to (0,0) and the size to 1/2 or 1/4 of the crop 286 * rectangle. 287 */ 288 sel->r.left = 0; 289 sel->r.top = 0; 290 291 crop = v4l2_subdev_state_get_crop(sd_state, sel->pad); 292 293 /* 294 * Clamp the width and height to acceptable values first and then 295 * compute the closest rounded dividing ratio. 296 * 297 * Ratio Rounded ratio 298 * -------------------------- 299 * [1.0 1.5[ 1 300 * [1.5 3.0[ 2 301 * [3.0 4.0] 4 302 * 303 * The rounded ratio can be computed using 304 * 305 * 1 << (ceil(ratio * 2) / 3) 306 */ 307 sel->r.width = clamp(sel->r.width, crop->width / 4, crop->width); 308 ratio = 1 << (crop->width * 2 / sel->r.width / 3); 309 sel->r.width = crop->width / ratio; 310 311 312 sel->r.height = clamp(sel->r.height, crop->height / 4, crop->height); 313 ratio = 1 << (crop->height * 2 / sel->r.height / 3); 314 sel->r.height = crop->height / ratio; 315 316 compose = v4l2_subdev_state_get_compose(sd_state, sel->pad); 317 *compose = sel->r; 318 319 return 0; 320 } 321 322 static int histo_set_selection(struct v4l2_subdev *subdev, 323 struct v4l2_subdev_state *sd_state, 324 struct v4l2_subdev_selection *sel) 325 { 326 struct vsp1_histogram *histo = subdev_to_histo(subdev); 327 struct v4l2_subdev_state *state; 328 int ret; 329 330 if (sel->pad != HISTO_PAD_SINK) 331 return -EINVAL; 332 333 mutex_lock(&histo->entity.lock); 334 335 state = vsp1_entity_get_state(&histo->entity, sd_state, sel->which); 336 if (!state) { 337 ret = -EINVAL; 338 goto done; 339 } 340 341 if (sel->target == V4L2_SEL_TGT_CROP) 342 ret = histo_set_crop(subdev, state, sel); 343 else if (sel->target == V4L2_SEL_TGT_COMPOSE) 344 ret = histo_set_compose(subdev, state, sel); 345 else 346 ret = -EINVAL; 347 348 done: 349 mutex_unlock(&histo->entity.lock); 350 return ret; 351 } 352 353 static int histo_set_format(struct v4l2_subdev *subdev, 354 struct v4l2_subdev_state *sd_state, 355 struct v4l2_subdev_format *fmt) 356 { 357 struct vsp1_histogram *histo = subdev_to_histo(subdev); 358 359 if (fmt->pad == HISTO_PAD_SOURCE) { 360 fmt->format.code = MEDIA_BUS_FMT_FIXED; 361 fmt->format.width = 0; 362 fmt->format.height = 0; 363 fmt->format.field = V4L2_FIELD_NONE; 364 fmt->format.colorspace = V4L2_COLORSPACE_RAW; 365 366 return 0; 367 } 368 369 return vsp1_subdev_set_pad_format(subdev, sd_state, fmt, 370 histo->formats, histo->num_formats, 371 HISTO_MIN_SIZE, HISTO_MIN_SIZE, 372 HISTO_MAX_SIZE, HISTO_MAX_SIZE); 373 } 374 375 static const struct v4l2_subdev_pad_ops histo_pad_ops = { 376 .enum_mbus_code = histo_enum_mbus_code, 377 .enum_frame_size = histo_enum_frame_size, 378 .get_fmt = vsp1_subdev_get_pad_format, 379 .set_fmt = histo_set_format, 380 .get_selection = histo_get_selection, 381 .set_selection = histo_set_selection, 382 }; 383 384 static const struct v4l2_subdev_ops histo_ops = { 385 .pad = &histo_pad_ops, 386 }; 387 388 /* ----------------------------------------------------------------------------- 389 * V4L2 ioctls 390 */ 391 392 static int histo_v4l2_querycap(struct file *file, void *fh, 393 struct v4l2_capability *cap) 394 { 395 struct v4l2_fh *vfh = file->private_data; 396 struct vsp1_histogram *histo = vdev_to_histo(vfh->vdev); 397 398 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING 399 | V4L2_CAP_VIDEO_CAPTURE_MPLANE 400 | V4L2_CAP_VIDEO_OUTPUT_MPLANE 401 | V4L2_CAP_META_CAPTURE; 402 403 strscpy(cap->driver, "vsp1", sizeof(cap->driver)); 404 strscpy(cap->card, histo->video.name, sizeof(cap->card)); 405 406 return 0; 407 } 408 409 static int histo_v4l2_enum_format(struct file *file, void *fh, 410 struct v4l2_fmtdesc *f) 411 { 412 struct v4l2_fh *vfh = file->private_data; 413 struct vsp1_histogram *histo = vdev_to_histo(vfh->vdev); 414 415 if (f->index > 0 || f->type != histo->queue.type) 416 return -EINVAL; 417 418 f->pixelformat = histo->meta_format; 419 420 return 0; 421 } 422 423 static int histo_v4l2_get_format(struct file *file, void *fh, 424 struct v4l2_format *format) 425 { 426 struct v4l2_fh *vfh = file->private_data; 427 struct vsp1_histogram *histo = vdev_to_histo(vfh->vdev); 428 struct v4l2_meta_format *meta = &format->fmt.meta; 429 430 if (format->type != histo->queue.type) 431 return -EINVAL; 432 433 memset(meta, 0, sizeof(*meta)); 434 435 meta->dataformat = histo->meta_format; 436 meta->buffersize = histo->data_size; 437 438 return 0; 439 } 440 441 static const struct v4l2_ioctl_ops histo_v4l2_ioctl_ops = { 442 .vidioc_querycap = histo_v4l2_querycap, 443 .vidioc_enum_fmt_meta_cap = histo_v4l2_enum_format, 444 .vidioc_g_fmt_meta_cap = histo_v4l2_get_format, 445 .vidioc_s_fmt_meta_cap = histo_v4l2_get_format, 446 .vidioc_try_fmt_meta_cap = histo_v4l2_get_format, 447 .vidioc_reqbufs = vb2_ioctl_reqbufs, 448 .vidioc_querybuf = vb2_ioctl_querybuf, 449 .vidioc_qbuf = vb2_ioctl_qbuf, 450 .vidioc_dqbuf = vb2_ioctl_dqbuf, 451 .vidioc_create_bufs = vb2_ioctl_create_bufs, 452 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 453 .vidioc_streamon = vb2_ioctl_streamon, 454 .vidioc_streamoff = vb2_ioctl_streamoff, 455 }; 456 457 /* ----------------------------------------------------------------------------- 458 * V4L2 File Operations 459 */ 460 461 static const struct v4l2_file_operations histo_v4l2_fops = { 462 .owner = THIS_MODULE, 463 .unlocked_ioctl = video_ioctl2, 464 .open = v4l2_fh_open, 465 .release = vb2_fop_release, 466 .poll = vb2_fop_poll, 467 .mmap = vb2_fop_mmap, 468 }; 469 470 static void vsp1_histogram_cleanup(struct vsp1_histogram *histo) 471 { 472 if (video_is_registered(&histo->video)) 473 video_unregister_device(&histo->video); 474 475 media_entity_cleanup(&histo->video.entity); 476 } 477 478 void vsp1_histogram_destroy(struct vsp1_entity *entity) 479 { 480 struct vsp1_histogram *histo = subdev_to_histo(&entity->subdev); 481 482 vsp1_histogram_cleanup(histo); 483 } 484 485 int vsp1_histogram_init(struct vsp1_device *vsp1, struct vsp1_histogram *histo, 486 enum vsp1_entity_type type, const char *name, 487 const struct vsp1_entity_operations *ops, 488 const unsigned int *formats, unsigned int num_formats, 489 size_t data_size, u32 meta_format) 490 { 491 int ret; 492 493 histo->formats = formats; 494 histo->num_formats = num_formats; 495 histo->data_size = data_size; 496 histo->meta_format = meta_format; 497 498 histo->pad.flags = MEDIA_PAD_FL_SINK; 499 histo->video.vfl_dir = VFL_DIR_RX; 500 501 mutex_init(&histo->lock); 502 spin_lock_init(&histo->irqlock); 503 INIT_LIST_HEAD(&histo->irqqueue); 504 init_waitqueue_head(&histo->wait_queue); 505 506 /* Initialize the VSP entity... */ 507 histo->entity.ops = ops; 508 histo->entity.type = type; 509 510 ret = vsp1_entity_init(vsp1, &histo->entity, name, 2, &histo_ops, 511 MEDIA_ENT_F_PROC_VIDEO_STATISTICS); 512 if (ret < 0) 513 return ret; 514 515 /* ... and the media entity... */ 516 ret = media_entity_pads_init(&histo->video.entity, 1, &histo->pad); 517 if (ret < 0) 518 return ret; 519 520 /* ... and the video node... */ 521 histo->video.v4l2_dev = &vsp1->v4l2_dev; 522 histo->video.fops = &histo_v4l2_fops; 523 snprintf(histo->video.name, sizeof(histo->video.name), 524 "%s histo", histo->entity.subdev.name); 525 histo->video.vfl_type = VFL_TYPE_VIDEO; 526 histo->video.release = video_device_release_empty; 527 histo->video.ioctl_ops = &histo_v4l2_ioctl_ops; 528 histo->video.device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING; 529 530 video_set_drvdata(&histo->video, histo); 531 532 /* ... and the buffers queue... */ 533 histo->queue.type = V4L2_BUF_TYPE_META_CAPTURE; 534 histo->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 535 histo->queue.lock = &histo->lock; 536 histo->queue.drv_priv = histo; 537 histo->queue.buf_struct_size = sizeof(struct vsp1_histogram_buffer); 538 histo->queue.ops = &histo_video_queue_qops; 539 histo->queue.mem_ops = &vb2_vmalloc_memops; 540 histo->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 541 histo->queue.dev = vsp1->dev; 542 ret = vb2_queue_init(&histo->queue); 543 if (ret < 0) { 544 dev_err(vsp1->dev, "failed to initialize vb2 queue\n"); 545 goto error; 546 } 547 548 /* ... and register the video device. */ 549 histo->video.queue = &histo->queue; 550 ret = video_register_device(&histo->video, VFL_TYPE_VIDEO, -1); 551 if (ret < 0) { 552 dev_err(vsp1->dev, "failed to register video device\n"); 553 goto error; 554 } 555 556 return 0; 557 558 error: 559 vsp1_histogram_cleanup(histo); 560 return ret; 561 } 562