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