1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * vimc-sensor.c Virtual Media Controller Driver
5 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
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>
19 #include "vimc-common.h"
21 #define VIMC_SEN_DRV_NAME "vimc-sensor"
23 struct vimc_sen_device {
24 struct vimc_ent_device ved;
25 struct v4l2_subdev sd;
29 /* The active format */
30 struct v4l2_mbus_framefmt mbus_format;
31 struct v4l2_ctrl_handler hdl;
34 static const struct v4l2_mbus_framefmt fmt_default = {
37 .code = MEDIA_BUS_FMT_RGB888_1X24,
38 .field = V4L2_FIELD_NONE,
39 .colorspace = V4L2_COLORSPACE_DEFAULT,
42 static int vimc_sen_init_cfg(struct v4l2_subdev *sd,
43 struct v4l2_subdev_pad_config *cfg)
47 for (i = 0; i < sd->entity.num_pads; i++) {
48 struct v4l2_mbus_framefmt *mf;
50 mf = v4l2_subdev_get_try_format(sd, cfg, i);
57 static int vimc_sen_enum_mbus_code(struct v4l2_subdev *sd,
58 struct v4l2_subdev_pad_config *cfg,
59 struct v4l2_subdev_mbus_code_enum *code)
61 const struct vimc_pix_map *vpix = vimc_pix_map_by_index(code->index);
66 code->code = vpix->code;
71 static int vimc_sen_enum_frame_size(struct v4l2_subdev *sd,
72 struct v4l2_subdev_pad_config *cfg,
73 struct v4l2_subdev_frame_size_enum *fse)
75 const struct vimc_pix_map *vpix;
80 /* Only accept code in the pix map table */
81 vpix = vimc_pix_map_by_code(fse->code);
85 fse->min_width = VIMC_FRAME_MIN_WIDTH;
86 fse->max_width = VIMC_FRAME_MAX_WIDTH;
87 fse->min_height = VIMC_FRAME_MIN_HEIGHT;
88 fse->max_height = VIMC_FRAME_MAX_HEIGHT;
93 static int vimc_sen_get_fmt(struct v4l2_subdev *sd,
94 struct v4l2_subdev_pad_config *cfg,
95 struct v4l2_subdev_format *fmt)
97 struct vimc_sen_device *vsen =
98 container_of(sd, struct vimc_sen_device, sd);
100 fmt->format = fmt->which == V4L2_SUBDEV_FORMAT_TRY ?
101 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) :
107 static void vimc_sen_tpg_s_format(struct vimc_sen_device *vsen)
109 const struct vimc_pix_map *vpix =
110 vimc_pix_map_by_code(vsen->mbus_format.code);
112 tpg_reset_source(&vsen->tpg, vsen->mbus_format.width,
113 vsen->mbus_format.height, vsen->mbus_format.field);
114 tpg_s_bytesperline(&vsen->tpg, 0, vsen->mbus_format.width * vpix->bpp);
115 tpg_s_buf_height(&vsen->tpg, vsen->mbus_format.height);
116 tpg_s_fourcc(&vsen->tpg, vpix->pixelformat);
117 /* TODO: add support for V4L2_FIELD_ALTERNATE */
118 tpg_s_field(&vsen->tpg, vsen->mbus_format.field, false);
119 tpg_s_colorspace(&vsen->tpg, vsen->mbus_format.colorspace);
120 tpg_s_ycbcr_enc(&vsen->tpg, vsen->mbus_format.ycbcr_enc);
121 tpg_s_quantization(&vsen->tpg, vsen->mbus_format.quantization);
122 tpg_s_xfer_func(&vsen->tpg, vsen->mbus_format.xfer_func);
125 static void vimc_sen_adjust_fmt(struct v4l2_mbus_framefmt *fmt)
127 const struct vimc_pix_map *vpix;
129 /* Only accept code in the pix map table */
130 vpix = vimc_pix_map_by_code(fmt->code);
132 fmt->code = fmt_default.code;
134 fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
135 VIMC_FRAME_MAX_WIDTH) & ~1;
136 fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
137 VIMC_FRAME_MAX_HEIGHT) & ~1;
139 /* TODO: add support for V4L2_FIELD_ALTERNATE */
140 if (fmt->field == V4L2_FIELD_ANY || fmt->field == V4L2_FIELD_ALTERNATE)
141 fmt->field = fmt_default.field;
143 vimc_colorimetry_clamp(fmt);
146 static int vimc_sen_set_fmt(struct v4l2_subdev *sd,
147 struct v4l2_subdev_pad_config *cfg,
148 struct v4l2_subdev_format *fmt)
150 struct vimc_sen_device *vsen = v4l2_get_subdevdata(sd);
151 struct v4l2_mbus_framefmt *mf;
153 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
154 /* Do not change the format while stream is on */
158 mf = &vsen->mbus_format;
160 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
163 /* Set the new format */
164 vimc_sen_adjust_fmt(&fmt->format);
166 dev_dbg(vsen->dev, "%s: format update: "
167 "old:%dx%d (0x%x, %d, %d, %d, %d) "
168 "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsen->sd.name,
170 mf->width, mf->height, mf->code,
171 mf->colorspace, mf->quantization,
172 mf->xfer_func, mf->ycbcr_enc,
174 fmt->format.width, fmt->format.height, fmt->format.code,
175 fmt->format.colorspace, fmt->format.quantization,
176 fmt->format.xfer_func, fmt->format.ycbcr_enc);
183 static const struct v4l2_subdev_pad_ops vimc_sen_pad_ops = {
184 .init_cfg = vimc_sen_init_cfg,
185 .enum_mbus_code = vimc_sen_enum_mbus_code,
186 .enum_frame_size = vimc_sen_enum_frame_size,
187 .get_fmt = vimc_sen_get_fmt,
188 .set_fmt = vimc_sen_set_fmt,
191 static void *vimc_sen_process_frame(struct vimc_ent_device *ved,
192 const void *sink_frame)
194 struct vimc_sen_device *vsen = container_of(ved, struct vimc_sen_device,
197 tpg_fill_plane_buffer(&vsen->tpg, 0, 0, vsen->frame);
201 static int vimc_sen_s_stream(struct v4l2_subdev *sd, int enable)
203 struct vimc_sen_device *vsen =
204 container_of(sd, struct vimc_sen_device, sd);
207 const struct vimc_pix_map *vpix;
208 unsigned int frame_size;
210 /* Calculate the frame size */
211 vpix = vimc_pix_map_by_code(vsen->mbus_format.code);
212 frame_size = vsen->mbus_format.width * vpix->bpp *
213 vsen->mbus_format.height;
216 * Allocate the frame buffer. Use vmalloc to be able to
217 * allocate a large amount of memory
219 vsen->frame = vmalloc(frame_size);
223 /* configure the test pattern generator */
224 vimc_sen_tpg_s_format(vsen);
235 static const struct v4l2_subdev_core_ops vimc_sen_core_ops = {
236 .log_status = v4l2_ctrl_subdev_log_status,
237 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
238 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
241 static const struct v4l2_subdev_video_ops vimc_sen_video_ops = {
242 .s_stream = vimc_sen_s_stream,
245 static const struct v4l2_subdev_ops vimc_sen_ops = {
246 .core = &vimc_sen_core_ops,
247 .pad = &vimc_sen_pad_ops,
248 .video = &vimc_sen_video_ops,
251 static int vimc_sen_s_ctrl(struct v4l2_ctrl *ctrl)
253 struct vimc_sen_device *vsen =
254 container_of(ctrl->handler, struct vimc_sen_device, hdl);
257 case VIMC_CID_TEST_PATTERN:
258 tpg_s_pattern(&vsen->tpg, ctrl->val);
261 tpg_s_hflip(&vsen->tpg, ctrl->val);
264 tpg_s_vflip(&vsen->tpg, ctrl->val);
266 case V4L2_CID_BRIGHTNESS:
267 tpg_s_brightness(&vsen->tpg, ctrl->val);
269 case V4L2_CID_CONTRAST:
270 tpg_s_contrast(&vsen->tpg, ctrl->val);
273 tpg_s_hue(&vsen->tpg, ctrl->val);
275 case V4L2_CID_SATURATION:
276 tpg_s_saturation(&vsen->tpg, ctrl->val);
284 static const struct v4l2_ctrl_ops vimc_sen_ctrl_ops = {
285 .s_ctrl = vimc_sen_s_ctrl,
288 static void vimc_sen_release(struct v4l2_subdev *sd)
290 struct vimc_sen_device *vsen =
291 container_of(sd, struct vimc_sen_device, sd);
293 v4l2_ctrl_handler_free(&vsen->hdl);
294 tpg_free(&vsen->tpg);
298 static const struct v4l2_subdev_internal_ops vimc_sen_int_ops = {
299 .release = vimc_sen_release,
302 static void vimc_sen_comp_unbind(struct device *comp, struct device *master,
305 struct vimc_ent_device *ved = dev_get_drvdata(comp);
306 struct vimc_sen_device *vsen =
307 container_of(ved, struct vimc_sen_device, ved);
309 vimc_ent_sd_unregister(ved, &vsen->sd);
312 /* Image Processing Controls */
313 static const struct v4l2_ctrl_config vimc_sen_ctrl_class = {
314 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY,
315 .id = VIMC_CID_VIMC_CLASS,
316 .name = "VIMC Controls",
317 .type = V4L2_CTRL_TYPE_CTRL_CLASS,
320 static const struct v4l2_ctrl_config vimc_sen_ctrl_test_pattern = {
321 .ops = &vimc_sen_ctrl_ops,
322 .id = VIMC_CID_TEST_PATTERN,
323 .name = "Test Pattern",
324 .type = V4L2_CTRL_TYPE_MENU,
325 .max = TPG_PAT_NOISE,
326 .qmenu = tpg_pattern_strings,
329 static int vimc_sen_comp_bind(struct device *comp, struct device *master,
332 struct v4l2_device *v4l2_dev = master_data;
333 struct vimc_platform_data *pdata = comp->platform_data;
334 struct vimc_sen_device *vsen;
337 /* Allocate the vsen struct */
338 vsen = kzalloc(sizeof(*vsen), GFP_KERNEL);
342 v4l2_ctrl_handler_init(&vsen->hdl, 4);
344 v4l2_ctrl_new_custom(&vsen->hdl, &vimc_sen_ctrl_class, NULL);
345 v4l2_ctrl_new_custom(&vsen->hdl, &vimc_sen_ctrl_test_pattern, NULL);
346 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
347 V4L2_CID_VFLIP, 0, 1, 1, 0);
348 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
349 V4L2_CID_HFLIP, 0, 1, 1, 0);
350 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
351 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
352 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
353 V4L2_CID_CONTRAST, 0, 255, 1, 128);
354 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
355 V4L2_CID_HUE, -128, 127, 1, 0);
356 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
357 V4L2_CID_SATURATION, 0, 255, 1, 128);
358 vsen->sd.ctrl_handler = &vsen->hdl;
359 if (vsen->hdl.error) {
360 ret = vsen->hdl.error;
364 /* Initialize ved and sd */
365 ret = vimc_ent_sd_register(&vsen->ved, &vsen->sd, v4l2_dev,
367 MEDIA_ENT_F_CAM_SENSOR, 1,
368 (const unsigned long[1]) {MEDIA_PAD_FL_SOURCE},
369 &vimc_sen_int_ops, &vimc_sen_ops);
373 vsen->ved.process_frame = vimc_sen_process_frame;
374 dev_set_drvdata(comp, &vsen->ved);
377 /* Initialize the frame format */
378 vsen->mbus_format = fmt_default;
380 /* Initialize the test pattern generator */
381 tpg_init(&vsen->tpg, vsen->mbus_format.width,
382 vsen->mbus_format.height);
383 ret = tpg_alloc(&vsen->tpg, VIMC_FRAME_MAX_WIDTH);
385 goto err_unregister_ent_sd;
389 err_unregister_ent_sd:
390 vimc_ent_sd_unregister(&vsen->ved, &vsen->sd);
392 v4l2_ctrl_handler_free(&vsen->hdl);
399 static const struct component_ops vimc_sen_comp_ops = {
400 .bind = vimc_sen_comp_bind,
401 .unbind = vimc_sen_comp_unbind,
404 static int vimc_sen_probe(struct platform_device *pdev)
406 return component_add(&pdev->dev, &vimc_sen_comp_ops);
409 static int vimc_sen_remove(struct platform_device *pdev)
411 component_del(&pdev->dev, &vimc_sen_comp_ops);
416 static const struct platform_device_id vimc_sen_driver_ids[] = {
418 .name = VIMC_SEN_DRV_NAME,
423 static struct platform_driver vimc_sen_pdrv = {
424 .probe = vimc_sen_probe,
425 .remove = vimc_sen_remove,
426 .id_table = vimc_sen_driver_ids,
428 .name = VIMC_SEN_DRV_NAME,
432 module_platform_driver(vimc_sen_pdrv);
434 MODULE_DEVICE_TABLE(platform, vimc_sen_driver_ids);
436 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Sensor");
437 MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
438 MODULE_LICENSE("GPL");