blob: ce1d0b4094dba544db5104363c810568faedf1d2 [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 Pincharta0cdac52016-01-17 19:53:56 -020020#include "vsp1_pipe.h"
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030021#include "vsp1_rwpf.h"
22#include "vsp1_video.h"
23
24#define WPF_MAX_WIDTH 2048
25#define WPF_MAX_HEIGHT 2048
26
27/* -----------------------------------------------------------------------------
28 * Device Access
29 */
30
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020031static inline void vsp1_wpf_write(struct vsp1_rwpf *wpf,
32 struct vsp1_dl_list *dl, u32 reg, u32 data)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030033{
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020034 vsp1_dl_list_write(dl, reg + wpf->entity.index * VI6_WPF_OFFSET, data);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030035}
36
37/* -----------------------------------------------------------------------------
38 * V4L2 Subdevice Core Operations
39 */
40
41static int wpf_s_stream(struct v4l2_subdev *subdev, int enable)
42{
43 struct vsp1_rwpf *wpf = to_rwpf(subdev);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030044 struct vsp1_device *vsp1 = wpf->entity.vsp1;
Laurent Pinchart7578c202014-05-21 19:00:05 -030045
Laurent Pinchart7b905f02015-11-17 13:10:26 -020046 if (enable)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030047 return 0;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030048
Laurent Pinchart7b905f02015-11-17 13:10:26 -020049 /* Write to registers directly when stopping the stream as there will be
50 * no pipeline run to apply the display list.
Laurent Pinchart629bb6d2013-07-10 18:03:46 -030051 */
Laurent Pinchart7b905f02015-11-17 13:10:26 -020052 vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0);
53 vsp1_write(vsp1, wpf->entity.index * VI6_WPF_OFFSET +
54 VI6_WPF_SRCRPF, 0);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030055
56 return 0;
57}
58
59/* -----------------------------------------------------------------------------
60 * V4L2 Subdevice Operations
61 */
62
63static struct v4l2_subdev_video_ops wpf_video_ops = {
64 .s_stream = wpf_s_stream,
65};
66
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030067static struct v4l2_subdev_ops wpf_ops = {
68 .video = &wpf_video_ops,
Laurent Pinchartc6c8efb2015-11-22 13:37:45 -020069 .pad = &vsp1_rwpf_pad_ops,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030070};
71
72/* -----------------------------------------------------------------------------
Laurent Pinchart52434532015-11-17 12:23:23 -020073 * VSP1 Entity Operations
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030074 */
75
Laurent Pinchart52434532015-11-17 12:23:23 -020076static void vsp1_wpf_destroy(struct vsp1_entity *entity)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030077{
Laurent Pinchart52434532015-11-17 12:23:23 -020078 struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
79
80 vsp1_dlm_destroy(wpf->dlm);
81}
82
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020083static void wpf_set_memory(struct vsp1_entity *entity, struct vsp1_dl_list *dl)
Laurent Pinchart52434532015-11-17 12:23:23 -020084{
85 struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
86
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020087 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_Y, wpf->mem.addr[0]);
88 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C0, wpf->mem.addr[1]);
89 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C1, wpf->mem.addr[2]);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030090}
91
Laurent Pinchart83dd0192016-01-14 14:17:32 -020092static void wpf_configure(struct vsp1_entity *entity,
93 struct vsp1_pipeline *pipe,
94 struct vsp1_dl_list *dl)
Laurent Pinchart7b905f02015-11-17 13:10:26 -020095{
Laurent Pinchart7b905f02015-11-17 13:10:26 -020096 struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev);
97 struct vsp1_device *vsp1 = wpf->entity.vsp1;
98 const struct v4l2_mbus_framefmt *source_format;
99 const struct v4l2_mbus_framefmt *sink_format;
100 const struct v4l2_rect *crop;
101 unsigned int i;
102 u32 outfmt = 0;
103 u32 srcrpf = 0;
104
105 /* Cropping */
106 crop = vsp1_rwpf_get_crop(wpf, wpf->entity.config);
107
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200108 vsp1_wpf_write(wpf, dl, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200109 (crop->left << VI6_WPF_SZCLIP_OFST_SHIFT) |
110 (crop->width << VI6_WPF_SZCLIP_SIZE_SHIFT));
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200111 vsp1_wpf_write(wpf, dl, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200112 (crop->top << VI6_WPF_SZCLIP_OFST_SHIFT) |
113 (crop->height << VI6_WPF_SZCLIP_SIZE_SHIFT));
114
115 /* Format */
116 sink_format = vsp1_entity_get_pad_format(&wpf->entity,
117 wpf->entity.config,
118 RWPF_PAD_SINK);
119 source_format = vsp1_entity_get_pad_format(&wpf->entity,
120 wpf->entity.config,
121 RWPF_PAD_SOURCE);
122
123 if (!pipe->lif) {
124 const struct v4l2_pix_format_mplane *format = &wpf->format;
125 const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
126
127 outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
128
129 if (fmtinfo->alpha)
130 outfmt |= VI6_WPF_OUTFMT_PXA;
131 if (fmtinfo->swap_yc)
132 outfmt |= VI6_WPF_OUTFMT_SPYCS;
133 if (fmtinfo->swap_uv)
134 outfmt |= VI6_WPF_OUTFMT_SPUVS;
135
136 /* Destination stride and byte swapping. */
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200137 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_STRIDE_Y,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200138 format->plane_fmt[0].bytesperline);
139 if (format->num_planes > 1)
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200140 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_STRIDE_C,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200141 format->plane_fmt[1].bytesperline);
142
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200143 vsp1_wpf_write(wpf, dl, VI6_WPF_DSWAP, fmtinfo->swap);
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200144 }
145
146 if (sink_format->code != source_format->code)
147 outfmt |= VI6_WPF_OUTFMT_CSC;
148
149 outfmt |= wpf->alpha << VI6_WPF_OUTFMT_PDV_SHIFT;
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200150 vsp1_wpf_write(wpf, dl, VI6_WPF_OUTFMT, outfmt);
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200151
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200152 vsp1_dl_list_write(dl, VI6_DPR_WPF_FPORCH(wpf->entity.index),
153 VI6_DPR_WPF_FPORCH_FP_WPFN);
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200154
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200155 vsp1_dl_list_write(dl, VI6_WPF_WRBCK_CTRL, 0);
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200156
157 /* Sources. If the pipeline has a single input and BRU is not used,
158 * configure it as the master layer. Otherwise configure all
159 * inputs as sub-layers and select the virtual RPF as the master
160 * layer.
161 */
162 for (i = 0; i < vsp1->info->rpf_count; ++i) {
163 struct vsp1_rwpf *input = pipe->inputs[i];
164
165 if (!input)
166 continue;
167
168 srcrpf |= (!pipe->bru && pipe->num_inputs == 1)
169 ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
170 : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
171 }
172
173 if (pipe->bru || pipe->num_inputs > 1)
174 srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
175
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200176 vsp1_wpf_write(wpf, dl, VI6_WPF_SRCRPF, srcrpf);
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200177
178 /* Enable interrupts */
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200179 vsp1_dl_list_write(dl, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
180 vsp1_dl_list_write(dl, VI6_WPF_IRQ_ENB(wpf->entity.index),
181 VI6_WFP_IRQ_ENB_FREE);
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200182}
183
Laurent Pinchart52434532015-11-17 12:23:23 -0200184static const struct vsp1_entity_operations wpf_entity_ops = {
185 .destroy = vsp1_wpf_destroy,
Laurent Pinchartb58faa92015-07-28 16:04:47 -0300186 .set_memory = wpf_set_memory,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200187 .configure = wpf_configure,
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}