blob: 7b2301dbd5844471d94c71b3aab38b967388bca7 [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#include "vsp1_pipe.h"
25
26void vsp1_mod_write(struct vsp1_entity *e, u32 reg, u32 data)
27{
28 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&e->subdev.entity);
29
Laurent Pinchart351bbf92015-11-01 15:18:56 -020030 vsp1_dl_list_write(pipe->dl, reg, data);
Laurent Pinchartaa380ea2015-11-01 10:46:25 -020031}
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030032
Laurent Pinchart665b6932015-08-02 18:58:31 -030033void vsp1_entity_route_setup(struct vsp1_entity *source)
34{
35 struct vsp1_entity *sink;
36
37 if (source->route->reg == 0)
38 return;
39
40 sink = container_of(source->sink, struct vsp1_entity, subdev.entity);
Takashi Saito1517b032015-09-07 01:40:25 -030041 vsp1_mod_write(source, source->route->reg,
42 sink->route->inputs[source->sink_pad]);
Laurent Pinchart665b6932015-08-02 18:58:31 -030043}
44
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030045/* -----------------------------------------------------------------------------
46 * V4L2 Subdevice Operations
47 */
48
49struct v4l2_mbus_framefmt *
50vsp1_entity_get_pad_format(struct vsp1_entity *entity,
Hans Verkuilf7234132015-03-04 01:47:54 -080051 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030052 unsigned int pad, u32 which)
53{
54 switch (which) {
55 case V4L2_SUBDEV_FORMAT_TRY:
Hans Verkuilf7234132015-03-04 01:47:54 -080056 return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030057 case V4L2_SUBDEV_FORMAT_ACTIVE:
58 return &entity->formats[pad];
59 default:
60 return NULL;
61 }
62}
63
64/*
65 * vsp1_entity_init_formats - Initialize formats on all pads
66 * @subdev: V4L2 subdevice
Hans Verkuilf7234132015-03-04 01:47:54 -080067 * @cfg: V4L2 subdev pad configuration
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030068 *
Hans Verkuilf7234132015-03-04 01:47:54 -080069 * Initialize all pad formats with default values. If cfg is not NULL, try
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030070 * formats are initialized on the file handle. Otherwise active formats are
71 * initialized on the device.
72 */
73void vsp1_entity_init_formats(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -080074 struct v4l2_subdev_pad_config *cfg)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030075{
76 struct v4l2_subdev_format format;
77 unsigned int pad;
78
79 for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
80 memset(&format, 0, sizeof(format));
81
82 format.pad = pad;
Hans Verkuilf7234132015-03-04 01:47:54 -080083 format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030084 : V4L2_SUBDEV_FORMAT_ACTIVE;
85
Hans Verkuilf7234132015-03-04 01:47:54 -080086 v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030087 }
88}
89
90static int vsp1_entity_open(struct v4l2_subdev *subdev,
91 struct v4l2_subdev_fh *fh)
92{
Hans Verkuilf7234132015-03-04 01:47:54 -080093 vsp1_entity_init_formats(subdev, fh->pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030094
95 return 0;
96}
97
98const struct v4l2_subdev_internal_ops vsp1_subdev_internal_ops = {
99 .open = vsp1_entity_open,
100};
101
102/* -----------------------------------------------------------------------------
103 * Media Operations
104 */
105
Laurent Pinchartbabca002015-08-05 17:14:41 -0300106int vsp1_entity_link_setup(struct media_entity *entity,
107 const struct media_pad *local,
108 const struct media_pad *remote, u32 flags)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300109{
110 struct vsp1_entity *source;
111
112 if (!(local->flags & MEDIA_PAD_FL_SOURCE))
113 return 0;
114
115 source = container_of(local->entity, struct vsp1_entity, subdev.entity);
116
117 if (!source->route)
118 return 0;
119
120 if (flags & MEDIA_LNK_FL_ENABLED) {
121 if (source->sink)
122 return -EBUSY;
123 source->sink = remote->entity;
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300124 source->sink_pad = remote->index;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300125 } else {
126 source->sink = NULL;
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300127 source->sink_pad = 0;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300128 }
129
130 return 0;
131}
132
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300133/* -----------------------------------------------------------------------------
134 * Initialization
135 */
136
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300137static const struct vsp1_route vsp1_routes[] = {
Laurent Pinchart629bb6d2013-07-10 18:03:46 -0300138 { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
139 { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
Laurent Pinchart7f2d50f2015-09-07 08:05:39 -0300140 VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3),
141 VI6_DPR_NODE_BRU_IN(4) } },
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300142 { VSP1_ENTITY_HSI, 0, VI6_DPR_HSI_ROUTE, { VI6_DPR_NODE_HSI, } },
143 { VSP1_ENTITY_HST, 0, VI6_DPR_HST_ROUTE, { VI6_DPR_NODE_HST, } },
144 { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, } },
145 { VSP1_ENTITY_LUT, 0, VI6_DPR_LUT_ROUTE, { VI6_DPR_NODE_LUT, } },
146 { VSP1_ENTITY_RPF, 0, VI6_DPR_RPF_ROUTE(0), { VI6_DPR_NODE_RPF(0), } },
147 { VSP1_ENTITY_RPF, 1, VI6_DPR_RPF_ROUTE(1), { VI6_DPR_NODE_RPF(1), } },
148 { VSP1_ENTITY_RPF, 2, VI6_DPR_RPF_ROUTE(2), { VI6_DPR_NODE_RPF(2), } },
149 { VSP1_ENTITY_RPF, 3, VI6_DPR_RPF_ROUTE(3), { VI6_DPR_NODE_RPF(3), } },
150 { VSP1_ENTITY_RPF, 4, VI6_DPR_RPF_ROUTE(4), { VI6_DPR_NODE_RPF(4), } },
151 { VSP1_ENTITY_SRU, 0, VI6_DPR_SRU_ROUTE, { VI6_DPR_NODE_SRU, } },
152 { VSP1_ENTITY_UDS, 0, VI6_DPR_UDS_ROUTE(0), { VI6_DPR_NODE_UDS(0), } },
153 { VSP1_ENTITY_UDS, 1, VI6_DPR_UDS_ROUTE(1), { VI6_DPR_NODE_UDS(1), } },
154 { VSP1_ENTITY_UDS, 2, VI6_DPR_UDS_ROUTE(2), { VI6_DPR_NODE_UDS(2), } },
155 { VSP1_ENTITY_WPF, 0, 0, { VI6_DPR_NODE_WPF(0), } },
156 { VSP1_ENTITY_WPF, 1, 0, { VI6_DPR_NODE_WPF(1), } },
157 { VSP1_ENTITY_WPF, 2, 0, { VI6_DPR_NODE_WPF(2), } },
158 { VSP1_ENTITY_WPF, 3, 0, { VI6_DPR_NODE_WPF(3), } },
159};
160
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300161int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
162 unsigned int num_pads)
163{
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300164 unsigned int i;
165
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300166 for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
167 if (vsp1_routes[i].type == entity->type &&
168 vsp1_routes[i].index == entity->index) {
169 entity->route = &vsp1_routes[i];
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300170 break;
171 }
172 }
173
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300174 if (i == ARRAY_SIZE(vsp1_routes))
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300175 return -EINVAL;
176
177 entity->vsp1 = vsp1;
178 entity->source_pad = num_pads - 1;
179
180 /* Allocate formats and pads. */
181 entity->formats = devm_kzalloc(vsp1->dev,
182 num_pads * sizeof(*entity->formats),
183 GFP_KERNEL);
184 if (entity->formats == NULL)
185 return -ENOMEM;
186
187 entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads),
188 GFP_KERNEL);
189 if (entity->pads == NULL)
190 return -ENOMEM;
191
192 /* Initialize pads. */
193 for (i = 0; i < num_pads - 1; ++i)
194 entity->pads[i].flags = MEDIA_PAD_FL_SINK;
195
196 entity->pads[num_pads - 1].flags = MEDIA_PAD_FL_SOURCE;
197
198 /* Initialize the media entity. */
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200199 return media_entity_pads_init(&entity->subdev.entity, num_pads,
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300200 entity->pads);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300201}
202
203void vsp1_entity_destroy(struct vsp1_entity *entity)
204{
Laurent Pinchartef9621b2015-11-14 22:27:52 -0200205 if (entity->destroy)
206 entity->destroy(entity);
Laurent Pincharta626e642013-07-10 12:03:30 -0300207 if (entity->subdev.ctrl_handler)
208 v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300209 media_entity_cleanup(&entity->subdev.entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300210}