blob: e4d6c7a1ed77ada3e5a82635610141779a0be5b3 [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 Pinchartaa380ea2015-11-01 10:46:25 -020024
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020025void vsp1_entity_route_setup(struct vsp1_entity *source,
26 struct vsp1_dl_list *dl)
Laurent Pinchart665b6932015-08-02 18:58:31 -030027{
28 struct vsp1_entity *sink;
29
30 if (source->route->reg == 0)
31 return;
32
33 sink = container_of(source->sink, struct vsp1_entity, subdev.entity);
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020034 vsp1_dl_list_write(dl, source->route->reg,
35 sink->route->inputs[source->sink_pad]);
Laurent Pinchart665b6932015-08-02 18:58:31 -030036}
37
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030038/* -----------------------------------------------------------------------------
39 * V4L2 Subdevice Operations
40 */
41
Laurent Pincharte790c3c2015-11-15 19:14:22 -020042/**
43 * vsp1_entity_get_pad_config - Get the pad configuration for an entity
44 * @entity: the entity
45 * @cfg: the TRY pad configuration
46 * @which: configuration selector (ACTIVE or TRY)
47 *
48 * Return the pad configuration requested by the which argument. The TRY
49 * configuration is passed explicitly to the function through the cfg argument
50 * and simply returned when requested. The ACTIVE configuration comes from the
51 * entity structure.
52 */
53struct v4l2_subdev_pad_config *
54vsp1_entity_get_pad_config(struct vsp1_entity *entity,
55 struct v4l2_subdev_pad_config *cfg,
56 enum v4l2_subdev_format_whence which)
57{
58 switch (which) {
59 case V4L2_SUBDEV_FORMAT_ACTIVE:
60 return entity->config;
61 case V4L2_SUBDEV_FORMAT_TRY:
62 default:
63 return cfg;
64 }
65}
66
67/**
68 * vsp1_entity_get_pad_format - Get a pad format from storage for an entity
69 * @entity: the entity
70 * @cfg: the configuration storage
71 * @pad: the pad number
72 *
73 * Return the format stored in the given configuration for an entity's pad. The
74 * configuration can be an ACTIVE or TRY configuration.
75 */
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030076struct v4l2_mbus_framefmt *
77vsp1_entity_get_pad_format(struct vsp1_entity *entity,
Hans Verkuilf7234132015-03-04 01:47:54 -080078 struct v4l2_subdev_pad_config *cfg,
Laurent Pincharte790c3c2015-11-15 19:14:22 -020079 unsigned int pad)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030080{
Laurent Pincharte790c3c2015-11-15 19:14:22 -020081 return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030082}
83
Laurent Pinchartb7e51072015-11-15 19:14:22 -020084struct v4l2_rect *
85vsp1_entity_get_pad_compose(struct vsp1_entity *entity,
86 struct v4l2_subdev_pad_config *cfg,
87 unsigned int pad)
88{
89 return v4l2_subdev_get_try_compose(&entity->subdev, cfg, pad);
90}
91
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030092/*
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -020093 * vsp1_entity_init_cfg - Initialize formats on all pads
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030094 * @subdev: V4L2 subdevice
Hans Verkuilf7234132015-03-04 01:47:54 -080095 * @cfg: V4L2 subdev pad configuration
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030096 *
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -020097 * Initialize all pad formats with default values in the given pad config. This
98 * function can be used as a handler for the subdev pad::init_cfg operation.
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030099 */
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200100int vsp1_entity_init_cfg(struct v4l2_subdev *subdev,
101 struct v4l2_subdev_pad_config *cfg)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300102{
103 struct v4l2_subdev_format format;
104 unsigned int pad;
105
106 for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
107 memset(&format, 0, sizeof(format));
108
109 format.pad = pad;
Hans Verkuilf7234132015-03-04 01:47:54 -0800110 format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300111 : V4L2_SUBDEV_FORMAT_ACTIVE;
112
Hans Verkuilf7234132015-03-04 01:47:54 -0800113 v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300114 }
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300115
116 return 0;
117}
118
Laurent Pinchart3f557222016-02-24 21:10:13 -0300119/*
120 * vsp1_subdev_get_pad_format - Subdev pad get_fmt handler
121 * @subdev: V4L2 subdevice
122 * @cfg: V4L2 subdev pad configuration
123 * @fmt: V4L2 subdev format
124 *
125 * This function implements the subdev get_fmt pad operation. It can be used as
126 * a direct drop-in for the operation handler.
127 */
128int vsp1_subdev_get_pad_format(struct v4l2_subdev *subdev,
129 struct v4l2_subdev_pad_config *cfg,
130 struct v4l2_subdev_format *fmt)
131{
132 struct vsp1_entity *entity = to_vsp1_entity(subdev);
133 struct v4l2_subdev_pad_config *config;
134
135 config = vsp1_entity_get_pad_config(entity, cfg, fmt->which);
136 if (!config)
137 return -EINVAL;
138
139 fmt->format = *vsp1_entity_get_pad_format(entity, config, fmt->pad);
140
141 return 0;
142}
143
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300144/* -----------------------------------------------------------------------------
145 * Media Operations
146 */
147
Laurent Pinchartbabca002015-08-05 17:14:41 -0300148int vsp1_entity_link_setup(struct media_entity *entity,
149 const struct media_pad *local,
150 const struct media_pad *remote, u32 flags)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300151{
152 struct vsp1_entity *source;
153
154 if (!(local->flags & MEDIA_PAD_FL_SOURCE))
155 return 0;
156
157 source = container_of(local->entity, struct vsp1_entity, subdev.entity);
158
159 if (!source->route)
160 return 0;
161
162 if (flags & MEDIA_LNK_FL_ENABLED) {
163 if (source->sink)
164 return -EBUSY;
165 source->sink = remote->entity;
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300166 source->sink_pad = remote->index;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300167 } else {
168 source->sink = NULL;
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300169 source->sink_pad = 0;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300170 }
171
172 return 0;
173}
174
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300175/* -----------------------------------------------------------------------------
176 * Initialization
177 */
178
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300179static const struct vsp1_route vsp1_routes[] = {
Laurent Pinchart629bb6d2013-07-10 18:03:46 -0300180 { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
181 { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
Laurent Pinchart7f2d50f2015-09-07 08:05:39 -0300182 VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3),
183 VI6_DPR_NODE_BRU_IN(4) } },
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300184 { VSP1_ENTITY_HSI, 0, VI6_DPR_HSI_ROUTE, { VI6_DPR_NODE_HSI, } },
185 { VSP1_ENTITY_HST, 0, VI6_DPR_HST_ROUTE, { VI6_DPR_NODE_HST, } },
186 { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, } },
187 { VSP1_ENTITY_LUT, 0, VI6_DPR_LUT_ROUTE, { VI6_DPR_NODE_LUT, } },
Laurent Pinchart5b22a11e2016-02-24 18:40:12 -0300188 { VSP1_ENTITY_RPF, 0, VI6_DPR_RPF_ROUTE(0), { 0, } },
189 { VSP1_ENTITY_RPF, 1, VI6_DPR_RPF_ROUTE(1), { 0, } },
190 { VSP1_ENTITY_RPF, 2, VI6_DPR_RPF_ROUTE(2), { 0, } },
191 { VSP1_ENTITY_RPF, 3, VI6_DPR_RPF_ROUTE(3), { 0, } },
192 { VSP1_ENTITY_RPF, 4, VI6_DPR_RPF_ROUTE(4), { 0, } },
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300193 { VSP1_ENTITY_SRU, 0, VI6_DPR_SRU_ROUTE, { VI6_DPR_NODE_SRU, } },
194 { VSP1_ENTITY_UDS, 0, VI6_DPR_UDS_ROUTE(0), { VI6_DPR_NODE_UDS(0), } },
195 { VSP1_ENTITY_UDS, 1, VI6_DPR_UDS_ROUTE(1), { VI6_DPR_NODE_UDS(1), } },
196 { VSP1_ENTITY_UDS, 2, VI6_DPR_UDS_ROUTE(2), { VI6_DPR_NODE_UDS(2), } },
197 { VSP1_ENTITY_WPF, 0, 0, { VI6_DPR_NODE_WPF(0), } },
198 { VSP1_ENTITY_WPF, 1, 0, { VI6_DPR_NODE_WPF(1), } },
199 { VSP1_ENTITY_WPF, 2, 0, { VI6_DPR_NODE_WPF(2), } },
200 { VSP1_ENTITY_WPF, 3, 0, { VI6_DPR_NODE_WPF(3), } },
201};
202
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300203int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
Laurent Pinchart823329d2015-11-15 19:42:01 -0200204 const char *name, unsigned int num_pads,
205 const struct v4l2_subdev_ops *ops)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300206{
Laurent Pinchart823329d2015-11-15 19:42:01 -0200207 struct v4l2_subdev *subdev;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300208 unsigned int i;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200209 int ret;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300210
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300211 for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
212 if (vsp1_routes[i].type == entity->type &&
213 vsp1_routes[i].index == entity->index) {
214 entity->route = &vsp1_routes[i];
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300215 break;
216 }
217 }
218
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300219 if (i == ARRAY_SIZE(vsp1_routes))
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300220 return -EINVAL;
221
222 entity->vsp1 = vsp1;
223 entity->source_pad = num_pads - 1;
224
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200225 /* Allocate and initialize pads. */
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300226 entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads),
227 GFP_KERNEL);
228 if (entity->pads == NULL)
229 return -ENOMEM;
230
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300231 for (i = 0; i < num_pads - 1; ++i)
232 entity->pads[i].flags = MEDIA_PAD_FL_SINK;
233
234 entity->pads[num_pads - 1].flags = MEDIA_PAD_FL_SOURCE;
235
236 /* Initialize the media entity. */
Laurent Pinchart823329d2015-11-15 19:42:01 -0200237 ret = media_entity_pads_init(&entity->subdev.entity, num_pads,
238 entity->pads);
239 if (ret < 0)
240 return ret;
241
242 /* Initialize the V4L2 subdev. */
243 subdev = &entity->subdev;
244 v4l2_subdev_init(subdev, ops);
245
246 subdev->entity.ops = &vsp1->media_ops;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200247 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
248
249 snprintf(subdev->name, sizeof(subdev->name), "%s %s",
250 dev_name(vsp1->dev), name);
251
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200252 vsp1_entity_init_cfg(subdev, NULL);
Laurent Pinchart823329d2015-11-15 19:42:01 -0200253
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200254 /* Allocate the pad configuration to store formats and selection
255 * rectangles.
256 */
257 entity->config = v4l2_subdev_alloc_pad_config(&entity->subdev);
258 if (entity->config == NULL) {
259 media_entity_cleanup(&entity->subdev.entity);
260 return -ENOMEM;
261 }
262
Laurent Pinchart823329d2015-11-15 19:42:01 -0200263 return 0;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300264}
265
266void vsp1_entity_destroy(struct vsp1_entity *entity)
267{
Laurent Pinchart52434532015-11-17 12:23:23 -0200268 if (entity->ops && entity->ops->destroy)
269 entity->ops->destroy(entity);
Laurent Pincharta626e642013-07-10 12:03:30 -0300270 if (entity->subdev.ctrl_handler)
271 v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200272 v4l2_subdev_free_pad_config(entity->config);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300273 media_entity_cleanup(&entity->subdev.entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300274}