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