Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 1 | /* |
| 2 | * vsp1_wpf.c -- R-Car VSP1 Write Pixel Formatter |
| 3 | * |
Laurent Pinchart | 8a1edc5 | 2014-02-06 14:42:31 -0300 | [diff] [blame] | 4 | * Copyright (C) 2013-2014 Renesas Electronics Corporation |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 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 | |
| 14 | #include <linux/device.h> |
| 15 | |
| 16 | #include <media/v4l2-subdev.h> |
| 17 | |
| 18 | #include "vsp1.h" |
| 19 | #include "vsp1_rwpf.h" |
| 20 | #include "vsp1_video.h" |
| 21 | |
| 22 | #define WPF_MAX_WIDTH 2048 |
| 23 | #define WPF_MAX_HEIGHT 2048 |
| 24 | |
| 25 | /* ----------------------------------------------------------------------------- |
| 26 | * Device Access |
| 27 | */ |
| 28 | |
| 29 | static inline u32 vsp1_wpf_read(struct vsp1_rwpf *wpf, u32 reg) |
| 30 | { |
| 31 | return vsp1_read(wpf->entity.vsp1, |
| 32 | reg + wpf->entity.index * VI6_WPF_OFFSET); |
| 33 | } |
| 34 | |
| 35 | static inline void vsp1_wpf_write(struct vsp1_rwpf *wpf, u32 reg, u32 data) |
| 36 | { |
| 37 | vsp1_write(wpf->entity.vsp1, |
| 38 | reg + wpf->entity.index * VI6_WPF_OFFSET, data); |
| 39 | } |
| 40 | |
| 41 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | 7578c20 | 2014-05-21 19:00:05 -0300 | [diff] [blame] | 42 | * Controls |
| 43 | */ |
| 44 | |
| 45 | static int wpf_s_ctrl(struct v4l2_ctrl *ctrl) |
| 46 | { |
| 47 | struct vsp1_rwpf *wpf = |
| 48 | container_of(ctrl->handler, struct vsp1_rwpf, ctrls); |
| 49 | u32 value; |
| 50 | |
| 51 | if (!vsp1_entity_is_streaming(&wpf->entity)) |
| 52 | return 0; |
| 53 | |
| 54 | switch (ctrl->id) { |
| 55 | case V4L2_CID_ALPHA_COMPONENT: |
| 56 | value = vsp1_wpf_read(wpf, VI6_WPF_OUTFMT); |
| 57 | value &= ~VI6_WPF_OUTFMT_PDV_MASK; |
| 58 | value |= ctrl->val << VI6_WPF_OUTFMT_PDV_SHIFT; |
| 59 | vsp1_wpf_write(wpf, VI6_WPF_OUTFMT, value); |
| 60 | break; |
| 61 | } |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static const struct v4l2_ctrl_ops wpf_ctrl_ops = { |
| 67 | .s_ctrl = wpf_s_ctrl, |
| 68 | }; |
| 69 | |
| 70 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 71 | * V4L2 Subdevice Core Operations |
| 72 | */ |
| 73 | |
| 74 | static int wpf_s_stream(struct v4l2_subdev *subdev, int enable) |
| 75 | { |
Laurent Pinchart | 5aeb01ad | 2014-05-27 20:35:36 -0300 | [diff] [blame] | 76 | struct vsp1_pipeline *pipe = to_vsp1_pipeline(&subdev->entity); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 77 | struct vsp1_rwpf *wpf = to_rwpf(subdev); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 78 | struct vsp1_device *vsp1 = wpf->entity.vsp1; |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 79 | const struct v4l2_rect *crop = &wpf->crop; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 80 | unsigned int i; |
| 81 | u32 srcrpf = 0; |
| 82 | u32 outfmt = 0; |
Laurent Pinchart | 7578c20 | 2014-05-21 19:00:05 -0300 | [diff] [blame] | 83 | int ret; |
| 84 | |
| 85 | ret = vsp1_entity_set_streaming(&wpf->entity, enable); |
| 86 | if (ret < 0) |
| 87 | return ret; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 88 | |
| 89 | if (!enable) { |
| 90 | vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0); |
Laurent Pinchart | d6c71e8 | 2014-05-28 13:10:33 -0300 | [diff] [blame] | 91 | vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, 0); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | |
Takanari Hayama | 5d0beee | 2014-11-26 22:25:02 -0300 | [diff] [blame] | 95 | /* Sources. If the pipeline has a single input and BRU is not used, |
| 96 | * configure it as the master layer. Otherwise configure all |
| 97 | * inputs as sub-layers and select the virtual RPF as the master |
| 98 | * layer. |
Laurent Pinchart | 629bb6d | 2013-07-10 18:03:46 -0300 | [diff] [blame] | 99 | */ |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 100 | for (i = 0; i < pipe->num_inputs; ++i) { |
| 101 | struct vsp1_rwpf *input = pipe->inputs[i]; |
| 102 | |
Takanari Hayama | 5d0beee | 2014-11-26 22:25:02 -0300 | [diff] [blame] | 103 | srcrpf |= (!pipe->bru && pipe->num_inputs == 1) |
Laurent Pinchart | 629bb6d | 2013-07-10 18:03:46 -0300 | [diff] [blame] | 104 | ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index) |
| 105 | : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 106 | } |
| 107 | |
Takanari Hayama | 5d0beee | 2014-11-26 22:25:02 -0300 | [diff] [blame] | 108 | if (pipe->bru || pipe->num_inputs > 1) |
Laurent Pinchart | 629bb6d | 2013-07-10 18:03:46 -0300 | [diff] [blame] | 109 | srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST; |
| 110 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 111 | vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, srcrpf); |
| 112 | |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 113 | /* Destination stride. */ |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 114 | if (!pipe->lif) { |
Laurent Pinchart | 86960ee | 2015-07-28 14:00:43 -0300 | [diff] [blame] | 115 | struct v4l2_pix_format_mplane *format = &wpf->format; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 116 | |
| 117 | vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_Y, |
| 118 | format->plane_fmt[0].bytesperline); |
| 119 | if (format->num_planes > 1) |
| 120 | vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_C, |
| 121 | format->plane_fmt[1].bytesperline); |
| 122 | } |
| 123 | |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 124 | vsp1_wpf_write(wpf, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN | |
| 125 | (crop->left << VI6_WPF_SZCLIP_OFST_SHIFT) | |
| 126 | (crop->width << VI6_WPF_SZCLIP_SIZE_SHIFT)); |
| 127 | vsp1_wpf_write(wpf, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN | |
| 128 | (crop->top << VI6_WPF_SZCLIP_OFST_SHIFT) | |
| 129 | (crop->height << VI6_WPF_SZCLIP_SIZE_SHIFT)); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 130 | |
| 131 | /* Format */ |
| 132 | if (!pipe->lif) { |
Laurent Pinchart | 86960ee | 2015-07-28 14:00:43 -0300 | [diff] [blame] | 133 | const struct vsp1_format_info *fmtinfo = wpf->fmtinfo; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 134 | |
| 135 | outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT; |
| 136 | |
Laurent Pinchart | 7a52b6d | 2014-05-26 20:12:53 -0300 | [diff] [blame] | 137 | if (fmtinfo->alpha) |
| 138 | outfmt |= VI6_WPF_OUTFMT_PXA; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 139 | if (fmtinfo->swap_yc) |
| 140 | outfmt |= VI6_WPF_OUTFMT_SPYCS; |
| 141 | if (fmtinfo->swap_uv) |
| 142 | outfmt |= VI6_WPF_OUTFMT_SPUVS; |
| 143 | |
| 144 | vsp1_wpf_write(wpf, VI6_WPF_DSWAP, fmtinfo->swap); |
| 145 | } |
| 146 | |
| 147 | if (wpf->entity.formats[RWPF_PAD_SINK].code != |
| 148 | wpf->entity.formats[RWPF_PAD_SOURCE].code) |
| 149 | outfmt |= VI6_WPF_OUTFMT_CSC; |
| 150 | |
Laurent Pinchart | 7578c20 | 2014-05-21 19:00:05 -0300 | [diff] [blame] | 151 | /* Take the control handler lock to ensure that the PDV value won't be |
| 152 | * changed behind our back by a set control operation. |
| 153 | */ |
| 154 | mutex_lock(wpf->ctrls.lock); |
| 155 | outfmt |= vsp1_wpf_read(wpf, VI6_WPF_OUTFMT) & VI6_WPF_OUTFMT_PDV_MASK; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 156 | vsp1_wpf_write(wpf, VI6_WPF_OUTFMT, outfmt); |
Laurent Pinchart | 7578c20 | 2014-05-21 19:00:05 -0300 | [diff] [blame] | 157 | mutex_unlock(wpf->ctrls.lock); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 158 | |
| 159 | vsp1_write(vsp1, VI6_DPR_WPF_FPORCH(wpf->entity.index), |
| 160 | VI6_DPR_WPF_FPORCH_FP_WPFN); |
| 161 | |
| 162 | vsp1_write(vsp1, VI6_WPF_WRBCK_CTRL, 0); |
| 163 | |
| 164 | /* Enable interrupts */ |
| 165 | vsp1_write(vsp1, VI6_WPF_IRQ_STA(wpf->entity.index), 0); |
| 166 | vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), |
| 167 | VI6_WFP_IRQ_ENB_FREE); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | /* ----------------------------------------------------------------------------- |
| 173 | * V4L2 Subdevice Operations |
| 174 | */ |
| 175 | |
| 176 | static struct v4l2_subdev_video_ops wpf_video_ops = { |
| 177 | .s_stream = wpf_s_stream, |
| 178 | }; |
| 179 | |
| 180 | static struct v4l2_subdev_pad_ops wpf_pad_ops = { |
| 181 | .enum_mbus_code = vsp1_rwpf_enum_mbus_code, |
| 182 | .enum_frame_size = vsp1_rwpf_enum_frame_size, |
| 183 | .get_fmt = vsp1_rwpf_get_format, |
| 184 | .set_fmt = vsp1_rwpf_set_format, |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 185 | .get_selection = vsp1_rwpf_get_selection, |
| 186 | .set_selection = vsp1_rwpf_set_selection, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | static struct v4l2_subdev_ops wpf_ops = { |
| 190 | .video = &wpf_video_ops, |
| 191 | .pad = &wpf_pad_ops, |
| 192 | }; |
| 193 | |
| 194 | /* ----------------------------------------------------------------------------- |
| 195 | * Video Device Operations |
| 196 | */ |
| 197 | |
Laurent Pinchart | b6af10c | 2015-07-28 14:05:56 -0300 | [diff] [blame^] | 198 | static void wpf_buf_queue(struct vsp1_rwpf *wpf, struct vsp1_video_buffer *buf) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 199 | { |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 200 | vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_Y, buf->addr[0]); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 201 | if (buf->buf.vb2_buf.num_planes > 1) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 202 | vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C0, buf->addr[1]); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 203 | if (buf->buf.vb2_buf.num_planes > 2) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 204 | vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C1, buf->addr[2]); |
| 205 | } |
| 206 | |
Laurent Pinchart | b6af10c | 2015-07-28 14:05:56 -0300 | [diff] [blame^] | 207 | static const struct vsp1_rwpf_operations wpf_vdev_ops = { |
| 208 | .queue = wpf_buf_queue, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | /* ----------------------------------------------------------------------------- |
| 212 | * Initialization and Cleanup |
| 213 | */ |
| 214 | |
| 215 | struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index) |
| 216 | { |
| 217 | struct v4l2_subdev *subdev; |
| 218 | struct vsp1_video *video; |
| 219 | struct vsp1_rwpf *wpf; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 220 | int ret; |
| 221 | |
| 222 | wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL); |
| 223 | if (wpf == NULL) |
| 224 | return ERR_PTR(-ENOMEM); |
| 225 | |
Laurent Pinchart | b6af10c | 2015-07-28 14:05:56 -0300 | [diff] [blame^] | 226 | wpf->ops = &wpf_vdev_ops; |
| 227 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 228 | wpf->max_width = WPF_MAX_WIDTH; |
| 229 | wpf->max_height = WPF_MAX_HEIGHT; |
| 230 | |
| 231 | wpf->entity.type = VSP1_ENTITY_WPF; |
| 232 | wpf->entity.index = index; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 233 | |
| 234 | ret = vsp1_entity_init(vsp1, &wpf->entity, 2); |
| 235 | if (ret < 0) |
| 236 | return ERR_PTR(ret); |
| 237 | |
| 238 | /* Initialize the V4L2 subdev. */ |
| 239 | subdev = &wpf->entity.subdev; |
| 240 | v4l2_subdev_init(subdev, &wpf_ops); |
| 241 | |
| 242 | subdev->entity.ops = &vsp1_media_ops; |
| 243 | subdev->internal_ops = &vsp1_subdev_internal_ops; |
| 244 | snprintf(subdev->name, sizeof(subdev->name), "%s wpf.%u", |
| 245 | dev_name(vsp1->dev), index); |
| 246 | v4l2_set_subdevdata(subdev, wpf); |
| 247 | subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; |
| 248 | |
| 249 | vsp1_entity_init_formats(subdev, NULL); |
| 250 | |
Laurent Pinchart | 7578c20 | 2014-05-21 19:00:05 -0300 | [diff] [blame] | 251 | /* Initialize the control handler. */ |
| 252 | v4l2_ctrl_handler_init(&wpf->ctrls, 1); |
| 253 | v4l2_ctrl_new_std(&wpf->ctrls, &wpf_ctrl_ops, V4L2_CID_ALPHA_COMPONENT, |
| 254 | 0, 255, 1, 255); |
| 255 | |
| 256 | wpf->entity.subdev.ctrl_handler = &wpf->ctrls; |
| 257 | |
| 258 | if (wpf->ctrls.error) { |
| 259 | dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n", |
| 260 | index); |
| 261 | ret = wpf->ctrls.error; |
| 262 | goto error; |
| 263 | } |
| 264 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 265 | /* Initialize the video device. */ |
| 266 | video = &wpf->video; |
| 267 | |
| 268 | video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 269 | video->vsp1 = vsp1; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 270 | |
Laurent Pinchart | 8b4a056 | 2015-07-28 13:54:03 -0300 | [diff] [blame] | 271 | ret = vsp1_video_init(video, wpf); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 272 | if (ret < 0) |
Laurent Pinchart | 1499be6 | 2014-05-28 12:49:13 -0300 | [diff] [blame] | 273 | goto error; |
| 274 | |
| 275 | wpf->entity.video = video; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 276 | wpf->entity.sink = &wpf->video.video.entity; |
| 277 | |
| 278 | return wpf; |
| 279 | |
Laurent Pinchart | 1499be6 | 2014-05-28 12:49:13 -0300 | [diff] [blame] | 280 | error: |
| 281 | vsp1_entity_destroy(&wpf->entity); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 282 | return ERR_PTR(ret); |
| 283 | } |