]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/platform/vsp1/vsp1_entity.c
[media] v4l: vsp1: Implement and use the subdev pad::init_cfg configuration
[karo-tx-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 (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
26 void vsp1_mod_write(struct vsp1_entity *e, u32 reg, u32 data)
27 {
28         struct vsp1_pipeline *pipe = to_vsp1_pipeline(&e->subdev.entity);
29
30         vsp1_dl_list_write(pipe->dl, reg, data);
31 }
32
33 void 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);
41         vsp1_mod_write(source, source->route->reg,
42                        sink->route->inputs[source->sink_pad]);
43 }
44
45 /* -----------------------------------------------------------------------------
46  * V4L2 Subdevice Operations
47  */
48
49 struct v4l2_mbus_framefmt *
50 vsp1_entity_get_pad_format(struct vsp1_entity *entity,
51                            struct v4l2_subdev_pad_config *cfg,
52                            unsigned int pad, u32 which)
53 {
54         switch (which) {
55         case V4L2_SUBDEV_FORMAT_TRY:
56                 return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad);
57         case V4L2_SUBDEV_FORMAT_ACTIVE:
58                 return &entity->formats[pad];
59         default:
60                 return NULL;
61         }
62 }
63
64 /*
65  * vsp1_entity_init_cfg - Initialize formats on all pads
66  * @subdev: V4L2 subdevice
67  * @cfg: V4L2 subdev pad configuration
68  *
69  * Initialize all pad formats with default values in the given pad config. This
70  * function can be used as a handler for the subdev pad::init_cfg operation.
71  */
72 int vsp1_entity_init_cfg(struct v4l2_subdev *subdev,
73                          struct v4l2_subdev_pad_config *cfg)
74 {
75         struct v4l2_subdev_format format;
76         unsigned int pad;
77
78         for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
79                 memset(&format, 0, sizeof(format));
80
81                 format.pad = pad;
82                 format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY
83                              : V4L2_SUBDEV_FORMAT_ACTIVE;
84
85                 v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format);
86         }
87
88         return 0;
89 }
90
91 /* -----------------------------------------------------------------------------
92  * Media Operations
93  */
94
95 int vsp1_entity_link_setup(struct media_entity *entity,
96                            const struct media_pad *local,
97                            const struct media_pad *remote, u32 flags)
98 {
99         struct vsp1_entity *source;
100
101         if (!(local->flags & MEDIA_PAD_FL_SOURCE))
102                 return 0;
103
104         source = container_of(local->entity, struct vsp1_entity, subdev.entity);
105
106         if (!source->route)
107                 return 0;
108
109         if (flags & MEDIA_LNK_FL_ENABLED) {
110                 if (source->sink)
111                         return -EBUSY;
112                 source->sink = remote->entity;
113                 source->sink_pad = remote->index;
114         } else {
115                 source->sink = NULL;
116                 source->sink_pad = 0;
117         }
118
119         return 0;
120 }
121
122 /* -----------------------------------------------------------------------------
123  * Initialization
124  */
125
126 static const struct vsp1_route vsp1_routes[] = {
127         { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
128           { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
129             VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3),
130             VI6_DPR_NODE_BRU_IN(4) } },
131         { VSP1_ENTITY_HSI, 0, VI6_DPR_HSI_ROUTE, { VI6_DPR_NODE_HSI, } },
132         { VSP1_ENTITY_HST, 0, VI6_DPR_HST_ROUTE, { VI6_DPR_NODE_HST, } },
133         { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, } },
134         { VSP1_ENTITY_LUT, 0, VI6_DPR_LUT_ROUTE, { VI6_DPR_NODE_LUT, } },
135         { VSP1_ENTITY_RPF, 0, VI6_DPR_RPF_ROUTE(0), { VI6_DPR_NODE_RPF(0), } },
136         { VSP1_ENTITY_RPF, 1, VI6_DPR_RPF_ROUTE(1), { VI6_DPR_NODE_RPF(1), } },
137         { VSP1_ENTITY_RPF, 2, VI6_DPR_RPF_ROUTE(2), { VI6_DPR_NODE_RPF(2), } },
138         { VSP1_ENTITY_RPF, 3, VI6_DPR_RPF_ROUTE(3), { VI6_DPR_NODE_RPF(3), } },
139         { VSP1_ENTITY_RPF, 4, VI6_DPR_RPF_ROUTE(4), { VI6_DPR_NODE_RPF(4), } },
140         { VSP1_ENTITY_SRU, 0, VI6_DPR_SRU_ROUTE, { VI6_DPR_NODE_SRU, } },
141         { VSP1_ENTITY_UDS, 0, VI6_DPR_UDS_ROUTE(0), { VI6_DPR_NODE_UDS(0), } },
142         { VSP1_ENTITY_UDS, 1, VI6_DPR_UDS_ROUTE(1), { VI6_DPR_NODE_UDS(1), } },
143         { VSP1_ENTITY_UDS, 2, VI6_DPR_UDS_ROUTE(2), { VI6_DPR_NODE_UDS(2), } },
144         { VSP1_ENTITY_WPF, 0, 0, { VI6_DPR_NODE_WPF(0), } },
145         { VSP1_ENTITY_WPF, 1, 0, { VI6_DPR_NODE_WPF(1), } },
146         { VSP1_ENTITY_WPF, 2, 0, { VI6_DPR_NODE_WPF(2), } },
147         { VSP1_ENTITY_WPF, 3, 0, { VI6_DPR_NODE_WPF(3), } },
148 };
149
150 int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
151                      const char *name, unsigned int num_pads,
152                      const struct v4l2_subdev_ops *ops)
153 {
154         struct v4l2_subdev *subdev;
155         unsigned int i;
156         int ret;
157
158         for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
159                 if (vsp1_routes[i].type == entity->type &&
160                     vsp1_routes[i].index == entity->index) {
161                         entity->route = &vsp1_routes[i];
162                         break;
163                 }
164         }
165
166         if (i == ARRAY_SIZE(vsp1_routes))
167                 return -EINVAL;
168
169         entity->vsp1 = vsp1;
170         entity->source_pad = num_pads - 1;
171
172         /* Allocate formats and pads. */
173         entity->formats = devm_kzalloc(vsp1->dev,
174                                        num_pads * sizeof(*entity->formats),
175                                        GFP_KERNEL);
176         if (entity->formats == NULL)
177                 return -ENOMEM;
178
179         entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads),
180                                     GFP_KERNEL);
181         if (entity->pads == NULL)
182                 return -ENOMEM;
183
184         /* Initialize pads. */
185         for (i = 0; i < num_pads - 1; ++i)
186                 entity->pads[i].flags = MEDIA_PAD_FL_SINK;
187
188         entity->pads[num_pads - 1].flags = MEDIA_PAD_FL_SOURCE;
189
190         /* Initialize the media entity. */
191         ret = media_entity_pads_init(&entity->subdev.entity, num_pads,
192                                      entity->pads);
193         if (ret < 0)
194                 return ret;
195
196         /* Initialize the V4L2 subdev. */
197         subdev = &entity->subdev;
198         v4l2_subdev_init(subdev, ops);
199
200         subdev->entity.ops = &vsp1->media_ops;
201         subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
202
203         snprintf(subdev->name, sizeof(subdev->name), "%s %s",
204                  dev_name(vsp1->dev), name);
205
206         vsp1_entity_init_cfg(subdev, NULL);
207
208         return 0;
209 }
210
211 void vsp1_entity_destroy(struct vsp1_entity *entity)
212 {
213         if (entity->ops && entity->ops->destroy)
214                 entity->ops->destroy(entity);
215         if (entity->subdev.ctrl_handler)
216                 v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
217         media_entity_cleanup(&entity->subdev.entity);
218 }