blob: ccf1f960c46a0db7274236a99433a6caac2c3ed8 [file] [log] [blame]
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03001/*
2 * vsp1_wpf.c -- R-Car VSP1 Write Pixel Formatter
3 *
Laurent Pinchart8a1edc52014-02-06 14:42:31 -03004 * Copyright (C) 2013-2014 Renesas Electronics Corporation
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03005 *
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 Pinchartef9621b2015-11-14 22:27:52 -020019#include "vsp1_dl.h"
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030020#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 Pinchart26e0ca22013-06-04 11:22:30 -030030static inline void vsp1_wpf_write(struct vsp1_rwpf *wpf, u32 reg, u32 data)
31{
Takashi Saito1517b032015-09-07 01:40:25 -030032 vsp1_mod_write(&wpf->entity,
33 reg + wpf->entity.index * VI6_WPF_OFFSET, data);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030034}
35
36/* -----------------------------------------------------------------------------
37 * V4L2 Subdevice Core Operations
38 */
39
40static int wpf_s_stream(struct v4l2_subdev *subdev, int enable)
41{
42 struct vsp1_rwpf *wpf = to_rwpf(subdev);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030043 struct vsp1_device *vsp1 = wpf->entity.vsp1;
Laurent Pinchart7578c202014-05-21 19:00:05 -030044
Laurent Pinchart7b905f02015-11-17 13:10:26 -020045 if (enable)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030046 return 0;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030047
Laurent Pinchart7b905f02015-11-17 13:10:26 -020048 /* Write to registers directly when stopping the stream as there will be
49 * no pipeline run to apply the display list.
Laurent Pinchart629bb6d2013-07-10 18:03:46 -030050 */
Laurent Pinchart7b905f02015-11-17 13:10:26 -020051 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 Pinchart26e0ca22013-06-04 11:22:30 -030054
55 return 0;
56}
57
58/* -----------------------------------------------------------------------------
59 * V4L2 Subdevice Operations
60 */
61
62static struct v4l2_subdev_video_ops wpf_video_ops = {
63 .s_stream = wpf_s_stream,
64};
65
66static struct v4l2_subdev_pad_ops wpf_pad_ops = {
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -020067 .init_cfg = vsp1_entity_init_cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030068 .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 Pincharte5ad37b2013-08-24 20:49:58 -030072 .get_selection = vsp1_rwpf_get_selection,
73 .set_selection = vsp1_rwpf_set_selection,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030074};
75
76static struct v4l2_subdev_ops wpf_ops = {
77 .video = &wpf_video_ops,
78 .pad = &wpf_pad_ops,
79};
80
81/* -----------------------------------------------------------------------------
Laurent Pinchart52434532015-11-17 12:23:23 -020082 * VSP1 Entity Operations
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030083 */
84
Laurent Pinchart52434532015-11-17 12:23:23 -020085static void vsp1_wpf_destroy(struct vsp1_entity *entity)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030086{
Laurent Pinchart52434532015-11-17 12:23:23 -020087 struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
88
89 vsp1_dlm_destroy(wpf->dlm);
90}
91
92static void wpf_set_memory(struct vsp1_entity *entity)
93{
94 struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
95
Laurent Pinchart351bbf92015-11-01 15:18:56 -020096 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 Pinchart26e0ca22013-06-04 11:22:30 -030099}
100
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200101static 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 Pinchart52434532015-11-17 12:23:23 -0200192static const struct vsp1_entity_operations wpf_entity_ops = {
193 .destroy = vsp1_wpf_destroy,
Laurent Pinchartb58faa92015-07-28 16:04:47 -0300194 .set_memory = wpf_set_memory,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200195 .configure = wpf_configure,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300196};
197
198/* -----------------------------------------------------------------------------
199 * Initialization and Cleanup
200 */
201
202struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
203{
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300204 struct vsp1_rwpf *wpf;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200205 char name[6];
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300206 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 Pinchart52434532015-11-17 12:23:23 -0200215 wpf->entity.ops = &wpf_entity_ops;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300216 wpf->entity.type = VSP1_ENTITY_WPF;
217 wpf->entity.index = index;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300218
Laurent Pinchart823329d2015-11-15 19:42:01 -0200219 sprintf(name, "wpf.%u", index);
220 ret = vsp1_entity_init(vsp1, &wpf->entity, name, 2, &wpf_ops);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300221 if (ret < 0)
222 return ERR_PTR(ret);
223
Laurent Pinchart351bbf92015-11-01 15:18:56 -0200224 /* 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 Pinchartef9621b2015-11-14 22:27:52 -0200229 }
230
Laurent Pinchart7578c202014-05-21 19:00:05 -0300231 /* Initialize the control handler. */
Laurent Pinchartbd2fdd52015-11-01 12:19:42 -0200232 ret = vsp1_rwpf_init_ctrls(wpf);
233 if (ret < 0) {
Laurent Pinchart7578c202014-05-21 19:00:05 -0300234 dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
235 index);
Laurent Pinchart7578c202014-05-21 19:00:05 -0300236 goto error;
237 }
238
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300239 return wpf;
240
Laurent Pinchart1499be62014-05-28 12:49:13 -0300241error:
242 vsp1_entity_destroy(&wpf->entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300243 return ERR_PTR(ret);
244}