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