Commit | Line | Data |
---|---|---|
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 |
30 | static inline void vsp1_lut_write(struct vsp1_lut *lut, u32 reg, u32 data) |
31 | { | |
32 | vsp1_write(lut->entity.vsp1, reg, data); | |
33 | } | |
34 | ||
35 | /* ----------------------------------------------------------------------------- | |
36 | * V4L2 Subdevice Core Operations | |
37 | */ | |
38 | ||
39 | static 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 | ||
45 | static 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 | ||
63 | static 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 | ||
79 | static 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 | 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 | ||
3f1ccf16 HV |
103 | format = vsp1_entity_get_pad_format(&lut->entity, cfg, |
104 | LUT_PAD_SINK, code->which); | |
989af883 LP |
105 | code->code = format->code; |
106 | } | |
107 | ||
108 | return 0; | |
109 | } | |
110 | ||
111 | static int lut_enum_frame_size(struct v4l2_subdev *subdev, | |
f7234138 | 112 | struct v4l2_subdev_pad_config *cfg, |
989af883 LP |
113 | struct v4l2_subdev_frame_size_enum *fse) |
114 | { | |
5778e749 | 115 | struct vsp1_lut *lut = to_lut(subdev); |
989af883 LP |
116 | struct v4l2_mbus_framefmt *format; |
117 | ||
5778e749 HV |
118 | format = vsp1_entity_get_pad_format(&lut->entity, cfg, |
119 | fse->pad, fse->which); | |
989af883 LP |
120 | |
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 | ||
f7234138 | 142 | static int lut_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg, |
989af883 LP |
143 | struct v4l2_subdev_format *fmt) |
144 | { | |
145 | struct vsp1_lut *lut = to_lut(subdev); | |
146 | ||
f7234138 | 147 | fmt->format = *vsp1_entity_get_pad_format(&lut->entity, cfg, fmt->pad, |
989af883 LP |
148 | fmt->which); |
149 | ||
150 | return 0; | |
151 | } | |
152 | ||
f7234138 | 153 | static int lut_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg, |
989af883 LP |
154 | struct v4l2_subdev_format *fmt) |
155 | { | |
156 | struct vsp1_lut *lut = to_lut(subdev); | |
157 | struct v4l2_mbus_framefmt *format; | |
158 | ||
159 | /* Default to YUV if the requested format is not supported. */ | |
27ffaeb0 BB |
160 | if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 && |
161 | fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 && | |
162 | fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32) | |
163 | fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32; | |
989af883 | 164 | |
f7234138 | 165 | format = vsp1_entity_get_pad_format(&lut->entity, cfg, fmt->pad, |
989af883 LP |
166 | fmt->which); |
167 | ||
168 | if (fmt->pad == LUT_PAD_SOURCE) { | |
169 | /* The LUT output format can't be modified. */ | |
170 | fmt->format = *format; | |
171 | return 0; | |
172 | } | |
173 | ||
174 | format->width = clamp_t(unsigned int, fmt->format.width, | |
175 | LUT_MIN_SIZE, LUT_MAX_SIZE); | |
176 | format->height = clamp_t(unsigned int, fmt->format.height, | |
177 | LUT_MIN_SIZE, LUT_MAX_SIZE); | |
178 | format->field = V4L2_FIELD_NONE; | |
179 | format->colorspace = V4L2_COLORSPACE_SRGB; | |
180 | ||
181 | fmt->format = *format; | |
182 | ||
183 | /* Propagate the format to the source pad. */ | |
f7234138 | 184 | format = vsp1_entity_get_pad_format(&lut->entity, cfg, LUT_PAD_SOURCE, |
989af883 LP |
185 | fmt->which); |
186 | *format = fmt->format; | |
187 | ||
188 | return 0; | |
189 | } | |
190 | ||
191 | /* ----------------------------------------------------------------------------- | |
192 | * V4L2 Subdevice Operations | |
193 | */ | |
194 | ||
195 | static struct v4l2_subdev_core_ops lut_core_ops = { | |
196 | .ioctl = lut_ioctl, | |
197 | }; | |
198 | ||
199 | static struct v4l2_subdev_video_ops lut_video_ops = { | |
200 | .s_stream = lut_s_stream, | |
201 | }; | |
202 | ||
203 | static struct v4l2_subdev_pad_ops lut_pad_ops = { | |
204 | .enum_mbus_code = lut_enum_mbus_code, | |
205 | .enum_frame_size = lut_enum_frame_size, | |
206 | .get_fmt = lut_get_format, | |
207 | .set_fmt = lut_set_format, | |
208 | }; | |
209 | ||
210 | static struct v4l2_subdev_ops lut_ops = { | |
211 | .core = &lut_core_ops, | |
212 | .video = &lut_video_ops, | |
213 | .pad = &lut_pad_ops, | |
214 | }; | |
215 | ||
216 | /* ----------------------------------------------------------------------------- | |
217 | * Initialization and Cleanup | |
218 | */ | |
219 | ||
220 | struct vsp1_lut *vsp1_lut_create(struct vsp1_device *vsp1) | |
221 | { | |
222 | struct v4l2_subdev *subdev; | |
223 | struct vsp1_lut *lut; | |
224 | int ret; | |
225 | ||
226 | lut = devm_kzalloc(vsp1->dev, sizeof(*lut), GFP_KERNEL); | |
227 | if (lut == NULL) | |
228 | return ERR_PTR(-ENOMEM); | |
229 | ||
230 | lut->entity.type = VSP1_ENTITY_LUT; | |
989af883 LP |
231 | |
232 | ret = vsp1_entity_init(vsp1, &lut->entity, 2); | |
233 | if (ret < 0) | |
234 | return ERR_PTR(ret); | |
235 | ||
236 | /* Initialize the V4L2 subdev. */ | |
237 | subdev = &lut->entity.subdev; | |
238 | v4l2_subdev_init(subdev, &lut_ops); | |
239 | ||
babca007 | 240 | subdev->entity.ops = &vsp1->media_ops; |
989af883 LP |
241 | subdev->internal_ops = &vsp1_subdev_internal_ops; |
242 | snprintf(subdev->name, sizeof(subdev->name), "%s lut", | |
243 | dev_name(vsp1->dev)); | |
244 | v4l2_set_subdevdata(subdev, lut); | |
245 | subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; | |
246 | ||
247 | vsp1_entity_init_formats(subdev, NULL); | |
248 | ||
249 | return lut; | |
250 | } |