blob: 0797927d14cff482f631be98d4cf4875389aaa7b [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{
Laurent Pinchart5aeb01ad2014-05-27 20:35:36 -030042 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&subdev->entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030043 struct vsp1_rwpf *wpf = to_rwpf(subdev);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030044 struct vsp1_device *vsp1 = wpf->entity.vsp1;
Laurent Pincharte790c3c2015-11-15 19:14:22 -020045 const struct v4l2_mbus_framefmt *source_format;
46 const struct v4l2_mbus_framefmt *sink_format;
Laurent Pinchartb7e51072015-11-15 19:14:22 -020047 const struct v4l2_rect *crop;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030048 unsigned int i;
49 u32 srcrpf = 0;
50 u32 outfmt = 0;
Laurent Pinchart7578c202014-05-21 19:00:05 -030051
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030052 if (!enable) {
53 vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0);
Takashi Saito1517b032015-09-07 01:40:25 -030054 vsp1_write(vsp1, wpf->entity.index * VI6_WPF_OFFSET +
55 VI6_WPF_SRCRPF, 0);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030056 return 0;
57 }
58
Takanari Hayama5d0beee2014-11-26 22:25:02 -030059 /* Sources. If the pipeline has a single input and BRU is not used,
60 * configure it as the master layer. Otherwise configure all
61 * inputs as sub-layers and select the virtual RPF as the master
62 * layer.
Laurent Pinchart629bb6d2013-07-10 18:03:46 -030063 */
Laurent Pinchart5aa2eb32015-12-05 20:17:10 -020064 for (i = 0; i < vsp1->info->rpf_count; ++i) {
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030065 struct vsp1_rwpf *input = pipe->inputs[i];
66
Laurent Pinchart96bfa6a2015-08-05 16:40:31 -030067 if (!input)
68 continue;
69
Takanari Hayama5d0beee2014-11-26 22:25:02 -030070 srcrpf |= (!pipe->bru && pipe->num_inputs == 1)
Laurent Pinchart629bb6d2013-07-10 18:03:46 -030071 ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
72 : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030073 }
74
Takanari Hayama5d0beee2014-11-26 22:25:02 -030075 if (pipe->bru || pipe->num_inputs > 1)
Laurent Pinchart629bb6d2013-07-10 18:03:46 -030076 srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
77
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030078 vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, srcrpf);
79
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030080 /* Destination stride. */
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030081 if (!pipe->lif) {
Laurent Pinchart86960ee2015-07-28 14:00:43 -030082 struct v4l2_pix_format_mplane *format = &wpf->format;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030083
84 vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_Y,
85 format->plane_fmt[0].bytesperline);
86 if (format->num_planes > 1)
87 vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_C,
88 format->plane_fmt[1].bytesperline);
89 }
90
Laurent Pinchartb7e51072015-11-15 19:14:22 -020091 crop = vsp1_rwpf_get_crop(wpf, wpf->entity.config);
92
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030093 vsp1_wpf_write(wpf, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
94 (crop->left << VI6_WPF_SZCLIP_OFST_SHIFT) |
95 (crop->width << VI6_WPF_SZCLIP_SIZE_SHIFT));
96 vsp1_wpf_write(wpf, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
97 (crop->top << VI6_WPF_SZCLIP_OFST_SHIFT) |
98 (crop->height << VI6_WPF_SZCLIP_SIZE_SHIFT));
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030099
100 /* Format */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200101 sink_format = vsp1_entity_get_pad_format(&wpf->entity,
102 wpf->entity.config,
103 RWPF_PAD_SINK);
104 source_format = vsp1_entity_get_pad_format(&wpf->entity,
105 wpf->entity.config,
106 RWPF_PAD_SOURCE);
107
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300108 if (!pipe->lif) {
Laurent Pinchart86960ee2015-07-28 14:00:43 -0300109 const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300110
111 outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
112
Laurent Pinchart7a52b6d2014-05-26 20:12:53 -0300113 if (fmtinfo->alpha)
114 outfmt |= VI6_WPF_OUTFMT_PXA;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300115 if (fmtinfo->swap_yc)
116 outfmt |= VI6_WPF_OUTFMT_SPYCS;
117 if (fmtinfo->swap_uv)
118 outfmt |= VI6_WPF_OUTFMT_SPUVS;
119
120 vsp1_wpf_write(wpf, VI6_WPF_DSWAP, fmtinfo->swap);
121 }
122
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200123 if (sink_format->code != source_format->code)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300124 outfmt |= VI6_WPF_OUTFMT_CSC;
125
Laurent Pinchartbd2fdd52015-11-01 12:19:42 -0200126 outfmt |= wpf->alpha << VI6_WPF_OUTFMT_PDV_SHIFT;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300127 vsp1_wpf_write(wpf, VI6_WPF_OUTFMT, outfmt);
128
Takashi Saito1517b032015-09-07 01:40:25 -0300129 vsp1_mod_write(&wpf->entity, VI6_DPR_WPF_FPORCH(wpf->entity.index),
130 VI6_DPR_WPF_FPORCH_FP_WPFN);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300131
Takashi Saito1517b032015-09-07 01:40:25 -0300132 vsp1_mod_write(&wpf->entity, VI6_WPF_WRBCK_CTRL, 0);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300133
134 /* Enable interrupts */
135 vsp1_write(vsp1, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
136 vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index),
137 VI6_WFP_IRQ_ENB_FREE);
138
139 return 0;
140}
141
142/* -----------------------------------------------------------------------------
143 * V4L2 Subdevice Operations
144 */
145
146static struct v4l2_subdev_video_ops wpf_video_ops = {
147 .s_stream = wpf_s_stream,
148};
149
150static struct v4l2_subdev_pad_ops wpf_pad_ops = {
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200151 .init_cfg = vsp1_entity_init_cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300152 .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
153 .enum_frame_size = vsp1_rwpf_enum_frame_size,
154 .get_fmt = vsp1_rwpf_get_format,
155 .set_fmt = vsp1_rwpf_set_format,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300156 .get_selection = vsp1_rwpf_get_selection,
157 .set_selection = vsp1_rwpf_set_selection,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300158};
159
160static struct v4l2_subdev_ops wpf_ops = {
161 .video = &wpf_video_ops,
162 .pad = &wpf_pad_ops,
163};
164
165/* -----------------------------------------------------------------------------
Laurent Pinchart52434532015-11-17 12:23:23 -0200166 * VSP1 Entity Operations
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300167 */
168
Laurent Pinchart52434532015-11-17 12:23:23 -0200169static void vsp1_wpf_destroy(struct vsp1_entity *entity)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300170{
Laurent Pinchart52434532015-11-17 12:23:23 -0200171 struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
172
173 vsp1_dlm_destroy(wpf->dlm);
174}
175
176static void wpf_set_memory(struct vsp1_entity *entity)
177{
178 struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
179
Laurent Pinchart351bbf92015-11-01 15:18:56 -0200180 vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_Y, wpf->mem.addr[0]);
181 vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C0, wpf->mem.addr[1]);
182 vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C1, wpf->mem.addr[2]);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300183}
184
Laurent Pinchart52434532015-11-17 12:23:23 -0200185static const struct vsp1_entity_operations wpf_entity_ops = {
186 .destroy = vsp1_wpf_destroy,
Laurent Pinchartb58faa92015-07-28 16:04:47 -0300187 .set_memory = wpf_set_memory,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300188};
189
190/* -----------------------------------------------------------------------------
191 * Initialization and Cleanup
192 */
193
194struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
195{
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300196 struct vsp1_rwpf *wpf;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200197 char name[6];
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300198 int ret;
199
200 wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
201 if (wpf == NULL)
202 return ERR_PTR(-ENOMEM);
203
204 wpf->max_width = WPF_MAX_WIDTH;
205 wpf->max_height = WPF_MAX_HEIGHT;
206
Laurent Pinchart52434532015-11-17 12:23:23 -0200207 wpf->entity.ops = &wpf_entity_ops;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300208 wpf->entity.type = VSP1_ENTITY_WPF;
209 wpf->entity.index = index;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300210
Laurent Pinchart823329d2015-11-15 19:42:01 -0200211 sprintf(name, "wpf.%u", index);
212 ret = vsp1_entity_init(vsp1, &wpf->entity, name, 2, &wpf_ops);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300213 if (ret < 0)
214 return ERR_PTR(ret);
215
Laurent Pinchart351bbf92015-11-01 15:18:56 -0200216 /* Initialize the display list manager. */
217 wpf->dlm = vsp1_dlm_create(vsp1, index, 4);
218 if (!wpf->dlm) {
219 ret = -ENOMEM;
220 goto error;
Laurent Pinchartef9621b2015-11-14 22:27:52 -0200221 }
222
Laurent Pinchart7578c202014-05-21 19:00:05 -0300223 /* Initialize the control handler. */
Laurent Pinchartbd2fdd52015-11-01 12:19:42 -0200224 ret = vsp1_rwpf_init_ctrls(wpf);
225 if (ret < 0) {
Laurent Pinchart7578c202014-05-21 19:00:05 -0300226 dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
227 index);
Laurent Pinchart7578c202014-05-21 19:00:05 -0300228 goto error;
229 }
230
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300231 return wpf;
232
Laurent Pinchart1499be62014-05-28 12:49:13 -0300233error:
234 vsp1_entity_destroy(&wpf->entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300235 return ERR_PTR(ret);
236}