vsp1_wpf.c 15 KB

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