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