blob: c06f7db093db1c2cb99ea3380ef8c8e57e3a9e6e [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 Pinchartc8663c82016-09-07 09:09:53 -030027void vsp1_entity_route_setup(struct vsp1_entity *entity,
28 struct vsp1_pipeline *pipe,
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020029 struct vsp1_dl_list *dl)
Laurent Pinchart665b6932015-08-02 18:58:31 -030030{
Laurent Pinchartc8663c82016-09-07 09:09:53 -030031 struct vsp1_entity *source;
Laurent Pinchart61341482017-05-25 00:16:57 +030032 u32 route;
Laurent Pinchart665b6932015-08-02 18:58:31 -030033
Laurent Pinchartf2421522016-02-24 20:40:22 -030034 if (entity->type == VSP1_ENTITY_HGO) {
35 u32 smppt;
36
37 /*
38 * The HGO is a special case, its routing is configured on the
39 * sink pad.
40 */
Laurent Pinchart2275e8d2017-06-25 20:15:22 +030041 source = entity->sources[0];
Laurent Pinchartf2421522016-02-24 20:40:22 -030042 smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT)
43 | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT);
44
45 vsp1_dl_list_write(dl, VI6_DPR_HGO_SMPPT, smppt);
46 return;
Niklas Söderlund0ac702d2016-09-06 11:38:56 -030047 } else if (entity->type == VSP1_ENTITY_HGT) {
48 u32 smppt;
49
50 /*
51 * The HGT is a special case, its routing is configured on the
52 * sink pad.
53 */
Laurent Pinchart2275e8d2017-06-25 20:15:22 +030054 source = entity->sources[0];
Niklas Söderlund0ac702d2016-09-06 11:38:56 -030055 smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT)
56 | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT);
57
58 vsp1_dl_list_write(dl, VI6_DPR_HGT_SMPPT, smppt);
59 return;
Laurent Pinchartf2421522016-02-24 20:40:22 -030060 }
61
Laurent Pinchartc8663c82016-09-07 09:09:53 -030062 source = entity;
Laurent Pinchart665b6932015-08-02 18:58:31 -030063 if (source->route->reg == 0)
64 return;
65
Laurent Pinchart61341482017-05-25 00:16:57 +030066 route = source->sink->route->inputs[source->sink_pad];
67 /*
68 * The ILV and BRS share the same data path route. The extra BRSSEL bit
69 * selects between the ILV and BRS.
70 */
71 if (source->type == VSP1_ENTITY_BRS)
72 route |= VI6_DPR_ROUTE_BRSSEL;
73 vsp1_dl_list_write(dl, source->route->reg, route);
Laurent Pinchart665b6932015-08-02 18:58:31 -030074}
75
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030076/* -----------------------------------------------------------------------------
77 * V4L2 Subdevice Operations
78 */
79
Laurent Pincharte790c3c2015-11-15 19:14:22 -020080/**
81 * vsp1_entity_get_pad_config - Get the pad configuration for an entity
82 * @entity: the entity
83 * @cfg: the TRY pad configuration
84 * @which: configuration selector (ACTIVE or TRY)
85 *
Laurent Pinchart34e77ed2016-06-26 08:09:31 -030086 * When called with which set to V4L2_SUBDEV_FORMAT_ACTIVE the caller must hold
87 * the entity lock to access the returned configuration.
88 *
Laurent Pincharte790c3c2015-11-15 19:14:22 -020089 * Return the pad configuration requested by the which argument. The TRY
90 * configuration is passed explicitly to the function through the cfg argument
91 * and simply returned when requested. The ACTIVE configuration comes from the
92 * entity structure.
93 */
94struct v4l2_subdev_pad_config *
95vsp1_entity_get_pad_config(struct vsp1_entity *entity,
96 struct v4l2_subdev_pad_config *cfg,
97 enum v4l2_subdev_format_whence which)
98{
99 switch (which) {
100 case V4L2_SUBDEV_FORMAT_ACTIVE:
101 return entity->config;
102 case V4L2_SUBDEV_FORMAT_TRY:
103 default:
104 return cfg;
105 }
106}
107
108/**
109 * vsp1_entity_get_pad_format - Get a pad format from storage for an entity
110 * @entity: the entity
111 * @cfg: the configuration storage
112 * @pad: the pad number
113 *
114 * Return the format stored in the given configuration for an entity's pad. The
115 * configuration can be an ACTIVE or TRY configuration.
116 */
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300117struct v4l2_mbus_framefmt *
118vsp1_entity_get_pad_format(struct vsp1_entity *entity,
Hans Verkuilf7234132015-03-04 01:47:54 -0800119 struct v4l2_subdev_pad_config *cfg,
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200120 unsigned int pad)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300121{
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200122 return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300123}
124
Laurent Pinchartccd3d952016-03-03 20:17:49 -0300125/**
126 * vsp1_entity_get_pad_selection - Get a pad selection from storage for entity
127 * @entity: the entity
128 * @cfg: the configuration storage
129 * @pad: the pad number
130 * @target: the selection target
131 *
132 * Return the selection rectangle stored in the given configuration for an
133 * entity's pad. The configuration can be an ACTIVE or TRY configuration. The
134 * selection target can be COMPOSE or CROP.
135 */
Laurent Pinchartb7e51072015-11-15 19:14:22 -0200136struct v4l2_rect *
Laurent Pinchartccd3d952016-03-03 20:17:49 -0300137vsp1_entity_get_pad_selection(struct vsp1_entity *entity,
138 struct v4l2_subdev_pad_config *cfg,
139 unsigned int pad, unsigned int target)
Laurent Pinchartb7e51072015-11-15 19:14:22 -0200140{
Laurent Pinchartccd3d952016-03-03 20:17:49 -0300141 switch (target) {
142 case V4L2_SEL_TGT_COMPOSE:
143 return v4l2_subdev_get_try_compose(&entity->subdev, cfg, pad);
144 case V4L2_SEL_TGT_CROP:
145 return v4l2_subdev_get_try_crop(&entity->subdev, cfg, pad);
146 default:
147 return NULL;
148 }
Laurent Pinchartb7e51072015-11-15 19:14:22 -0200149}
150
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300151/*
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200152 * vsp1_entity_init_cfg - Initialize formats on all pads
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300153 * @subdev: V4L2 subdevice
Hans Verkuilf7234132015-03-04 01:47:54 -0800154 * @cfg: V4L2 subdev pad configuration
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300155 *
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200156 * Initialize all pad formats with default values in the given pad config. This
157 * function can be used as a handler for the subdev pad::init_cfg operation.
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300158 */
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200159int vsp1_entity_init_cfg(struct v4l2_subdev *subdev,
160 struct v4l2_subdev_pad_config *cfg)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300161{
162 struct v4l2_subdev_format format;
163 unsigned int pad;
164
165 for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
166 memset(&format, 0, sizeof(format));
167
168 format.pad = pad;
Hans Verkuilf7234132015-03-04 01:47:54 -0800169 format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300170 : V4L2_SUBDEV_FORMAT_ACTIVE;
171
Hans Verkuilf7234132015-03-04 01:47:54 -0800172 v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300173 }
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300174
175 return 0;
176}
177
Laurent Pinchart3f557222016-02-24 21:10:13 -0300178/*
179 * vsp1_subdev_get_pad_format - Subdev pad get_fmt handler
180 * @subdev: V4L2 subdevice
181 * @cfg: V4L2 subdev pad configuration
182 * @fmt: V4L2 subdev format
183 *
184 * This function implements the subdev get_fmt pad operation. It can be used as
185 * a direct drop-in for the operation handler.
186 */
187int vsp1_subdev_get_pad_format(struct v4l2_subdev *subdev,
188 struct v4l2_subdev_pad_config *cfg,
189 struct v4l2_subdev_format *fmt)
190{
191 struct vsp1_entity *entity = to_vsp1_entity(subdev);
192 struct v4l2_subdev_pad_config *config;
193
194 config = vsp1_entity_get_pad_config(entity, cfg, fmt->which);
195 if (!config)
196 return -EINVAL;
197
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300198 mutex_lock(&entity->lock);
Laurent Pinchart3f557222016-02-24 21:10:13 -0300199 fmt->format = *vsp1_entity_get_pad_format(entity, config, fmt->pad);
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300200 mutex_unlock(&entity->lock);
Laurent Pinchart3f557222016-02-24 21:10:13 -0300201
202 return 0;
203}
204
Laurent Pinchart6ad9ba92016-02-24 20:25:42 -0300205/*
206 * vsp1_subdev_enum_mbus_code - Subdev pad enum_mbus_code handler
207 * @subdev: V4L2 subdevice
208 * @cfg: V4L2 subdev pad configuration
209 * @code: Media bus code enumeration
210 * @codes: Array of supported media bus codes
211 * @ncodes: Number of supported media bus codes
212 *
213 * This function implements the subdev enum_mbus_code pad operation for entities
214 * that do not support format conversion. It enumerates the given supported
215 * media bus codes on the sink pad and reports a source pad format identical to
216 * the sink pad.
217 */
218int vsp1_subdev_enum_mbus_code(struct v4l2_subdev *subdev,
219 struct v4l2_subdev_pad_config *cfg,
220 struct v4l2_subdev_mbus_code_enum *code,
221 const unsigned int *codes, unsigned int ncodes)
222{
223 struct vsp1_entity *entity = to_vsp1_entity(subdev);
224
225 if (code->pad == 0) {
226 if (code->index >= ncodes)
227 return -EINVAL;
228
229 code->code = codes[code->index];
230 } else {
231 struct v4l2_subdev_pad_config *config;
232 struct v4l2_mbus_framefmt *format;
233
Laurent Pinchart9dbed952017-02-26 10:29:50 -0300234 /*
235 * The entity can't perform format conversion, the sink format
Laurent Pinchart6ad9ba92016-02-24 20:25:42 -0300236 * is always identical to the source format.
237 */
238 if (code->index)
239 return -EINVAL;
240
241 config = vsp1_entity_get_pad_config(entity, cfg, code->which);
242 if (!config)
243 return -EINVAL;
244
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300245 mutex_lock(&entity->lock);
Laurent Pinchart6ad9ba92016-02-24 20:25:42 -0300246 format = vsp1_entity_get_pad_format(entity, config, 0);
247 code->code = format->code;
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300248 mutex_unlock(&entity->lock);
Laurent Pinchart6ad9ba92016-02-24 20:25:42 -0300249 }
250
251 return 0;
252}
253
Laurent Pinchart076e8342016-02-24 20:25:42 -0300254/*
255 * vsp1_subdev_enum_frame_size - Subdev pad enum_frame_size handler
256 * @subdev: V4L2 subdevice
257 * @cfg: V4L2 subdev pad configuration
258 * @fse: Frame size enumeration
259 * @min_width: Minimum image width
260 * @min_height: Minimum image height
261 * @max_width: Maximum image width
262 * @max_height: Maximum image height
263 *
264 * This function implements the subdev enum_frame_size pad operation for
265 * entities that do not support scaling or cropping. It reports the given
266 * minimum and maximum frame width and height on the sink pad, and a fixed
267 * source pad size identical to the sink pad.
268 */
269int vsp1_subdev_enum_frame_size(struct v4l2_subdev *subdev,
270 struct v4l2_subdev_pad_config *cfg,
271 struct v4l2_subdev_frame_size_enum *fse,
272 unsigned int min_width, unsigned int min_height,
273 unsigned int max_width, unsigned int max_height)
274{
275 struct vsp1_entity *entity = to_vsp1_entity(subdev);
276 struct v4l2_subdev_pad_config *config;
277 struct v4l2_mbus_framefmt *format;
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300278 int ret = 0;
Laurent Pinchart076e8342016-02-24 20:25:42 -0300279
280 config = vsp1_entity_get_pad_config(entity, cfg, fse->which);
281 if (!config)
282 return -EINVAL;
283
284 format = vsp1_entity_get_pad_format(entity, config, fse->pad);
285
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300286 mutex_lock(&entity->lock);
287
288 if (fse->index || fse->code != format->code) {
289 ret = -EINVAL;
290 goto done;
291 }
Laurent Pinchart076e8342016-02-24 20:25:42 -0300292
293 if (fse->pad == 0) {
294 fse->min_width = min_width;
295 fse->max_width = max_width;
296 fse->min_height = min_height;
297 fse->max_height = max_height;
298 } else {
Laurent Pinchart9dbed952017-02-26 10:29:50 -0300299 /*
300 * The size on the source pad are fixed and always identical to
Laurent Pinchart076e8342016-02-24 20:25:42 -0300301 * the size on the sink pad.
302 */
303 fse->min_width = format->width;
304 fse->max_width = format->width;
305 fse->min_height = format->height;
306 fse->max_height = format->height;
307 }
308
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300309done:
310 mutex_unlock(&entity->lock);
311 return ret;
Laurent Pinchart076e8342016-02-24 20:25:42 -0300312}
313
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300314/* -----------------------------------------------------------------------------
315 * Media Operations
316 */
317
Laurent Pinchart2275e8d2017-06-25 20:15:22 +0300318static inline struct vsp1_entity *
319media_entity_to_vsp1_entity(struct media_entity *entity)
320{
321 return container_of(entity, struct vsp1_entity, subdev.entity);
322}
323
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300324static int vsp1_entity_link_setup_source(const struct media_pad *source_pad,
325 const struct media_pad *sink_pad,
326 u32 flags)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300327{
328 struct vsp1_entity *source;
329
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300330 source = media_entity_to_vsp1_entity(source_pad->entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300331
332 if (!source->route)
333 return 0;
334
335 if (flags & MEDIA_LNK_FL_ENABLED) {
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300336 struct vsp1_entity *sink
337 = media_entity_to_vsp1_entity(sink_pad->entity);
338
339 /*
340 * Fan-out is limited to one for the normal data path plus
341 * optional HGO and HGT. We ignore the HGO and HGT here.
342 */
343 if (sink->type != VSP1_ENTITY_HGO &&
344 sink->type != VSP1_ENTITY_HGT) {
345 if (source->sink)
346 return -EBUSY;
Laurent Pinchart2275e8d2017-06-25 20:15:22 +0300347 source->sink = sink;
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300348 source->sink_pad = sink_pad->index;
349 }
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300350 } else {
351 source->sink = NULL;
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300352 source->sink_pad = 0;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300353 }
354
355 return 0;
356}
357
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300358static int vsp1_entity_link_setup_sink(const struct media_pad *source_pad,
359 const struct media_pad *sink_pad,
360 u32 flags)
361{
362 struct vsp1_entity *sink;
Laurent Pinchart2275e8d2017-06-25 20:15:22 +0300363 struct vsp1_entity *source;
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300364
365 sink = media_entity_to_vsp1_entity(sink_pad->entity);
Laurent Pinchart2275e8d2017-06-25 20:15:22 +0300366 source = media_entity_to_vsp1_entity(source_pad->entity);
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300367
368 if (flags & MEDIA_LNK_FL_ENABLED) {
369 /* Fan-in is limited to one. */
370 if (sink->sources[sink_pad->index])
371 return -EBUSY;
372
Laurent Pinchart2275e8d2017-06-25 20:15:22 +0300373 sink->sources[sink_pad->index] = source;
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300374 } else {
375 sink->sources[sink_pad->index] = NULL;
376 }
377
378 return 0;
379}
380
381int vsp1_entity_link_setup(struct media_entity *entity,
382 const struct media_pad *local,
383 const struct media_pad *remote, u32 flags)
384{
385 if (local->flags & MEDIA_PAD_FL_SOURCE)
386 return vsp1_entity_link_setup_source(local, remote, flags);
387 else
388 return vsp1_entity_link_setup_sink(remote, local, flags);
389}
390
391/**
392 * vsp1_entity_remote_pad - Find the pad at the remote end of a link
393 * @pad: Pad at the local end of the link
394 *
395 * Search for a remote pad connected to the given pad by iterating over all
396 * links originating or terminating at that pad until an enabled link is found.
397 *
398 * Our link setup implementation guarantees that the output fan-out will not be
399 * higher than one for the data pipelines, except for the links to the HGO and
400 * HGT that can be enabled in addition to a regular data link. When traversing
401 * outgoing links this function ignores HGO and HGT entities and should thus be
402 * used in place of the generic media_entity_remote_pad() function to traverse
403 * data pipelines.
404 *
405 * Return a pointer to the pad at the remote end of the first found enabled
406 * link, or NULL if no enabled link has been found.
407 */
408struct media_pad *vsp1_entity_remote_pad(struct media_pad *pad)
409{
410 struct media_link *link;
411
412 list_for_each_entry(link, &pad->entity->links, list) {
413 struct vsp1_entity *entity;
414
415 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
416 continue;
417
418 /* If we're the sink the source will never be an HGO or HGT. */
419 if (link->sink == pad)
420 return link->source;
421
422 if (link->source != pad)
423 continue;
424
425 /* If the sink isn't a subdevice it can't be an HGO or HGT. */
426 if (!is_media_entity_v4l2_subdev(link->sink->entity))
427 return link->sink;
428
429 entity = media_entity_to_vsp1_entity(link->sink->entity);
430 if (entity->type != VSP1_ENTITY_HGO &&
431 entity->type != VSP1_ENTITY_HGT)
432 return link->sink;
433 }
434
435 return NULL;
436
437}
438
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300439/* -----------------------------------------------------------------------------
440 * Initialization
441 */
442
Laurent Pinchart44f46192016-02-24 19:10:04 -0300443#define VSP1_ENTITY_ROUTE(ent) \
444 { VSP1_ENTITY_##ent, 0, VI6_DPR_##ent##_ROUTE, \
445 { VI6_DPR_NODE_##ent }, VI6_DPR_NODE_##ent }
446
447#define VSP1_ENTITY_ROUTE_RPF(idx) \
448 { VSP1_ENTITY_RPF, idx, VI6_DPR_RPF_ROUTE(idx), \
449 { 0, }, VI6_DPR_NODE_RPF(idx) }
450
451#define VSP1_ENTITY_ROUTE_UDS(idx) \
452 { VSP1_ENTITY_UDS, idx, VI6_DPR_UDS_ROUTE(idx), \
453 { VI6_DPR_NODE_UDS(idx) }, VI6_DPR_NODE_UDS(idx) }
454
455#define VSP1_ENTITY_ROUTE_WPF(idx) \
456 { VSP1_ENTITY_WPF, idx, 0, \
457 { VI6_DPR_NODE_WPF(idx) }, VI6_DPR_NODE_WPF(idx) }
458
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300459static const struct vsp1_route vsp1_routes[] = {
Laurent Pinchart61341482017-05-25 00:16:57 +0300460 { VSP1_ENTITY_BRS, 0, VI6_DPR_ILV_BRS_ROUTE,
461 { VI6_DPR_NODE_BRS_IN(0), VI6_DPR_NODE_BRS_IN(1) }, 0 },
Laurent Pinchart629bb6d2013-07-10 18:03:46 -0300462 { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
463 { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
Laurent Pinchart7f2d50f2015-09-07 08:05:39 -0300464 VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3),
Laurent Pinchart44f46192016-02-24 19:10:04 -0300465 VI6_DPR_NODE_BRU_IN(4) }, VI6_DPR_NODE_BRU_OUT },
Laurent Pinchart1fd87bf2015-11-11 23:04:44 -0200466 VSP1_ENTITY_ROUTE(CLU),
Laurent Pinchartf2421522016-02-24 20:40:22 -0300467 { VSP1_ENTITY_HGO, 0, 0, { 0, }, 0 },
Niklas Söderlund0ac702d2016-09-06 11:38:56 -0300468 { VSP1_ENTITY_HGT, 0, 0, { 0, }, 0 },
Laurent Pinchart44f46192016-02-24 19:10:04 -0300469 VSP1_ENTITY_ROUTE(HSI),
470 VSP1_ENTITY_ROUTE(HST),
471 { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, }, VI6_DPR_NODE_LIF },
472 VSP1_ENTITY_ROUTE(LUT),
473 VSP1_ENTITY_ROUTE_RPF(0),
474 VSP1_ENTITY_ROUTE_RPF(1),
475 VSP1_ENTITY_ROUTE_RPF(2),
476 VSP1_ENTITY_ROUTE_RPF(3),
477 VSP1_ENTITY_ROUTE_RPF(4),
478 VSP1_ENTITY_ROUTE(SRU),
479 VSP1_ENTITY_ROUTE_UDS(0),
480 VSP1_ENTITY_ROUTE_UDS(1),
481 VSP1_ENTITY_ROUTE_UDS(2),
482 VSP1_ENTITY_ROUTE_WPF(0),
483 VSP1_ENTITY_ROUTE_WPF(1),
484 VSP1_ENTITY_ROUTE_WPF(2),
485 VSP1_ENTITY_ROUTE_WPF(3),
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300486};
487
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300488int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
Laurent Pinchart823329d2015-11-15 19:42:01 -0200489 const char *name, unsigned int num_pads,
Laurent Pinchart6a8e07b2016-02-15 22:10:26 -0200490 const struct v4l2_subdev_ops *ops, u32 function)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300491{
Laurent Pinchart823329d2015-11-15 19:42:01 -0200492 struct v4l2_subdev *subdev;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300493 unsigned int i;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200494 int ret;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300495
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300496 for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
497 if (vsp1_routes[i].type == entity->type &&
498 vsp1_routes[i].index == entity->index) {
499 entity->route = &vsp1_routes[i];
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300500 break;
501 }
502 }
503
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300504 if (i == ARRAY_SIZE(vsp1_routes))
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300505 return -EINVAL;
506
Laurent Pinchart34e77ed2016-06-26 08:09:31 -0300507 mutex_init(&entity->lock);
508
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300509 entity->vsp1 = vsp1;
510 entity->source_pad = num_pads - 1;
511
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200512 /* Allocate and initialize pads. */
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300513 entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads),
514 GFP_KERNEL);
515 if (entity->pads == NULL)
516 return -ENOMEM;
517
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300518 for (i = 0; i < num_pads - 1; ++i)
519 entity->pads[i].flags = MEDIA_PAD_FL_SINK;
520
Laurent Pinchartc8663c82016-09-07 09:09:53 -0300521 entity->sources = devm_kcalloc(vsp1->dev, max(num_pads - 1, 1U),
522 sizeof(*entity->sources), GFP_KERNEL);
523 if (entity->sources == NULL)
524 return -ENOMEM;
525
526 /* Single-pad entities only have a sink. */
527 entity->pads[num_pads - 1].flags = num_pads > 1 ? MEDIA_PAD_FL_SOURCE
528 : MEDIA_PAD_FL_SINK;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300529
530 /* Initialize the media entity. */
Laurent Pinchart823329d2015-11-15 19:42:01 -0200531 ret = media_entity_pads_init(&entity->subdev.entity, num_pads,
532 entity->pads);
533 if (ret < 0)
534 return ret;
535
536 /* Initialize the V4L2 subdev. */
537 subdev = &entity->subdev;
538 v4l2_subdev_init(subdev, ops);
539
Laurent Pinchart6a8e07b2016-02-15 22:10:26 -0200540 subdev->entity.function = function;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200541 subdev->entity.ops = &vsp1->media_ops;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200542 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
543
544 snprintf(subdev->name, sizeof(subdev->name), "%s %s",
545 dev_name(vsp1->dev), name);
546
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200547 vsp1_entity_init_cfg(subdev, NULL);
Laurent Pinchart823329d2015-11-15 19:42:01 -0200548
Laurent Pinchart9dbed952017-02-26 10:29:50 -0300549 /*
550 * Allocate the pad configuration to store formats and selection
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200551 * rectangles.
552 */
553 entity->config = v4l2_subdev_alloc_pad_config(&entity->subdev);
554 if (entity->config == NULL) {
555 media_entity_cleanup(&entity->subdev.entity);
556 return -ENOMEM;
557 }
558
Laurent Pinchart823329d2015-11-15 19:42:01 -0200559 return 0;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300560}
561
562void vsp1_entity_destroy(struct vsp1_entity *entity)
563{
Laurent Pinchart52434532015-11-17 12:23:23 -0200564 if (entity->ops && entity->ops->destroy)
565 entity->ops->destroy(entity);
Laurent Pincharta626e642013-07-10 12:03:30 -0300566 if (entity->subdev.ctrl_handler)
567 v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200568 v4l2_subdev_free_pad_config(entity->config);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300569 media_entity_cleanup(&entity->subdev.entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300570}