blob: 20a78fbd36910cf3358bd6083d9449bb1a31c1f7 [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"
22#include "vsp1_entity.h"
23
Laurent Pinchart960de2c2014-05-31 10:40:51 -030024bool vsp1_entity_is_streaming(struct vsp1_entity *entity)
25{
Laurent Pinchartadb89632015-04-13 11:43:40 -030026 unsigned long flags;
Laurent Pinchart960de2c2014-05-31 10:40:51 -030027 bool streaming;
28
Laurent Pinchartadb89632015-04-13 11:43:40 -030029 spin_lock_irqsave(&entity->lock, flags);
Laurent Pinchart960de2c2014-05-31 10:40:51 -030030 streaming = entity->streaming;
Laurent Pinchartadb89632015-04-13 11:43:40 -030031 spin_unlock_irqrestore(&entity->lock, flags);
Laurent Pinchart960de2c2014-05-31 10:40:51 -030032
33 return streaming;
34}
35
36int vsp1_entity_set_streaming(struct vsp1_entity *entity, bool streaming)
37{
Laurent Pinchartadb89632015-04-13 11:43:40 -030038 unsigned long flags;
Laurent Pinchart960de2c2014-05-31 10:40:51 -030039 int ret;
40
Laurent Pinchartadb89632015-04-13 11:43:40 -030041 spin_lock_irqsave(&entity->lock, flags);
Laurent Pinchart960de2c2014-05-31 10:40:51 -030042 entity->streaming = streaming;
Laurent Pinchartadb89632015-04-13 11:43:40 -030043 spin_unlock_irqrestore(&entity->lock, flags);
Laurent Pinchart960de2c2014-05-31 10:40:51 -030044
45 if (!streaming)
46 return 0;
47
Laurent Pinchart5aa2eb32015-12-05 20:17:10 -020048 if (!entity->vsp1->info->uapi || !entity->subdev.ctrl_handler)
Laurent Pinchart960de2c2014-05-31 10:40:51 -030049 return 0;
50
51 ret = v4l2_ctrl_handler_setup(entity->subdev.ctrl_handler);
52 if (ret < 0) {
Laurent Pinchartadb89632015-04-13 11:43:40 -030053 spin_lock_irqsave(&entity->lock, flags);
Laurent Pinchart960de2c2014-05-31 10:40:51 -030054 entity->streaming = false;
Laurent Pinchartadb89632015-04-13 11:43:40 -030055 spin_unlock_irqrestore(&entity->lock, flags);
Laurent Pinchart960de2c2014-05-31 10:40:51 -030056 }
57
58 return ret;
59}
60
Laurent Pinchart665b6932015-08-02 18:58:31 -030061void vsp1_entity_route_setup(struct vsp1_entity *source)
62{
63 struct vsp1_entity *sink;
64
65 if (source->route->reg == 0)
66 return;
67
68 sink = container_of(source->sink, struct vsp1_entity, subdev.entity);
Takashi Saito1517b032015-09-07 01:40:25 -030069 vsp1_mod_write(source, source->route->reg,
70 sink->route->inputs[source->sink_pad]);
Laurent Pinchart665b6932015-08-02 18:58:31 -030071}
72
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030073/* -----------------------------------------------------------------------------
74 * V4L2 Subdevice Operations
75 */
76
77struct v4l2_mbus_framefmt *
78vsp1_entity_get_pad_format(struct vsp1_entity *entity,
Hans Verkuilf7234132015-03-04 01:47:54 -080079 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030080 unsigned int pad, u32 which)
81{
82 switch (which) {
83 case V4L2_SUBDEV_FORMAT_TRY:
Hans Verkuilf7234132015-03-04 01:47:54 -080084 return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030085 case V4L2_SUBDEV_FORMAT_ACTIVE:
86 return &entity->formats[pad];
87 default:
88 return NULL;
89 }
90}
91
92/*
93 * vsp1_entity_init_formats - Initialize formats on all pads
94 * @subdev: V4L2 subdevice
Hans Verkuilf7234132015-03-04 01:47:54 -080095 * @cfg: V4L2 subdev pad configuration
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030096 *
Hans Verkuilf7234132015-03-04 01:47:54 -080097 * Initialize all pad formats with default values. If cfg is not NULL, try
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030098 * formats are initialized on the file handle. Otherwise active formats are
99 * initialized on the device.
100 */
101void vsp1_entity_init_formats(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -0800102 struct v4l2_subdev_pad_config *cfg)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300103{
104 struct v4l2_subdev_format format;
105 unsigned int pad;
106
107 for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
108 memset(&format, 0, sizeof(format));
109
110 format.pad = pad;
Hans Verkuilf7234132015-03-04 01:47:54 -0800111 format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300112 : V4L2_SUBDEV_FORMAT_ACTIVE;
113
Hans Verkuilf7234132015-03-04 01:47:54 -0800114 v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300115 }
116}
117
118static int vsp1_entity_open(struct v4l2_subdev *subdev,
119 struct v4l2_subdev_fh *fh)
120{
Hans Verkuilf7234132015-03-04 01:47:54 -0800121 vsp1_entity_init_formats(subdev, fh->pad);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300122
123 return 0;
124}
125
126const struct v4l2_subdev_internal_ops vsp1_subdev_internal_ops = {
127 .open = vsp1_entity_open,
128};
129
130/* -----------------------------------------------------------------------------
131 * Media Operations
132 */
133
Laurent Pinchartbabca002015-08-05 17:14:41 -0300134int vsp1_entity_link_setup(struct media_entity *entity,
135 const struct media_pad *local,
136 const struct media_pad *remote, u32 flags)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300137{
138 struct vsp1_entity *source;
139
140 if (!(local->flags & MEDIA_PAD_FL_SOURCE))
141 return 0;
142
143 source = container_of(local->entity, struct vsp1_entity, subdev.entity);
144
145 if (!source->route)
146 return 0;
147
148 if (flags & MEDIA_LNK_FL_ENABLED) {
149 if (source->sink)
150 return -EBUSY;
151 source->sink = remote->entity;
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300152 source->sink_pad = remote->index;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300153 } else {
154 source->sink = NULL;
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300155 source->sink_pad = 0;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300156 }
157
158 return 0;
159}
160
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300161/* -----------------------------------------------------------------------------
162 * Initialization
163 */
164
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300165static const struct vsp1_route vsp1_routes[] = {
Laurent Pinchart629bb6d2013-07-10 18:03:46 -0300166 { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
167 { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
Laurent Pinchart7f2d50f2015-09-07 08:05:39 -0300168 VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3),
169 VI6_DPR_NODE_BRU_IN(4) } },
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300170 { VSP1_ENTITY_HSI, 0, VI6_DPR_HSI_ROUTE, { VI6_DPR_NODE_HSI, } },
171 { VSP1_ENTITY_HST, 0, VI6_DPR_HST_ROUTE, { VI6_DPR_NODE_HST, } },
172 { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, } },
173 { VSP1_ENTITY_LUT, 0, VI6_DPR_LUT_ROUTE, { VI6_DPR_NODE_LUT, } },
174 { VSP1_ENTITY_RPF, 0, VI6_DPR_RPF_ROUTE(0), { VI6_DPR_NODE_RPF(0), } },
175 { VSP1_ENTITY_RPF, 1, VI6_DPR_RPF_ROUTE(1), { VI6_DPR_NODE_RPF(1), } },
176 { VSP1_ENTITY_RPF, 2, VI6_DPR_RPF_ROUTE(2), { VI6_DPR_NODE_RPF(2), } },
177 { VSP1_ENTITY_RPF, 3, VI6_DPR_RPF_ROUTE(3), { VI6_DPR_NODE_RPF(3), } },
178 { VSP1_ENTITY_RPF, 4, VI6_DPR_RPF_ROUTE(4), { VI6_DPR_NODE_RPF(4), } },
179 { VSP1_ENTITY_SRU, 0, VI6_DPR_SRU_ROUTE, { VI6_DPR_NODE_SRU, } },
180 { VSP1_ENTITY_UDS, 0, VI6_DPR_UDS_ROUTE(0), { VI6_DPR_NODE_UDS(0), } },
181 { VSP1_ENTITY_UDS, 1, VI6_DPR_UDS_ROUTE(1), { VI6_DPR_NODE_UDS(1), } },
182 { VSP1_ENTITY_UDS, 2, VI6_DPR_UDS_ROUTE(2), { VI6_DPR_NODE_UDS(2), } },
183 { VSP1_ENTITY_WPF, 0, 0, { VI6_DPR_NODE_WPF(0), } },
184 { VSP1_ENTITY_WPF, 1, 0, { VI6_DPR_NODE_WPF(1), } },
185 { VSP1_ENTITY_WPF, 2, 0, { VI6_DPR_NODE_WPF(2), } },
186 { VSP1_ENTITY_WPF, 3, 0, { VI6_DPR_NODE_WPF(3), } },
187};
188
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300189int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
190 unsigned int num_pads)
191{
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300192 unsigned int i;
193
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300194 for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
195 if (vsp1_routes[i].type == entity->type &&
196 vsp1_routes[i].index == entity->index) {
197 entity->route = &vsp1_routes[i];
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300198 break;
199 }
200 }
201
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300202 if (i == ARRAY_SIZE(vsp1_routes))
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300203 return -EINVAL;
204
Laurent Pinchartadb89632015-04-13 11:43:40 -0300205 spin_lock_init(&entity->lock);
Laurent Pinchart960de2c2014-05-31 10:40:51 -0300206
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300207 entity->vsp1 = vsp1;
208 entity->source_pad = num_pads - 1;
209
210 /* Allocate formats and pads. */
211 entity->formats = devm_kzalloc(vsp1->dev,
212 num_pads * sizeof(*entity->formats),
213 GFP_KERNEL);
214 if (entity->formats == NULL)
215 return -ENOMEM;
216
217 entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads),
218 GFP_KERNEL);
219 if (entity->pads == NULL)
220 return -ENOMEM;
221
222 /* Initialize pads. */
223 for (i = 0; i < num_pads - 1; ++i)
224 entity->pads[i].flags = MEDIA_PAD_FL_SINK;
225
226 entity->pads[num_pads - 1].flags = MEDIA_PAD_FL_SOURCE;
227
228 /* Initialize the media entity. */
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200229 return media_entity_pads_init(&entity->subdev.entity, num_pads,
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300230 entity->pads);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300231}
232
233void vsp1_entity_destroy(struct vsp1_entity *entity)
234{
Laurent Pincharta626e642013-07-10 12:03:30 -0300235 if (entity->subdev.ctrl_handler)
236 v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300237 media_entity_cleanup(&entity->subdev.entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300238}