v4l: vsp1: Cleanup video nodes at removal time
[platform/adaptation/renesas_rcar/renesas_kernel.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_entity.h"
23 #include "vsp1_video.h"
24
25 /* -----------------------------------------------------------------------------
26  * V4L2 Subdevice Operations
27  */
28
29 struct v4l2_mbus_framefmt *
30 vsp1_entity_get_pad_format(struct vsp1_entity *entity,
31                            struct v4l2_subdev_fh *fh,
32                            unsigned int pad, u32 which)
33 {
34         switch (which) {
35         case V4L2_SUBDEV_FORMAT_TRY:
36                 return v4l2_subdev_get_try_format(fh, pad);
37         case V4L2_SUBDEV_FORMAT_ACTIVE:
38                 return &entity->formats[pad];
39         default:
40                 return NULL;
41         }
42 }
43
44 /*
45  * vsp1_entity_init_formats - Initialize formats on all pads
46  * @subdev: V4L2 subdevice
47  * @fh: V4L2 subdev file handle
48  *
49  * Initialize all pad formats with default values. If fh is not NULL, try
50  * formats are initialized on the file handle. Otherwise active formats are
51  * initialized on the device.
52  */
53 void vsp1_entity_init_formats(struct v4l2_subdev *subdev,
54                             struct v4l2_subdev_fh *fh)
55 {
56         struct v4l2_subdev_format format;
57         unsigned int pad;
58
59         for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
60                 memset(&format, 0, sizeof(format));
61
62                 format.pad = pad;
63                 format.which = fh ? V4L2_SUBDEV_FORMAT_TRY
64                              : V4L2_SUBDEV_FORMAT_ACTIVE;
65
66                 v4l2_subdev_call(subdev, pad, set_fmt, fh, &format);
67         }
68 }
69
70 static int vsp1_entity_open(struct v4l2_subdev *subdev,
71                             struct v4l2_subdev_fh *fh)
72 {
73         vsp1_entity_init_formats(subdev, fh);
74
75         return 0;
76 }
77
78 const struct v4l2_subdev_internal_ops vsp1_subdev_internal_ops = {
79         .open = vsp1_entity_open,
80 };
81
82 /* -----------------------------------------------------------------------------
83  * Media Operations
84  */
85
86 static int vsp1_entity_link_setup(struct media_entity *entity,
87                                   const struct media_pad *local,
88                                   const struct media_pad *remote, u32 flags)
89 {
90         struct vsp1_entity *source;
91
92         if (!(local->flags & MEDIA_PAD_FL_SOURCE))
93                 return 0;
94
95         source = container_of(local->entity, struct vsp1_entity, subdev.entity);
96
97         if (!source->route)
98                 return 0;
99
100         if (flags & MEDIA_LNK_FL_ENABLED) {
101                 if (source->sink)
102                         return -EBUSY;
103                 source->sink = remote->entity;
104                 source->sink_pad = remote->index;
105         } else {
106                 source->sink = NULL;
107                 source->sink_pad = 0;
108         }
109
110         return 0;
111 }
112
113 const struct media_entity_operations vsp1_media_ops = {
114         .link_setup = vsp1_entity_link_setup,
115         .link_validate = v4l2_subdev_link_validate,
116 };
117
118 /* -----------------------------------------------------------------------------
119  * Initialization
120  */
121
122 static const struct vsp1_route vsp1_routes[] = {
123         { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
124           { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
125             VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3), } },
126         { VSP1_ENTITY_HSI, 0, VI6_DPR_HSI_ROUTE, { VI6_DPR_NODE_HSI, } },
127         { VSP1_ENTITY_HST, 0, VI6_DPR_HST_ROUTE, { VI6_DPR_NODE_HST, } },
128         { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, } },
129         { VSP1_ENTITY_LUT, 0, VI6_DPR_LUT_ROUTE, { VI6_DPR_NODE_LUT, } },
130         { VSP1_ENTITY_RPF, 0, VI6_DPR_RPF_ROUTE(0), { VI6_DPR_NODE_RPF(0), } },
131         { VSP1_ENTITY_RPF, 1, VI6_DPR_RPF_ROUTE(1), { VI6_DPR_NODE_RPF(1), } },
132         { VSP1_ENTITY_RPF, 2, VI6_DPR_RPF_ROUTE(2), { VI6_DPR_NODE_RPF(2), } },
133         { VSP1_ENTITY_RPF, 3, VI6_DPR_RPF_ROUTE(3), { VI6_DPR_NODE_RPF(3), } },
134         { VSP1_ENTITY_RPF, 4, VI6_DPR_RPF_ROUTE(4), { VI6_DPR_NODE_RPF(4), } },
135         { VSP1_ENTITY_SRU, 0, VI6_DPR_SRU_ROUTE, { VI6_DPR_NODE_SRU, } },
136         { VSP1_ENTITY_UDS, 0, VI6_DPR_UDS_ROUTE(0), { VI6_DPR_NODE_UDS(0), } },
137         { VSP1_ENTITY_UDS, 1, VI6_DPR_UDS_ROUTE(1), { VI6_DPR_NODE_UDS(1), } },
138         { VSP1_ENTITY_UDS, 2, VI6_DPR_UDS_ROUTE(2), { VI6_DPR_NODE_UDS(2), } },
139         { VSP1_ENTITY_WPF, 0, 0, { VI6_DPR_NODE_WPF(0), } },
140         { VSP1_ENTITY_WPF, 1, 0, { VI6_DPR_NODE_WPF(1), } },
141         { VSP1_ENTITY_WPF, 2, 0, { VI6_DPR_NODE_WPF(2), } },
142         { VSP1_ENTITY_WPF, 3, 0, { VI6_DPR_NODE_WPF(3), } },
143 };
144
145 int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
146                      unsigned int num_pads)
147 {
148         unsigned int i;
149
150         for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
151                 if (vsp1_routes[i].type == entity->type &&
152                     vsp1_routes[i].index == entity->index) {
153                         entity->route = &vsp1_routes[i];
154                         break;
155                 }
156         }
157
158         if (i == ARRAY_SIZE(vsp1_routes))
159                 return -EINVAL;
160
161         entity->vsp1 = vsp1;
162         entity->source_pad = num_pads - 1;
163
164         /* Allocate formats and pads. */
165         entity->formats = devm_kzalloc(vsp1->dev,
166                                        num_pads * sizeof(*entity->formats),
167                                        GFP_KERNEL);
168         if (entity->formats == NULL)
169                 return -ENOMEM;
170
171         entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads),
172                                     GFP_KERNEL);
173         if (entity->pads == NULL)
174                 return -ENOMEM;
175
176         /* Initialize pads. */
177         for (i = 0; i < num_pads - 1; ++i)
178                 entity->pads[i].flags = MEDIA_PAD_FL_SINK;
179
180         entity->pads[num_pads - 1].flags = MEDIA_PAD_FL_SOURCE;
181
182         /* Initialize the media entity. */
183         return media_entity_init(&entity->subdev.entity, num_pads,
184                                  entity->pads, 0);
185 }
186
187 void vsp1_entity_destroy(struct vsp1_entity *entity)
188 {
189         if (entity->video)
190                 vsp1_video_cleanup(entity->video);
191         if (entity->subdev.ctrl_handler)
192                 v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
193         media_entity_cleanup(&entity->subdev.entity);
194 }