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" |
Laurent Pinchart | ef9621b | 2015-11-14 22:27:52 -0200 | [diff] [blame] | 19 | #include "vsp1_dl.h" |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 20 | #include "vsp1_rwpf.h" |
| 21 | #include "vsp1_video.h" |
| 22 | |
| 23 | #define WPF_MAX_WIDTH 2048 |
| 24 | #define WPF_MAX_HEIGHT 2048 |
| 25 | |
| 26 | /* ----------------------------------------------------------------------------- |
| 27 | * Device Access |
| 28 | */ |
| 29 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 30 | static inline void vsp1_wpf_write(struct vsp1_rwpf *wpf, u32 reg, u32 data) |
| 31 | { |
Takashi Saito | 1517b03 | 2015-09-07 01:40:25 -0300 | [diff] [blame] | 32 | vsp1_mod_write(&wpf->entity, |
| 33 | reg + wpf->entity.index * VI6_WPF_OFFSET, data); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /* ----------------------------------------------------------------------------- |
| 37 | * V4L2 Subdevice Core Operations |
| 38 | */ |
| 39 | |
| 40 | static int wpf_s_stream(struct v4l2_subdev *subdev, int enable) |
| 41 | { |
| 42 | struct vsp1_rwpf *wpf = to_rwpf(subdev); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 43 | struct vsp1_device *vsp1 = wpf->entity.vsp1; |
Laurent Pinchart | 7578c20 | 2014-05-21 19:00:05 -0300 | [diff] [blame] | 44 | |
Laurent Pinchart | 7b905f0 | 2015-11-17 13:10:26 -0200 | [diff] [blame^] | 45 | if (enable) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 46 | return 0; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 47 | |
Laurent Pinchart | 7b905f0 | 2015-11-17 13:10:26 -0200 | [diff] [blame^] | 48 | /* Write to registers directly when stopping the stream as there will be |
| 49 | * no pipeline run to apply the display list. |
Laurent Pinchart | 629bb6d | 2013-07-10 18:03:46 -0300 | [diff] [blame] | 50 | */ |
Laurent Pinchart | 7b905f0 | 2015-11-17 13:10:26 -0200 | [diff] [blame^] | 51 | vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0); |
| 52 | vsp1_write(vsp1, wpf->entity.index * VI6_WPF_OFFSET + |
| 53 | VI6_WPF_SRCRPF, 0); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | /* ----------------------------------------------------------------------------- |
| 59 | * V4L2 Subdevice Operations |
| 60 | */ |
| 61 | |
| 62 | static struct v4l2_subdev_video_ops wpf_video_ops = { |
| 63 | .s_stream = wpf_s_stream, |
| 64 | }; |
| 65 | |
| 66 | static struct v4l2_subdev_pad_ops wpf_pad_ops = { |
Laurent Pinchart | 0efdf0f | 2015-11-15 20:09:08 -0200 | [diff] [blame] | 67 | .init_cfg = vsp1_entity_init_cfg, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 68 | .enum_mbus_code = vsp1_rwpf_enum_mbus_code, |
| 69 | .enum_frame_size = vsp1_rwpf_enum_frame_size, |
| 70 | .get_fmt = vsp1_rwpf_get_format, |
| 71 | .set_fmt = vsp1_rwpf_set_format, |
Laurent Pinchart | e5ad37b | 2013-08-24 20:49:58 -0300 | [diff] [blame] | 72 | .get_selection = vsp1_rwpf_get_selection, |
| 73 | .set_selection = vsp1_rwpf_set_selection, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | static struct v4l2_subdev_ops wpf_ops = { |
| 77 | .video = &wpf_video_ops, |
| 78 | .pad = &wpf_pad_ops, |
| 79 | }; |
| 80 | |
| 81 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | 5243453 | 2015-11-17 12:23:23 -0200 | [diff] [blame] | 82 | * VSP1 Entity Operations |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 83 | */ |
| 84 | |
Laurent Pinchart | 5243453 | 2015-11-17 12:23:23 -0200 | [diff] [blame] | 85 | static void vsp1_wpf_destroy(struct vsp1_entity *entity) |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 86 | { |
Laurent Pinchart | 5243453 | 2015-11-17 12:23:23 -0200 | [diff] [blame] | 87 | struct vsp1_rwpf *wpf = entity_to_rwpf(entity); |
| 88 | |
| 89 | vsp1_dlm_destroy(wpf->dlm); |
| 90 | } |
| 91 | |
| 92 | static void wpf_set_memory(struct vsp1_entity *entity) |
| 93 | { |
| 94 | struct vsp1_rwpf *wpf = entity_to_rwpf(entity); |
| 95 | |
Laurent Pinchart | 351bbf9 | 2015-11-01 15:18:56 -0200 | [diff] [blame] | 96 | vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_Y, wpf->mem.addr[0]); |
| 97 | vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C0, wpf->mem.addr[1]); |
| 98 | vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C1, wpf->mem.addr[2]); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 99 | } |
| 100 | |
Laurent Pinchart | 7b905f0 | 2015-11-17 13:10:26 -0200 | [diff] [blame^] | 101 | static void wpf_configure(struct vsp1_entity *entity) |
| 102 | { |
| 103 | struct vsp1_pipeline *pipe = to_vsp1_pipeline(&entity->subdev.entity); |
| 104 | struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev); |
| 105 | struct vsp1_device *vsp1 = wpf->entity.vsp1; |
| 106 | const struct v4l2_mbus_framefmt *source_format; |
| 107 | const struct v4l2_mbus_framefmt *sink_format; |
| 108 | const struct v4l2_rect *crop; |
| 109 | unsigned int i; |
| 110 | u32 outfmt = 0; |
| 111 | u32 srcrpf = 0; |
| 112 | |
| 113 | /* Cropping */ |
| 114 | crop = vsp1_rwpf_get_crop(wpf, wpf->entity.config); |
| 115 | |
| 116 | vsp1_wpf_write(wpf, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN | |
| 117 | (crop->left << VI6_WPF_SZCLIP_OFST_SHIFT) | |
| 118 | (crop->width << VI6_WPF_SZCLIP_SIZE_SHIFT)); |
| 119 | vsp1_wpf_write(wpf, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN | |
| 120 | (crop->top << VI6_WPF_SZCLIP_OFST_SHIFT) | |
| 121 | (crop->height << VI6_WPF_SZCLIP_SIZE_SHIFT)); |
| 122 | |
| 123 | /* Format */ |
| 124 | sink_format = vsp1_entity_get_pad_format(&wpf->entity, |
| 125 | wpf->entity.config, |
| 126 | RWPF_PAD_SINK); |
| 127 | source_format = vsp1_entity_get_pad_format(&wpf->entity, |
| 128 | wpf->entity.config, |
| 129 | RWPF_PAD_SOURCE); |
| 130 | |
| 131 | if (!pipe->lif) { |
| 132 | const struct v4l2_pix_format_mplane *format = &wpf->format; |
| 133 | const struct vsp1_format_info *fmtinfo = wpf->fmtinfo; |
| 134 | |
| 135 | outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT; |
| 136 | |
| 137 | if (fmtinfo->alpha) |
| 138 | outfmt |= VI6_WPF_OUTFMT_PXA; |
| 139 | if (fmtinfo->swap_yc) |
| 140 | outfmt |= VI6_WPF_OUTFMT_SPYCS; |
| 141 | if (fmtinfo->swap_uv) |
| 142 | outfmt |= VI6_WPF_OUTFMT_SPUVS; |
| 143 | |
| 144 | /* Destination stride and byte swapping. */ |
| 145 | vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_Y, |
| 146 | format->plane_fmt[0].bytesperline); |
| 147 | if (format->num_planes > 1) |
| 148 | vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_C, |
| 149 | format->plane_fmt[1].bytesperline); |
| 150 | |
| 151 | vsp1_wpf_write(wpf, VI6_WPF_DSWAP, fmtinfo->swap); |
| 152 | } |
| 153 | |
| 154 | if (sink_format->code != source_format->code) |
| 155 | outfmt |= VI6_WPF_OUTFMT_CSC; |
| 156 | |
| 157 | outfmt |= wpf->alpha << VI6_WPF_OUTFMT_PDV_SHIFT; |
| 158 | vsp1_wpf_write(wpf, VI6_WPF_OUTFMT, outfmt); |
| 159 | |
| 160 | vsp1_mod_write(&wpf->entity, VI6_DPR_WPF_FPORCH(wpf->entity.index), |
| 161 | VI6_DPR_WPF_FPORCH_FP_WPFN); |
| 162 | |
| 163 | vsp1_mod_write(&wpf->entity, VI6_WPF_WRBCK_CTRL, 0); |
| 164 | |
| 165 | /* Sources. If the pipeline has a single input and BRU is not used, |
| 166 | * configure it as the master layer. Otherwise configure all |
| 167 | * inputs as sub-layers and select the virtual RPF as the master |
| 168 | * layer. |
| 169 | */ |
| 170 | for (i = 0; i < vsp1->info->rpf_count; ++i) { |
| 171 | struct vsp1_rwpf *input = pipe->inputs[i]; |
| 172 | |
| 173 | if (!input) |
| 174 | continue; |
| 175 | |
| 176 | srcrpf |= (!pipe->bru && pipe->num_inputs == 1) |
| 177 | ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index) |
| 178 | : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index); |
| 179 | } |
| 180 | |
| 181 | if (pipe->bru || pipe->num_inputs > 1) |
| 182 | srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST; |
| 183 | |
| 184 | vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, srcrpf); |
| 185 | |
| 186 | /* Enable interrupts */ |
| 187 | vsp1_mod_write(&wpf->entity, VI6_WPF_IRQ_STA(wpf->entity.index), 0); |
| 188 | vsp1_mod_write(&wpf->entity, VI6_WPF_IRQ_ENB(wpf->entity.index), |
| 189 | VI6_WFP_IRQ_ENB_FREE); |
| 190 | } |
| 191 | |
Laurent Pinchart | 5243453 | 2015-11-17 12:23:23 -0200 | [diff] [blame] | 192 | static const struct vsp1_entity_operations wpf_entity_ops = { |
| 193 | .destroy = vsp1_wpf_destroy, |
Laurent Pinchart | b58faa9 | 2015-07-28 16:04:47 -0300 | [diff] [blame] | 194 | .set_memory = wpf_set_memory, |
Laurent Pinchart | 7b905f0 | 2015-11-17 13:10:26 -0200 | [diff] [blame^] | 195 | .configure = wpf_configure, |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | /* ----------------------------------------------------------------------------- |
| 199 | * Initialization and Cleanup |
| 200 | */ |
| 201 | |
| 202 | struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index) |
| 203 | { |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 204 | struct vsp1_rwpf *wpf; |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 205 | char name[6]; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 206 | int ret; |
| 207 | |
| 208 | wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL); |
| 209 | if (wpf == NULL) |
| 210 | return ERR_PTR(-ENOMEM); |
| 211 | |
| 212 | wpf->max_width = WPF_MAX_WIDTH; |
| 213 | wpf->max_height = WPF_MAX_HEIGHT; |
| 214 | |
Laurent Pinchart | 5243453 | 2015-11-17 12:23:23 -0200 | [diff] [blame] | 215 | wpf->entity.ops = &wpf_entity_ops; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 216 | wpf->entity.type = VSP1_ENTITY_WPF; |
| 217 | wpf->entity.index = index; |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 218 | |
Laurent Pinchart | 823329d | 2015-11-15 19:42:01 -0200 | [diff] [blame] | 219 | sprintf(name, "wpf.%u", index); |
| 220 | ret = vsp1_entity_init(vsp1, &wpf->entity, name, 2, &wpf_ops); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 221 | if (ret < 0) |
| 222 | return ERR_PTR(ret); |
| 223 | |
Laurent Pinchart | 351bbf9 | 2015-11-01 15:18:56 -0200 | [diff] [blame] | 224 | /* Initialize the display list manager. */ |
| 225 | wpf->dlm = vsp1_dlm_create(vsp1, index, 4); |
| 226 | if (!wpf->dlm) { |
| 227 | ret = -ENOMEM; |
| 228 | goto error; |
Laurent Pinchart | ef9621b | 2015-11-14 22:27:52 -0200 | [diff] [blame] | 229 | } |
| 230 | |
Laurent Pinchart | 7578c20 | 2014-05-21 19:00:05 -0300 | [diff] [blame] | 231 | /* Initialize the control handler. */ |
Laurent Pinchart | bd2fdd5 | 2015-11-01 12:19:42 -0200 | [diff] [blame] | 232 | ret = vsp1_rwpf_init_ctrls(wpf); |
| 233 | if (ret < 0) { |
Laurent Pinchart | 7578c20 | 2014-05-21 19:00:05 -0300 | [diff] [blame] | 234 | dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n", |
| 235 | index); |
Laurent Pinchart | 7578c20 | 2014-05-21 19:00:05 -0300 | [diff] [blame] | 236 | goto error; |
| 237 | } |
| 238 | |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 239 | return wpf; |
| 240 | |
Laurent Pinchart | 1499be6 | 2014-05-28 12:49:13 -0300 | [diff] [blame] | 241 | error: |
| 242 | vsp1_entity_destroy(&wpf->entity); |
Laurent Pinchart | 26e0ca2 | 2013-06-04 11:22:30 -0300 | [diff] [blame] | 243 | return ERR_PTR(ret); |
| 244 | } |