blob: c1587e3f01cb1c086afa6cbe961d1e2227f1ca42 [file] [log] [blame]
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03001/*
2 * vsp1_entity.c -- R-Car VSP1 Base Entity
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#include <linux/gfp.h>
16
17#include <media/media-entity.h>
Laurent Pincharta626e642013-07-10 12:03:30 -030018#include <media/v4l2-ctrls.h>
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030019#include <media/v4l2-subdev.h>
20
21#include "vsp1.h"
Laurent Pinchartaa380ea2015-11-01 10:46:25 -020022#include "vsp1_dl.h"
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030023#include "vsp1_entity.h"
Laurent Pinchartc8663c82016-09-07 09:09:53 -030024#include "vsp1_pipe.h"
25#include "vsp1_rwpf.h"
Laurent Pinchartaa380ea2015-11-01 10:46:25 -020026
Laurent Pinchart7cf0f122016-03-03 20:06:22 -030027static inline struct vsp1_entity *
28media_entity_to_vsp1_entity(struct media_entity *entity)
29{
30 return container_of(entity, struct vsp1_entity, subdev.entity);
31}
32
Laurent Pinchartc8663c82016-09-07 09:09:53 -030033void vsp1_entity_route_setup(struct vsp1_entity *entity,
34 struct vsp1_pipeline *pipe,
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020035 struct vsp1_dl_list *dl)
Laurent Pinchart665b6932015-08-02 18:58:31 -030036{
Laurent Pinchartc8663c82016-09-07 09:09:53 -030037 struct vsp1_entity *source;
Laurent Pinchart665b6932015-08-02 18:58:31 -030038 struct vsp1_entity *sink;
39
Laurent Pinchartf2421522016-02-24 20:40:22 -030040 if (entity->type == VSP1_ENTITY_HGO) {
41 u32 smppt;
42
43 /*
44 * The HGO is a special case, its routing is configured on the
45 * sink pad.
46 */
47 source = media_entity_to_vsp1_entity(entity->sources[0]);
48 smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT)
49 | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT);
50
51 vsp1_dl_list_write(dl, VI6_DPR_HGO_SMPPT, smppt);
52 return;
53 }
54
Laurent Pinchartc8663c82016-09-07 09:09:53 -030055 source = entity;
Laurent Pinchart665b6932015-08-02 18:58:31 -030056 if (source->route->reg == 0)
57 return;
58
Laurent Pinchart7cf0f122016-03-03 20:06:22 -030059 sink = media_entity_to_vsp1_entity(source->sink);
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020060 vsp1_dl_list_write(dl, source->route->reg,
61 sink->route->inputs[source->sink_pad]);
Laurent Pinchart665b6932015-08-02 18:58:31 -030062}
63
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030064/* -----------------------------------------------------------------------------
65 * V4L2 Subdevice Operations
66 */
67
Laurent Pincharte790c3c2015-11-15 19:14:22 -020068/**
69 * vsp1_entity_get_pad_config - Get the pad configuration for an entity
70 * @entity: the entity
71 * @cfg: the TRY pad configuration
72 * @which: configuration selector (ACTIVE or TRY)
73 *
Laurent Pinchart34e77ed2016-06-26 08:09:31 -030074 * When called with which set to V4L2_SUBDEV_FORMAT_ACTIVE the caller must hold
75 * the entity lock to access the returned configuration.
76 *
Laurent Pincharte790c3c2015-11-15 19:14:22 -020077 * Return the pad configuration requested by the which argument. The TRY
78 * configuration is passed explicitly to the function through the cfg argument
79 * and simply returned when requested. The ACTIVE configuration comes from the
80 * entity structure.
81 */
82struct v4l2_subdev_pad_config *
83vsp1_entity_get_pad_config(struct vsp1_entity *entity,
84 struct v4l2_subdev_pad_config *cfg,
85 enum v4l2_subdev_format_whence which)
86{
87 switch (which) {
88 case V4L2_SUBDEV_FORMAT_ACTIVE:
89 return entity->config;
90 case V4L2_SUBDEV_FORMAT_TRY:
91 default:
92 return cfg;
93 }
94}
95
96/**
97 * vsp1_entity_get_pad_format - Get a pad format from storage for an entity
98 * @entity: the entity
99 * @cfg: the configuration storage
100 * @pad: the pad number
101 *
102 * Return the format stored in the given configuration for an entity's pad. The
103 * configuration can be an ACTIVE or TRY configuration.
104 */
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300105struct v4l2_mbus_framefmt *
106vsp1_entity_get_pad_format(struct vsp1_entity *entity,
Hans Verkuilf7234132015-03-04 01:47:54 -0800107 struct v4l2_subdev_pad_config *cfg,
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200108 unsigned int pad)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300109{
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200110 return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300111}
112
Laurent Pinchartccd3d952016-03-03 20:17:49 -0300113/**
114 * vsp1_entity_get_pad_selection - Get a pad selection from storage for entity
115 * @entity: the entity
116 * @cfg: the configuration storage
117 * @pad: the pad number
118 * @target: the selection target
119 *
120 * Return the selection rectangle stored in the given configuration for an
121 * entity's pad. The configuration can be an ACTIVE or TRY configuration. The
122 * selection target can be COMPOSE or CROP.
123 */
Laurent Pinchartb7e51072015-11-15 19:14:22 -0200124struct v4l2_rect *
Laurent Pinchartccd3d952016-03-03 20:17:49 -0300125vsp1_entity_get_pad_selection(struct vsp1_entity *entity,
126 struct v4l2_subdev_pad_config *cfg,
127 unsigned int pad, unsigned int target)
Laurent Pinchartb7e51072015-11-15 19:14:22 -0200128{
Laurent Pinchartccd3d952016-03-03 20:17:49 -0300129 switch (target) {
130 case V4L2_SEL_TGT_COMPOSE:
131 return v4l2_subdev_get_try_compose(&entity->subdev, cfg, pad);
132 case V4L2_SEL_TGT_CROP:
133 return v4l2_subdev_get_try_crop(&entity->subdev, cfg, pad);
134 default:
135 return NULL;
136 }
Laurent Pinchartb7e51072015-11-15 19:14:22 -0200137}
138
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300139/*
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200140 * vsp1_entity_init_cfg - Initialize formats on all pads
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300141 * @subdev: V4L2 subdevice
Hans Verkuilf7234132015-03-04 01:47:54 -0800142 * @cfg: V4L2 subdev pad configuration
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300143 *
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200144 * Initialize all pad formats with default values in the given pad config. This
145 * function can be used as a handler for the subdev pad::init_cfg operation.
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300146 */
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200147int vsp1_entity_init_cfg(struct v4l2_subdev *subdev,
148 struct v4l2_subdev_pad_config *cfg)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300149{
150 struct v4l2_subdev_format format;
151 unsigned int pad;
152
153 for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
154 memset(&format, 0, sizeof(format));
155
156 format.pad = pad;
Hans Verkuilf7234132015-03-04 01:47:54 -0800157 format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300158 : V4L2_SUBDEV_FORMAT_ACTIVE;
159
Hans Verkuilf7234132015-03-04 01:47:54 -0800160 v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300161 }
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300162
163 return 0;
164}
165
Laurent Pinchart3f557222016-02-24 21:10:13 -0300166/*
167 * vsp1_subdev_get_pad_format - Subdev pad get_fmt handler
168 * @subdev: V4L2 subdevice
169 * @cfg: V4L2 subdev pad configuration
170 * @fmt: V4L2 subdev format
171 *
172 * This function implements the subdev get_fmt pad operation. It can be used as
173 * a direct drop-in for the operation handler.
174 */
175int vsp1_subdev_get_pad_format(struct v4l2_subdev *subdev,
176 struct v4l2_subdev_pad_config *cfg,
177 struct v4l2_subdev_format *fmt)
178{
179 struct vsp1_entity *entity = to_vsp1_entity(subdev);
180 struct v4l2_subdev_pad_config *config;
181
182 config = vsp1_entity_get_pad_config(entity, cfg, fmt->which);
183 if (!config)
184 return -EINVAL;
185
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300186 mutex_lock(&entity->lock);
Laurent Pinchart3f557222016-02-24 21:10:13 -0300187 fmt->format = *vsp1_entity_get_pad_format(entity, config, fmt->pad);
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300188 mutex_unlock(&entity->lock);
Laurent Pinchart3f557222016-02-24 21:10:13 -0300189
190 return 0;
191}
192
Laurent Pinchart6ad9ba92016-02-24 20:25:42 -0300193/*
194 * vsp1_subdev_enum_mbus_code - Subdev pad enum_mbus_code handler
195 * @subdev: V4L2 subdevice
196 * @cfg: V4L2 subdev pad configuration
197 * @code: Media bus code enumeration
198 * @codes: Array of supported media bus codes
199 * @ncodes: Number of supported media bus codes
200 *
201 * This function implements the subdev enum_mbus_code pad operation for entities
202 * that do not support format conversion. It enumerates the given supported
203 * media bus codes on the sink pad and reports a source pad format identical to
204 * the sink pad.
205 */
206int vsp1_subdev_enum_mbus_code(struct v4l2_subdev *subdev,
207 struct v4l2_subdev_pad_config *cfg,
208 struct v4l2_subdev_mbus_code_enum *code,
209 const unsigned int *codes, unsigned int ncodes)
210{
211 struct vsp1_entity *entity = to_vsp1_entity(subdev);
212
213 if (code->pad == 0) {
214 if (code->index >= ncodes)
215 return -EINVAL;
216
217 code->code = codes[code->index];
218 } else {
219 struct v4l2_subdev_pad_config *config;
220 struct v4l2_mbus_framefmt *format;
221
Laurent Pinchart9dbed952017-02-26 10:29:50 -0300222 /*
223 * The entity can't perform format conversion, the sink format
Laurent Pinchart6ad9ba92016-02-24 20:25:42 -0300224 * is always identical to the source format.
225 */
226 if (code->index)
227 return -EINVAL;
228
229 config = vsp1_entity_get_pad_config(entity, cfg, code->which);
230 if (!config)
231 return -EINVAL;
232
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300233 mutex_lock(&entity->lock);
Laurent Pinchart6ad9ba92016-02-24 20:25:42 -0300234 format = vsp1_entity_get_pad_format(entity, config, 0);
235 code->code = format->code;
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300236 mutex_unlock(&entity->lock);
Laurent Pinchart6ad9ba92016-02-24 20:25:42 -0300237 }
238
239 return 0;
240}
241
Laurent Pinchart076e8342016-02-24 20:25:42 -0300242/*
243 * vsp1_subdev_enum_frame_size - Subdev pad enum_frame_size handler
244 * @subdev: V4L2 subdevice
245 * @cfg: V4L2 subdev pad configuration
246 * @fse: Frame size enumeration
247 * @min_width: Minimum image width
248 * @min_height: Minimum image height
249 * @max_width: Maximum image width
250 * @max_height: Maximum image height
251 *
252 * This function implements the subdev enum_frame_size pad operation for
253 * entities that do not support scaling or cropping. It reports the given
254 * minimum and maximum frame width and height on the sink pad, and a fixed
255 * source pad size identical to the sink pad.
256 */
257int vsp1_subdev_enum_frame_size(struct v4l2_subdev *subdev,
258 struct v4l2_subdev_pad_config *cfg,
259 struct v4l2_subdev_frame_size_enum *fse,
260 unsigned int min_width, unsigned int min_height,
261 unsigned int max_width, unsigned int max_height)
262{
263 struct vsp1_entity *entity = to_vsp1_entity(subdev);
264 struct v4l2_subdev_pad_config *config;
265 struct v4l2_mbus_framefmt *format;
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300266 int ret = 0;
Laurent Pinchart076e8342016-02-24 20:25:42 -0300267
268 config = vsp1_entity_get_pad_config(entity, cfg, fse->which);
269 if (!config)
270 return -EINVAL;
271
272 format = vsp1_entity_get_pad_format(entity, config, fse->pad);
273
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300274 mutex_lock(&entity->lock);
275
276 if (fse->index || fse->code != format->code) {
277 ret = -EINVAL;
278 goto done;
279 }
Laurent Pinchart076e8342016-02-24 20:25:42 -0300280
281 if (fse->pad == 0) {
282 fse->min_width = min_width;
283 fse->max_width = max_width;
284 fse->min_height = min_height;
285 fse->max_height = max_height;
286 } else {
Laurent Pinchart9dbed952017-02-26 10:29:50 -0300287 /*
288 * The size on the source pad are fixed and always identical to
Laurent Pinchart076e8342016-02-24 20:25:42 -0300289 * the size on the sink pad.
290 */
291 fse->min_width = format->width;
292 fse->max_width = format->width;
293 fse->min_height = format->height;
294 fse->max_height = format->height;
295 }
296
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300297done:
298 mutex_unlock(&entity->lock);
299 return ret;
Laurent Pinchart076e8342016-02-24 20:25:42 -0300300}
301
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300302/* -----------------------------------------------------------------------------
303 * Media Operations
304 */
305
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300306static int vsp1_entity_link_setup_source(const struct media_pad *source_pad,
307 const struct media_pad *sink_pad,
308 u32 flags)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300309{
310 struct vsp1_entity *source;
311
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300312 source = media_entity_to_vsp1_entity(source_pad->entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300313
314 if (!source->route)
315 return 0;
316
317 if (flags & MEDIA_LNK_FL_ENABLED) {
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300318 struct vsp1_entity *sink
319 = media_entity_to_vsp1_entity(sink_pad->entity);
320
321 /*
322 * Fan-out is limited to one for the normal data path plus
323 * optional HGO and HGT. We ignore the HGO and HGT here.
324 */
325 if (sink->type != VSP1_ENTITY_HGO &&
326 sink->type != VSP1_ENTITY_HGT) {
327 if (source->sink)
328 return -EBUSY;
329 source->sink = sink_pad->entity;
330 source->sink_pad = sink_pad->index;
331 }
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300332 } else {
333 source->sink = NULL;
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300334 source->sink_pad = 0;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300335 }
336
337 return 0;
338}
339
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300340static int vsp1_entity_link_setup_sink(const struct media_pad *source_pad,
341 const struct media_pad *sink_pad,
342 u32 flags)
343{
344 struct vsp1_entity *sink;
345
346 sink = media_entity_to_vsp1_entity(sink_pad->entity);
347
348 if (flags & MEDIA_LNK_FL_ENABLED) {
349 /* Fan-in is limited to one. */
350 if (sink->sources[sink_pad->index])
351 return -EBUSY;
352
353 sink->sources[sink_pad->index] = source_pad->entity;
354 } else {
355 sink->sources[sink_pad->index] = NULL;
356 }
357
358 return 0;
359}
360
361int vsp1_entity_link_setup(struct media_entity *entity,
362 const struct media_pad *local,
363 const struct media_pad *remote, u32 flags)
364{
365 if (local->flags & MEDIA_PAD_FL_SOURCE)
366 return vsp1_entity_link_setup_source(local, remote, flags);
367 else
368 return vsp1_entity_link_setup_sink(remote, local, flags);
369}
370
371/**
372 * vsp1_entity_remote_pad - Find the pad at the remote end of a link
373 * @pad: Pad at the local end of the link
374 *
375 * Search for a remote pad connected to the given pad by iterating over all
376 * links originating or terminating at that pad until an enabled link is found.
377 *
378 * Our link setup implementation guarantees that the output fan-out will not be
379 * higher than one for the data pipelines, except for the links to the HGO and
380 * HGT that can be enabled in addition to a regular data link. When traversing
381 * outgoing links this function ignores HGO and HGT entities and should thus be
382 * used in place of the generic media_entity_remote_pad() function to traverse
383 * data pipelines.
384 *
385 * Return a pointer to the pad at the remote end of the first found enabled
386 * link, or NULL if no enabled link has been found.
387 */
388struct media_pad *vsp1_entity_remote_pad(struct media_pad *pad)
389{
390 struct media_link *link;
391
392 list_for_each_entry(link, &pad->entity->links, list) {
393 struct vsp1_entity *entity;
394
395 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
396 continue;
397
398 /* If we're the sink the source will never be an HGO or HGT. */
399 if (link->sink == pad)
400 return link->source;
401
402 if (link->source != pad)
403 continue;
404
405 /* If the sink isn't a subdevice it can't be an HGO or HGT. */
406 if (!is_media_entity_v4l2_subdev(link->sink->entity))
407 return link->sink;
408
409 entity = media_entity_to_vsp1_entity(link->sink->entity);
410 if (entity->type != VSP1_ENTITY_HGO &&
411 entity->type != VSP1_ENTITY_HGT)
412 return link->sink;
413 }
414
415 return NULL;
416
417}
418
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300419/* -----------------------------------------------------------------------------
420 * Initialization
421 */
422
Laurent Pinchart44f46192016-02-24 19:10:04 -0300423#define VSP1_ENTITY_ROUTE(ent) \
424 { VSP1_ENTITY_##ent, 0, VI6_DPR_##ent##_ROUTE, \
425 { VI6_DPR_NODE_##ent }, VI6_DPR_NODE_##ent }
426
427#define VSP1_ENTITY_ROUTE_RPF(idx) \
428 { VSP1_ENTITY_RPF, idx, VI6_DPR_RPF_ROUTE(idx), \
429 { 0, }, VI6_DPR_NODE_RPF(idx) }
430
431#define VSP1_ENTITY_ROUTE_UDS(idx) \
432 { VSP1_ENTITY_UDS, idx, VI6_DPR_UDS_ROUTE(idx), \
433 { VI6_DPR_NODE_UDS(idx) }, VI6_DPR_NODE_UDS(idx) }
434
435#define VSP1_ENTITY_ROUTE_WPF(idx) \
436 { VSP1_ENTITY_WPF, idx, 0, \
437 { VI6_DPR_NODE_WPF(idx) }, VI6_DPR_NODE_WPF(idx) }
438
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300439static const struct vsp1_route vsp1_routes[] = {
Laurent Pinchart629bb6d2013-07-10 18:03:46 -0300440 { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
441 { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
Laurent Pinchart7f2d50f2015-09-07 08:05:39 -0300442 VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3),
Laurent Pinchart44f46192016-02-24 19:10:04 -0300443 VI6_DPR_NODE_BRU_IN(4) }, VI6_DPR_NODE_BRU_OUT },
Laurent Pinchart1fd87bf2015-11-11 23:04:44 -0200444 VSP1_ENTITY_ROUTE(CLU),
Laurent Pinchartf2421522016-02-24 20:40:22 -0300445 { VSP1_ENTITY_HGO, 0, 0, { 0, }, 0 },
Laurent Pinchart44f46192016-02-24 19:10:04 -0300446 VSP1_ENTITY_ROUTE(HSI),
447 VSP1_ENTITY_ROUTE(HST),
448 { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, }, VI6_DPR_NODE_LIF },
449 VSP1_ENTITY_ROUTE(LUT),
450 VSP1_ENTITY_ROUTE_RPF(0),
451 VSP1_ENTITY_ROUTE_RPF(1),
452 VSP1_ENTITY_ROUTE_RPF(2),
453 VSP1_ENTITY_ROUTE_RPF(3),
454 VSP1_ENTITY_ROUTE_RPF(4),
455 VSP1_ENTITY_ROUTE(SRU),
456 VSP1_ENTITY_ROUTE_UDS(0),
457 VSP1_ENTITY_ROUTE_UDS(1),
458 VSP1_ENTITY_ROUTE_UDS(2),
459 VSP1_ENTITY_ROUTE_WPF(0),
460 VSP1_ENTITY_ROUTE_WPF(1),
461 VSP1_ENTITY_ROUTE_WPF(2),
462 VSP1_ENTITY_ROUTE_WPF(3),
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300463};
464
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300465int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
Laurent Pinchart823329d2015-11-15 19:42:01 -0200466 const char *name, unsigned int num_pads,
Laurent Pinchart6a8e07b2016-02-15 22:10:26 -0200467 const struct v4l2_subdev_ops *ops, u32 function)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300468{
Laurent Pinchart823329d2015-11-15 19:42:01 -0200469 struct v4l2_subdev *subdev;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300470 unsigned int i;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200471 int ret;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300472
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300473 for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
474 if (vsp1_routes[i].type == entity->type &&
475 vsp1_routes[i].index == entity->index) {
476 entity->route = &vsp1_routes[i];
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300477 break;
478 }
479 }
480
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300481 if (i == ARRAY_SIZE(vsp1_routes))
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300482 return -EINVAL;
483
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300484 mutex_init(&entity->lock);
485
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300486 entity->vsp1 = vsp1;
487 entity->source_pad = num_pads - 1;
488
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200489 /* Allocate and initialize pads. */
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300490 entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads),
491 GFP_KERNEL);
492 if (entity->pads == NULL)
493 return -ENOMEM;
494
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300495 for (i = 0; i < num_pads - 1; ++i)
496 entity->pads[i].flags = MEDIA_PAD_FL_SINK;
497
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300498 entity->sources = devm_kcalloc(vsp1->dev, max(num_pads - 1, 1U),
499 sizeof(*entity->sources), GFP_KERNEL);
500 if (entity->sources == NULL)
501 return -ENOMEM;
502
503 /* Single-pad entities only have a sink. */
504 entity->pads[num_pads - 1].flags = num_pads > 1 ? MEDIA_PAD_FL_SOURCE
505 : MEDIA_PAD_FL_SINK;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300506
507 /* Initialize the media entity. */
Laurent Pinchart823329d2015-11-15 19:42:01 -0200508 ret = media_entity_pads_init(&entity->subdev.entity, num_pads,
509 entity->pads);
510 if (ret < 0)
511 return ret;
512
513 /* Initialize the V4L2 subdev. */
514 subdev = &entity->subdev;
515 v4l2_subdev_init(subdev, ops);
516
Laurent Pinchart6a8e07b2016-02-15 22:10:26 -0200517 subdev->entity.function = function;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200518 subdev->entity.ops = &vsp1->media_ops;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200519 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
520
521 snprintf(subdev->name, sizeof(subdev->name), "%s %s",
522 dev_name(vsp1->dev), name);
523
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200524 vsp1_entity_init_cfg(subdev, NULL);
Laurent Pinchart823329d2015-11-15 19:42:01 -0200525
Laurent Pinchart9dbed952017-02-26 10:29:50 -0300526 /*
527 * Allocate the pad configuration to store formats and selection
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200528 * rectangles.
529 */
530 entity->config = v4l2_subdev_alloc_pad_config(&entity->subdev);
531 if (entity->config == NULL) {
532 media_entity_cleanup(&entity->subdev.entity);
533 return -ENOMEM;
534 }
535
Laurent Pinchart823329d2015-11-15 19:42:01 -0200536 return 0;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300537}
538
539void vsp1_entity_destroy(struct vsp1_entity *entity)
540{
Laurent Pinchart52434532015-11-17 12:23:23 -0200541 if (entity->ops && entity->ops->destroy)
542 entity->ops->destroy(entity);
Laurent Pincharta626e642013-07-10 12:03:30 -0300543 if (entity->subdev.ctrl_handler)
544 v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200545 v4l2_subdev_free_pad_config(entity->config);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300546 media_entity_cleanup(&entity->subdev.entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300547}