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