vsp1_wpf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * vsp1_wpf.c -- R-Car VSP1 Write Pixel Formatter
  3. *
  4. * Copyright (C) 2013-2014 Renesas Electronics Corporation
  5. *
  6. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/device.h>
  14. #include <media/v4l2-subdev.h>
  15. #include "vsp1.h"
  16. #include "vsp1_dl.h"
  17. #include "vsp1_pipe.h"
  18. #include "vsp1_rwpf.h"
  19. #include "vsp1_video.h"
  20. #define WPF_GEN2_MAX_WIDTH 2048U
  21. #define WPF_GEN2_MAX_HEIGHT 2048U
  22. #define WPF_GEN3_MAX_WIDTH 8190U
  23. #define WPF_GEN3_MAX_HEIGHT 8190U
  24. /* -----------------------------------------------------------------------------
  25. * Device Access
  26. */
  27. static inline void vsp1_wpf_write(struct vsp1_rwpf *wpf,
  28. struct vsp1_dl_list *dl, u32 reg, u32 data)
  29. {
  30. vsp1_dl_list_write(dl, reg + wpf->entity.index * VI6_WPF_OFFSET, data);
  31. }
  32. /* -----------------------------------------------------------------------------
  33. * Controls
  34. */
  35. enum wpf_flip_ctrl {
  36. WPF_CTRL_VFLIP = 0,
  37. WPF_CTRL_HFLIP = 1,
  38. };
  39. static int vsp1_wpf_set_rotation(struct vsp1_rwpf *wpf, unsigned int rotation)
  40. {
  41. struct vsp1_video *video = wpf->video;
  42. struct v4l2_mbus_framefmt *sink_format;
  43. struct v4l2_mbus_framefmt *source_format;
  44. bool rotate;
  45. int ret = 0;
  46. /*
  47. * Only consider the 0°/180° from/to 90°/270° modifications, the rest
  48. * is taken care of by the flipping configuration.
  49. */
  50. rotate = rotation == 90 || rotation == 270;
  51. if (rotate == wpf->flip.rotate)
  52. return 0;
  53. /* Changing rotation isn't allowed when buffers are allocated. */
  54. mutex_lock(&video->lock);
  55. if (vb2_is_busy(&video->queue)) {
  56. ret = -EBUSY;
  57. goto done;
  58. }
  59. sink_format = vsp1_entity_get_pad_format(&wpf->entity,
  60. wpf->entity.config,
  61. RWPF_PAD_SINK);
  62. source_format = vsp1_entity_get_pad_format(&wpf->entity,
  63. wpf->entity.config,
  64. RWPF_PAD_SOURCE);
  65. mutex_lock(&wpf->entity.lock);
  66. if (rotate) {
  67. source_format->width = sink_format->height;
  68. source_format->height = sink_format->width;
  69. } else {
  70. source_format->width = sink_format->width;
  71. source_format->height = sink_format->height;
  72. }
  73. wpf->flip.rotate = rotate;
  74. mutex_unlock(&wpf->entity.lock);
  75. done:
  76. mutex_unlock(&video->lock);
  77. return ret;
  78. }
  79. static int vsp1_wpf_s_ctrl(struct v4l2_ctrl *ctrl)
  80. {
  81. struct vsp1_rwpf *wpf =
  82. container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
  83. unsigned int rotation;
  84. u32 flip = 0;
  85. int ret;
  86. /* Update the rotation. */
  87. rotation = wpf->flip.ctrls.rotate ? wpf->flip.ctrls.rotate->val : 0;
  88. ret = vsp1_wpf_set_rotation(wpf, rotation);
  89. if (ret < 0)
  90. return ret;
  91. /*
  92. * Compute the flip value resulting from all three controls, with
  93. * rotation by 180° flipping the image in both directions. Store the
  94. * result in the pending flip field for the next frame that will be
  95. * processed.
  96. */
  97. if (wpf->flip.ctrls.vflip->val)
  98. flip |= BIT(WPF_CTRL_VFLIP);
  99. if (wpf->flip.ctrls.hflip && wpf->flip.ctrls.hflip->val)
  100. flip |= BIT(WPF_CTRL_HFLIP);
  101. if (rotation == 180 || rotation == 270)
  102. flip ^= BIT(WPF_CTRL_VFLIP) | BIT(WPF_CTRL_HFLIP);
  103. spin_lock_irq(&wpf->flip.lock);
  104. wpf->flip.pending = flip;
  105. spin_unlock_irq(&wpf->flip.lock);
  106. return 0;
  107. }
  108. static const struct v4l2_ctrl_ops vsp1_wpf_ctrl_ops = {
  109. .s_ctrl = vsp1_wpf_s_ctrl,
  110. };
  111. static int wpf_init_controls(struct vsp1_rwpf *wpf)
  112. {
  113. struct vsp1_device *vsp1 = wpf->entity.vsp1;
  114. unsigned int num_flip_ctrls;
  115. spin_lock_init(&wpf->flip.lock);
  116. if (wpf->entity.index != 0) {
  117. /* Only WPF0 supports flipping. */
  118. num_flip_ctrls = 0;
  119. } else if (vsp1->info->features & VSP1_HAS_WPF_HFLIP) {
  120. /*
  121. * When horizontal flip is supported the WPF implements three
  122. * controls (horizontal flip, vertical flip and rotation).
  123. */
  124. num_flip_ctrls = 3;
  125. } else if (vsp1->info->features & VSP1_HAS_WPF_VFLIP) {
  126. /*
  127. * When only vertical flip is supported the WPF implements a
  128. * single control (vertical flip).
  129. */
  130. num_flip_ctrls = 1;
  131. } else {
  132. /* Otherwise flipping is not supported. */
  133. num_flip_ctrls = 0;
  134. }
  135. vsp1_rwpf_init_ctrls(wpf, num_flip_ctrls);
  136. if (num_flip_ctrls >= 1) {
  137. wpf->flip.ctrls.vflip =
  138. v4l2_ctrl_new_std(&wpf->ctrls, &vsp1_wpf_ctrl_ops,
  139. V4L2_CID_VFLIP, 0, 1, 1, 0);
  140. }
  141. if (num_flip_ctrls == 3) {
  142. wpf->flip.ctrls.hflip =
  143. v4l2_ctrl_new_std(&wpf->ctrls, &vsp1_wpf_ctrl_ops,
  144. V4L2_CID_HFLIP, 0, 1, 1, 0);
  145. wpf->flip.ctrls.rotate =
  146. v4l2_ctrl_new_std(&wpf->ctrls, &vsp1_wpf_ctrl_ops,
  147. V4L2_CID_ROTATE, 0, 270, 90, 0);
  148. v4l2_ctrl_cluster(3, &wpf->flip.ctrls.vflip);
  149. }
  150. if (wpf->ctrls.error) {
  151. dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
  152. wpf->entity.index);
  153. return wpf->ctrls.error;
  154. }
  155. return 0;
  156. }
  157. /* -----------------------------------------------------------------------------
  158. * V4L2 Subdevice Core Operations
  159. */
  160. static int wpf_s_stream(struct v4l2_subdev *subdev, int enable)
  161. {
  162. struct vsp1_rwpf *wpf = to_rwpf(subdev);
  163. struct vsp1_device *vsp1 = wpf->entity.vsp1;
  164. if (enable)
  165. return 0;
  166. /*
  167. * Write to registers directly when stopping the stream as there will be
  168. * no pipeline run to apply the display list.
  169. */
  170. vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0);
  171. vsp1_write(vsp1, wpf->entity.index * VI6_WPF_OFFSET +
  172. VI6_WPF_SRCRPF, 0);
  173. return 0;
  174. }
  175. /* -----------------------------------------------------------------------------
  176. * V4L2 Subdevice Operations
  177. */
  178. static const struct v4l2_subdev_video_ops wpf_video_ops = {
  179. .s_stream = wpf_s_stream,
  180. };
  181. static const struct v4l2_subdev_ops wpf_ops = {
  182. .video = &wpf_video_ops,
  183. .pad = &vsp1_rwpf_pad_ops,
  184. };
  185. /* -----------------------------------------------------------------------------
  186. * VSP1 Entity Operations
  187. */
  188. static void vsp1_wpf_destroy(struct vsp1_entity *entity)
  189. {
  190. struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
  191. vsp1_dlm_destroy(wpf->dlm);
  192. }
  193. static void wpf_configure(struct vsp1_entity *entity,
  194. struct vsp1_pipeline *pipe,
  195. struct vsp1_dl_list *dl,
  196. enum vsp1_entity_params params)
  197. {
  198. struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev);
  199. struct vsp1_device *vsp1 = wpf->entity.vsp1;
  200. const struct v4l2_mbus_framefmt *source_format;
  201. const struct v4l2_mbus_framefmt *sink_format;
  202. unsigned int i;
  203. u32 outfmt = 0;
  204. u32 srcrpf = 0;
  205. if (params == VSP1_ENTITY_PARAMS_RUNTIME) {
  206. const unsigned int mask = BIT(WPF_CTRL_VFLIP)
  207. | BIT(WPF_CTRL_HFLIP);
  208. unsigned long flags;
  209. spin_lock_irqsave(&wpf->flip.lock, flags);
  210. wpf->flip.active = (wpf->flip.active & ~mask)
  211. | (wpf->flip.pending & mask);
  212. spin_unlock_irqrestore(&wpf->flip.lock, flags);
  213. outfmt = (wpf->alpha << VI6_WPF_OUTFMT_PDV_SHIFT) | wpf->outfmt;
  214. if (wpf->flip.active & BIT(WPF_CTRL_VFLIP))
  215. outfmt |= VI6_WPF_OUTFMT_FLP;
  216. if (wpf->flip.active & BIT(WPF_CTRL_HFLIP))
  217. outfmt |= VI6_WPF_OUTFMT_HFLP;
  218. vsp1_wpf_write(wpf, dl, VI6_WPF_OUTFMT, outfmt);
  219. return;
  220. }
  221. sink_format = vsp1_entity_get_pad_format(&wpf->entity,
  222. wpf->entity.config,
  223. RWPF_PAD_SINK);
  224. source_format = vsp1_entity_get_pad_format(&wpf->entity,
  225. wpf->entity.config,
  226. RWPF_PAD_SOURCE);
  227. if (params == VSP1_ENTITY_PARAMS_PARTITION) {
  228. const struct v4l2_pix_format_mplane *format = &wpf->format;
  229. const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
  230. struct vsp1_rwpf_memory mem = wpf->mem;
  231. unsigned int flip = wpf->flip.active;
  232. unsigned int width = sink_format->width;
  233. unsigned int height = sink_format->height;
  234. unsigned int offset;
  235. /*
  236. * Cropping. The partition algorithm can split the image into
  237. * multiple slices.
  238. */
  239. if (pipe->partitions > 1)
  240. width = pipe->partition.width;
  241. vsp1_wpf_write(wpf, dl, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
  242. (0 << VI6_WPF_SZCLIP_OFST_SHIFT) |
  243. (width << VI6_WPF_SZCLIP_SIZE_SHIFT));
  244. vsp1_wpf_write(wpf, dl, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
  245. (0 << VI6_WPF_SZCLIP_OFST_SHIFT) |
  246. (height << VI6_WPF_SZCLIP_SIZE_SHIFT));
  247. if (pipe->lif)
  248. return;
  249. /*
  250. * Update the memory offsets based on flipping configuration.
  251. * The destination addresses point to the locations where the
  252. * VSP starts writing to memory, which can be any corner of the
  253. * image depending on the combination of flipping and rotation.
  254. */
  255. /*
  256. * First take the partition left coordinate into account.
  257. * Compute the offset to order the partitions correctly on the
  258. * output based on whether flipping is enabled. Consider
  259. * horizontal flipping when rotation is disabled but vertical
  260. * flipping when rotation is enabled, as rotating the image
  261. * switches the horizontal and vertical directions. The offset
  262. * is applied horizontally or vertically accordingly.
  263. */
  264. if (flip & BIT(WPF_CTRL_HFLIP) && !wpf->flip.rotate)
  265. offset = format->width - pipe->partition.left
  266. - pipe->partition.width;
  267. else if (flip & BIT(WPF_CTRL_VFLIP) && wpf->flip.rotate)
  268. offset = format->height - pipe->partition.left
  269. - pipe->partition.width;
  270. else
  271. offset = pipe->partition.left;
  272. for (i = 0; i < format->num_planes; ++i) {
  273. unsigned int hsub = i > 0 ? fmtinfo->hsub : 1;
  274. unsigned int vsub = i > 0 ? fmtinfo->vsub : 1;
  275. if (wpf->flip.rotate)
  276. mem.addr[i] += offset / vsub
  277. * format->plane_fmt[i].bytesperline;
  278. else
  279. mem.addr[i] += offset / hsub
  280. * fmtinfo->bpp[i] / 8;
  281. }
  282. if (flip & BIT(WPF_CTRL_VFLIP)) {
  283. /*
  284. * When rotating the output (after rotation) image
  285. * height is equal to the partition width (before
  286. * rotation). Otherwise it is equal to the output
  287. * image height.
  288. */
  289. if (wpf->flip.rotate)
  290. height = pipe->partition.width;
  291. else
  292. height = format->height;
  293. mem.addr[0] += (height - 1)
  294. * format->plane_fmt[0].bytesperline;
  295. if (format->num_planes > 1) {
  296. offset = (height / fmtinfo->vsub - 1)
  297. * format->plane_fmt[1].bytesperline;
  298. mem.addr[1] += offset;
  299. mem.addr[2] += offset;
  300. }
  301. }
  302. if (wpf->flip.rotate && !(flip & BIT(WPF_CTRL_HFLIP))) {
  303. unsigned int hoffset = max(0, (int)format->width - 16);
  304. /*
  305. * Compute the output coordinate. The partition
  306. * horizontal (left) offset becomes a vertical offset.
  307. */
  308. for (i = 0; i < format->num_planes; ++i) {
  309. unsigned int hsub = i > 0 ? fmtinfo->hsub : 1;
  310. mem.addr[i] += hoffset / hsub
  311. * fmtinfo->bpp[i] / 8;
  312. }
  313. }
  314. /*
  315. * On Gen3 hardware the SPUVS bit has no effect on 3-planar
  316. * formats. Swap the U and V planes manually in that case.
  317. */
  318. if (vsp1->info->gen == 3 && format->num_planes == 3 &&
  319. fmtinfo->swap_uv)
  320. swap(mem.addr[1], mem.addr[2]);
  321. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_Y, mem.addr[0]);
  322. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C0, mem.addr[1]);
  323. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C1, mem.addr[2]);
  324. return;
  325. }
  326. /* Format */
  327. if (!pipe->lif) {
  328. const struct v4l2_pix_format_mplane *format = &wpf->format;
  329. const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
  330. outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
  331. if (wpf->flip.rotate)
  332. outfmt |= VI6_WPF_OUTFMT_ROT;
  333. if (fmtinfo->alpha)
  334. outfmt |= VI6_WPF_OUTFMT_PXA;
  335. if (fmtinfo->swap_yc)
  336. outfmt |= VI6_WPF_OUTFMT_SPYCS;
  337. if (fmtinfo->swap_uv)
  338. outfmt |= VI6_WPF_OUTFMT_SPUVS;
  339. /* Destination stride and byte swapping. */
  340. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_STRIDE_Y,
  341. format->plane_fmt[0].bytesperline);
  342. if (format->num_planes > 1)
  343. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_STRIDE_C,
  344. format->plane_fmt[1].bytesperline);
  345. vsp1_wpf_write(wpf, dl, VI6_WPF_DSWAP, fmtinfo->swap);
  346. if (vsp1->info->features & VSP1_HAS_WPF_HFLIP &&
  347. wpf->entity.index == 0)
  348. vsp1_wpf_write(wpf, dl, VI6_WPF_ROT_CTRL,
  349. VI6_WPF_ROT_CTRL_LN16 |
  350. (256 << VI6_WPF_ROT_CTRL_LMEM_WD_SHIFT));
  351. }
  352. if (sink_format->code != source_format->code)
  353. outfmt |= VI6_WPF_OUTFMT_CSC;
  354. wpf->outfmt = outfmt;
  355. vsp1_dl_list_write(dl, VI6_DPR_WPF_FPORCH(wpf->entity.index),
  356. VI6_DPR_WPF_FPORCH_FP_WPFN);
  357. vsp1_dl_list_write(dl, VI6_WPF_WRBCK_CTRL, 0);
  358. /*
  359. * Sources. If the pipeline has a single input and BRU is not used,
  360. * configure it as the master layer. Otherwise configure all
  361. * inputs as sub-layers and select the virtual RPF as the master
  362. * layer.
  363. */
  364. for (i = 0; i < vsp1->info->rpf_count; ++i) {
  365. struct vsp1_rwpf *input = pipe->inputs[i];
  366. if (!input)
  367. continue;
  368. srcrpf |= (!pipe->bru && pipe->num_inputs == 1)
  369. ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
  370. : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
  371. }
  372. if (pipe->bru || pipe->num_inputs > 1)
  373. srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
  374. vsp1_wpf_write(wpf, dl, VI6_WPF_SRCRPF, srcrpf);
  375. /* Enable interrupts */
  376. vsp1_dl_list_write(dl, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
  377. vsp1_dl_list_write(dl, VI6_WPF_IRQ_ENB(wpf->entity.index),
  378. VI6_WFP_IRQ_ENB_DFEE);
  379. }
  380. static unsigned int wpf_max_width(struct vsp1_entity *entity,
  381. struct vsp1_pipeline *pipe)
  382. {
  383. struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev);
  384. return wpf->flip.rotate ? 256 : wpf->max_width;
  385. }
  386. static const struct vsp1_entity_operations wpf_entity_ops = {
  387. .destroy = vsp1_wpf_destroy,
  388. .configure = wpf_configure,
  389. .max_width = wpf_max_width,
  390. };
  391. /* -----------------------------------------------------------------------------
  392. * Initialization and Cleanup
  393. */
  394. struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
  395. {
  396. struct vsp1_rwpf *wpf;
  397. char name[6];
  398. int ret;
  399. wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
  400. if (wpf == NULL)
  401. return ERR_PTR(-ENOMEM);
  402. if (vsp1->info->gen == 2) {
  403. wpf->max_width = WPF_GEN2_MAX_WIDTH;
  404. wpf->max_height = WPF_GEN2_MAX_HEIGHT;
  405. } else {
  406. wpf->max_width = WPF_GEN3_MAX_WIDTH;
  407. wpf->max_height = WPF_GEN3_MAX_HEIGHT;
  408. }
  409. wpf->entity.ops = &wpf_entity_ops;
  410. wpf->entity.type = VSP1_ENTITY_WPF;
  411. wpf->entity.index = index;
  412. sprintf(name, "wpf.%u", index);
  413. ret = vsp1_entity_init(vsp1, &wpf->entity, name, 2, &wpf_ops,
  414. MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
  415. if (ret < 0)
  416. return ERR_PTR(ret);
  417. /* Initialize the display list manager. */
  418. wpf->dlm = vsp1_dlm_create(vsp1, index, 64);
  419. if (!wpf->dlm) {
  420. ret = -ENOMEM;
  421. goto error;
  422. }
  423. /* Initialize the control handler. */
  424. ret = wpf_init_controls(wpf);
  425. if (ret < 0) {
  426. dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
  427. index);
  428. goto error;
  429. }
  430. v4l2_ctrl_handler_setup(&wpf->ctrls);
  431. return wpf;
  432. error:
  433. vsp1_entity_destroy(&wpf->entity);
  434. return ERR_PTR(ret);
  435. }