Commit | Line | Data |
---|---|---|
26e0ca22 LP |
1 | /* |
2 | * vsp1_entity.c -- R-Car VSP1 Base Entity | |
3 | * | |
8a1edc55 | 4 | * Copyright (C) 2013-2014 Renesas Electronics Corporation |
26e0ca22 LP |
5 | * |
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> | |
a626e64e | 18 | #include <media/v4l2-ctrls.h> |
26e0ca22 LP |
19 | #include <media/v4l2-subdev.h> |
20 | ||
21 | #include "vsp1.h" | |
aa380ea0 | 22 | #include "vsp1_dl.h" |
26e0ca22 | 23 | #include "vsp1_entity.h" |
aa380ea0 | 24 | |
5e8dbbf3 LP |
25 | void vsp1_entity_route_setup(struct vsp1_entity *source, |
26 | struct vsp1_dl_list *dl) | |
665b693c LP |
27 | { |
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); | |
5e8dbbf3 LP |
34 | vsp1_dl_list_write(dl, source->route->reg, |
35 | sink->route->inputs[source->sink_pad]); | |
665b693c LP |
36 | } |
37 | ||
26e0ca22 LP |
38 | /* ----------------------------------------------------------------------------- |
39 | * V4L2 Subdevice Operations | |
40 | */ | |
41 | ||
e790c3cb LP |
42 | /** |
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 | */ | |
53 | struct v4l2_subdev_pad_config * | |
54 | vsp1_entity_get_pad_config(struct vsp1_entity *entity, | |
f7234138 | 55 | struct v4l2_subdev_pad_config *cfg, |
e790c3cb | 56 | enum v4l2_subdev_format_whence which) |
26e0ca22 LP |
57 | { |
58 | switch (which) { | |
26e0ca22 | 59 | case V4L2_SUBDEV_FORMAT_ACTIVE: |
e790c3cb LP |
60 | return entity->config; |
61 | case V4L2_SUBDEV_FORMAT_TRY: | |
26e0ca22 | 62 | default: |
e790c3cb | 63 | return cfg; |
26e0ca22 LP |
64 | } |
65 | } | |
66 | ||
e790c3cb LP |
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 | */ | |
76 | struct v4l2_mbus_framefmt * | |
77 | vsp1_entity_get_pad_format(struct vsp1_entity *entity, | |
78 | struct v4l2_subdev_pad_config *cfg, | |
79 | unsigned int pad) | |
80 | { | |
81 | return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad); | |
82 | } | |
83 | ||
b7e5107e LP |
84 | struct v4l2_rect * |
85 | vsp1_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 | ||
26e0ca22 | 92 | /* |
0efdf0f5 | 93 | * vsp1_entity_init_cfg - Initialize formats on all pads |
26e0ca22 | 94 | * @subdev: V4L2 subdevice |
f7234138 | 95 | * @cfg: V4L2 subdev pad configuration |
26e0ca22 | 96 | * |
0efdf0f5 LP |
97 | * 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. | |
26e0ca22 | 99 | */ |
0efdf0f5 LP |
100 | int vsp1_entity_init_cfg(struct v4l2_subdev *subdev, |
101 | struct v4l2_subdev_pad_config *cfg) | |
26e0ca22 LP |
102 | { |
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; | |
f7234138 | 110 | format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY |
26e0ca22 LP |
111 | : V4L2_SUBDEV_FORMAT_ACTIVE; |
112 | ||
f7234138 | 113 | v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format); |
26e0ca22 | 114 | } |
26e0ca22 LP |
115 | |
116 | return 0; | |
117 | } | |
118 | ||
26e0ca22 LP |
119 | /* ----------------------------------------------------------------------------- |
120 | * Media Operations | |
121 | */ | |
122 | ||
babca007 LP |
123 | int vsp1_entity_link_setup(struct media_entity *entity, |
124 | const struct media_pad *local, | |
125 | const struct media_pad *remote, u32 flags) | |
26e0ca22 LP |
126 | { |
127 | struct vsp1_entity *source; | |
128 | ||
129 | if (!(local->flags & MEDIA_PAD_FL_SOURCE)) | |
130 | return 0; | |
131 | ||
132 | source = container_of(local->entity, struct vsp1_entity, subdev.entity); | |
133 | ||
134 | if (!source->route) | |
135 | return 0; | |
136 | ||
137 | if (flags & MEDIA_LNK_FL_ENABLED) { | |
138 | if (source->sink) | |
139 | return -EBUSY; | |
140 | source->sink = remote->entity; | |
d9b45ed3 | 141 | source->sink_pad = remote->index; |
26e0ca22 LP |
142 | } else { |
143 | source->sink = NULL; | |
d9b45ed3 | 144 | source->sink_pad = 0; |
26e0ca22 LP |
145 | } |
146 | ||
147 | return 0; | |
148 | } | |
149 | ||
26e0ca22 LP |
150 | /* ----------------------------------------------------------------------------- |
151 | * Initialization | |
152 | */ | |
153 | ||
d9b45ed3 | 154 | static const struct vsp1_route vsp1_routes[] = { |
629bb6d4 LP |
155 | { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE, |
156 | { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1), | |
7f2d50f8 LP |
157 | VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3), |
158 | VI6_DPR_NODE_BRU_IN(4) } }, | |
d9b45ed3 LP |
159 | { VSP1_ENTITY_HSI, 0, VI6_DPR_HSI_ROUTE, { VI6_DPR_NODE_HSI, } }, |
160 | { VSP1_ENTITY_HST, 0, VI6_DPR_HST_ROUTE, { VI6_DPR_NODE_HST, } }, | |
161 | { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, } }, | |
162 | { VSP1_ENTITY_LUT, 0, VI6_DPR_LUT_ROUTE, { VI6_DPR_NODE_LUT, } }, | |
5b22a11e LP |
163 | { VSP1_ENTITY_RPF, 0, VI6_DPR_RPF_ROUTE(0), { 0, } }, |
164 | { VSP1_ENTITY_RPF, 1, VI6_DPR_RPF_ROUTE(1), { 0, } }, | |
165 | { VSP1_ENTITY_RPF, 2, VI6_DPR_RPF_ROUTE(2), { 0, } }, | |
166 | { VSP1_ENTITY_RPF, 3, VI6_DPR_RPF_ROUTE(3), { 0, } }, | |
167 | { VSP1_ENTITY_RPF, 4, VI6_DPR_RPF_ROUTE(4), { 0, } }, | |
d9b45ed3 LP |
168 | { VSP1_ENTITY_SRU, 0, VI6_DPR_SRU_ROUTE, { VI6_DPR_NODE_SRU, } }, |
169 | { VSP1_ENTITY_UDS, 0, VI6_DPR_UDS_ROUTE(0), { VI6_DPR_NODE_UDS(0), } }, | |
170 | { VSP1_ENTITY_UDS, 1, VI6_DPR_UDS_ROUTE(1), { VI6_DPR_NODE_UDS(1), } }, | |
171 | { VSP1_ENTITY_UDS, 2, VI6_DPR_UDS_ROUTE(2), { VI6_DPR_NODE_UDS(2), } }, | |
172 | { VSP1_ENTITY_WPF, 0, 0, { VI6_DPR_NODE_WPF(0), } }, | |
173 | { VSP1_ENTITY_WPF, 1, 0, { VI6_DPR_NODE_WPF(1), } }, | |
174 | { VSP1_ENTITY_WPF, 2, 0, { VI6_DPR_NODE_WPF(2), } }, | |
175 | { VSP1_ENTITY_WPF, 3, 0, { VI6_DPR_NODE_WPF(3), } }, | |
176 | }; | |
177 | ||
26e0ca22 | 178 | int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity, |
823329df LP |
179 | const char *name, unsigned int num_pads, |
180 | const struct v4l2_subdev_ops *ops) | |
26e0ca22 | 181 | { |
823329df | 182 | struct v4l2_subdev *subdev; |
26e0ca22 | 183 | unsigned int i; |
823329df | 184 | int ret; |
26e0ca22 | 185 | |
d9b45ed3 LP |
186 | for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) { |
187 | if (vsp1_routes[i].type == entity->type && | |
188 | vsp1_routes[i].index == entity->index) { | |
189 | entity->route = &vsp1_routes[i]; | |
26e0ca22 LP |
190 | break; |
191 | } | |
192 | } | |
193 | ||
d9b45ed3 | 194 | if (i == ARRAY_SIZE(vsp1_routes)) |
26e0ca22 LP |
195 | return -EINVAL; |
196 | ||
197 | entity->vsp1 = vsp1; | |
198 | entity->source_pad = num_pads - 1; | |
199 | ||
e790c3cb | 200 | /* Allocate and initialize pads. */ |
26e0ca22 LP |
201 | entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads), |
202 | GFP_KERNEL); | |
203 | if (entity->pads == NULL) | |
204 | return -ENOMEM; | |
205 | ||
26e0ca22 LP |
206 | for (i = 0; i < num_pads - 1; ++i) |
207 | entity->pads[i].flags = MEDIA_PAD_FL_SINK; | |
208 | ||
209 | entity->pads[num_pads - 1].flags = MEDIA_PAD_FL_SOURCE; | |
210 | ||
211 | /* Initialize the media entity. */ | |
823329df LP |
212 | ret = media_entity_pads_init(&entity->subdev.entity, num_pads, |
213 | entity->pads); | |
214 | if (ret < 0) | |
215 | return ret; | |
216 | ||
217 | /* Initialize the V4L2 subdev. */ | |
218 | subdev = &entity->subdev; | |
219 | v4l2_subdev_init(subdev, ops); | |
220 | ||
221 | subdev->entity.ops = &vsp1->media_ops; | |
823329df LP |
222 | subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; |
223 | ||
224 | snprintf(subdev->name, sizeof(subdev->name), "%s %s", | |
225 | dev_name(vsp1->dev), name); | |
226 | ||
0efdf0f5 | 227 | vsp1_entity_init_cfg(subdev, NULL); |
823329df | 228 | |
e790c3cb LP |
229 | /* Allocate the pad configuration to store formats and selection |
230 | * rectangles. | |
231 | */ | |
232 | entity->config = v4l2_subdev_alloc_pad_config(&entity->subdev); | |
233 | if (entity->config == NULL) { | |
234 | media_entity_cleanup(&entity->subdev.entity); | |
235 | return -ENOMEM; | |
236 | } | |
237 | ||
823329df | 238 | return 0; |
26e0ca22 LP |
239 | } |
240 | ||
241 | void vsp1_entity_destroy(struct vsp1_entity *entity) | |
242 | { | |
52434534 LP |
243 | if (entity->ops && entity->ops->destroy) |
244 | entity->ops->destroy(entity); | |
a626e64e LP |
245 | if (entity->subdev.ctrl_handler) |
246 | v4l2_ctrl_handler_free(entity->subdev.ctrl_handler); | |
e790c3cb | 247 | v4l2_subdev_free_pad_config(entity->config); |
26e0ca22 LP |
248 | media_entity_cleanup(&entity->subdev.entity); |
249 | } |