blob: 88a2aae182ba05289392fe3482e7b9233d452da9 [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 Pinchartc8663c82016-09-07 09:09:53 -030040 source = entity;
Laurent Pinchart665b6932015-08-02 18:58:31 -030041 if (source->route->reg == 0)
42 return;
43
Laurent Pinchart7cf0f122016-03-03 20:06:22 -030044 sink = media_entity_to_vsp1_entity(source->sink);
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020045 vsp1_dl_list_write(dl, source->route->reg,
46 sink->route->inputs[source->sink_pad]);
Laurent Pinchart665b6932015-08-02 18:58:31 -030047}
48
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030049/* -----------------------------------------------------------------------------
50 * V4L2 Subdevice Operations
51 */
52
Laurent Pincharte790c3c2015-11-15 19:14:22 -020053/**
54 * vsp1_entity_get_pad_config - Get the pad configuration for an entity
55 * @entity: the entity
56 * @cfg: the TRY pad configuration
57 * @which: configuration selector (ACTIVE or TRY)
58 *
Laurent Pinchart34e77ed2016-06-26 08:09:31 -030059 * When called with which set to V4L2_SUBDEV_FORMAT_ACTIVE the caller must hold
60 * the entity lock to access the returned configuration.
61 *
Laurent Pincharte790c3c2015-11-15 19:14:22 -020062 * Return the pad configuration requested by the which argument. The TRY
63 * configuration is passed explicitly to the function through the cfg argument
64 * and simply returned when requested. The ACTIVE configuration comes from the
65 * entity structure.
66 */
67struct v4l2_subdev_pad_config *
68vsp1_entity_get_pad_config(struct vsp1_entity *entity,
69 struct v4l2_subdev_pad_config *cfg,
70 enum v4l2_subdev_format_whence which)
71{
72 switch (which) {
73 case V4L2_SUBDEV_FORMAT_ACTIVE:
74 return entity->config;
75 case V4L2_SUBDEV_FORMAT_TRY:
76 default:
77 return cfg;
78 }
79}
80
81/**
82 * vsp1_entity_get_pad_format - Get a pad format from storage for an entity
83 * @entity: the entity
84 * @cfg: the configuration storage
85 * @pad: the pad number
86 *
87 * Return the format stored in the given configuration for an entity's pad. The
88 * configuration can be an ACTIVE or TRY configuration.
89 */
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030090struct v4l2_mbus_framefmt *
91vsp1_entity_get_pad_format(struct vsp1_entity *entity,
Hans Verkuilf7234132015-03-04 01:47:54 -080092 struct v4l2_subdev_pad_config *cfg,
Laurent Pincharte790c3c2015-11-15 19:14:22 -020093 unsigned int pad)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030094{
Laurent Pincharte790c3c2015-11-15 19:14:22 -020095 return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030096}
97
Laurent Pinchartccd3d952016-03-03 20:17:49 -030098/**
99 * vsp1_entity_get_pad_selection - Get a pad selection from storage for entity
100 * @entity: the entity
101 * @cfg: the configuration storage
102 * @pad: the pad number
103 * @target: the selection target
104 *
105 * Return the selection rectangle stored in the given configuration for an
106 * entity's pad. The configuration can be an ACTIVE or TRY configuration. The
107 * selection target can be COMPOSE or CROP.
108 */
Laurent Pinchartb7e51072015-11-15 19:14:22 -0200109struct v4l2_rect *
Laurent Pinchartccd3d952016-03-03 20:17:49 -0300110vsp1_entity_get_pad_selection(struct vsp1_entity *entity,
111 struct v4l2_subdev_pad_config *cfg,
112 unsigned int pad, unsigned int target)
Laurent Pinchartb7e51072015-11-15 19:14:22 -0200113{
Laurent Pinchartccd3d952016-03-03 20:17:49 -0300114 switch (target) {
115 case V4L2_SEL_TGT_COMPOSE:
116 return v4l2_subdev_get_try_compose(&entity->subdev, cfg, pad);
117 case V4L2_SEL_TGT_CROP:
118 return v4l2_subdev_get_try_crop(&entity->subdev, cfg, pad);
119 default:
120 return NULL;
121 }
Laurent Pinchartb7e51072015-11-15 19:14:22 -0200122}
123
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300124/*
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200125 * vsp1_entity_init_cfg - Initialize formats on all pads
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300126 * @subdev: V4L2 subdevice
Hans Verkuilf7234132015-03-04 01:47:54 -0800127 * @cfg: V4L2 subdev pad configuration
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300128 *
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200129 * Initialize all pad formats with default values in the given pad config. This
130 * function can be used as a handler for the subdev pad::init_cfg operation.
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300131 */
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200132int vsp1_entity_init_cfg(struct v4l2_subdev *subdev,
133 struct v4l2_subdev_pad_config *cfg)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300134{
135 struct v4l2_subdev_format format;
136 unsigned int pad;
137
138 for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
139 memset(&format, 0, sizeof(format));
140
141 format.pad = pad;
Hans Verkuilf7234132015-03-04 01:47:54 -0800142 format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300143 : V4L2_SUBDEV_FORMAT_ACTIVE;
144
Hans Verkuilf7234132015-03-04 01:47:54 -0800145 v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300146 }
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300147
148 return 0;
149}
150
Laurent Pinchart3f557222016-02-24 21:10:13 -0300151/*
152 * vsp1_subdev_get_pad_format - Subdev pad get_fmt handler
153 * @subdev: V4L2 subdevice
154 * @cfg: V4L2 subdev pad configuration
155 * @fmt: V4L2 subdev format
156 *
157 * This function implements the subdev get_fmt pad operation. It can be used as
158 * a direct drop-in for the operation handler.
159 */
160int vsp1_subdev_get_pad_format(struct v4l2_subdev *subdev,
161 struct v4l2_subdev_pad_config *cfg,
162 struct v4l2_subdev_format *fmt)
163{
164 struct vsp1_entity *entity = to_vsp1_entity(subdev);
165 struct v4l2_subdev_pad_config *config;
166
167 config = vsp1_entity_get_pad_config(entity, cfg, fmt->which);
168 if (!config)
169 return -EINVAL;
170
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300171 mutex_lock(&entity->lock);
Laurent Pinchart3f557222016-02-24 21:10:13 -0300172 fmt->format = *vsp1_entity_get_pad_format(entity, config, fmt->pad);
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300173 mutex_unlock(&entity->lock);
Laurent Pinchart3f557222016-02-24 21:10:13 -0300174
175 return 0;
176}
177
Laurent Pinchart6ad9ba92016-02-24 20:25:42 -0300178/*
179 * vsp1_subdev_enum_mbus_code - Subdev pad enum_mbus_code handler
180 * @subdev: V4L2 subdevice
181 * @cfg: V4L2 subdev pad configuration
182 * @code: Media bus code enumeration
183 * @codes: Array of supported media bus codes
184 * @ncodes: Number of supported media bus codes
185 *
186 * This function implements the subdev enum_mbus_code pad operation for entities
187 * that do not support format conversion. It enumerates the given supported
188 * media bus codes on the sink pad and reports a source pad format identical to
189 * the sink pad.
190 */
191int vsp1_subdev_enum_mbus_code(struct v4l2_subdev *subdev,
192 struct v4l2_subdev_pad_config *cfg,
193 struct v4l2_subdev_mbus_code_enum *code,
194 const unsigned int *codes, unsigned int ncodes)
195{
196 struct vsp1_entity *entity = to_vsp1_entity(subdev);
197
198 if (code->pad == 0) {
199 if (code->index >= ncodes)
200 return -EINVAL;
201
202 code->code = codes[code->index];
203 } else {
204 struct v4l2_subdev_pad_config *config;
205 struct v4l2_mbus_framefmt *format;
206
Laurent Pinchart9dbed952017-02-26 10:29:50 -0300207 /*
208 * The entity can't perform format conversion, the sink format
Laurent Pinchart6ad9ba92016-02-24 20:25:42 -0300209 * is always identical to the source format.
210 */
211 if (code->index)
212 return -EINVAL;
213
214 config = vsp1_entity_get_pad_config(entity, cfg, code->which);
215 if (!config)
216 return -EINVAL;
217
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300218 mutex_lock(&entity->lock);
Laurent Pinchart6ad9ba92016-02-24 20:25:42 -0300219 format = vsp1_entity_get_pad_format(entity, config, 0);
220 code->code = format->code;
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300221 mutex_unlock(&entity->lock);
Laurent Pinchart6ad9ba92016-02-24 20:25:42 -0300222 }
223
224 return 0;
225}
226
Laurent Pinchart076e8342016-02-24 20:25:42 -0300227/*
228 * vsp1_subdev_enum_frame_size - Subdev pad enum_frame_size handler
229 * @subdev: V4L2 subdevice
230 * @cfg: V4L2 subdev pad configuration
231 * @fse: Frame size enumeration
232 * @min_width: Minimum image width
233 * @min_height: Minimum image height
234 * @max_width: Maximum image width
235 * @max_height: Maximum image height
236 *
237 * This function implements the subdev enum_frame_size pad operation for
238 * entities that do not support scaling or cropping. It reports the given
239 * minimum and maximum frame width and height on the sink pad, and a fixed
240 * source pad size identical to the sink pad.
241 */
242int vsp1_subdev_enum_frame_size(struct v4l2_subdev *subdev,
243 struct v4l2_subdev_pad_config *cfg,
244 struct v4l2_subdev_frame_size_enum *fse,
245 unsigned int min_width, unsigned int min_height,
246 unsigned int max_width, unsigned int max_height)
247{
248 struct vsp1_entity *entity = to_vsp1_entity(subdev);
249 struct v4l2_subdev_pad_config *config;
250 struct v4l2_mbus_framefmt *format;
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300251 int ret = 0;
Laurent Pinchart076e8342016-02-24 20:25:42 -0300252
253 config = vsp1_entity_get_pad_config(entity, cfg, fse->which);
254 if (!config)
255 return -EINVAL;
256
257 format = vsp1_entity_get_pad_format(entity, config, fse->pad);
258
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300259 mutex_lock(&entity->lock);
260
261 if (fse->index || fse->code != format->code) {
262 ret = -EINVAL;
263 goto done;
264 }
Laurent Pinchart076e8342016-02-24 20:25:42 -0300265
266 if (fse->pad == 0) {
267 fse->min_width = min_width;
268 fse->max_width = max_width;
269 fse->min_height = min_height;
270 fse->max_height = max_height;
271 } else {
Laurent Pinchart9dbed952017-02-26 10:29:50 -0300272 /*
273 * The size on the source pad are fixed and always identical to
Laurent Pinchart076e8342016-02-24 20:25:42 -0300274 * the size on the sink pad.
275 */
276 fse->min_width = format->width;
277 fse->max_width = format->width;
278 fse->min_height = format->height;
279 fse->max_height = format->height;
280 }
281
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300282done:
283 mutex_unlock(&entity->lock);
284 return ret;
Laurent Pinchart076e8342016-02-24 20:25:42 -0300285}
286
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300287/* -----------------------------------------------------------------------------
288 * Media Operations
289 */
290
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300291static int vsp1_entity_link_setup_source(const struct media_pad *source_pad,
292 const struct media_pad *sink_pad,
293 u32 flags)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300294{
295 struct vsp1_entity *source;
296
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300297 source = media_entity_to_vsp1_entity(source_pad->entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300298
299 if (!source->route)
300 return 0;
301
302 if (flags & MEDIA_LNK_FL_ENABLED) {
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300303 struct vsp1_entity *sink
304 = media_entity_to_vsp1_entity(sink_pad->entity);
305
306 /*
307 * Fan-out is limited to one for the normal data path plus
308 * optional HGO and HGT. We ignore the HGO and HGT here.
309 */
310 if (sink->type != VSP1_ENTITY_HGO &&
311 sink->type != VSP1_ENTITY_HGT) {
312 if (source->sink)
313 return -EBUSY;
314 source->sink = sink_pad->entity;
315 source->sink_pad = sink_pad->index;
316 }
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300317 } else {
318 source->sink = NULL;
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300319 source->sink_pad = 0;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300320 }
321
322 return 0;
323}
324
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300325static int vsp1_entity_link_setup_sink(const struct media_pad *source_pad,
326 const struct media_pad *sink_pad,
327 u32 flags)
328{
329 struct vsp1_entity *sink;
330
331 sink = media_entity_to_vsp1_entity(sink_pad->entity);
332
333 if (flags & MEDIA_LNK_FL_ENABLED) {
334 /* Fan-in is limited to one. */
335 if (sink->sources[sink_pad->index])
336 return -EBUSY;
337
338 sink->sources[sink_pad->index] = source_pad->entity;
339 } else {
340 sink->sources[sink_pad->index] = NULL;
341 }
342
343 return 0;
344}
345
346int vsp1_entity_link_setup(struct media_entity *entity,
347 const struct media_pad *local,
348 const struct media_pad *remote, u32 flags)
349{
350 if (local->flags & MEDIA_PAD_FL_SOURCE)
351 return vsp1_entity_link_setup_source(local, remote, flags);
352 else
353 return vsp1_entity_link_setup_sink(remote, local, flags);
354}
355
356/**
357 * vsp1_entity_remote_pad - Find the pad at the remote end of a link
358 * @pad: Pad at the local end of the link
359 *
360 * Search for a remote pad connected to the given pad by iterating over all
361 * links originating or terminating at that pad until an enabled link is found.
362 *
363 * Our link setup implementation guarantees that the output fan-out will not be
364 * higher than one for the data pipelines, except for the links to the HGO and
365 * HGT that can be enabled in addition to a regular data link. When traversing
366 * outgoing links this function ignores HGO and HGT entities and should thus be
367 * used in place of the generic media_entity_remote_pad() function to traverse
368 * data pipelines.
369 *
370 * Return a pointer to the pad at the remote end of the first found enabled
371 * link, or NULL if no enabled link has been found.
372 */
373struct media_pad *vsp1_entity_remote_pad(struct media_pad *pad)
374{
375 struct media_link *link;
376
377 list_for_each_entry(link, &pad->entity->links, list) {
378 struct vsp1_entity *entity;
379
380 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
381 continue;
382
383 /* If we're the sink the source will never be an HGO or HGT. */
384 if (link->sink == pad)
385 return link->source;
386
387 if (link->source != pad)
388 continue;
389
390 /* If the sink isn't a subdevice it can't be an HGO or HGT. */
391 if (!is_media_entity_v4l2_subdev(link->sink->entity))
392 return link->sink;
393
394 entity = media_entity_to_vsp1_entity(link->sink->entity);
395 if (entity->type != VSP1_ENTITY_HGO &&
396 entity->type != VSP1_ENTITY_HGT)
397 return link->sink;
398 }
399
400 return NULL;
401
402}
403
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300404/* -----------------------------------------------------------------------------
405 * Initialization
406 */
407
Laurent Pinchart44f46192016-02-24 19:10:04 -0300408#define VSP1_ENTITY_ROUTE(ent) \
409 { VSP1_ENTITY_##ent, 0, VI6_DPR_##ent##_ROUTE, \
410 { VI6_DPR_NODE_##ent }, VI6_DPR_NODE_##ent }
411
412#define VSP1_ENTITY_ROUTE_RPF(idx) \
413 { VSP1_ENTITY_RPF, idx, VI6_DPR_RPF_ROUTE(idx), \
414 { 0, }, VI6_DPR_NODE_RPF(idx) }
415
416#define VSP1_ENTITY_ROUTE_UDS(idx) \
417 { VSP1_ENTITY_UDS, idx, VI6_DPR_UDS_ROUTE(idx), \
418 { VI6_DPR_NODE_UDS(idx) }, VI6_DPR_NODE_UDS(idx) }
419
420#define VSP1_ENTITY_ROUTE_WPF(idx) \
421 { VSP1_ENTITY_WPF, idx, 0, \
422 { VI6_DPR_NODE_WPF(idx) }, VI6_DPR_NODE_WPF(idx) }
423
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300424static const struct vsp1_route vsp1_routes[] = {
Laurent Pinchart629bb6d2013-07-10 18:03:46 -0300425 { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
426 { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
Laurent Pinchart7f2d50f2015-09-07 08:05:39 -0300427 VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3),
Laurent Pinchart44f46192016-02-24 19:10:04 -0300428 VI6_DPR_NODE_BRU_IN(4) }, VI6_DPR_NODE_BRU_OUT },
Laurent Pinchart1fd87bf2015-11-11 23:04:44 -0200429 VSP1_ENTITY_ROUTE(CLU),
Laurent Pinchart44f46192016-02-24 19:10:04 -0300430 VSP1_ENTITY_ROUTE(HSI),
431 VSP1_ENTITY_ROUTE(HST),
432 { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, }, VI6_DPR_NODE_LIF },
433 VSP1_ENTITY_ROUTE(LUT),
434 VSP1_ENTITY_ROUTE_RPF(0),
435 VSP1_ENTITY_ROUTE_RPF(1),
436 VSP1_ENTITY_ROUTE_RPF(2),
437 VSP1_ENTITY_ROUTE_RPF(3),
438 VSP1_ENTITY_ROUTE_RPF(4),
439 VSP1_ENTITY_ROUTE(SRU),
440 VSP1_ENTITY_ROUTE_UDS(0),
441 VSP1_ENTITY_ROUTE_UDS(1),
442 VSP1_ENTITY_ROUTE_UDS(2),
443 VSP1_ENTITY_ROUTE_WPF(0),
444 VSP1_ENTITY_ROUTE_WPF(1),
445 VSP1_ENTITY_ROUTE_WPF(2),
446 VSP1_ENTITY_ROUTE_WPF(3),
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300447};
448
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300449int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
Laurent Pinchart823329d2015-11-15 19:42:01 -0200450 const char *name, unsigned int num_pads,
Laurent Pinchart6a8e07b2016-02-15 22:10:26 -0200451 const struct v4l2_subdev_ops *ops, u32 function)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300452{
Laurent Pinchart823329d2015-11-15 19:42:01 -0200453 struct v4l2_subdev *subdev;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300454 unsigned int i;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200455 int ret;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300456
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300457 for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
458 if (vsp1_routes[i].type == entity->type &&
459 vsp1_routes[i].index == entity->index) {
460 entity->route = &vsp1_routes[i];
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300461 break;
462 }
463 }
464
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300465 if (i == ARRAY_SIZE(vsp1_routes))
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300466 return -EINVAL;
467
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300468 mutex_init(&entity->lock);
469
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300470 entity->vsp1 = vsp1;
471 entity->source_pad = num_pads - 1;
472
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200473 /* Allocate and initialize pads. */
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300474 entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads),
475 GFP_KERNEL);
476 if (entity->pads == NULL)
477 return -ENOMEM;
478
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300479 for (i = 0; i < num_pads - 1; ++i)
480 entity->pads[i].flags = MEDIA_PAD_FL_SINK;
481
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300482 entity->sources = devm_kcalloc(vsp1->dev, max(num_pads - 1, 1U),
483 sizeof(*entity->sources), GFP_KERNEL);
484 if (entity->sources == NULL)
485 return -ENOMEM;
486
487 /* Single-pad entities only have a sink. */
488 entity->pads[num_pads - 1].flags = num_pads > 1 ? MEDIA_PAD_FL_SOURCE
489 : MEDIA_PAD_FL_SINK;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300490
491 /* Initialize the media entity. */
Laurent Pinchart823329d2015-11-15 19:42:01 -0200492 ret = media_entity_pads_init(&entity->subdev.entity, num_pads,
493 entity->pads);
494 if (ret < 0)
495 return ret;
496
497 /* Initialize the V4L2 subdev. */
498 subdev = &entity->subdev;
499 v4l2_subdev_init(subdev, ops);
500
Laurent Pinchart6a8e07b2016-02-15 22:10:26 -0200501 subdev->entity.function = function;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200502 subdev->entity.ops = &vsp1->media_ops;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200503 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
504
505 snprintf(subdev->name, sizeof(subdev->name), "%s %s",
506 dev_name(vsp1->dev), name);
507
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200508 vsp1_entity_init_cfg(subdev, NULL);
Laurent Pinchart823329d2015-11-15 19:42:01 -0200509
Laurent Pinchart9dbed952017-02-26 10:29:50 -0300510 /*
511 * Allocate the pad configuration to store formats and selection
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200512 * rectangles.
513 */
514 entity->config = v4l2_subdev_alloc_pad_config(&entity->subdev);
515 if (entity->config == NULL) {
516 media_entity_cleanup(&entity->subdev.entity);
517 return -ENOMEM;
518 }
519
Laurent Pinchart823329d2015-11-15 19:42:01 -0200520 return 0;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300521}
522
523void vsp1_entity_destroy(struct vsp1_entity *entity)
524{
Laurent Pinchart52434532015-11-17 12:23:23 -0200525 if (entity->ops && entity->ops->destroy)
526 entity->ops->destroy(entity);
Laurent Pincharta626e642013-07-10 12:03:30 -0300527 if (entity->subdev.ctrl_handler)
528 v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200529 v4l2_subdev_free_pad_config(entity->config);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300530 media_entity_cleanup(&entity->subdev.entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300531}