1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * vsp1_entity.c -- R-Car VSP1 Base Entity
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2013-2014 Renesas Electronics Corporation
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/device.h>
11*4882a593Smuzhiyun #include <linux/gfp.h>
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <media/media-entity.h>
14*4882a593Smuzhiyun #include <media/v4l2-ctrls.h>
15*4882a593Smuzhiyun #include <media/v4l2-subdev.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include "vsp1.h"
18*4882a593Smuzhiyun #include "vsp1_dl.h"
19*4882a593Smuzhiyun #include "vsp1_entity.h"
20*4882a593Smuzhiyun #include "vsp1_pipe.h"
21*4882a593Smuzhiyun #include "vsp1_rwpf.h"
22*4882a593Smuzhiyun
vsp1_entity_route_setup(struct vsp1_entity * entity,struct vsp1_pipeline * pipe,struct vsp1_dl_body * dlb)23*4882a593Smuzhiyun void vsp1_entity_route_setup(struct vsp1_entity *entity,
24*4882a593Smuzhiyun struct vsp1_pipeline *pipe,
25*4882a593Smuzhiyun struct vsp1_dl_body *dlb)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun struct vsp1_entity *source;
28*4882a593Smuzhiyun u32 route;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun if (entity->type == VSP1_ENTITY_HGO) {
31*4882a593Smuzhiyun u32 smppt;
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun /*
34*4882a593Smuzhiyun * The HGO is a special case, its routing is configured on the
35*4882a593Smuzhiyun * sink pad.
36*4882a593Smuzhiyun */
37*4882a593Smuzhiyun source = entity->sources[0];
38*4882a593Smuzhiyun smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT)
39*4882a593Smuzhiyun | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT);
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun vsp1_dl_body_write(dlb, VI6_DPR_HGO_SMPPT, smppt);
42*4882a593Smuzhiyun return;
43*4882a593Smuzhiyun } else if (entity->type == VSP1_ENTITY_HGT) {
44*4882a593Smuzhiyun u32 smppt;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /*
47*4882a593Smuzhiyun * The HGT is a special case, its routing is configured on the
48*4882a593Smuzhiyun * sink pad.
49*4882a593Smuzhiyun */
50*4882a593Smuzhiyun source = entity->sources[0];
51*4882a593Smuzhiyun smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT)
52*4882a593Smuzhiyun | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT);
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun vsp1_dl_body_write(dlb, VI6_DPR_HGT_SMPPT, smppt);
55*4882a593Smuzhiyun return;
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun source = entity;
59*4882a593Smuzhiyun if (source->route->reg == 0)
60*4882a593Smuzhiyun return;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun route = source->sink->route->inputs[source->sink_pad];
63*4882a593Smuzhiyun /*
64*4882a593Smuzhiyun * The ILV and BRS share the same data path route. The extra BRSSEL bit
65*4882a593Smuzhiyun * selects between the ILV and BRS.
66*4882a593Smuzhiyun */
67*4882a593Smuzhiyun if (source->type == VSP1_ENTITY_BRS)
68*4882a593Smuzhiyun route |= VI6_DPR_ROUTE_BRSSEL;
69*4882a593Smuzhiyun vsp1_dl_body_write(dlb, source->route->reg, route);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
vsp1_entity_configure_stream(struct vsp1_entity * entity,struct vsp1_pipeline * pipe,struct vsp1_dl_list * dl,struct vsp1_dl_body * dlb)72*4882a593Smuzhiyun void vsp1_entity_configure_stream(struct vsp1_entity *entity,
73*4882a593Smuzhiyun struct vsp1_pipeline *pipe,
74*4882a593Smuzhiyun struct vsp1_dl_list *dl,
75*4882a593Smuzhiyun struct vsp1_dl_body *dlb)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun if (entity->ops->configure_stream)
78*4882a593Smuzhiyun entity->ops->configure_stream(entity, pipe, dl, dlb);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
vsp1_entity_configure_frame(struct vsp1_entity * entity,struct vsp1_pipeline * pipe,struct vsp1_dl_list * dl,struct vsp1_dl_body * dlb)81*4882a593Smuzhiyun void vsp1_entity_configure_frame(struct vsp1_entity *entity,
82*4882a593Smuzhiyun struct vsp1_pipeline *pipe,
83*4882a593Smuzhiyun struct vsp1_dl_list *dl,
84*4882a593Smuzhiyun struct vsp1_dl_body *dlb)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun if (entity->ops->configure_frame)
87*4882a593Smuzhiyun entity->ops->configure_frame(entity, pipe, dl, dlb);
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun
vsp1_entity_configure_partition(struct vsp1_entity * entity,struct vsp1_pipeline * pipe,struct vsp1_dl_list * dl,struct vsp1_dl_body * dlb)90*4882a593Smuzhiyun void vsp1_entity_configure_partition(struct vsp1_entity *entity,
91*4882a593Smuzhiyun struct vsp1_pipeline *pipe,
92*4882a593Smuzhiyun struct vsp1_dl_list *dl,
93*4882a593Smuzhiyun struct vsp1_dl_body *dlb)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun if (entity->ops->configure_partition)
96*4882a593Smuzhiyun entity->ops->configure_partition(entity, pipe, dl, dlb);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
100*4882a593Smuzhiyun * V4L2 Subdevice Operations
101*4882a593Smuzhiyun */
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /**
104*4882a593Smuzhiyun * vsp1_entity_get_pad_config - Get the pad configuration for an entity
105*4882a593Smuzhiyun * @entity: the entity
106*4882a593Smuzhiyun * @cfg: the TRY pad configuration
107*4882a593Smuzhiyun * @which: configuration selector (ACTIVE or TRY)
108*4882a593Smuzhiyun *
109*4882a593Smuzhiyun * When called with which set to V4L2_SUBDEV_FORMAT_ACTIVE the caller must hold
110*4882a593Smuzhiyun * the entity lock to access the returned configuration.
111*4882a593Smuzhiyun *
112*4882a593Smuzhiyun * Return the pad configuration requested by the which argument. The TRY
113*4882a593Smuzhiyun * configuration is passed explicitly to the function through the cfg argument
114*4882a593Smuzhiyun * and simply returned when requested. The ACTIVE configuration comes from the
115*4882a593Smuzhiyun * entity structure.
116*4882a593Smuzhiyun */
117*4882a593Smuzhiyun struct v4l2_subdev_pad_config *
vsp1_entity_get_pad_config(struct vsp1_entity * entity,struct v4l2_subdev_pad_config * cfg,enum v4l2_subdev_format_whence which)118*4882a593Smuzhiyun vsp1_entity_get_pad_config(struct vsp1_entity *entity,
119*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg,
120*4882a593Smuzhiyun enum v4l2_subdev_format_whence which)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun switch (which) {
123*4882a593Smuzhiyun case V4L2_SUBDEV_FORMAT_ACTIVE:
124*4882a593Smuzhiyun return entity->config;
125*4882a593Smuzhiyun case V4L2_SUBDEV_FORMAT_TRY:
126*4882a593Smuzhiyun default:
127*4882a593Smuzhiyun return cfg;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /**
132*4882a593Smuzhiyun * vsp1_entity_get_pad_format - Get a pad format from storage for an entity
133*4882a593Smuzhiyun * @entity: the entity
134*4882a593Smuzhiyun * @cfg: the configuration storage
135*4882a593Smuzhiyun * @pad: the pad number
136*4882a593Smuzhiyun *
137*4882a593Smuzhiyun * Return the format stored in the given configuration for an entity's pad. The
138*4882a593Smuzhiyun * configuration can be an ACTIVE or TRY configuration.
139*4882a593Smuzhiyun */
140*4882a593Smuzhiyun struct v4l2_mbus_framefmt *
vsp1_entity_get_pad_format(struct vsp1_entity * entity,struct v4l2_subdev_pad_config * cfg,unsigned int pad)141*4882a593Smuzhiyun vsp1_entity_get_pad_format(struct vsp1_entity *entity,
142*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg,
143*4882a593Smuzhiyun unsigned int pad)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad);
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /**
149*4882a593Smuzhiyun * vsp1_entity_get_pad_selection - Get a pad selection from storage for entity
150*4882a593Smuzhiyun * @entity: the entity
151*4882a593Smuzhiyun * @cfg: the configuration storage
152*4882a593Smuzhiyun * @pad: the pad number
153*4882a593Smuzhiyun * @target: the selection target
154*4882a593Smuzhiyun *
155*4882a593Smuzhiyun * Return the selection rectangle stored in the given configuration for an
156*4882a593Smuzhiyun * entity's pad. The configuration can be an ACTIVE or TRY configuration. The
157*4882a593Smuzhiyun * selection target can be COMPOSE or CROP.
158*4882a593Smuzhiyun */
159*4882a593Smuzhiyun struct v4l2_rect *
vsp1_entity_get_pad_selection(struct vsp1_entity * entity,struct v4l2_subdev_pad_config * cfg,unsigned int pad,unsigned int target)160*4882a593Smuzhiyun vsp1_entity_get_pad_selection(struct vsp1_entity *entity,
161*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg,
162*4882a593Smuzhiyun unsigned int pad, unsigned int target)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun switch (target) {
165*4882a593Smuzhiyun case V4L2_SEL_TGT_COMPOSE:
166*4882a593Smuzhiyun return v4l2_subdev_get_try_compose(&entity->subdev, cfg, pad);
167*4882a593Smuzhiyun case V4L2_SEL_TGT_CROP:
168*4882a593Smuzhiyun return v4l2_subdev_get_try_crop(&entity->subdev, cfg, pad);
169*4882a593Smuzhiyun default:
170*4882a593Smuzhiyun return NULL;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /*
175*4882a593Smuzhiyun * vsp1_entity_init_cfg - Initialize formats on all pads
176*4882a593Smuzhiyun * @subdev: V4L2 subdevice
177*4882a593Smuzhiyun * @cfg: V4L2 subdev pad configuration
178*4882a593Smuzhiyun *
179*4882a593Smuzhiyun * Initialize all pad formats with default values in the given pad config. This
180*4882a593Smuzhiyun * function can be used as a handler for the subdev pad::init_cfg operation.
181*4882a593Smuzhiyun */
vsp1_entity_init_cfg(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg)182*4882a593Smuzhiyun int vsp1_entity_init_cfg(struct v4l2_subdev *subdev,
183*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun struct v4l2_subdev_format format;
186*4882a593Smuzhiyun unsigned int pad;
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
189*4882a593Smuzhiyun memset(&format, 0, sizeof(format));
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun format.pad = pad;
192*4882a593Smuzhiyun format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY
193*4882a593Smuzhiyun : V4L2_SUBDEV_FORMAT_ACTIVE;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format);
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun return 0;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun /*
202*4882a593Smuzhiyun * vsp1_subdev_get_pad_format - Subdev pad get_fmt handler
203*4882a593Smuzhiyun * @subdev: V4L2 subdevice
204*4882a593Smuzhiyun * @cfg: V4L2 subdev pad configuration
205*4882a593Smuzhiyun * @fmt: V4L2 subdev format
206*4882a593Smuzhiyun *
207*4882a593Smuzhiyun * This function implements the subdev get_fmt pad operation. It can be used as
208*4882a593Smuzhiyun * a direct drop-in for the operation handler.
209*4882a593Smuzhiyun */
vsp1_subdev_get_pad_format(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)210*4882a593Smuzhiyun int vsp1_subdev_get_pad_format(struct v4l2_subdev *subdev,
211*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg,
212*4882a593Smuzhiyun struct v4l2_subdev_format *fmt)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun struct vsp1_entity *entity = to_vsp1_entity(subdev);
215*4882a593Smuzhiyun struct v4l2_subdev_pad_config *config;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun config = vsp1_entity_get_pad_config(entity, cfg, fmt->which);
218*4882a593Smuzhiyun if (!config)
219*4882a593Smuzhiyun return -EINVAL;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun mutex_lock(&entity->lock);
222*4882a593Smuzhiyun fmt->format = *vsp1_entity_get_pad_format(entity, config, fmt->pad);
223*4882a593Smuzhiyun mutex_unlock(&entity->lock);
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun return 0;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun /*
229*4882a593Smuzhiyun * vsp1_subdev_enum_mbus_code - Subdev pad enum_mbus_code handler
230*4882a593Smuzhiyun * @subdev: V4L2 subdevice
231*4882a593Smuzhiyun * @cfg: V4L2 subdev pad configuration
232*4882a593Smuzhiyun * @code: Media bus code enumeration
233*4882a593Smuzhiyun * @codes: Array of supported media bus codes
234*4882a593Smuzhiyun * @ncodes: Number of supported media bus codes
235*4882a593Smuzhiyun *
236*4882a593Smuzhiyun * This function implements the subdev enum_mbus_code pad operation for entities
237*4882a593Smuzhiyun * that do not support format conversion. It enumerates the given supported
238*4882a593Smuzhiyun * media bus codes on the sink pad and reports a source pad format identical to
239*4882a593Smuzhiyun * the sink pad.
240*4882a593Smuzhiyun */
vsp1_subdev_enum_mbus_code(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code,const unsigned int * codes,unsigned int ncodes)241*4882a593Smuzhiyun int vsp1_subdev_enum_mbus_code(struct v4l2_subdev *subdev,
242*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg,
243*4882a593Smuzhiyun struct v4l2_subdev_mbus_code_enum *code,
244*4882a593Smuzhiyun const unsigned int *codes, unsigned int ncodes)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun struct vsp1_entity *entity = to_vsp1_entity(subdev);
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun if (code->pad == 0) {
249*4882a593Smuzhiyun if (code->index >= ncodes)
250*4882a593Smuzhiyun return -EINVAL;
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun code->code = codes[code->index];
253*4882a593Smuzhiyun } else {
254*4882a593Smuzhiyun struct v4l2_subdev_pad_config *config;
255*4882a593Smuzhiyun struct v4l2_mbus_framefmt *format;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun /*
258*4882a593Smuzhiyun * The entity can't perform format conversion, the sink format
259*4882a593Smuzhiyun * is always identical to the source format.
260*4882a593Smuzhiyun */
261*4882a593Smuzhiyun if (code->index)
262*4882a593Smuzhiyun return -EINVAL;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun config = vsp1_entity_get_pad_config(entity, cfg, code->which);
265*4882a593Smuzhiyun if (!config)
266*4882a593Smuzhiyun return -EINVAL;
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun mutex_lock(&entity->lock);
269*4882a593Smuzhiyun format = vsp1_entity_get_pad_format(entity, config, 0);
270*4882a593Smuzhiyun code->code = format->code;
271*4882a593Smuzhiyun mutex_unlock(&entity->lock);
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun return 0;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun /*
278*4882a593Smuzhiyun * vsp1_subdev_enum_frame_size - Subdev pad enum_frame_size handler
279*4882a593Smuzhiyun * @subdev: V4L2 subdevice
280*4882a593Smuzhiyun * @cfg: V4L2 subdev pad configuration
281*4882a593Smuzhiyun * @fse: Frame size enumeration
282*4882a593Smuzhiyun * @min_width: Minimum image width
283*4882a593Smuzhiyun * @min_height: Minimum image height
284*4882a593Smuzhiyun * @max_width: Maximum image width
285*4882a593Smuzhiyun * @max_height: Maximum image height
286*4882a593Smuzhiyun *
287*4882a593Smuzhiyun * This function implements the subdev enum_frame_size pad operation for
288*4882a593Smuzhiyun * entities that do not support scaling or cropping. It reports the given
289*4882a593Smuzhiyun * minimum and maximum frame width and height on the sink pad, and a fixed
290*4882a593Smuzhiyun * source pad size identical to the sink pad.
291*4882a593Smuzhiyun */
vsp1_subdev_enum_frame_size(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse,unsigned int min_width,unsigned int min_height,unsigned int max_width,unsigned int max_height)292*4882a593Smuzhiyun int vsp1_subdev_enum_frame_size(struct v4l2_subdev *subdev,
293*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg,
294*4882a593Smuzhiyun struct v4l2_subdev_frame_size_enum *fse,
295*4882a593Smuzhiyun unsigned int min_width, unsigned int min_height,
296*4882a593Smuzhiyun unsigned int max_width, unsigned int max_height)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun struct vsp1_entity *entity = to_vsp1_entity(subdev);
299*4882a593Smuzhiyun struct v4l2_subdev_pad_config *config;
300*4882a593Smuzhiyun struct v4l2_mbus_framefmt *format;
301*4882a593Smuzhiyun int ret = 0;
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun config = vsp1_entity_get_pad_config(entity, cfg, fse->which);
304*4882a593Smuzhiyun if (!config)
305*4882a593Smuzhiyun return -EINVAL;
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun format = vsp1_entity_get_pad_format(entity, config, fse->pad);
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun mutex_lock(&entity->lock);
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun if (fse->index || fse->code != format->code) {
312*4882a593Smuzhiyun ret = -EINVAL;
313*4882a593Smuzhiyun goto done;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun if (fse->pad == 0) {
317*4882a593Smuzhiyun fse->min_width = min_width;
318*4882a593Smuzhiyun fse->max_width = max_width;
319*4882a593Smuzhiyun fse->min_height = min_height;
320*4882a593Smuzhiyun fse->max_height = max_height;
321*4882a593Smuzhiyun } else {
322*4882a593Smuzhiyun /*
323*4882a593Smuzhiyun * The size on the source pad are fixed and always identical to
324*4882a593Smuzhiyun * the size on the sink pad.
325*4882a593Smuzhiyun */
326*4882a593Smuzhiyun fse->min_width = format->width;
327*4882a593Smuzhiyun fse->max_width = format->width;
328*4882a593Smuzhiyun fse->min_height = format->height;
329*4882a593Smuzhiyun fse->max_height = format->height;
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun done:
333*4882a593Smuzhiyun mutex_unlock(&entity->lock);
334*4882a593Smuzhiyun return ret;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun /*
338*4882a593Smuzhiyun * vsp1_subdev_set_pad_format - Subdev pad set_fmt handler
339*4882a593Smuzhiyun * @subdev: V4L2 subdevice
340*4882a593Smuzhiyun * @cfg: V4L2 subdev pad configuration
341*4882a593Smuzhiyun * @fmt: V4L2 subdev format
342*4882a593Smuzhiyun * @codes: Array of supported media bus codes
343*4882a593Smuzhiyun * @ncodes: Number of supported media bus codes
344*4882a593Smuzhiyun * @min_width: Minimum image width
345*4882a593Smuzhiyun * @min_height: Minimum image height
346*4882a593Smuzhiyun * @max_width: Maximum image width
347*4882a593Smuzhiyun * @max_height: Maximum image height
348*4882a593Smuzhiyun *
349*4882a593Smuzhiyun * This function implements the subdev set_fmt pad operation for entities that
350*4882a593Smuzhiyun * do not support scaling or cropping. It defaults to the first supplied media
351*4882a593Smuzhiyun * bus code if the requested code isn't supported, clamps the size to the
352*4882a593Smuzhiyun * supplied minimum and maximum, and propagates the sink pad format to the
353*4882a593Smuzhiyun * source pad.
354*4882a593Smuzhiyun */
vsp1_subdev_set_pad_format(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt,const unsigned int * codes,unsigned int ncodes,unsigned int min_width,unsigned int min_height,unsigned int max_width,unsigned int max_height)355*4882a593Smuzhiyun int vsp1_subdev_set_pad_format(struct v4l2_subdev *subdev,
356*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg,
357*4882a593Smuzhiyun struct v4l2_subdev_format *fmt,
358*4882a593Smuzhiyun const unsigned int *codes, unsigned int ncodes,
359*4882a593Smuzhiyun unsigned int min_width, unsigned int min_height,
360*4882a593Smuzhiyun unsigned int max_width, unsigned int max_height)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun struct vsp1_entity *entity = to_vsp1_entity(subdev);
363*4882a593Smuzhiyun struct v4l2_subdev_pad_config *config;
364*4882a593Smuzhiyun struct v4l2_mbus_framefmt *format;
365*4882a593Smuzhiyun struct v4l2_rect *selection;
366*4882a593Smuzhiyun unsigned int i;
367*4882a593Smuzhiyun int ret = 0;
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun mutex_lock(&entity->lock);
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun config = vsp1_entity_get_pad_config(entity, cfg, fmt->which);
372*4882a593Smuzhiyun if (!config) {
373*4882a593Smuzhiyun ret = -EINVAL;
374*4882a593Smuzhiyun goto done;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun format = vsp1_entity_get_pad_format(entity, config, fmt->pad);
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun if (fmt->pad == entity->source_pad) {
380*4882a593Smuzhiyun /* The output format can't be modified. */
381*4882a593Smuzhiyun fmt->format = *format;
382*4882a593Smuzhiyun goto done;
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun /*
386*4882a593Smuzhiyun * Default to the first media bus code if the requested format is not
387*4882a593Smuzhiyun * supported.
388*4882a593Smuzhiyun */
389*4882a593Smuzhiyun for (i = 0; i < ncodes; ++i) {
390*4882a593Smuzhiyun if (fmt->format.code == codes[i])
391*4882a593Smuzhiyun break;
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun format->code = i < ncodes ? codes[i] : codes[0];
395*4882a593Smuzhiyun format->width = clamp_t(unsigned int, fmt->format.width,
396*4882a593Smuzhiyun min_width, max_width);
397*4882a593Smuzhiyun format->height = clamp_t(unsigned int, fmt->format.height,
398*4882a593Smuzhiyun min_height, max_height);
399*4882a593Smuzhiyun format->field = V4L2_FIELD_NONE;
400*4882a593Smuzhiyun format->colorspace = V4L2_COLORSPACE_SRGB;
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun fmt->format = *format;
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun /* Propagate the format to the source pad. */
405*4882a593Smuzhiyun format = vsp1_entity_get_pad_format(entity, config, entity->source_pad);
406*4882a593Smuzhiyun *format = fmt->format;
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun /* Reset the crop and compose rectangles. */
409*4882a593Smuzhiyun selection = vsp1_entity_get_pad_selection(entity, config, fmt->pad,
410*4882a593Smuzhiyun V4L2_SEL_TGT_CROP);
411*4882a593Smuzhiyun selection->left = 0;
412*4882a593Smuzhiyun selection->top = 0;
413*4882a593Smuzhiyun selection->width = format->width;
414*4882a593Smuzhiyun selection->height = format->height;
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun selection = vsp1_entity_get_pad_selection(entity, config, fmt->pad,
417*4882a593Smuzhiyun V4L2_SEL_TGT_COMPOSE);
418*4882a593Smuzhiyun selection->left = 0;
419*4882a593Smuzhiyun selection->top = 0;
420*4882a593Smuzhiyun selection->width = format->width;
421*4882a593Smuzhiyun selection->height = format->height;
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun done:
424*4882a593Smuzhiyun mutex_unlock(&entity->lock);
425*4882a593Smuzhiyun return ret;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
429*4882a593Smuzhiyun * Media Operations
430*4882a593Smuzhiyun */
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun static inline struct vsp1_entity *
media_entity_to_vsp1_entity(struct media_entity * entity)433*4882a593Smuzhiyun media_entity_to_vsp1_entity(struct media_entity *entity)
434*4882a593Smuzhiyun {
435*4882a593Smuzhiyun return container_of(entity, struct vsp1_entity, subdev.entity);
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun
vsp1_entity_link_setup_source(const struct media_pad * source_pad,const struct media_pad * sink_pad,u32 flags)438*4882a593Smuzhiyun static int vsp1_entity_link_setup_source(const struct media_pad *source_pad,
439*4882a593Smuzhiyun const struct media_pad *sink_pad,
440*4882a593Smuzhiyun u32 flags)
441*4882a593Smuzhiyun {
442*4882a593Smuzhiyun struct vsp1_entity *source;
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun source = media_entity_to_vsp1_entity(source_pad->entity);
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun if (!source->route)
447*4882a593Smuzhiyun return 0;
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun if (flags & MEDIA_LNK_FL_ENABLED) {
450*4882a593Smuzhiyun struct vsp1_entity *sink
451*4882a593Smuzhiyun = media_entity_to_vsp1_entity(sink_pad->entity);
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun /*
454*4882a593Smuzhiyun * Fan-out is limited to one for the normal data path plus
455*4882a593Smuzhiyun * optional HGO and HGT. We ignore the HGO and HGT here.
456*4882a593Smuzhiyun */
457*4882a593Smuzhiyun if (sink->type != VSP1_ENTITY_HGO &&
458*4882a593Smuzhiyun sink->type != VSP1_ENTITY_HGT) {
459*4882a593Smuzhiyun if (source->sink)
460*4882a593Smuzhiyun return -EBUSY;
461*4882a593Smuzhiyun source->sink = sink;
462*4882a593Smuzhiyun source->sink_pad = sink_pad->index;
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun } else {
465*4882a593Smuzhiyun source->sink = NULL;
466*4882a593Smuzhiyun source->sink_pad = 0;
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun return 0;
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun
vsp1_entity_link_setup_sink(const struct media_pad * source_pad,const struct media_pad * sink_pad,u32 flags)472*4882a593Smuzhiyun static int vsp1_entity_link_setup_sink(const struct media_pad *source_pad,
473*4882a593Smuzhiyun const struct media_pad *sink_pad,
474*4882a593Smuzhiyun u32 flags)
475*4882a593Smuzhiyun {
476*4882a593Smuzhiyun struct vsp1_entity *sink;
477*4882a593Smuzhiyun struct vsp1_entity *source;
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun sink = media_entity_to_vsp1_entity(sink_pad->entity);
480*4882a593Smuzhiyun source = media_entity_to_vsp1_entity(source_pad->entity);
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun if (flags & MEDIA_LNK_FL_ENABLED) {
483*4882a593Smuzhiyun /* Fan-in is limited to one. */
484*4882a593Smuzhiyun if (sink->sources[sink_pad->index])
485*4882a593Smuzhiyun return -EBUSY;
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun sink->sources[sink_pad->index] = source;
488*4882a593Smuzhiyun } else {
489*4882a593Smuzhiyun sink->sources[sink_pad->index] = NULL;
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun return 0;
493*4882a593Smuzhiyun }
494*4882a593Smuzhiyun
vsp1_entity_link_setup(struct media_entity * entity,const struct media_pad * local,const struct media_pad * remote,u32 flags)495*4882a593Smuzhiyun int vsp1_entity_link_setup(struct media_entity *entity,
496*4882a593Smuzhiyun const struct media_pad *local,
497*4882a593Smuzhiyun const struct media_pad *remote, u32 flags)
498*4882a593Smuzhiyun {
499*4882a593Smuzhiyun if (local->flags & MEDIA_PAD_FL_SOURCE)
500*4882a593Smuzhiyun return vsp1_entity_link_setup_source(local, remote, flags);
501*4882a593Smuzhiyun else
502*4882a593Smuzhiyun return vsp1_entity_link_setup_sink(remote, local, flags);
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun /**
506*4882a593Smuzhiyun * vsp1_entity_remote_pad - Find the pad at the remote end of a link
507*4882a593Smuzhiyun * @pad: Pad at the local end of the link
508*4882a593Smuzhiyun *
509*4882a593Smuzhiyun * Search for a remote pad connected to the given pad by iterating over all
510*4882a593Smuzhiyun * links originating or terminating at that pad until an enabled link is found.
511*4882a593Smuzhiyun *
512*4882a593Smuzhiyun * Our link setup implementation guarantees that the output fan-out will not be
513*4882a593Smuzhiyun * higher than one for the data pipelines, except for the links to the HGO and
514*4882a593Smuzhiyun * HGT that can be enabled in addition to a regular data link. When traversing
515*4882a593Smuzhiyun * outgoing links this function ignores HGO and HGT entities and should thus be
516*4882a593Smuzhiyun * used in place of the generic media_entity_remote_pad() function to traverse
517*4882a593Smuzhiyun * data pipelines.
518*4882a593Smuzhiyun *
519*4882a593Smuzhiyun * Return a pointer to the pad at the remote end of the first found enabled
520*4882a593Smuzhiyun * link, or NULL if no enabled link has been found.
521*4882a593Smuzhiyun */
vsp1_entity_remote_pad(struct media_pad * pad)522*4882a593Smuzhiyun struct media_pad *vsp1_entity_remote_pad(struct media_pad *pad)
523*4882a593Smuzhiyun {
524*4882a593Smuzhiyun struct media_link *link;
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun list_for_each_entry(link, &pad->entity->links, list) {
527*4882a593Smuzhiyun struct vsp1_entity *entity;
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun if (!(link->flags & MEDIA_LNK_FL_ENABLED))
530*4882a593Smuzhiyun continue;
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun /* If we're the sink the source will never be an HGO or HGT. */
533*4882a593Smuzhiyun if (link->sink == pad)
534*4882a593Smuzhiyun return link->source;
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun if (link->source != pad)
537*4882a593Smuzhiyun continue;
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun /* If the sink isn't a subdevice it can't be an HGO or HGT. */
540*4882a593Smuzhiyun if (!is_media_entity_v4l2_subdev(link->sink->entity))
541*4882a593Smuzhiyun return link->sink;
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun entity = media_entity_to_vsp1_entity(link->sink->entity);
544*4882a593Smuzhiyun if (entity->type != VSP1_ENTITY_HGO &&
545*4882a593Smuzhiyun entity->type != VSP1_ENTITY_HGT)
546*4882a593Smuzhiyun return link->sink;
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun return NULL;
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
554*4882a593Smuzhiyun * Initialization
555*4882a593Smuzhiyun */
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun #define VSP1_ENTITY_ROUTE(ent) \
558*4882a593Smuzhiyun { VSP1_ENTITY_##ent, 0, VI6_DPR_##ent##_ROUTE, \
559*4882a593Smuzhiyun { VI6_DPR_NODE_##ent }, VI6_DPR_NODE_##ent }
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun #define VSP1_ENTITY_ROUTE_RPF(idx) \
562*4882a593Smuzhiyun { VSP1_ENTITY_RPF, idx, VI6_DPR_RPF_ROUTE(idx), \
563*4882a593Smuzhiyun { 0, }, VI6_DPR_NODE_RPF(idx) }
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun #define VSP1_ENTITY_ROUTE_UDS(idx) \
566*4882a593Smuzhiyun { VSP1_ENTITY_UDS, idx, VI6_DPR_UDS_ROUTE(idx), \
567*4882a593Smuzhiyun { VI6_DPR_NODE_UDS(idx) }, VI6_DPR_NODE_UDS(idx) }
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun #define VSP1_ENTITY_ROUTE_UIF(idx) \
570*4882a593Smuzhiyun { VSP1_ENTITY_UIF, idx, VI6_DPR_UIF_ROUTE(idx), \
571*4882a593Smuzhiyun { VI6_DPR_NODE_UIF(idx) }, VI6_DPR_NODE_UIF(idx) }
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun #define VSP1_ENTITY_ROUTE_WPF(idx) \
574*4882a593Smuzhiyun { VSP1_ENTITY_WPF, idx, 0, \
575*4882a593Smuzhiyun { VI6_DPR_NODE_WPF(idx) }, VI6_DPR_NODE_WPF(idx) }
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun static const struct vsp1_route vsp1_routes[] = {
578*4882a593Smuzhiyun { VSP1_ENTITY_BRS, 0, VI6_DPR_ILV_BRS_ROUTE,
579*4882a593Smuzhiyun { VI6_DPR_NODE_BRS_IN(0), VI6_DPR_NODE_BRS_IN(1) }, 0 },
580*4882a593Smuzhiyun { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
581*4882a593Smuzhiyun { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
582*4882a593Smuzhiyun VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3),
583*4882a593Smuzhiyun VI6_DPR_NODE_BRU_IN(4) }, VI6_DPR_NODE_BRU_OUT },
584*4882a593Smuzhiyun VSP1_ENTITY_ROUTE(CLU),
585*4882a593Smuzhiyun { VSP1_ENTITY_HGO, 0, 0, { 0, }, 0 },
586*4882a593Smuzhiyun { VSP1_ENTITY_HGT, 0, 0, { 0, }, 0 },
587*4882a593Smuzhiyun VSP1_ENTITY_ROUTE(HSI),
588*4882a593Smuzhiyun VSP1_ENTITY_ROUTE(HST),
589*4882a593Smuzhiyun { VSP1_ENTITY_LIF, 0, 0, { 0, }, 0 },
590*4882a593Smuzhiyun { VSP1_ENTITY_LIF, 1, 0, { 0, }, 0 },
591*4882a593Smuzhiyun VSP1_ENTITY_ROUTE(LUT),
592*4882a593Smuzhiyun VSP1_ENTITY_ROUTE_RPF(0),
593*4882a593Smuzhiyun VSP1_ENTITY_ROUTE_RPF(1),
594*4882a593Smuzhiyun VSP1_ENTITY_ROUTE_RPF(2),
595*4882a593Smuzhiyun VSP1_ENTITY_ROUTE_RPF(3),
596*4882a593Smuzhiyun VSP1_ENTITY_ROUTE_RPF(4),
597*4882a593Smuzhiyun VSP1_ENTITY_ROUTE(SRU),
598*4882a593Smuzhiyun VSP1_ENTITY_ROUTE_UDS(0),
599*4882a593Smuzhiyun VSP1_ENTITY_ROUTE_UDS(1),
600*4882a593Smuzhiyun VSP1_ENTITY_ROUTE_UDS(2),
601*4882a593Smuzhiyun VSP1_ENTITY_ROUTE_UIF(0), /* Named UIF4 in the documentation */
602*4882a593Smuzhiyun VSP1_ENTITY_ROUTE_UIF(1), /* Named UIF5 in the documentation */
603*4882a593Smuzhiyun VSP1_ENTITY_ROUTE_WPF(0),
604*4882a593Smuzhiyun VSP1_ENTITY_ROUTE_WPF(1),
605*4882a593Smuzhiyun VSP1_ENTITY_ROUTE_WPF(2),
606*4882a593Smuzhiyun VSP1_ENTITY_ROUTE_WPF(3),
607*4882a593Smuzhiyun };
608*4882a593Smuzhiyun
vsp1_entity_init(struct vsp1_device * vsp1,struct vsp1_entity * entity,const char * name,unsigned int num_pads,const struct v4l2_subdev_ops * ops,u32 function)609*4882a593Smuzhiyun int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
610*4882a593Smuzhiyun const char *name, unsigned int num_pads,
611*4882a593Smuzhiyun const struct v4l2_subdev_ops *ops, u32 function)
612*4882a593Smuzhiyun {
613*4882a593Smuzhiyun struct v4l2_subdev *subdev;
614*4882a593Smuzhiyun unsigned int i;
615*4882a593Smuzhiyun int ret;
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
618*4882a593Smuzhiyun if (vsp1_routes[i].type == entity->type &&
619*4882a593Smuzhiyun vsp1_routes[i].index == entity->index) {
620*4882a593Smuzhiyun entity->route = &vsp1_routes[i];
621*4882a593Smuzhiyun break;
622*4882a593Smuzhiyun }
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun
625*4882a593Smuzhiyun if (i == ARRAY_SIZE(vsp1_routes))
626*4882a593Smuzhiyun return -EINVAL;
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun mutex_init(&entity->lock);
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun entity->vsp1 = vsp1;
631*4882a593Smuzhiyun entity->source_pad = num_pads - 1;
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun /* Allocate and initialize pads. */
634*4882a593Smuzhiyun entity->pads = devm_kcalloc(vsp1->dev,
635*4882a593Smuzhiyun num_pads, sizeof(*entity->pads),
636*4882a593Smuzhiyun GFP_KERNEL);
637*4882a593Smuzhiyun if (entity->pads == NULL)
638*4882a593Smuzhiyun return -ENOMEM;
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun for (i = 0; i < num_pads - 1; ++i)
641*4882a593Smuzhiyun entity->pads[i].flags = MEDIA_PAD_FL_SINK;
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun entity->sources = devm_kcalloc(vsp1->dev, max(num_pads - 1, 1U),
644*4882a593Smuzhiyun sizeof(*entity->sources), GFP_KERNEL);
645*4882a593Smuzhiyun if (entity->sources == NULL)
646*4882a593Smuzhiyun return -ENOMEM;
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun /* Single-pad entities only have a sink. */
649*4882a593Smuzhiyun entity->pads[num_pads - 1].flags = num_pads > 1 ? MEDIA_PAD_FL_SOURCE
650*4882a593Smuzhiyun : MEDIA_PAD_FL_SINK;
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun /* Initialize the media entity. */
653*4882a593Smuzhiyun ret = media_entity_pads_init(&entity->subdev.entity, num_pads,
654*4882a593Smuzhiyun entity->pads);
655*4882a593Smuzhiyun if (ret < 0)
656*4882a593Smuzhiyun return ret;
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun /* Initialize the V4L2 subdev. */
659*4882a593Smuzhiyun subdev = &entity->subdev;
660*4882a593Smuzhiyun v4l2_subdev_init(subdev, ops);
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun subdev->entity.function = function;
663*4882a593Smuzhiyun subdev->entity.ops = &vsp1->media_ops;
664*4882a593Smuzhiyun subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun snprintf(subdev->name, sizeof(subdev->name), "%s %s",
667*4882a593Smuzhiyun dev_name(vsp1->dev), name);
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun vsp1_entity_init_cfg(subdev, NULL);
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun /*
672*4882a593Smuzhiyun * Allocate the pad configuration to store formats and selection
673*4882a593Smuzhiyun * rectangles.
674*4882a593Smuzhiyun */
675*4882a593Smuzhiyun entity->config = v4l2_subdev_alloc_pad_config(&entity->subdev);
676*4882a593Smuzhiyun if (entity->config == NULL) {
677*4882a593Smuzhiyun media_entity_cleanup(&entity->subdev.entity);
678*4882a593Smuzhiyun return -ENOMEM;
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun return 0;
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun
vsp1_entity_destroy(struct vsp1_entity * entity)684*4882a593Smuzhiyun void vsp1_entity_destroy(struct vsp1_entity *entity)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun if (entity->ops && entity->ops->destroy)
687*4882a593Smuzhiyun entity->ops->destroy(entity);
688*4882a593Smuzhiyun if (entity->subdev.ctrl_handler)
689*4882a593Smuzhiyun v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
690*4882a593Smuzhiyun v4l2_subdev_free_pad_config(entity->config);
691*4882a593Smuzhiyun media_entity_cleanup(&entity->subdev.entity);
692*4882a593Smuzhiyun }
693