Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * vimc-sensor.c Virtual Media Controller Driver
4 *
5 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
6 */
7
8#include <linux/v4l2-mediabus.h>
9#include <linux/vmalloc.h>
10#include <media/v4l2-ctrls.h>
11#include <media/v4l2-event.h>
12#include <media/v4l2-subdev.h>
13#include <media/tpg/v4l2-tpg.h>
14
15#include "vimc-common.h"
16
17struct vimc_sen_device {
18 struct vimc_ent_device ved;
19 struct v4l2_subdev sd;
20 struct tpg_data tpg;
21 u8 *frame;
22 /* The active format */
23 struct v4l2_mbus_framefmt mbus_format;
24 struct v4l2_ctrl_handler hdl;
25 struct media_pad pad;
26};
27
28static const struct v4l2_mbus_framefmt fmt_default = {
29 .width = 640,
30 .height = 480,
31 .code = MEDIA_BUS_FMT_RGB888_1X24,
32 .field = V4L2_FIELD_NONE,
33 .colorspace = V4L2_COLORSPACE_SRGB,
34};
35
36static int vimc_sen_init_cfg(struct v4l2_subdev *sd,
37 struct v4l2_subdev_pad_config *cfg)
38{
39 unsigned int i;
40
41 for (i = 0; i < sd->entity.num_pads; i++) {
42 struct v4l2_mbus_framefmt *mf;
43
44 mf = v4l2_subdev_get_try_format(sd, cfg, i);
45 *mf = fmt_default;
46 }
47
48 return 0;
49}
50
51static int vimc_sen_enum_mbus_code(struct v4l2_subdev *sd,
52 struct v4l2_subdev_pad_config *cfg,
53 struct v4l2_subdev_mbus_code_enum *code)
54{
55 u32 mbus_code = vimc_mbus_code_by_index(code->index);
56
57 if (!mbus_code)
58 return -EINVAL;
59
60 code->code = mbus_code;
61
62 return 0;
63}
64
65static int vimc_sen_enum_frame_size(struct v4l2_subdev *sd,
66 struct v4l2_subdev_pad_config *cfg,
67 struct v4l2_subdev_frame_size_enum *fse)
68{
69 const struct vimc_pix_map *vpix;
70
71 if (fse->index)
72 return -EINVAL;
73
74 /* Only accept code in the pix map table */
75 vpix = vimc_pix_map_by_code(fse->code);
76 if (!vpix)
77 return -EINVAL;
78
79 fse->min_width = VIMC_FRAME_MIN_WIDTH;
80 fse->max_width = VIMC_FRAME_MAX_WIDTH;
81 fse->min_height = VIMC_FRAME_MIN_HEIGHT;
82 fse->max_height = VIMC_FRAME_MAX_HEIGHT;
83
84 return 0;
85}
86
87static int vimc_sen_get_fmt(struct v4l2_subdev *sd,
88 struct v4l2_subdev_pad_config *cfg,
89 struct v4l2_subdev_format *fmt)
90{
91 struct vimc_sen_device *vsen =
92 container_of(sd, struct vimc_sen_device, sd);
93
94 fmt->format = fmt->which == V4L2_SUBDEV_FORMAT_TRY ?
95 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) :
96 vsen->mbus_format;
97
98 return 0;
99}
100
101static void vimc_sen_tpg_s_format(struct vimc_sen_device *vsen)
102{
103 const struct vimc_pix_map *vpix =
104 vimc_pix_map_by_code(vsen->mbus_format.code);
105
106 tpg_reset_source(&vsen->tpg, vsen->mbus_format.width,
107 vsen->mbus_format.height, vsen->mbus_format.field);
108 tpg_s_bytesperline(&vsen->tpg, 0, vsen->mbus_format.width * vpix->bpp);
109 tpg_s_buf_height(&vsen->tpg, vsen->mbus_format.height);
110 tpg_s_fourcc(&vsen->tpg, vpix->pixelformat);
111 /* TODO: add support for V4L2_FIELD_ALTERNATE */
112 tpg_s_field(&vsen->tpg, vsen->mbus_format.field, false);
113 tpg_s_colorspace(&vsen->tpg, vsen->mbus_format.colorspace);
114 tpg_s_ycbcr_enc(&vsen->tpg, vsen->mbus_format.ycbcr_enc);
115 tpg_s_quantization(&vsen->tpg, vsen->mbus_format.quantization);
116 tpg_s_xfer_func(&vsen->tpg, vsen->mbus_format.xfer_func);
117}
118
119static void vimc_sen_adjust_fmt(struct v4l2_mbus_framefmt *fmt)
120{
121 const struct vimc_pix_map *vpix;
122
123 /* Only accept code in the pix map table */
124 vpix = vimc_pix_map_by_code(fmt->code);
125 if (!vpix)
126 fmt->code = fmt_default.code;
127
128 fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
129 VIMC_FRAME_MAX_WIDTH) & ~1;
130 fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
131 VIMC_FRAME_MAX_HEIGHT) & ~1;
132
133 /* TODO: add support for V4L2_FIELD_ALTERNATE */
134 if (fmt->field == V4L2_FIELD_ANY || fmt->field == V4L2_FIELD_ALTERNATE)
135 fmt->field = fmt_default.field;
136
137 vimc_colorimetry_clamp(fmt);
138}
139
140static int vimc_sen_set_fmt(struct v4l2_subdev *sd,
141 struct v4l2_subdev_pad_config *cfg,
142 struct v4l2_subdev_format *fmt)
143{
144 struct vimc_sen_device *vsen = v4l2_get_subdevdata(sd);
145 struct v4l2_mbus_framefmt *mf;
146
147 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
148 /* Do not change the format while stream is on */
149 if (vsen->frame)
150 return -EBUSY;
151
152 mf = &vsen->mbus_format;
153 } else {
154 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
155 }
156
157 /* Set the new format */
158 vimc_sen_adjust_fmt(&fmt->format);
159
160 dev_dbg(vsen->ved.dev, "%s: format update: "
161 "old:%dx%d (0x%x, %d, %d, %d, %d) "
162 "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsen->sd.name,
163 /* old */
164 mf->width, mf->height, mf->code,
165 mf->colorspace, mf->quantization,
166 mf->xfer_func, mf->ycbcr_enc,
167 /* new */
168 fmt->format.width, fmt->format.height, fmt->format.code,
169 fmt->format.colorspace, fmt->format.quantization,
170 fmt->format.xfer_func, fmt->format.ycbcr_enc);
171
172 *mf = fmt->format;
173
174 return 0;
175}
176
177static const struct v4l2_subdev_pad_ops vimc_sen_pad_ops = {
178 .init_cfg = vimc_sen_init_cfg,
179 .enum_mbus_code = vimc_sen_enum_mbus_code,
180 .enum_frame_size = vimc_sen_enum_frame_size,
181 .get_fmt = vimc_sen_get_fmt,
182 .set_fmt = vimc_sen_set_fmt,
183};
184
185static void *vimc_sen_process_frame(struct vimc_ent_device *ved,
186 const void *sink_frame)
187{
188 struct vimc_sen_device *vsen = container_of(ved, struct vimc_sen_device,
189 ved);
190
191 tpg_fill_plane_buffer(&vsen->tpg, 0, 0, vsen->frame);
192 return vsen->frame;
193}
194
195static int vimc_sen_s_stream(struct v4l2_subdev *sd, int enable)
196{
197 struct vimc_sen_device *vsen =
198 container_of(sd, struct vimc_sen_device, sd);
199
200 if (enable) {
201 const struct vimc_pix_map *vpix;
202 unsigned int frame_size;
203
204 /* Calculate the frame size */
205 vpix = vimc_pix_map_by_code(vsen->mbus_format.code);
206 frame_size = vsen->mbus_format.width * vpix->bpp *
207 vsen->mbus_format.height;
208
209 /*
210 * Allocate the frame buffer. Use vmalloc to be able to
211 * allocate a large amount of memory
212 */
213 vsen->frame = vmalloc(frame_size);
214 if (!vsen->frame)
215 return -ENOMEM;
216
217 /* configure the test pattern generator */
218 vimc_sen_tpg_s_format(vsen);
219
220 } else {
221
222 vfree(vsen->frame);
223 vsen->frame = NULL;
224 }
225
226 return 0;
227}
228
229static const struct v4l2_subdev_core_ops vimc_sen_core_ops = {
230 .log_status = v4l2_ctrl_subdev_log_status,
231 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
232 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
233};
234
235static const struct v4l2_subdev_video_ops vimc_sen_video_ops = {
236 .s_stream = vimc_sen_s_stream,
237};
238
239static const struct v4l2_subdev_ops vimc_sen_ops = {
240 .core = &vimc_sen_core_ops,
241 .pad = &vimc_sen_pad_ops,
242 .video = &vimc_sen_video_ops,
243};
244
245static int vimc_sen_s_ctrl(struct v4l2_ctrl *ctrl)
246{
247 struct vimc_sen_device *vsen =
248 container_of(ctrl->handler, struct vimc_sen_device, hdl);
249
250 switch (ctrl->id) {
251 case VIMC_CID_TEST_PATTERN:
252 tpg_s_pattern(&vsen->tpg, ctrl->val);
253 break;
254 case V4L2_CID_HFLIP:
255 tpg_s_hflip(&vsen->tpg, ctrl->val);
256 break;
257 case V4L2_CID_VFLIP:
258 tpg_s_vflip(&vsen->tpg, ctrl->val);
259 break;
260 case V4L2_CID_BRIGHTNESS:
261 tpg_s_brightness(&vsen->tpg, ctrl->val);
262 break;
263 case V4L2_CID_CONTRAST:
264 tpg_s_contrast(&vsen->tpg, ctrl->val);
265 break;
266 case V4L2_CID_HUE:
267 tpg_s_hue(&vsen->tpg, ctrl->val);
268 break;
269 case V4L2_CID_SATURATION:
270 tpg_s_saturation(&vsen->tpg, ctrl->val);
271 break;
272 default:
273 return -EINVAL;
274 }
275 return 0;
276}
277
278static const struct v4l2_ctrl_ops vimc_sen_ctrl_ops = {
279 .s_ctrl = vimc_sen_s_ctrl,
280};
281
282static void vimc_sen_release(struct vimc_ent_device *ved)
283{
284 struct vimc_sen_device *vsen =
285 container_of(ved, struct vimc_sen_device, ved);
286
287 v4l2_ctrl_handler_free(&vsen->hdl);
288 tpg_free(&vsen->tpg);
289 media_entity_cleanup(vsen->ved.ent);
290 kfree(vsen);
291}
292
293/* Image Processing Controls */
294static const struct v4l2_ctrl_config vimc_sen_ctrl_class = {
295 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY,
296 .id = VIMC_CID_VIMC_CLASS,
297 .name = "VIMC Controls",
298 .type = V4L2_CTRL_TYPE_CTRL_CLASS,
299};
300
301static const struct v4l2_ctrl_config vimc_sen_ctrl_test_pattern = {
302 .ops = &vimc_sen_ctrl_ops,
303 .id = VIMC_CID_TEST_PATTERN,
304 .name = "Test Pattern",
305 .type = V4L2_CTRL_TYPE_MENU,
306 .max = TPG_PAT_NOISE,
307 .qmenu = tpg_pattern_strings,
308};
309
310static struct vimc_ent_device *vimc_sen_add(struct vimc_device *vimc,
311 const char *vcfg_name)
312{
313 struct v4l2_device *v4l2_dev = &vimc->v4l2_dev;
314 struct vimc_sen_device *vsen;
315 int ret;
316
317 /* Allocate the vsen struct */
318 vsen = kzalloc(sizeof(*vsen), GFP_KERNEL);
319 if (!vsen)
320 return ERR_PTR(-ENOMEM);
321
322 v4l2_ctrl_handler_init(&vsen->hdl, 4);
323
324 v4l2_ctrl_new_custom(&vsen->hdl, &vimc_sen_ctrl_class, NULL);
325 v4l2_ctrl_new_custom(&vsen->hdl, &vimc_sen_ctrl_test_pattern, NULL);
326 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
327 V4L2_CID_VFLIP, 0, 1, 1, 0);
328 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
329 V4L2_CID_HFLIP, 0, 1, 1, 0);
330 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
331 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
332 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
333 V4L2_CID_CONTRAST, 0, 255, 1, 128);
334 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
335 V4L2_CID_HUE, -128, 127, 1, 0);
336 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
337 V4L2_CID_SATURATION, 0, 255, 1, 128);
338 vsen->sd.ctrl_handler = &vsen->hdl;
339 if (vsen->hdl.error) {
340 ret = vsen->hdl.error;
341 goto err_free_vsen;
342 }
343
344 /* Initialize the test pattern generator */
345 tpg_init(&vsen->tpg, vsen->mbus_format.width,
346 vsen->mbus_format.height);
347 ret = tpg_alloc(&vsen->tpg, VIMC_FRAME_MAX_WIDTH);
348 if (ret)
349 goto err_free_hdl;
350
351 /* Initialize ved and sd */
352 vsen->pad.flags = MEDIA_PAD_FL_SOURCE;
353 ret = vimc_ent_sd_register(&vsen->ved, &vsen->sd, v4l2_dev,
354 vcfg_name,
355 MEDIA_ENT_F_CAM_SENSOR, 1, &vsen->pad,
356 &vimc_sen_ops);
357 if (ret)
358 goto err_free_tpg;
359
360 vsen->ved.process_frame = vimc_sen_process_frame;
361 vsen->ved.dev = vimc->mdev.dev;
362
363 /* Initialize the frame format */
364 vsen->mbus_format = fmt_default;
365
366 return &vsen->ved;
367
368err_free_tpg:
369 tpg_free(&vsen->tpg);
370err_free_hdl:
371 v4l2_ctrl_handler_free(&vsen->hdl);
372err_free_vsen:
373 kfree(vsen);
374
375 return ERR_PTR(ret);
376}
377
378struct vimc_ent_type vimc_sen_type = {
379 .add = vimc_sen_add,
380 .release = vimc_sen_release
381};