[media] v4l: vsp1: Add HST and HSI support
[deliverable/linux.git] / drivers / media / platform / vsp1 / vsp1_entity.c
CommitLineData
26e0ca22
LP
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
27struct v4l2_mbus_framefmt *
28vsp1_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 */
51void 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
68static 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
76const struct v4l2_subdev_internal_ops vsp1_subdev_internal_ops = {
77 .open = vsp1_entity_open,
78};
79
80/* -----------------------------------------------------------------------------
81 * Media Operations
82 */
83
84static 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
109const 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
118int 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[] = {
5cdf5741
LP
125 { VI6_DPR_NODE_HSI, VI6_DPR_HSI_ROUTE },
126 { VI6_DPR_NODE_HST, VI6_DPR_HST_ROUTE },
26e0ca22
LP
127 { VI6_DPR_NODE_LIF, 0 },
128 { VI6_DPR_NODE_RPF(0), VI6_DPR_RPF_ROUTE(0) },
129 { VI6_DPR_NODE_RPF(1), VI6_DPR_RPF_ROUTE(1) },
130 { VI6_DPR_NODE_RPF(2), VI6_DPR_RPF_ROUTE(2) },
131 { VI6_DPR_NODE_RPF(3), VI6_DPR_RPF_ROUTE(3) },
132 { VI6_DPR_NODE_RPF(4), VI6_DPR_RPF_ROUTE(4) },
133 { VI6_DPR_NODE_UDS(0), VI6_DPR_UDS_ROUTE(0) },
134 { VI6_DPR_NODE_UDS(1), VI6_DPR_UDS_ROUTE(1) },
135 { VI6_DPR_NODE_UDS(2), VI6_DPR_UDS_ROUTE(2) },
136 { VI6_DPR_NODE_WPF(0), 0 },
137 { VI6_DPR_NODE_WPF(1), 0 },
138 { VI6_DPR_NODE_WPF(2), 0 },
139 { VI6_DPR_NODE_WPF(3), 0 },
140 };
141
142 unsigned int i;
143
144 for (i = 0; i < ARRAY_SIZE(routes); ++i) {
145 if (routes[i].id == entity->id) {
146 entity->route = routes[i].reg;
147 break;
148 }
149 }
150
151 if (i == ARRAY_SIZE(routes))
152 return -EINVAL;
153
154 entity->vsp1 = vsp1;
155 entity->source_pad = num_pads - 1;
156
157 /* Allocate formats and pads. */
158 entity->formats = devm_kzalloc(vsp1->dev,
159 num_pads * sizeof(*entity->formats),
160 GFP_KERNEL);
161 if (entity->formats == NULL)
162 return -ENOMEM;
163
164 entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads),
165 GFP_KERNEL);
166 if (entity->pads == NULL)
167 return -ENOMEM;
168
169 /* Initialize pads. */
170 for (i = 0; i < num_pads - 1; ++i)
171 entity->pads[i].flags = MEDIA_PAD_FL_SINK;
172
173 entity->pads[num_pads - 1].flags = MEDIA_PAD_FL_SOURCE;
174
175 /* Initialize the media entity. */
176 return media_entity_init(&entity->subdev.entity, num_pads,
177 entity->pads, 0);
178}
179
180void vsp1_entity_destroy(struct vsp1_entity *entity)
181{
182 media_entity_cleanup(&entity->subdev.entity);
183}
This page took 0.044606 seconds and 5 git commands to generate.