blob: f0afc4291387943c41f5e9a0d52e0317b5d8b812 [file] [log] [blame]
Laurent Pinchart989af882013-07-10 12:03:30 -03001/*
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
Laurent Pinchart989af882013-07-10 12:03:30 -030030static inline void vsp1_lut_write(struct vsp1_lut *lut, u32 reg, u32 data)
31{
Laurent Pinchart773abaf2015-11-01 12:26:14 -020032 vsp1_mod_write(&lut->entity, reg, data);
Laurent Pinchart989af882013-07-10 12:03:30 -030033}
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,
Hans Verkuilf7234132015-03-04 01:47:54 -080080 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart989af882013-07-10 12:03:30 -030081 struct v4l2_subdev_mbus_code_enum *code)
82{
83 static const unsigned int codes[] = {
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030084 MEDIA_BUS_FMT_ARGB8888_1X32,
85 MEDIA_BUS_FMT_AHSV8888_1X32,
86 MEDIA_BUS_FMT_AYUV8_1X32,
Laurent Pinchart989af882013-07-10 12:03:30 -030087 };
Hans Verkuil3f1ccf12015-03-04 01:47:57 -080088 struct vsp1_lut *lut = to_lut(subdev);
Laurent Pinchart989af882013-07-10 12:03:30 -030089 struct v4l2_mbus_framefmt *format;
90
91 if (code->pad == LUT_PAD_SINK) {
92 if (code->index >= ARRAY_SIZE(codes))
93 return -EINVAL;
94
95 code->code = codes[code->index];
96 } else {
97 /* The LUT can't perform format conversion, the sink format is
98 * always identical to the source format.
99 */
100 if (code->index)
101 return -EINVAL;
102
Hans Verkuil3f1ccf12015-03-04 01:47:57 -0800103 format = vsp1_entity_get_pad_format(&lut->entity, cfg,
104 LUT_PAD_SINK, code->which);
Laurent Pinchart989af882013-07-10 12:03:30 -0300105 code->code = format->code;
106 }
107
108 return 0;
109}
110
111static int lut_enum_frame_size(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -0800112 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart989af882013-07-10 12:03:30 -0300113 struct v4l2_subdev_frame_size_enum *fse)
114{
Hans Verkuil5778e742015-03-04 01:47:58 -0800115 struct vsp1_lut *lut = to_lut(subdev);
Laurent Pinchart989af882013-07-10 12:03:30 -0300116 struct v4l2_mbus_framefmt *format;
117
Hans Verkuil5778e742015-03-04 01:47:58 -0800118 format = vsp1_entity_get_pad_format(&lut->entity, cfg,
119 fse->pad, fse->which);
Laurent Pinchart989af882013-07-10 12:03:30 -0300120
121 if (fse->index || fse->code != format->code)
122 return -EINVAL;
123
124 if (fse->pad == LUT_PAD_SINK) {
125 fse->min_width = LUT_MIN_SIZE;
126 fse->max_width = LUT_MAX_SIZE;
127 fse->min_height = LUT_MIN_SIZE;
128 fse->max_height = LUT_MAX_SIZE;
129 } else {
130 /* The size on the source pad are fixed and always identical to
131 * the size on the sink pad.
132 */
133 fse->min_width = format->width;
134 fse->max_width = format->width;
135 fse->min_height = format->height;
136 fse->max_height = format->height;
137 }
138
139 return 0;
140}
141
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -0200142static int lut_get_format(struct v4l2_subdev *subdev,
143 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart989af882013-07-10 12:03:30 -0300144 struct v4l2_subdev_format *fmt)
145{
146 struct vsp1_lut *lut = to_lut(subdev);
147
Hans Verkuilf7234132015-03-04 01:47:54 -0800148 fmt->format = *vsp1_entity_get_pad_format(&lut->entity, cfg, fmt->pad,
Laurent Pinchart989af882013-07-10 12:03:30 -0300149 fmt->which);
150
151 return 0;
152}
153
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -0200154static int lut_set_format(struct v4l2_subdev *subdev,
155 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart989af882013-07-10 12:03:30 -0300156 struct v4l2_subdev_format *fmt)
157{
158 struct vsp1_lut *lut = to_lut(subdev);
159 struct v4l2_mbus_framefmt *format;
160
161 /* Default to YUV if the requested format is not supported. */
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300162 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
163 fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 &&
164 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
165 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
Laurent Pinchart989af882013-07-10 12:03:30 -0300166
Hans Verkuilf7234132015-03-04 01:47:54 -0800167 format = vsp1_entity_get_pad_format(&lut->entity, cfg, fmt->pad,
Laurent Pinchart989af882013-07-10 12:03:30 -0300168 fmt->which);
169
170 if (fmt->pad == LUT_PAD_SOURCE) {
171 /* The LUT output format can't be modified. */
172 fmt->format = *format;
173 return 0;
174 }
175
176 format->width = clamp_t(unsigned int, fmt->format.width,
177 LUT_MIN_SIZE, LUT_MAX_SIZE);
178 format->height = clamp_t(unsigned int, fmt->format.height,
179 LUT_MIN_SIZE, LUT_MAX_SIZE);
180 format->field = V4L2_FIELD_NONE;
181 format->colorspace = V4L2_COLORSPACE_SRGB;
182
183 fmt->format = *format;
184
185 /* Propagate the format to the source pad. */
Hans Verkuilf7234132015-03-04 01:47:54 -0800186 format = vsp1_entity_get_pad_format(&lut->entity, cfg, LUT_PAD_SOURCE,
Laurent Pinchart989af882013-07-10 12:03:30 -0300187 fmt->which);
188 *format = fmt->format;
189
190 return 0;
191}
192
193/* -----------------------------------------------------------------------------
194 * V4L2 Subdevice Operations
195 */
196
197static struct v4l2_subdev_core_ops lut_core_ops = {
198 .ioctl = lut_ioctl,
199};
200
201static struct v4l2_subdev_video_ops lut_video_ops = {
202 .s_stream = lut_s_stream,
203};
204
205static struct v4l2_subdev_pad_ops lut_pad_ops = {
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200206 .init_cfg = vsp1_entity_init_cfg,
Laurent Pinchart989af882013-07-10 12:03:30 -0300207 .enum_mbus_code = lut_enum_mbus_code,
208 .enum_frame_size = lut_enum_frame_size,
209 .get_fmt = lut_get_format,
210 .set_fmt = lut_set_format,
211};
212
213static struct v4l2_subdev_ops lut_ops = {
214 .core = &lut_core_ops,
215 .video = &lut_video_ops,
216 .pad = &lut_pad_ops,
217};
218
219/* -----------------------------------------------------------------------------
220 * Initialization and Cleanup
221 */
222
223struct vsp1_lut *vsp1_lut_create(struct vsp1_device *vsp1)
224{
Laurent Pinchart989af882013-07-10 12:03:30 -0300225 struct vsp1_lut *lut;
226 int ret;
227
228 lut = devm_kzalloc(vsp1->dev, sizeof(*lut), GFP_KERNEL);
229 if (lut == NULL)
230 return ERR_PTR(-ENOMEM);
231
232 lut->entity.type = VSP1_ENTITY_LUT;
Laurent Pinchart989af882013-07-10 12:03:30 -0300233
Laurent Pinchart823329d2015-11-15 19:42:01 -0200234 ret = vsp1_entity_init(vsp1, &lut->entity, "lut", 2, &lut_ops);
Laurent Pinchart989af882013-07-10 12:03:30 -0300235 if (ret < 0)
236 return ERR_PTR(ret);
237
Laurent Pinchart989af882013-07-10 12:03:30 -0300238 return lut;
239}