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/component.h>
9#include <linux/module.h>
10#include <linux/mod_devicetable.h>
11#include <linux/platform_device.h>
12#include <linux/v4l2-mediabus.h>
13#include <linux/vmalloc.h>
14#include <media/v4l2-ctrls.h>
15#include <media/v4l2-event.h>
16#include <media/v4l2-subdev.h>
17#include <media/tpg/v4l2-tpg.h>
18
19#include "vimc-common.h"
20
21#define VIMC_SEN_DRV_NAME "vimc-sensor"
22
23struct vimc_sen_device {
24 struct vimc_ent_device ved;
25 struct v4l2_subdev sd;
26 struct device *dev;
27 struct tpg_data tpg;
28 struct task_struct *kthread_sen;
29 u8 *frame;
30 /* The active format */
31 struct v4l2_mbus_framefmt mbus_format;
32 struct v4l2_ctrl_handler hdl;
33};
34
35static const struct v4l2_mbus_framefmt fmt_default = {
36 .width = 640,
37 .height = 480,
38 .code = MEDIA_BUS_FMT_RGB888_1X24,
39 .field = V4L2_FIELD_NONE,
40 .colorspace = V4L2_COLORSPACE_DEFAULT,
41};
42
43static int vimc_sen_init_cfg(struct v4l2_subdev *sd,
44 struct v4l2_subdev_pad_config *cfg)
45{
46 unsigned int i;
47
48 for (i = 0; i < sd->entity.num_pads; i++) {
49 struct v4l2_mbus_framefmt *mf;
50
51 mf = v4l2_subdev_get_try_format(sd, cfg, i);
52 *mf = fmt_default;
53 }
54
55 return 0;
56}
57
58static int vimc_sen_enum_frame_size(struct v4l2_subdev *sd,
59 struct v4l2_subdev_pad_config *cfg,
60 struct v4l2_subdev_frame_size_enum *fse)
61{
62 if (fse->index)
63 return -EINVAL;
64
65 fse->min_width = VIMC_FRAME_MIN_WIDTH;
66 fse->max_width = VIMC_FRAME_MAX_WIDTH;
67 fse->min_height = VIMC_FRAME_MIN_HEIGHT;
68 fse->max_height = VIMC_FRAME_MAX_HEIGHT;
69
70 return 0;
71}
72
73static int vimc_sen_get_fmt(struct v4l2_subdev *sd,
74 struct v4l2_subdev_pad_config *cfg,
75 struct v4l2_subdev_format *fmt)
76{
77 struct vimc_sen_device *vsen =
78 container_of(sd, struct vimc_sen_device, sd);
79
80 fmt->format = fmt->which == V4L2_SUBDEV_FORMAT_TRY ?
81 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) :
82 vsen->mbus_format;
83
84 return 0;
85}
86
87static void vimc_sen_tpg_s_format(struct vimc_sen_device *vsen)
88{
89 u32 pixelformat = vsen->ved.stream->producer_pixfmt;
90 const struct v4l2_format_info *pix_info;
91
92 pix_info = v4l2_format_info(pixelformat);
93
94 tpg_reset_source(&vsen->tpg, vsen->mbus_format.width,
95 vsen->mbus_format.height, vsen->mbus_format.field);
96 tpg_s_bytesperline(&vsen->tpg, 0,
97 vsen->mbus_format.width * pix_info->bpp[0]);
98 tpg_s_buf_height(&vsen->tpg, vsen->mbus_format.height);
99 tpg_s_fourcc(&vsen->tpg, pixelformat);
100 /* TODO: add support for V4L2_FIELD_ALTERNATE */
101 tpg_s_field(&vsen->tpg, vsen->mbus_format.field, false);
102 tpg_s_colorspace(&vsen->tpg, vsen->mbus_format.colorspace);
103 tpg_s_ycbcr_enc(&vsen->tpg, vsen->mbus_format.ycbcr_enc);
104 tpg_s_quantization(&vsen->tpg, vsen->mbus_format.quantization);
105 tpg_s_xfer_func(&vsen->tpg, vsen->mbus_format.xfer_func);
106}
107
108static void vimc_sen_adjust_fmt(struct v4l2_mbus_framefmt *fmt)
109{
110 fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
111 VIMC_FRAME_MAX_WIDTH) & ~1;
112 fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
113 VIMC_FRAME_MAX_HEIGHT) & ~1;
114
115 /* TODO: add support for V4L2_FIELD_ALTERNATE */
116 if (fmt->field == V4L2_FIELD_ANY || fmt->field == V4L2_FIELD_ALTERNATE)
117 fmt->field = fmt_default.field;
118
119 vimc_colorimetry_clamp(fmt);
120}
121
122static int vimc_sen_set_fmt(struct v4l2_subdev *sd,
123 struct v4l2_subdev_pad_config *cfg,
124 struct v4l2_subdev_format *fmt)
125{
126 struct vimc_sen_device *vsen = v4l2_get_subdevdata(sd);
127 struct v4l2_mbus_framefmt *mf;
128
129 if (!vimc_mbus_code_supported(fmt->format.code))
130 fmt->format.code = fmt_default.code;
131
132 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
133 /* Do not change the format while stream is on */
134 if (vsen->ved.stream)
135 return -EBUSY;
136
137 mf = &vsen->mbus_format;
138 } else {
139 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
140 }
141
142 /* Set the new format */
143 vimc_sen_adjust_fmt(&fmt->format);
144
145 dev_dbg(vsen->dev, "%s: format update: "
146 "old:%dx%d (0x%x, %d, %d, %d, %d) "
147 "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsen->sd.name,
148 /* old */
149 mf->width, mf->height, mf->code,
150 mf->colorspace, mf->quantization,
151 mf->xfer_func, mf->ycbcr_enc,
152 /* new */
153 fmt->format.width, fmt->format.height, fmt->format.code,
154 fmt->format.colorspace, fmt->format.quantization,
155 fmt->format.xfer_func, fmt->format.ycbcr_enc);
156
157 *mf = fmt->format;
158
159 return 0;
160}
161
162static const struct v4l2_subdev_pad_ops vimc_sen_pad_ops = {
163 .init_cfg = vimc_sen_init_cfg,
164 .enum_mbus_code = vimc_enum_mbus_code,
165 .enum_frame_size = vimc_sen_enum_frame_size,
166 .get_fmt = vimc_sen_get_fmt,
167 .set_fmt = vimc_sen_set_fmt,
168};
169
170static void *vimc_sen_process_frame(struct vimc_ent_device *ved,
171 const void *sink_frame)
172{
173 struct vimc_sen_device *vsen = container_of(ved, struct vimc_sen_device,
174 ved);
175
176 tpg_fill_plane_buffer(&vsen->tpg, 0, 0, vsen->frame);
177 return vsen->frame;
178}
179
180static int vimc_sen_s_stream(struct v4l2_subdev *sd, int enable)
181{
182 struct vimc_sen_device *vsen =
183 container_of(sd, struct vimc_sen_device, sd);
184
185 if (enable) {
186 u32 pixelformat = vsen->ved.stream->producer_pixfmt;
187 const struct v4l2_format_info *pix_info;
188 unsigned int frame_size;
189
190 /* Calculate the frame size */
191 pix_info = v4l2_format_info(pixelformat);
192 frame_size = vsen->mbus_format.width * pix_info->bpp[0] *
193 vsen->mbus_format.height;
194
195 /*
196 * Allocate the frame buffer. Use vmalloc to be able to
197 * allocate a large amount of memory
198 */
199 vsen->frame = vmalloc(frame_size);
200 if (!vsen->frame)
201 return -ENOMEM;
202
203 /* configure the test pattern generator */
204 vimc_sen_tpg_s_format(vsen);
205
206 } else {
207
208 vfree(vsen->frame);
209 vsen->frame = NULL;
210 }
211
212 return 0;
213}
214
215static const struct v4l2_subdev_core_ops vimc_sen_core_ops = {
216 .log_status = v4l2_ctrl_subdev_log_status,
217 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
218 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
219};
220
221static const struct v4l2_subdev_video_ops vimc_sen_video_ops = {
222 .s_stream = vimc_sen_s_stream,
223};
224
225static const struct v4l2_subdev_ops vimc_sen_ops = {
226 .core = &vimc_sen_core_ops,
227 .pad = &vimc_sen_pad_ops,
228 .video = &vimc_sen_video_ops,
229};
230
231static int vimc_sen_s_ctrl(struct v4l2_ctrl *ctrl)
232{
233 struct vimc_sen_device *vsen =
234 container_of(ctrl->handler, struct vimc_sen_device, hdl);
235
236 switch (ctrl->id) {
237 case VIMC_CID_TEST_PATTERN:
238 tpg_s_pattern(&vsen->tpg, ctrl->val);
239 break;
240 case V4L2_CID_HFLIP:
241 tpg_s_hflip(&vsen->tpg, ctrl->val);
242 break;
243 case V4L2_CID_VFLIP:
244 tpg_s_vflip(&vsen->tpg, ctrl->val);
245 break;
246 case V4L2_CID_BRIGHTNESS:
247 tpg_s_brightness(&vsen->tpg, ctrl->val);
248 break;
249 case V4L2_CID_CONTRAST:
250 tpg_s_contrast(&vsen->tpg, ctrl->val);
251 break;
252 case V4L2_CID_HUE:
253 tpg_s_hue(&vsen->tpg, ctrl->val);
254 break;
255 case V4L2_CID_SATURATION:
256 tpg_s_saturation(&vsen->tpg, ctrl->val);
257 break;
258 default:
259 return -EINVAL;
260 }
261 return 0;
262}
263
264static const struct v4l2_ctrl_ops vimc_sen_ctrl_ops = {
265 .s_ctrl = vimc_sen_s_ctrl,
266};
267
268static void vimc_sen_release(struct v4l2_subdev *sd)
269{
270 struct vimc_sen_device *vsen =
271 container_of(sd, struct vimc_sen_device, sd);
272
273 v4l2_ctrl_handler_free(&vsen->hdl);
274 tpg_free(&vsen->tpg);
275 kfree(vsen);
276}
277
278static const struct v4l2_subdev_internal_ops vimc_sen_int_ops = {
279 .release = vimc_sen_release,
280};
281
282static void vimc_sen_comp_unbind(struct device *comp, struct device *master,
283 void *master_data)
284{
285 struct vimc_ent_device *ved = dev_get_drvdata(comp);
286 struct vimc_sen_device *vsen =
287 container_of(ved, struct vimc_sen_device, ved);
288
289 vimc_ent_sd_unregister(ved, &vsen->sd);
290}
291
292/* Image Processing Controls */
293static const struct v4l2_ctrl_config vimc_sen_ctrl_class = {
294 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY,
295 .id = VIMC_CID_VIMC_CLASS,
296 .name = "VIMC Controls",
297 .type = V4L2_CTRL_TYPE_CTRL_CLASS,
298};
299
300static const struct v4l2_ctrl_config vimc_sen_ctrl_test_pattern = {
301 .ops = &vimc_sen_ctrl_ops,
302 .id = VIMC_CID_TEST_PATTERN,
303 .name = "Test Pattern",
304 .type = V4L2_CTRL_TYPE_MENU,
305 .max = TPG_PAT_NOISE,
306 .qmenu = tpg_pattern_strings,
307};
308
309static int vimc_sen_comp_bind(struct device *comp, struct device *master,
310 void *master_data)
311{
312 struct v4l2_device *v4l2_dev = master_data;
313 struct vimc_platform_data *pdata = comp->platform_data;
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 -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 ved and sd */
345 ret = vimc_ent_sd_register(&vsen->ved, &vsen->sd, v4l2_dev,
346 pdata->entity_name,
347 MEDIA_ENT_F_CAM_SENSOR, 1,
348 (const unsigned long[1]) {MEDIA_PAD_FL_SOURCE},
349 &vimc_sen_int_ops, &vimc_sen_ops);
350 if (ret)
351 goto err_free_hdl;
352
353 vsen->ved.process_frame = vimc_sen_process_frame;
354 dev_set_drvdata(comp, &vsen->ved);
355 vsen->dev = comp;
356
357 /* Initialize the frame format */
358 vsen->mbus_format = fmt_default;
359
360 /* Initialize the test pattern generator */
361 tpg_init(&vsen->tpg, vsen->mbus_format.width,
362 vsen->mbus_format.height);
363 ret = tpg_alloc(&vsen->tpg, VIMC_FRAME_MAX_WIDTH);
364 if (ret)
365 goto err_unregister_ent_sd;
366
367 return 0;
368
369err_unregister_ent_sd:
370 vimc_ent_sd_unregister(&vsen->ved, &vsen->sd);
371err_free_hdl:
372 v4l2_ctrl_handler_free(&vsen->hdl);
373err_free_vsen:
374 kfree(vsen);
375
376 return ret;
377}
378
379static const struct component_ops vimc_sen_comp_ops = {
380 .bind = vimc_sen_comp_bind,
381 .unbind = vimc_sen_comp_unbind,
382};
383
384static int vimc_sen_probe(struct platform_device *pdev)
385{
386 return component_add(&pdev->dev, &vimc_sen_comp_ops);
387}
388
389static int vimc_sen_remove(struct platform_device *pdev)
390{
391 component_del(&pdev->dev, &vimc_sen_comp_ops);
392
393 return 0;
394}
395
396static const struct platform_device_id vimc_sen_driver_ids[] = {
397 {
398 .name = VIMC_SEN_DRV_NAME,
399 },
400 { }
401};
402
403static struct platform_driver vimc_sen_pdrv = {
404 .probe = vimc_sen_probe,
405 .remove = vimc_sen_remove,
406 .id_table = vimc_sen_driver_ids,
407 .driver = {
408 .name = VIMC_SEN_DRV_NAME,
409 },
410};
411
412module_platform_driver(vimc_sen_pdrv);
413
414MODULE_DEVICE_TABLE(platform, vimc_sen_driver_ids);
415
416MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Sensor");
417MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
418MODULE_LICENSE("GPL");