]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/media/platform/vsp1/vsp1_lut.c
[media] v4l: vsp1: Store active formats in a pad config structure
[mirror_ubuntu-zesty-kernel.git] / drivers / media / platform / vsp1 / vsp1_lut.c
CommitLineData
989af883
LP
1/*
2 * vsp1_lut.c -- R-Car VSP1 Look-Up Table
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#include <linux/vsp1.h>
17
18#include <media/v4l2-subdev.h>
19
20#include "vsp1.h"
21#include "vsp1_lut.h"
22
23#define LUT_MIN_SIZE 4U
24#define LUT_MAX_SIZE 8190U
25
26/* -----------------------------------------------------------------------------
27 * Device Access
28 */
29
989af883
LP
30static inline void vsp1_lut_write(struct vsp1_lut *lut, u32 reg, u32 data)
31{
773abafe 32 vsp1_mod_write(&lut->entity, reg, data);
989af883
LP
33}
34
35/* -----------------------------------------------------------------------------
36 * V4L2 Subdevice Core Operations
37 */
38
39static void lut_configure(struct vsp1_lut *lut, struct vsp1_lut_config *config)
40{
41 memcpy_toio(lut->entity.vsp1->mmio + VI6_LUT_TABLE, config->lut,
42 sizeof(config->lut));
43}
44
45static long lut_ioctl(struct v4l2_subdev *subdev, unsigned int cmd, void *arg)
46{
47 struct vsp1_lut *lut = to_lut(subdev);
48
49 switch (cmd) {
50 case VIDIOC_VSP1_LUT_CONFIG:
51 lut_configure(lut, arg);
52 return 0;
53
54 default:
55 return -ENOIOCTLCMD;
56 }
57}
58
59/* -----------------------------------------------------------------------------
60 * V4L2 Subdevice Video Operations
61 */
62
63static int lut_s_stream(struct v4l2_subdev *subdev, int enable)
64{
65 struct vsp1_lut *lut = to_lut(subdev);
66
67 if (!enable)
68 return 0;
69
70 vsp1_lut_write(lut, VI6_LUT_CTRL, VI6_LUT_CTRL_EN);
71
72 return 0;
73}
74
75/* -----------------------------------------------------------------------------
76 * V4L2 Subdevice Pad Operations
77 */
78
79static int lut_enum_mbus_code(struct v4l2_subdev *subdev,
f7234138 80 struct v4l2_subdev_pad_config *cfg,
989af883
LP
81 struct v4l2_subdev_mbus_code_enum *code)
82{
83 static const unsigned int codes[] = {
27ffaeb0
BB
84 MEDIA_BUS_FMT_ARGB8888_1X32,
85 MEDIA_BUS_FMT_AHSV8888_1X32,
86 MEDIA_BUS_FMT_AYUV8_1X32,
989af883 87 };
3f1ccf16 88 struct vsp1_lut *lut = to_lut(subdev);
989af883
LP
89
90 if (code->pad == LUT_PAD_SINK) {
91 if (code->index >= ARRAY_SIZE(codes))
92 return -EINVAL;
93
94 code->code = codes[code->index];
95 } else {
e790c3cb
LP
96 struct v4l2_subdev_pad_config *config;
97 struct v4l2_mbus_framefmt *format;
98
989af883
LP
99 /* The LUT can't perform format conversion, the sink format is
100 * always identical to the source format.
101 */
102 if (code->index)
103 return -EINVAL;
104
e790c3cb
LP
105 config = vsp1_entity_get_pad_config(&lut->entity, cfg,
106 code->which);
107 if (!config)
108 return -EINVAL;
109
110 format = vsp1_entity_get_pad_format(&lut->entity, config,
111 LUT_PAD_SINK);
989af883
LP
112 code->code = format->code;
113 }
114
115 return 0;
116}
117
118static int lut_enum_frame_size(struct v4l2_subdev *subdev,
f7234138 119 struct v4l2_subdev_pad_config *cfg,
989af883
LP
120 struct v4l2_subdev_frame_size_enum *fse)
121{
5778e749 122 struct vsp1_lut *lut = to_lut(subdev);
e790c3cb 123 struct v4l2_subdev_pad_config *config;
989af883
LP
124 struct v4l2_mbus_framefmt *format;
125
e790c3cb
LP
126 config = vsp1_entity_get_pad_config(&lut->entity, cfg, fse->which);
127 if (!config)
128 return -EINVAL;
129
130 format = vsp1_entity_get_pad_format(&lut->entity, config, fse->pad);
989af883
LP
131
132 if (fse->index || fse->code != format->code)
133 return -EINVAL;
134
135 if (fse->pad == LUT_PAD_SINK) {
136 fse->min_width = LUT_MIN_SIZE;
137 fse->max_width = LUT_MAX_SIZE;
138 fse->min_height = LUT_MIN_SIZE;
139 fse->max_height = LUT_MAX_SIZE;
140 } else {
141 /* The size on the source pad are fixed and always identical to
142 * the size on the sink pad.
143 */
144 fse->min_width = format->width;
145 fse->max_width = format->width;
146 fse->min_height = format->height;
147 fse->max_height = format->height;
148 }
149
150 return 0;
151}
152
1bd0a1bd
LP
153static int lut_get_format(struct v4l2_subdev *subdev,
154 struct v4l2_subdev_pad_config *cfg,
989af883
LP
155 struct v4l2_subdev_format *fmt)
156{
157 struct vsp1_lut *lut = to_lut(subdev);
e790c3cb
LP
158 struct v4l2_subdev_pad_config *config;
159
160 config = vsp1_entity_get_pad_config(&lut->entity, cfg, fmt->which);
161 if (!config)
162 return -EINVAL;
989af883 163
e790c3cb
LP
164 fmt->format = *vsp1_entity_get_pad_format(&lut->entity, config,
165 fmt->pad);
989af883
LP
166
167 return 0;
168}
169
1bd0a1bd
LP
170static int lut_set_format(struct v4l2_subdev *subdev,
171 struct v4l2_subdev_pad_config *cfg,
989af883
LP
172 struct v4l2_subdev_format *fmt)
173{
174 struct vsp1_lut *lut = to_lut(subdev);
e790c3cb 175 struct v4l2_subdev_pad_config *config;
989af883
LP
176 struct v4l2_mbus_framefmt *format;
177
e790c3cb
LP
178 config = vsp1_entity_get_pad_config(&lut->entity, cfg, fmt->which);
179 if (!config)
180 return -EINVAL;
181
989af883 182 /* Default to YUV if the requested format is not supported. */
27ffaeb0
BB
183 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
184 fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 &&
185 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
186 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
989af883 187
e790c3cb 188 format = vsp1_entity_get_pad_format(&lut->entity, config, fmt->pad);
989af883
LP
189
190 if (fmt->pad == LUT_PAD_SOURCE) {
191 /* The LUT output format can't be modified. */
192 fmt->format = *format;
193 return 0;
194 }
195
196 format->width = clamp_t(unsigned int, fmt->format.width,
197 LUT_MIN_SIZE, LUT_MAX_SIZE);
198 format->height = clamp_t(unsigned int, fmt->format.height,
199 LUT_MIN_SIZE, LUT_MAX_SIZE);
200 format->field = V4L2_FIELD_NONE;
201 format->colorspace = V4L2_COLORSPACE_SRGB;
202
203 fmt->format = *format;
204
205 /* Propagate the format to the source pad. */
e790c3cb
LP
206 format = vsp1_entity_get_pad_format(&lut->entity, config,
207 LUT_PAD_SOURCE);
989af883
LP
208 *format = fmt->format;
209
210 return 0;
211}
212
213/* -----------------------------------------------------------------------------
214 * V4L2 Subdevice Operations
215 */
216
217static struct v4l2_subdev_core_ops lut_core_ops = {
218 .ioctl = lut_ioctl,
219};
220
221static struct v4l2_subdev_video_ops lut_video_ops = {
222 .s_stream = lut_s_stream,
223};
224
225static struct v4l2_subdev_pad_ops lut_pad_ops = {
0efdf0f5 226 .init_cfg = vsp1_entity_init_cfg,
989af883
LP
227 .enum_mbus_code = lut_enum_mbus_code,
228 .enum_frame_size = lut_enum_frame_size,
229 .get_fmt = lut_get_format,
230 .set_fmt = lut_set_format,
231};
232
233static struct v4l2_subdev_ops lut_ops = {
234 .core = &lut_core_ops,
235 .video = &lut_video_ops,
236 .pad = &lut_pad_ops,
237};
238
239/* -----------------------------------------------------------------------------
240 * Initialization and Cleanup
241 */
242
243struct vsp1_lut *vsp1_lut_create(struct vsp1_device *vsp1)
244{
989af883
LP
245 struct vsp1_lut *lut;
246 int ret;
247
248 lut = devm_kzalloc(vsp1->dev, sizeof(*lut), GFP_KERNEL);
249 if (lut == NULL)
250 return ERR_PTR(-ENOMEM);
251
252 lut->entity.type = VSP1_ENTITY_LUT;
989af883 253
823329df 254 ret = vsp1_entity_init(vsp1, &lut->entity, "lut", 2, &lut_ops);
989af883
LP
255 if (ret < 0)
256 return ERR_PTR(ret);
257
989af883
LP
258 return lut;
259}