]> Git Repo - linux.git/blob - drivers/media/platform/vsp1/vsp1_entity.c
sched/deadline: Move DL related code from sched/core.c to sched/deadline.c
[linux.git] / drivers / media / platform / vsp1 / vsp1_entity.c
1 /*
2  * vsp1_entity.c  --  R-Car VSP1 Base Entity
3  *
4  * Copyright (C) 2013-2014 Renesas Electronics Corporation
5  *
6  * Contact: Laurent Pinchart ([email protected])
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>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-subdev.h>
20
21 #include "vsp1.h"
22 #include "vsp1_dl.h"
23 #include "vsp1_entity.h"
24 #include "vsp1_pipe.h"
25 #include "vsp1_rwpf.h"
26
27 static inline struct vsp1_entity *
28 media_entity_to_vsp1_entity(struct media_entity *entity)
29 {
30         return container_of(entity, struct vsp1_entity, subdev.entity);
31 }
32
33 void vsp1_entity_route_setup(struct vsp1_entity *entity,
34                              struct vsp1_pipeline *pipe,
35                              struct vsp1_dl_list *dl)
36 {
37         struct vsp1_entity *source;
38         struct vsp1_entity *sink;
39
40         if (entity->type == VSP1_ENTITY_HGO) {
41                 u32 smppt;
42
43                 /*
44                  * The HGO is a special case, its routing is configured on the
45                  * sink pad.
46                  */
47                 source = media_entity_to_vsp1_entity(entity->sources[0]);
48                 smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT)
49                       | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT);
50
51                 vsp1_dl_list_write(dl, VI6_DPR_HGO_SMPPT, smppt);
52                 return;
53         } else if (entity->type == VSP1_ENTITY_HGT) {
54                 u32 smppt;
55
56                 /*
57                  * The HGT is a special case, its routing is configured on the
58                  * sink pad.
59                  */
60                 source = media_entity_to_vsp1_entity(entity->sources[0]);
61                 smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT)
62                       | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT);
63
64                 vsp1_dl_list_write(dl, VI6_DPR_HGT_SMPPT, smppt);
65                 return;
66         }
67
68         source = entity;
69         if (source->route->reg == 0)
70                 return;
71
72         sink = media_entity_to_vsp1_entity(source->sink);
73         vsp1_dl_list_write(dl, source->route->reg,
74                            sink->route->inputs[source->sink_pad]);
75 }
76
77 /* -----------------------------------------------------------------------------
78  * V4L2 Subdevice Operations
79  */
80
81 /**
82  * vsp1_entity_get_pad_config - Get the pad configuration for an entity
83  * @entity: the entity
84  * @cfg: the TRY pad configuration
85  * @which: configuration selector (ACTIVE or TRY)
86  *
87  * When called with which set to V4L2_SUBDEV_FORMAT_ACTIVE the caller must hold
88  * the entity lock to access the returned configuration.
89  *
90  * Return the pad configuration requested by the which argument. The TRY
91  * configuration is passed explicitly to the function through the cfg argument
92  * and simply returned when requested. The ACTIVE configuration comes from the
93  * entity structure.
94  */
95 struct v4l2_subdev_pad_config *
96 vsp1_entity_get_pad_config(struct vsp1_entity *entity,
97                            struct v4l2_subdev_pad_config *cfg,
98                            enum v4l2_subdev_format_whence which)
99 {
100         switch (which) {
101         case V4L2_SUBDEV_FORMAT_ACTIVE:
102                 return entity->config;
103         case V4L2_SUBDEV_FORMAT_TRY:
104         default:
105                 return cfg;
106         }
107 }
108
109 /**
110  * vsp1_entity_get_pad_format - Get a pad format from storage for an entity
111  * @entity: the entity
112  * @cfg: the configuration storage
113  * @pad: the pad number
114  *
115  * Return the format stored in the given configuration for an entity's pad. The
116  * configuration can be an ACTIVE or TRY configuration.
117  */
118 struct v4l2_mbus_framefmt *
119 vsp1_entity_get_pad_format(struct vsp1_entity *entity,
120                            struct v4l2_subdev_pad_config *cfg,
121                            unsigned int pad)
122 {
123         return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad);
124 }
125
126 /**
127  * vsp1_entity_get_pad_selection - Get a pad selection from storage for entity
128  * @entity: the entity
129  * @cfg: the configuration storage
130  * @pad: the pad number
131  * @target: the selection target
132  *
133  * Return the selection rectangle stored in the given configuration for an
134  * entity's pad. The configuration can be an ACTIVE or TRY configuration. The
135  * selection target can be COMPOSE or CROP.
136  */
137 struct v4l2_rect *
138 vsp1_entity_get_pad_selection(struct vsp1_entity *entity,
139                               struct v4l2_subdev_pad_config *cfg,
140                               unsigned int pad, unsigned int target)
141 {
142         switch (target) {
143         case V4L2_SEL_TGT_COMPOSE:
144                 return v4l2_subdev_get_try_compose(&entity->subdev, cfg, pad);
145         case V4L2_SEL_TGT_CROP:
146                 return v4l2_subdev_get_try_crop(&entity->subdev, cfg, pad);
147         default:
148                 return NULL;
149         }
150 }
151
152 /*
153  * vsp1_entity_init_cfg - Initialize formats on all pads
154  * @subdev: V4L2 subdevice
155  * @cfg: V4L2 subdev pad configuration
156  *
157  * Initialize all pad formats with default values in the given pad config. This
158  * function can be used as a handler for the subdev pad::init_cfg operation.
159  */
160 int vsp1_entity_init_cfg(struct v4l2_subdev *subdev,
161                          struct v4l2_subdev_pad_config *cfg)
162 {
163         struct v4l2_subdev_format format;
164         unsigned int pad;
165
166         for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
167                 memset(&format, 0, sizeof(format));
168
169                 format.pad = pad;
170                 format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY
171                              : V4L2_SUBDEV_FORMAT_ACTIVE;
172
173                 v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format);
174         }
175
176         return 0;
177 }
178
179 /*
180  * vsp1_subdev_get_pad_format - Subdev pad get_fmt handler
181  * @subdev: V4L2 subdevice
182  * @cfg: V4L2 subdev pad configuration
183  * @fmt: V4L2 subdev format
184  *
185  * This function implements the subdev get_fmt pad operation. It can be used as
186  * a direct drop-in for the operation handler.
187  */
188 int vsp1_subdev_get_pad_format(struct v4l2_subdev *subdev,
189                                struct v4l2_subdev_pad_config *cfg,
190                                struct v4l2_subdev_format *fmt)
191 {
192         struct vsp1_entity *entity = to_vsp1_entity(subdev);
193         struct v4l2_subdev_pad_config *config;
194
195         config = vsp1_entity_get_pad_config(entity, cfg, fmt->which);
196         if (!config)
197                 return -EINVAL;
198
199         mutex_lock(&entity->lock);
200         fmt->format = *vsp1_entity_get_pad_format(entity, config, fmt->pad);
201         mutex_unlock(&entity->lock);
202
203         return 0;
204 }
205
206 /*
207  * vsp1_subdev_enum_mbus_code - Subdev pad enum_mbus_code handler
208  * @subdev: V4L2 subdevice
209  * @cfg: V4L2 subdev pad configuration
210  * @code: Media bus code enumeration
211  * @codes: Array of supported media bus codes
212  * @ncodes: Number of supported media bus codes
213  *
214  * This function implements the subdev enum_mbus_code pad operation for entities
215  * that do not support format conversion. It enumerates the given supported
216  * media bus codes on the sink pad and reports a source pad format identical to
217  * the sink pad.
218  */
219 int vsp1_subdev_enum_mbus_code(struct v4l2_subdev *subdev,
220                                struct v4l2_subdev_pad_config *cfg,
221                                struct v4l2_subdev_mbus_code_enum *code,
222                                const unsigned int *codes, unsigned int ncodes)
223 {
224         struct vsp1_entity *entity = to_vsp1_entity(subdev);
225
226         if (code->pad == 0) {
227                 if (code->index >= ncodes)
228                         return -EINVAL;
229
230                 code->code = codes[code->index];
231         } else {
232                 struct v4l2_subdev_pad_config *config;
233                 struct v4l2_mbus_framefmt *format;
234
235                 /*
236                  * The entity can't perform format conversion, the sink format
237                  * is always identical to the source format.
238                  */
239                 if (code->index)
240                         return -EINVAL;
241
242                 config = vsp1_entity_get_pad_config(entity, cfg, code->which);
243                 if (!config)
244                         return -EINVAL;
245
246                 mutex_lock(&entity->lock);
247                 format = vsp1_entity_get_pad_format(entity, config, 0);
248                 code->code = format->code;
249                 mutex_unlock(&entity->lock);
250         }
251
252         return 0;
253 }
254
255 /*
256  * vsp1_subdev_enum_frame_size - Subdev pad enum_frame_size handler
257  * @subdev: V4L2 subdevice
258  * @cfg: V4L2 subdev pad configuration
259  * @fse: Frame size enumeration
260  * @min_width: Minimum image width
261  * @min_height: Minimum image height
262  * @max_width: Maximum image width
263  * @max_height: Maximum image height
264  *
265  * This function implements the subdev enum_frame_size pad operation for
266  * entities that do not support scaling or cropping. It reports the given
267  * minimum and maximum frame width and height on the sink pad, and a fixed
268  * source pad size identical to the sink pad.
269  */
270 int vsp1_subdev_enum_frame_size(struct v4l2_subdev *subdev,
271                                 struct v4l2_subdev_pad_config *cfg,
272                                 struct v4l2_subdev_frame_size_enum *fse,
273                                 unsigned int min_width, unsigned int min_height,
274                                 unsigned int max_width, unsigned int max_height)
275 {
276         struct vsp1_entity *entity = to_vsp1_entity(subdev);
277         struct v4l2_subdev_pad_config *config;
278         struct v4l2_mbus_framefmt *format;
279         int ret = 0;
280
281         config = vsp1_entity_get_pad_config(entity, cfg, fse->which);
282         if (!config)
283                 return -EINVAL;
284
285         format = vsp1_entity_get_pad_format(entity, config, fse->pad);
286
287         mutex_lock(&entity->lock);
288
289         if (fse->index || fse->code != format->code) {
290                 ret = -EINVAL;
291                 goto done;
292         }
293
294         if (fse->pad == 0) {
295                 fse->min_width = min_width;
296                 fse->max_width = max_width;
297                 fse->min_height = min_height;
298                 fse->max_height = max_height;
299         } else {
300                 /*
301                  * The size on the source pad are fixed and always identical to
302                  * the size on the sink pad.
303                  */
304                 fse->min_width = format->width;
305                 fse->max_width = format->width;
306                 fse->min_height = format->height;
307                 fse->max_height = format->height;
308         }
309
310 done:
311         mutex_unlock(&entity->lock);
312         return ret;
313 }
314
315 /* -----------------------------------------------------------------------------
316  * Media Operations
317  */
318
319 static int vsp1_entity_link_setup_source(const struct media_pad *source_pad,
320                                          const struct media_pad *sink_pad,
321                                          u32 flags)
322 {
323         struct vsp1_entity *source;
324
325         source = media_entity_to_vsp1_entity(source_pad->entity);
326
327         if (!source->route)
328                 return 0;
329
330         if (flags & MEDIA_LNK_FL_ENABLED) {
331                 struct vsp1_entity *sink
332                         = media_entity_to_vsp1_entity(sink_pad->entity);
333
334                 /*
335                  * Fan-out is limited to one for the normal data path plus
336                  * optional HGO and HGT. We ignore the HGO and HGT here.
337                  */
338                 if (sink->type != VSP1_ENTITY_HGO &&
339                     sink->type != VSP1_ENTITY_HGT) {
340                         if (source->sink)
341                                 return -EBUSY;
342                         source->sink = sink_pad->entity;
343                         source->sink_pad = sink_pad->index;
344                 }
345         } else {
346                 source->sink = NULL;
347                 source->sink_pad = 0;
348         }
349
350         return 0;
351 }
352
353 static int vsp1_entity_link_setup_sink(const struct media_pad *source_pad,
354                                        const struct media_pad *sink_pad,
355                                        u32 flags)
356 {
357         struct vsp1_entity *sink;
358
359         sink = media_entity_to_vsp1_entity(sink_pad->entity);
360
361         if (flags & MEDIA_LNK_FL_ENABLED) {
362                 /* Fan-in is limited to one. */
363                 if (sink->sources[sink_pad->index])
364                         return -EBUSY;
365
366                 sink->sources[sink_pad->index] = source_pad->entity;
367         } else {
368                 sink->sources[sink_pad->index] = NULL;
369         }
370
371         return 0;
372 }
373
374 int vsp1_entity_link_setup(struct media_entity *entity,
375                            const struct media_pad *local,
376                            const struct media_pad *remote, u32 flags)
377 {
378         if (local->flags & MEDIA_PAD_FL_SOURCE)
379                 return vsp1_entity_link_setup_source(local, remote, flags);
380         else
381                 return vsp1_entity_link_setup_sink(remote, local, flags);
382 }
383
384 /**
385  * vsp1_entity_remote_pad - Find the pad at the remote end of a link
386  * @pad: Pad at the local end of the link
387  *
388  * Search for a remote pad connected to the given pad by iterating over all
389  * links originating or terminating at that pad until an enabled link is found.
390  *
391  * Our link setup implementation guarantees that the output fan-out will not be
392  * higher than one for the data pipelines, except for the links to the HGO and
393  * HGT that can be enabled in addition to a regular data link. When traversing
394  * outgoing links this function ignores HGO and HGT entities and should thus be
395  * used in place of the generic media_entity_remote_pad() function to traverse
396  * data pipelines.
397  *
398  * Return a pointer to the pad at the remote end of the first found enabled
399  * link, or NULL if no enabled link has been found.
400  */
401 struct media_pad *vsp1_entity_remote_pad(struct media_pad *pad)
402 {
403         struct media_link *link;
404
405         list_for_each_entry(link, &pad->entity->links, list) {
406                 struct vsp1_entity *entity;
407
408                 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
409                         continue;
410
411                 /* If we're the sink the source will never be an HGO or HGT. */
412                 if (link->sink == pad)
413                         return link->source;
414
415                 if (link->source != pad)
416                         continue;
417
418                 /* If the sink isn't a subdevice it can't be an HGO or HGT. */
419                 if (!is_media_entity_v4l2_subdev(link->sink->entity))
420                         return link->sink;
421
422                 entity = media_entity_to_vsp1_entity(link->sink->entity);
423                 if (entity->type != VSP1_ENTITY_HGO &&
424                     entity->type != VSP1_ENTITY_HGT)
425                         return link->sink;
426         }
427
428         return NULL;
429
430 }
431
432 /* -----------------------------------------------------------------------------
433  * Initialization
434  */
435
436 #define VSP1_ENTITY_ROUTE(ent)                                          \
437         { VSP1_ENTITY_##ent, 0, VI6_DPR_##ent##_ROUTE,                  \
438           { VI6_DPR_NODE_##ent }, VI6_DPR_NODE_##ent }
439
440 #define VSP1_ENTITY_ROUTE_RPF(idx)                                      \
441         { VSP1_ENTITY_RPF, idx, VI6_DPR_RPF_ROUTE(idx),                 \
442           { 0, }, VI6_DPR_NODE_RPF(idx) }
443
444 #define VSP1_ENTITY_ROUTE_UDS(idx)                                      \
445         { VSP1_ENTITY_UDS, idx, VI6_DPR_UDS_ROUTE(idx),                 \
446           { VI6_DPR_NODE_UDS(idx) }, VI6_DPR_NODE_UDS(idx) }
447
448 #define VSP1_ENTITY_ROUTE_WPF(idx)                                      \
449         { VSP1_ENTITY_WPF, idx, 0,                                      \
450           { VI6_DPR_NODE_WPF(idx) }, VI6_DPR_NODE_WPF(idx) }
451
452 static const struct vsp1_route vsp1_routes[] = {
453         { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
454           { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
455             VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3),
456             VI6_DPR_NODE_BRU_IN(4) }, VI6_DPR_NODE_BRU_OUT },
457         VSP1_ENTITY_ROUTE(CLU),
458         { VSP1_ENTITY_HGO, 0, 0, { 0, }, 0 },
459         { VSP1_ENTITY_HGT, 0, 0, { 0, }, 0 },
460         VSP1_ENTITY_ROUTE(HSI),
461         VSP1_ENTITY_ROUTE(HST),
462         { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, }, VI6_DPR_NODE_LIF },
463         VSP1_ENTITY_ROUTE(LUT),
464         VSP1_ENTITY_ROUTE_RPF(0),
465         VSP1_ENTITY_ROUTE_RPF(1),
466         VSP1_ENTITY_ROUTE_RPF(2),
467         VSP1_ENTITY_ROUTE_RPF(3),
468         VSP1_ENTITY_ROUTE_RPF(4),
469         VSP1_ENTITY_ROUTE(SRU),
470         VSP1_ENTITY_ROUTE_UDS(0),
471         VSP1_ENTITY_ROUTE_UDS(1),
472         VSP1_ENTITY_ROUTE_UDS(2),
473         VSP1_ENTITY_ROUTE_WPF(0),
474         VSP1_ENTITY_ROUTE_WPF(1),
475         VSP1_ENTITY_ROUTE_WPF(2),
476         VSP1_ENTITY_ROUTE_WPF(3),
477 };
478
479 int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
480                      const char *name, unsigned int num_pads,
481                      const struct v4l2_subdev_ops *ops, u32 function)
482 {
483         struct v4l2_subdev *subdev;
484         unsigned int i;
485         int ret;
486
487         for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
488                 if (vsp1_routes[i].type == entity->type &&
489                     vsp1_routes[i].index == entity->index) {
490                         entity->route = &vsp1_routes[i];
491                         break;
492                 }
493         }
494
495         if (i == ARRAY_SIZE(vsp1_routes))
496                 return -EINVAL;
497
498         mutex_init(&entity->lock);
499
500         entity->vsp1 = vsp1;
501         entity->source_pad = num_pads - 1;
502
503         /* Allocate and initialize pads. */
504         entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads),
505                                     GFP_KERNEL);
506         if (entity->pads == NULL)
507                 return -ENOMEM;
508
509         for (i = 0; i < num_pads - 1; ++i)
510                 entity->pads[i].flags = MEDIA_PAD_FL_SINK;
511
512         entity->sources = devm_kcalloc(vsp1->dev, max(num_pads - 1, 1U),
513                                        sizeof(*entity->sources), GFP_KERNEL);
514         if (entity->sources == NULL)
515                 return -ENOMEM;
516
517         /* Single-pad entities only have a sink. */
518         entity->pads[num_pads - 1].flags = num_pads > 1 ? MEDIA_PAD_FL_SOURCE
519                                          : MEDIA_PAD_FL_SINK;
520
521         /* Initialize the media entity. */
522         ret = media_entity_pads_init(&entity->subdev.entity, num_pads,
523                                      entity->pads);
524         if (ret < 0)
525                 return ret;
526
527         /* Initialize the V4L2 subdev. */
528         subdev = &entity->subdev;
529         v4l2_subdev_init(subdev, ops);
530
531         subdev->entity.function = function;
532         subdev->entity.ops = &vsp1->media_ops;
533         subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
534
535         snprintf(subdev->name, sizeof(subdev->name), "%s %s",
536                  dev_name(vsp1->dev), name);
537
538         vsp1_entity_init_cfg(subdev, NULL);
539
540         /*
541          * Allocate the pad configuration to store formats and selection
542          * rectangles.
543          */
544         entity->config = v4l2_subdev_alloc_pad_config(&entity->subdev);
545         if (entity->config == NULL) {
546                 media_entity_cleanup(&entity->subdev.entity);
547                 return -ENOMEM;
548         }
549
550         return 0;
551 }
552
553 void vsp1_entity_destroy(struct vsp1_entity *entity)
554 {
555         if (entity->ops && entity->ops->destroy)
556                 entity->ops->destroy(entity);
557         if (entity->subdev.ctrl_handler)
558                 v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
559         v4l2_subdev_free_pad_config(entity->config);
560         media_entity_cleanup(&entity->subdev.entity);
561 }
This page took 0.064696 seconds and 4 git commands to generate.