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 | { |
Takashi Saito | 1517b03 | 2015-09-07 01:40:25 -0300 | [diff] [blame] | 37 | vsp1_mod_write(&wpf->entity, |
| 38 | reg + wpf->entity.index * VI6_WPF_OFFSET, data); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 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); |
Takashi Saito | 1517b03 | 2015-09-07 01:40:25 -0300 | [diff] [blame] | 91 | vsp1_write(vsp1, wpf->entity.index * VI6_WPF_OFFSET + |
| 92 | VI6_WPF_SRCRPF, 0); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
Takanari Hayama | 5d0beee | 2014-11-26 22:25:02 -0300 | [diff] [blame] | 96 | /* Sources. If the pipeline has a single input and BRU is not used, |
| 97 | * configure it as the master layer. Otherwise configure all |
| 98 | * inputs as sub-layers and select the virtual RPF as the master |
| 99 | * layer. |
Laurent Pinchart | 629bb6d | 2013-07-10 18:03:46 -0300 | [diff] [blame] | 100 | */ |
Laurent Pinchart | 5aa2eb3 | 2015-12-05 20:17:10 -0200 | [diff] [blame] | 101 | for (i = 0; i < vsp1->info->rpf_count; ++i) { |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 102 | struct vsp1_rwpf *input = pipe->inputs[i]; |
| 103 | |
Laurent Pinchart | 96bfa6a | 2015-08-05 16:40:31 -0300 | [diff] [blame] | 104 | if (!input) |
| 105 | continue; |
| 106 | |
Takanari Hayama | 5d0beee | 2014-11-26 22:25:02 -0300 | [diff] [blame] | 107 | srcrpf |= (!pipe->bru && pipe->num_inputs == 1) |
Laurent Pinchart | 629bb6d | 2013-07-10 18:03:46 -0300 | [diff] [blame] | 108 | ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index) |
| 109 | : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 110 | } |
| 111 | |
Takanari Hayama | 5d0beee | 2014-11-26 22:25:02 -0300 | [diff] [blame] | 112 | if (pipe->bru || pipe->num_inputs > 1) |
Laurent Pinchart | 629bb6d | 2013-07-10 18:03:46 -0300 | [diff] [blame] | 113 | srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST; |
| 114 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 115 | vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, srcrpf); |
| 116 | |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 117 | /* Destination stride. */ |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 118 | if (!pipe->lif) { |
Laurent Pinchart | 86960ee | 2015-07-28 14:00:43 -0300 | [diff] [blame] | 119 | struct v4l2_pix_format_mplane *format = &wpf->format; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 120 | |
| 121 | vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_Y, |
| 122 | format->plane_fmt[0].bytesperline); |
| 123 | if (format->num_planes > 1) |
| 124 | vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_C, |
| 125 | format->plane_fmt[1].bytesperline); |
| 126 | } |
| 127 | |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 128 | vsp1_wpf_write(wpf, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN | |
| 129 | (crop->left << VI6_WPF_SZCLIP_OFST_SHIFT) | |
| 130 | (crop->width << VI6_WPF_SZCLIP_SIZE_SHIFT)); |
| 131 | vsp1_wpf_write(wpf, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN | |
| 132 | (crop->top << VI6_WPF_SZCLIP_OFST_SHIFT) | |
| 133 | (crop->height << VI6_WPF_SZCLIP_SIZE_SHIFT)); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 134 | |
| 135 | /* Format */ |
| 136 | if (!pipe->lif) { |
Laurent Pinchart | 86960ee | 2015-07-28 14:00:43 -0300 | [diff] [blame] | 137 | const struct vsp1_format_info *fmtinfo = wpf->fmtinfo; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 138 | |
| 139 | outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT; |
| 140 | |
Laurent Pinchart | 7a52b6d | 2014-05-26 20:12:53 -0300 | [diff] [blame] | 141 | if (fmtinfo->alpha) |
| 142 | outfmt |= VI6_WPF_OUTFMT_PXA; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 143 | if (fmtinfo->swap_yc) |
| 144 | outfmt |= VI6_WPF_OUTFMT_SPYCS; |
| 145 | if (fmtinfo->swap_uv) |
| 146 | outfmt |= VI6_WPF_OUTFMT_SPUVS; |
| 147 | |
| 148 | vsp1_wpf_write(wpf, VI6_WPF_DSWAP, fmtinfo->swap); |
| 149 | } |
| 150 | |
| 151 | if (wpf->entity.formats[RWPF_PAD_SINK].code != |
| 152 | wpf->entity.formats[RWPF_PAD_SOURCE].code) |
| 153 | outfmt |= VI6_WPF_OUTFMT_CSC; |
| 154 | |
Laurent Pinchart | 7578c20 | 2014-05-21 19:00:05 -0300 | [diff] [blame] | 155 | /* Take the control handler lock to ensure that the PDV value won't be |
| 156 | * changed behind our back by a set control operation. |
| 157 | */ |
Laurent Pinchart | 5aa2eb3 | 2015-12-05 20:17:10 -0200 | [diff] [blame] | 158 | if (vsp1->info->uapi) |
Laurent Pinchart | f2ed459 | 2015-07-28 16:16:05 -0300 | [diff] [blame] | 159 | mutex_lock(wpf->ctrls.lock); |
Laurent Pinchart | 3dbb610 | 2015-08-05 16:57:35 -0300 | [diff] [blame] | 160 | outfmt |= wpf->alpha->cur.val << VI6_WPF_OUTFMT_PDV_SHIFT; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 161 | vsp1_wpf_write(wpf, VI6_WPF_OUTFMT, outfmt); |
Laurent Pinchart | 5aa2eb3 | 2015-12-05 20:17:10 -0200 | [diff] [blame] | 162 | if (vsp1->info->uapi) |
Laurent Pinchart | f2ed459 | 2015-07-28 16:16:05 -0300 | [diff] [blame] | 163 | mutex_unlock(wpf->ctrls.lock); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 164 | |
Takashi Saito | 1517b03 | 2015-09-07 01:40:25 -0300 | [diff] [blame] | 165 | vsp1_mod_write(&wpf->entity, VI6_DPR_WPF_FPORCH(wpf->entity.index), |
| 166 | VI6_DPR_WPF_FPORCH_FP_WPFN); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 167 | |
Takashi Saito | 1517b03 | 2015-09-07 01:40:25 -0300 | [diff] [blame] | 168 | vsp1_mod_write(&wpf->entity, VI6_WPF_WRBCK_CTRL, 0); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 169 | |
| 170 | /* Enable interrupts */ |
| 171 | vsp1_write(vsp1, VI6_WPF_IRQ_STA(wpf->entity.index), 0); |
| 172 | vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), |
| 173 | VI6_WFP_IRQ_ENB_FREE); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /* ----------------------------------------------------------------------------- |
| 179 | * V4L2 Subdevice Operations |
| 180 | */ |
| 181 | |
| 182 | static struct v4l2_subdev_video_ops wpf_video_ops = { |
| 183 | .s_stream = wpf_s_stream, |
| 184 | }; |
| 185 | |
| 186 | static struct v4l2_subdev_pad_ops wpf_pad_ops = { |
| 187 | .enum_mbus_code = vsp1_rwpf_enum_mbus_code, |
| 188 | .enum_frame_size = vsp1_rwpf_enum_frame_size, |
| 189 | .get_fmt = vsp1_rwpf_get_format, |
| 190 | .set_fmt = vsp1_rwpf_set_format, |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 191 | .get_selection = vsp1_rwpf_get_selection, |
| 192 | .set_selection = vsp1_rwpf_set_selection, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | static struct v4l2_subdev_ops wpf_ops = { |
| 196 | .video = &wpf_video_ops, |
| 197 | .pad = &wpf_pad_ops, |
| 198 | }; |
| 199 | |
| 200 | /* ----------------------------------------------------------------------------- |
| 201 | * Video Device Operations |
| 202 | */ |
| 203 | |
Laurent Pinchart | b58faa9 | 2015-07-28 16:04:47 -0300 | [diff] [blame] | 204 | static void wpf_set_memory(struct vsp1_rwpf *wpf, struct vsp1_rwpf_memory *mem) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 205 | { |
Laurent Pinchart | b58faa9 | 2015-07-28 16:04:47 -0300 | [diff] [blame] | 206 | vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_Y, mem->addr[0]); |
| 207 | if (mem->num_planes > 1) |
| 208 | vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C0, mem->addr[1]); |
| 209 | if (mem->num_planes > 2) |
| 210 | vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C1, mem->addr[2]); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 211 | } |
| 212 | |
Laurent Pinchart | b6af10c | 2015-07-28 14:05:56 -0300 | [diff] [blame] | 213 | static const struct vsp1_rwpf_operations wpf_vdev_ops = { |
Laurent Pinchart | b58faa9 | 2015-07-28 16:04:47 -0300 | [diff] [blame] | 214 | .set_memory = wpf_set_memory, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | /* ----------------------------------------------------------------------------- |
| 218 | * Initialization and Cleanup |
| 219 | */ |
| 220 | |
| 221 | struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index) |
| 222 | { |
| 223 | struct v4l2_subdev *subdev; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 224 | struct vsp1_rwpf *wpf; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 225 | int ret; |
| 226 | |
| 227 | wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL); |
| 228 | if (wpf == NULL) |
| 229 | return ERR_PTR(-ENOMEM); |
| 230 | |
Laurent Pinchart | b6af10c | 2015-07-28 14:05:56 -0300 | [diff] [blame] | 231 | wpf->ops = &wpf_vdev_ops; |
| 232 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 233 | wpf->max_width = WPF_MAX_WIDTH; |
| 234 | wpf->max_height = WPF_MAX_HEIGHT; |
| 235 | |
| 236 | wpf->entity.type = VSP1_ENTITY_WPF; |
| 237 | wpf->entity.index = index; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 238 | |
| 239 | ret = vsp1_entity_init(vsp1, &wpf->entity, 2); |
| 240 | if (ret < 0) |
| 241 | return ERR_PTR(ret); |
| 242 | |
| 243 | /* Initialize the V4L2 subdev. */ |
| 244 | subdev = &wpf->entity.subdev; |
| 245 | v4l2_subdev_init(subdev, &wpf_ops); |
| 246 | |
Laurent Pinchart | babca00 | 2015-08-05 17:14:41 -0300 | [diff] [blame] | 247 | subdev->entity.ops = &vsp1->media_ops; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 248 | subdev->internal_ops = &vsp1_subdev_internal_ops; |
| 249 | snprintf(subdev->name, sizeof(subdev->name), "%s wpf.%u", |
| 250 | dev_name(vsp1->dev), index); |
| 251 | v4l2_set_subdevdata(subdev, wpf); |
| 252 | subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; |
| 253 | |
| 254 | vsp1_entity_init_formats(subdev, NULL); |
| 255 | |
Laurent Pinchart | 7578c20 | 2014-05-21 19:00:05 -0300 | [diff] [blame] | 256 | /* Initialize the control handler. */ |
| 257 | v4l2_ctrl_handler_init(&wpf->ctrls, 1); |
Laurent Pinchart | 3dbb610 | 2015-08-05 16:57:35 -0300 | [diff] [blame] | 258 | wpf->alpha = v4l2_ctrl_new_std(&wpf->ctrls, &wpf_ctrl_ops, |
| 259 | V4L2_CID_ALPHA_COMPONENT, |
| 260 | 0, 255, 1, 255); |
Laurent Pinchart | 7578c20 | 2014-05-21 19:00:05 -0300 | [diff] [blame] | 261 | |
| 262 | wpf->entity.subdev.ctrl_handler = &wpf->ctrls; |
| 263 | |
| 264 | if (wpf->ctrls.error) { |
| 265 | dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n", |
| 266 | index); |
| 267 | ret = wpf->ctrls.error; |
| 268 | goto error; |
| 269 | } |
| 270 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 271 | return wpf; |
| 272 | |
Laurent Pinchart | 1499be6 | 2014-05-28 12:49:13 -0300 | [diff] [blame] | 273 | error: |
| 274 | vsp1_entity_destroy(&wpf->entity); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 275 | return ERR_PTR(ret); |
| 276 | } |