blob: d46910db7e08bbf34bfc7d8defe03cc1c96477cd [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 Pincharte5ad37b2013-08-24 20:49:58 -030045 const struct v4l2_rect *crop = &wpf->crop;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030046 unsigned int i;
47 u32 srcrpf = 0;
48 u32 outfmt = 0;
Laurent Pinchart7578c202014-05-21 19:00:05 -030049
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030050 if (!enable) {
51 vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0);
Takashi Saito1517b032015-09-07 01:40:25 -030052 vsp1_write(vsp1, wpf->entity.index * VI6_WPF_OFFSET +
53 VI6_WPF_SRCRPF, 0);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030054 return 0;
55 }
56
Takanari Hayama5d0beee2014-11-26 22:25:02 -030057 /* Sources. If the pipeline has a single input and BRU is not used,
58 * configure it as the master layer. Otherwise configure all
59 * inputs as sub-layers and select the virtual RPF as the master
60 * layer.
Laurent Pinchart629bb6d2013-07-10 18:03:46 -030061 */
Laurent Pinchart5aa2eb32015-12-05 20:17:10 -020062 for (i = 0; i < vsp1->info->rpf_count; ++i) {
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030063 struct vsp1_rwpf *input = pipe->inputs[i];
64
Laurent Pinchart96bfa6a2015-08-05 16:40:31 -030065 if (!input)
66 continue;
67
Takanari Hayama5d0beee2014-11-26 22:25:02 -030068 srcrpf |= (!pipe->bru && pipe->num_inputs == 1)
Laurent Pinchart629bb6d2013-07-10 18:03:46 -030069 ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
70 : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030071 }
72
Takanari Hayama5d0beee2014-11-26 22:25:02 -030073 if (pipe->bru || pipe->num_inputs > 1)
Laurent Pinchart629bb6d2013-07-10 18:03:46 -030074 srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
75
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030076 vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, srcrpf);
77
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030078 /* Destination stride. */
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030079 if (!pipe->lif) {
Laurent Pinchart86960ee2015-07-28 14:00:43 -030080 struct v4l2_pix_format_mplane *format = &wpf->format;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030081
82 vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_Y,
83 format->plane_fmt[0].bytesperline);
84 if (format->num_planes > 1)
85 vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_C,
86 format->plane_fmt[1].bytesperline);
87 }
88
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030089 vsp1_wpf_write(wpf, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
90 (crop->left << VI6_WPF_SZCLIP_OFST_SHIFT) |
91 (crop->width << VI6_WPF_SZCLIP_SIZE_SHIFT));
92 vsp1_wpf_write(wpf, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
93 (crop->top << VI6_WPF_SZCLIP_OFST_SHIFT) |
94 (crop->height << VI6_WPF_SZCLIP_SIZE_SHIFT));
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030095
96 /* Format */
97 if (!pipe->lif) {
Laurent Pinchart86960ee2015-07-28 14:00:43 -030098 const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030099
100 outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
101
Laurent Pinchart7a52b6d2014-05-26 20:12:53 -0300102 if (fmtinfo->alpha)
103 outfmt |= VI6_WPF_OUTFMT_PXA;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300104 if (fmtinfo->swap_yc)
105 outfmt |= VI6_WPF_OUTFMT_SPYCS;
106 if (fmtinfo->swap_uv)
107 outfmt |= VI6_WPF_OUTFMT_SPUVS;
108
109 vsp1_wpf_write(wpf, VI6_WPF_DSWAP, fmtinfo->swap);
110 }
111
112 if (wpf->entity.formats[RWPF_PAD_SINK].code !=
113 wpf->entity.formats[RWPF_PAD_SOURCE].code)
114 outfmt |= VI6_WPF_OUTFMT_CSC;
115
Laurent Pinchartbd2fdd52015-11-01 12:19:42 -0200116 outfmt |= wpf->alpha << VI6_WPF_OUTFMT_PDV_SHIFT;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300117 vsp1_wpf_write(wpf, VI6_WPF_OUTFMT, outfmt);
118
Takashi Saito1517b032015-09-07 01:40:25 -0300119 vsp1_mod_write(&wpf->entity, VI6_DPR_WPF_FPORCH(wpf->entity.index),
120 VI6_DPR_WPF_FPORCH_FP_WPFN);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300121
Takashi Saito1517b032015-09-07 01:40:25 -0300122 vsp1_mod_write(&wpf->entity, VI6_WPF_WRBCK_CTRL, 0);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300123
124 /* Enable interrupts */
125 vsp1_write(vsp1, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
126 vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index),
127 VI6_WFP_IRQ_ENB_FREE);
128
129 return 0;
130}
131
132/* -----------------------------------------------------------------------------
133 * V4L2 Subdevice Operations
134 */
135
136static struct v4l2_subdev_video_ops wpf_video_ops = {
137 .s_stream = wpf_s_stream,
138};
139
140static struct v4l2_subdev_pad_ops wpf_pad_ops = {
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200141 .init_cfg = vsp1_entity_init_cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300142 .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
143 .enum_frame_size = vsp1_rwpf_enum_frame_size,
144 .get_fmt = vsp1_rwpf_get_format,
145 .set_fmt = vsp1_rwpf_set_format,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300146 .get_selection = vsp1_rwpf_get_selection,
147 .set_selection = vsp1_rwpf_set_selection,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300148};
149
150static struct v4l2_subdev_ops wpf_ops = {
151 .video = &wpf_video_ops,
152 .pad = &wpf_pad_ops,
153};
154
155/* -----------------------------------------------------------------------------
Laurent Pinchart52434532015-11-17 12:23:23 -0200156 * VSP1 Entity Operations
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300157 */
158
Laurent Pinchart52434532015-11-17 12:23:23 -0200159static void vsp1_wpf_destroy(struct vsp1_entity *entity)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300160{
Laurent Pinchart52434532015-11-17 12:23:23 -0200161 struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
162
163 vsp1_dlm_destroy(wpf->dlm);
164}
165
166static void wpf_set_memory(struct vsp1_entity *entity)
167{
168 struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
169
Laurent Pinchart351bbf92015-11-01 15:18:56 -0200170 vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_Y, wpf->mem.addr[0]);
171 vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C0, wpf->mem.addr[1]);
172 vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C1, wpf->mem.addr[2]);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300173}
174
Laurent Pinchart52434532015-11-17 12:23:23 -0200175static const struct vsp1_entity_operations wpf_entity_ops = {
176 .destroy = vsp1_wpf_destroy,
Laurent Pinchartb58faa92015-07-28 16:04:47 -0300177 .set_memory = wpf_set_memory,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300178};
179
180/* -----------------------------------------------------------------------------
181 * Initialization and Cleanup
182 */
183
184struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
185{
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300186 struct vsp1_rwpf *wpf;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200187 char name[6];
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300188 int ret;
189
190 wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
191 if (wpf == NULL)
192 return ERR_PTR(-ENOMEM);
193
194 wpf->max_width = WPF_MAX_WIDTH;
195 wpf->max_height = WPF_MAX_HEIGHT;
196
Laurent Pinchart52434532015-11-17 12:23:23 -0200197 wpf->entity.ops = &wpf_entity_ops;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300198 wpf->entity.type = VSP1_ENTITY_WPF;
199 wpf->entity.index = index;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300200
Laurent Pinchart823329d2015-11-15 19:42:01 -0200201 sprintf(name, "wpf.%u", index);
202 ret = vsp1_entity_init(vsp1, &wpf->entity, name, 2, &wpf_ops);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300203 if (ret < 0)
204 return ERR_PTR(ret);
205
Laurent Pinchart351bbf92015-11-01 15:18:56 -0200206 /* Initialize the display list manager. */
207 wpf->dlm = vsp1_dlm_create(vsp1, index, 4);
208 if (!wpf->dlm) {
209 ret = -ENOMEM;
210 goto error;
Laurent Pinchartef9621b2015-11-14 22:27:52 -0200211 }
212
Laurent Pinchart7578c202014-05-21 19:00:05 -0300213 /* Initialize the control handler. */
Laurent Pinchartbd2fdd52015-11-01 12:19:42 -0200214 ret = vsp1_rwpf_init_ctrls(wpf);
215 if (ret < 0) {
Laurent Pinchart7578c202014-05-21 19:00:05 -0300216 dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
217 index);
Laurent Pinchart7578c202014-05-21 19:00:05 -0300218 goto error;
219 }
220
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300221 return wpf;
222
Laurent Pinchart1499be62014-05-28 12:49:13 -0300223error:
224 vsp1_entity_destroy(&wpf->entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300225 return ERR_PTR(ret);
226}