2 * vsp1_lut.c -- R-Car VSP1 Look-Up Table
4 * Copyright (C) 2013 Renesas Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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.
14 #include <linux/device.h>
15 #include <linux/gfp.h>
16 #include <linux/vsp1.h>
18 #include <media/v4l2-subdev.h>
23 #define LUT_MIN_SIZE 4U
24 #define LUT_MAX_SIZE 8190U
26 /* -----------------------------------------------------------------------------
30 static inline u32 vsp1_lut_read(struct vsp1_lut *lut, u32 reg)
32 return vsp1_read(lut->entity.vsp1, reg);
35 static inline void vsp1_lut_write(struct vsp1_lut *lut, u32 reg, u32 data)
37 vsp1_write(lut->entity.vsp1, reg, data);
40 /* -----------------------------------------------------------------------------
41 * V4L2 Subdevice Core Operations
44 static void lut_configure(struct vsp1_lut *lut, struct vsp1_lut_config *config)
46 memcpy_toio(lut->entity.vsp1->mmio + VI6_LUT_TABLE, config->lut,
50 static long lut_ioctl(struct v4l2_subdev *subdev, unsigned int cmd, void *arg)
52 struct vsp1_lut *lut = to_lut(subdev);
55 case VIDIOC_VSP1_LUT_CONFIG:
56 lut_configure(lut, arg);
64 /* -----------------------------------------------------------------------------
65 * V4L2 Subdevice Video Operations
68 static int lut_s_stream(struct v4l2_subdev *subdev, int enable)
70 struct vsp1_lut *lut = to_lut(subdev);
75 vsp1_lut_write(lut, VI6_LUT_CTRL, VI6_LUT_CTRL_EN);
80 /* -----------------------------------------------------------------------------
81 * V4L2 Subdevice Pad Operations
84 static int lut_enum_mbus_code(struct v4l2_subdev *subdev,
85 struct v4l2_subdev_pad_config *cfg,
86 struct v4l2_subdev_mbus_code_enum *code)
88 static const unsigned int codes[] = {
89 MEDIA_BUS_FMT_ARGB8888_1X32,
90 MEDIA_BUS_FMT_AHSV8888_1X32,
91 MEDIA_BUS_FMT_AYUV8_1X32,
93 struct vsp1_lut *lut = to_lut(subdev);
94 struct v4l2_mbus_framefmt *format;
96 if (code->pad == LUT_PAD_SINK) {
97 if (code->index >= ARRAY_SIZE(codes))
100 code->code = codes[code->index];
102 /* The LUT can't perform format conversion, the sink format is
103 * always identical to the source format.
108 format = vsp1_entity_get_pad_format(&lut->entity, cfg,
109 LUT_PAD_SINK, code->which);
110 code->code = format->code;
116 static int lut_enum_frame_size(struct v4l2_subdev *subdev,
117 struct v4l2_subdev_pad_config *cfg,
118 struct v4l2_subdev_frame_size_enum *fse)
120 struct vsp1_lut *lut = to_lut(subdev);
121 struct v4l2_mbus_framefmt *format;
123 format = vsp1_entity_get_pad_format(&lut->entity, cfg,
124 fse->pad, fse->which);
126 if (fse->index || fse->code != format->code)
129 if (fse->pad == LUT_PAD_SINK) {
130 fse->min_width = LUT_MIN_SIZE;
131 fse->max_width = LUT_MAX_SIZE;
132 fse->min_height = LUT_MIN_SIZE;
133 fse->max_height = LUT_MAX_SIZE;
135 /* The size on the source pad are fixed and always identical to
136 * the size on the sink pad.
138 fse->min_width = format->width;
139 fse->max_width = format->width;
140 fse->min_height = format->height;
141 fse->max_height = format->height;
147 static int lut_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
148 struct v4l2_subdev_format *fmt)
150 struct vsp1_lut *lut = to_lut(subdev);
152 fmt->format = *vsp1_entity_get_pad_format(&lut->entity, cfg, fmt->pad,
158 static int lut_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
159 struct v4l2_subdev_format *fmt)
161 struct vsp1_lut *lut = to_lut(subdev);
162 struct v4l2_mbus_framefmt *format;
164 /* Default to YUV if the requested format is not supported. */
165 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
166 fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 &&
167 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
168 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
170 format = vsp1_entity_get_pad_format(&lut->entity, cfg, fmt->pad,
173 if (fmt->pad == LUT_PAD_SOURCE) {
174 /* The LUT output format can't be modified. */
175 fmt->format = *format;
179 format->width = clamp_t(unsigned int, fmt->format.width,
180 LUT_MIN_SIZE, LUT_MAX_SIZE);
181 format->height = clamp_t(unsigned int, fmt->format.height,
182 LUT_MIN_SIZE, LUT_MAX_SIZE);
183 format->field = V4L2_FIELD_NONE;
184 format->colorspace = V4L2_COLORSPACE_SRGB;
186 fmt->format = *format;
188 /* Propagate the format to the source pad. */
189 format = vsp1_entity_get_pad_format(&lut->entity, cfg, LUT_PAD_SOURCE,
191 *format = fmt->format;
196 /* -----------------------------------------------------------------------------
197 * V4L2 Subdevice Operations
200 static struct v4l2_subdev_core_ops lut_core_ops = {
204 static struct v4l2_subdev_video_ops lut_video_ops = {
205 .s_stream = lut_s_stream,
208 static struct v4l2_subdev_pad_ops lut_pad_ops = {
209 .enum_mbus_code = lut_enum_mbus_code,
210 .enum_frame_size = lut_enum_frame_size,
211 .get_fmt = lut_get_format,
212 .set_fmt = lut_set_format,
215 static struct v4l2_subdev_ops lut_ops = {
216 .core = &lut_core_ops,
217 .video = &lut_video_ops,
221 /* -----------------------------------------------------------------------------
222 * Initialization and Cleanup
225 struct vsp1_lut *vsp1_lut_create(struct vsp1_device *vsp1)
227 struct v4l2_subdev *subdev;
228 struct vsp1_lut *lut;
231 lut = devm_kzalloc(vsp1->dev, sizeof(*lut), GFP_KERNEL);
233 return ERR_PTR(-ENOMEM);
235 lut->entity.type = VSP1_ENTITY_LUT;
237 ret = vsp1_entity_init(vsp1, &lut->entity, 2);
241 /* Initialize the V4L2 subdev. */
242 subdev = &lut->entity.subdev;
243 v4l2_subdev_init(subdev, &lut_ops);
245 subdev->entity.ops = &vsp1_media_ops;
246 subdev->internal_ops = &vsp1_subdev_internal_ops;
247 snprintf(subdev->name, sizeof(subdev->name), "%s lut",
248 dev_name(vsp1->dev));
249 v4l2_set_subdevdata(subdev, lut);
250 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
252 vsp1_entity_init_formats(subdev, NULL);