vsp1_histo.c 16 KB

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