2 * vimc-sensor.c Virtual Media Controller Driver
4 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/component.h>
19 #include <linux/module.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/platform_device.h>
22 #include <linux/v4l2-mediabus.h>
23 #include <linux/vmalloc.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-event.h>
26 #include <media/v4l2-subdev.h>
27 #include <media/tpg/v4l2-tpg.h>
29 #include "vimc-common.h"
31 #define VIMC_SEN_DRV_NAME "vimc-sensor"
33 struct vimc_sen_device
{
34 struct vimc_ent_device ved
;
35 struct v4l2_subdev sd
;
38 struct task_struct
*kthread_sen
;
40 /* The active format */
41 struct v4l2_mbus_framefmt mbus_format
;
42 struct v4l2_ctrl_handler hdl
;
45 static const struct v4l2_mbus_framefmt fmt_default
= {
48 .code
= MEDIA_BUS_FMT_RGB888_1X24
,
49 .field
= V4L2_FIELD_NONE
,
50 .colorspace
= V4L2_COLORSPACE_DEFAULT
,
53 static int vimc_sen_init_cfg(struct v4l2_subdev
*sd
,
54 struct v4l2_subdev_pad_config
*cfg
)
58 for (i
= 0; i
< sd
->entity
.num_pads
; i
++) {
59 struct v4l2_mbus_framefmt
*mf
;
61 mf
= v4l2_subdev_get_try_format(sd
, cfg
, i
);
68 static int vimc_sen_enum_frame_size(struct v4l2_subdev
*sd
,
69 struct v4l2_subdev_pad_config
*cfg
,
70 struct v4l2_subdev_frame_size_enum
*fse
)
75 fse
->min_width
= VIMC_FRAME_MIN_WIDTH
;
76 fse
->max_width
= VIMC_FRAME_MAX_WIDTH
;
77 fse
->min_height
= VIMC_FRAME_MIN_HEIGHT
;
78 fse
->max_height
= VIMC_FRAME_MAX_HEIGHT
;
83 static int vimc_sen_get_fmt(struct v4l2_subdev
*sd
,
84 struct v4l2_subdev_pad_config
*cfg
,
85 struct v4l2_subdev_format
*fmt
)
87 struct vimc_sen_device
*vsen
=
88 container_of(sd
, struct vimc_sen_device
, sd
);
90 fmt
->format
= fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
?
91 *v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
) :
97 static void vimc_sen_tpg_s_format(struct vimc_sen_device
*vsen
)
99 u32 pixelformat
= vsen
->ved
.stream
->producer_pixfmt
;
100 const struct v4l2_format_info
*pix_info
;
102 pix_info
= v4l2_format_info(pixelformat
);
104 tpg_reset_source(&vsen
->tpg
, vsen
->mbus_format
.width
,
105 vsen
->mbus_format
.height
, vsen
->mbus_format
.field
);
106 tpg_s_bytesperline(&vsen
->tpg
, 0,
107 vsen
->mbus_format
.width
* pix_info
->bpp
[0]);
108 tpg_s_buf_height(&vsen
->tpg
, vsen
->mbus_format
.height
);
109 tpg_s_fourcc(&vsen
->tpg
, pixelformat
);
110 /* TODO: add support for V4L2_FIELD_ALTERNATE */
111 tpg_s_field(&vsen
->tpg
, vsen
->mbus_format
.field
, false);
112 tpg_s_colorspace(&vsen
->tpg
, vsen
->mbus_format
.colorspace
);
113 tpg_s_ycbcr_enc(&vsen
->tpg
, vsen
->mbus_format
.ycbcr_enc
);
114 tpg_s_quantization(&vsen
->tpg
, vsen
->mbus_format
.quantization
);
115 tpg_s_xfer_func(&vsen
->tpg
, vsen
->mbus_format
.xfer_func
);
118 static void vimc_sen_adjust_fmt(struct v4l2_mbus_framefmt
*fmt
)
120 fmt
->width
= clamp_t(u32
, fmt
->width
, VIMC_FRAME_MIN_WIDTH
,
121 VIMC_FRAME_MAX_WIDTH
) & ~1;
122 fmt
->height
= clamp_t(u32
, fmt
->height
, VIMC_FRAME_MIN_HEIGHT
,
123 VIMC_FRAME_MAX_HEIGHT
) & ~1;
125 /* TODO: add support for V4L2_FIELD_ALTERNATE */
126 if (fmt
->field
== V4L2_FIELD_ANY
|| fmt
->field
== V4L2_FIELD_ALTERNATE
)
127 fmt
->field
= fmt_default
.field
;
129 vimc_colorimetry_clamp(fmt
);
132 static int vimc_sen_set_fmt(struct v4l2_subdev
*sd
,
133 struct v4l2_subdev_pad_config
*cfg
,
134 struct v4l2_subdev_format
*fmt
)
136 struct vimc_sen_device
*vsen
= v4l2_get_subdevdata(sd
);
137 struct v4l2_mbus_framefmt
*mf
;
139 if (!vimc_mbus_code_supported(fmt
->format
.code
))
140 fmt
->format
.code
= fmt_default
.code
;
142 if (fmt
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
143 /* Do not change the format while stream is on */
147 mf
= &vsen
->mbus_format
;
149 mf
= v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
);
152 /* Set the new format */
153 vimc_sen_adjust_fmt(&fmt
->format
);
155 dev_dbg(vsen
->dev
, "%s: format update: "
156 "old:%dx%d (0x%x, %d, %d, %d, %d) "
157 "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsen
->sd
.name
,
159 mf
->width
, mf
->height
, mf
->code
,
160 mf
->colorspace
, mf
->quantization
,
161 mf
->xfer_func
, mf
->ycbcr_enc
,
163 fmt
->format
.width
, fmt
->format
.height
, fmt
->format
.code
,
164 fmt
->format
.colorspace
, fmt
->format
.quantization
,
165 fmt
->format
.xfer_func
, fmt
->format
.ycbcr_enc
);
172 static const struct v4l2_subdev_pad_ops vimc_sen_pad_ops
= {
173 .init_cfg
= vimc_sen_init_cfg
,
174 .enum_mbus_code
= vimc_enum_mbus_code
,
175 .enum_frame_size
= vimc_sen_enum_frame_size
,
176 .get_fmt
= vimc_sen_get_fmt
,
177 .set_fmt
= vimc_sen_set_fmt
,
180 static void *vimc_sen_process_frame(struct vimc_ent_device
*ved
,
181 const void *sink_frame
)
183 struct vimc_sen_device
*vsen
= container_of(ved
, struct vimc_sen_device
,
186 tpg_fill_plane_buffer(&vsen
->tpg
, 0, 0, vsen
->frame
);
190 static int vimc_sen_s_stream(struct v4l2_subdev
*sd
, int enable
)
192 struct vimc_sen_device
*vsen
=
193 container_of(sd
, struct vimc_sen_device
, sd
);
196 u32 pixelformat
= vsen
->ved
.stream
->producer_pixfmt
;
197 const struct v4l2_format_info
*pix_info
;
198 unsigned int frame_size
;
200 if (vsen
->kthread_sen
)
201 /* tpg is already executing */
204 /* Calculate the frame size */
205 pix_info
= v4l2_format_info(pixelformat
);
206 frame_size
= vsen
->mbus_format
.width
* pix_info
->bpp
[0] *
207 vsen
->mbus_format
.height
;
210 * Allocate the frame buffer. Use vmalloc to be able to
211 * allocate a large amount of memory
213 vsen
->frame
= vmalloc(frame_size
);
217 /* configure the test pattern generator */
218 vimc_sen_tpg_s_format(vsen
);
230 static const struct v4l2_subdev_core_ops vimc_sen_core_ops
= {
231 .log_status
= v4l2_ctrl_subdev_log_status
,
232 .subscribe_event
= v4l2_ctrl_subdev_subscribe_event
,
233 .unsubscribe_event
= v4l2_event_subdev_unsubscribe
,
236 static const struct v4l2_subdev_video_ops vimc_sen_video_ops
= {
237 .s_stream
= vimc_sen_s_stream
,
240 static const struct v4l2_subdev_ops vimc_sen_ops
= {
241 .core
= &vimc_sen_core_ops
,
242 .pad
= &vimc_sen_pad_ops
,
243 .video
= &vimc_sen_video_ops
,
246 static int vimc_sen_s_ctrl(struct v4l2_ctrl
*ctrl
)
248 struct vimc_sen_device
*vsen
=
249 container_of(ctrl
->handler
, struct vimc_sen_device
, hdl
);
252 case VIMC_CID_TEST_PATTERN
:
253 tpg_s_pattern(&vsen
->tpg
, ctrl
->val
);
256 tpg_s_hflip(&vsen
->tpg
, ctrl
->val
);
259 tpg_s_vflip(&vsen
->tpg
, ctrl
->val
);
261 case V4L2_CID_BRIGHTNESS
:
262 tpg_s_brightness(&vsen
->tpg
, ctrl
->val
);
264 case V4L2_CID_CONTRAST
:
265 tpg_s_contrast(&vsen
->tpg
, ctrl
->val
);
268 tpg_s_hue(&vsen
->tpg
, ctrl
->val
);
270 case V4L2_CID_SATURATION
:
271 tpg_s_saturation(&vsen
->tpg
, ctrl
->val
);
279 static const struct v4l2_ctrl_ops vimc_sen_ctrl_ops
= {
280 .s_ctrl
= vimc_sen_s_ctrl
,
283 static void vimc_sen_release(struct v4l2_subdev
*sd
)
285 struct vimc_sen_device
*vsen
=
286 container_of(sd
, struct vimc_sen_device
, sd
);
288 v4l2_ctrl_handler_free(&vsen
->hdl
);
289 tpg_free(&vsen
->tpg
);
293 static const struct v4l2_subdev_internal_ops vimc_sen_int_ops
= {
294 .release
= vimc_sen_release
,
297 static void vimc_sen_comp_unbind(struct device
*comp
, struct device
*master
,
300 struct vimc_ent_device
*ved
= dev_get_drvdata(comp
);
301 struct vimc_sen_device
*vsen
=
302 container_of(ved
, struct vimc_sen_device
, ved
);
304 vimc_ent_sd_unregister(ved
, &vsen
->sd
);
307 /* Image Processing Controls */
308 static const struct v4l2_ctrl_config vimc_sen_ctrl_class
= {
309 .flags
= V4L2_CTRL_FLAG_READ_ONLY
| V4L2_CTRL_FLAG_WRITE_ONLY
,
310 .id
= VIMC_CID_VIMC_CLASS
,
311 .name
= "VIMC Controls",
312 .type
= V4L2_CTRL_TYPE_CTRL_CLASS
,
315 static const struct v4l2_ctrl_config vimc_sen_ctrl_test_pattern
= {
316 .ops
= &vimc_sen_ctrl_ops
,
317 .id
= VIMC_CID_TEST_PATTERN
,
318 .name
= "Test Pattern",
319 .type
= V4L2_CTRL_TYPE_MENU
,
320 .max
= TPG_PAT_NOISE
,
321 .qmenu
= tpg_pattern_strings
,
324 static int vimc_sen_comp_bind(struct device
*comp
, struct device
*master
,
327 struct v4l2_device
*v4l2_dev
= master_data
;
328 struct vimc_platform_data
*pdata
= comp
->platform_data
;
329 struct vimc_sen_device
*vsen
;
332 /* Allocate the vsen struct */
333 vsen
= kzalloc(sizeof(*vsen
), GFP_KERNEL
);
337 v4l2_ctrl_handler_init(&vsen
->hdl
, 4);
339 v4l2_ctrl_new_custom(&vsen
->hdl
, &vimc_sen_ctrl_class
, NULL
);
340 v4l2_ctrl_new_custom(&vsen
->hdl
, &vimc_sen_ctrl_test_pattern
, NULL
);
341 v4l2_ctrl_new_std(&vsen
->hdl
, &vimc_sen_ctrl_ops
,
342 V4L2_CID_VFLIP
, 0, 1, 1, 0);
343 v4l2_ctrl_new_std(&vsen
->hdl
, &vimc_sen_ctrl_ops
,
344 V4L2_CID_HFLIP
, 0, 1, 1, 0);
345 v4l2_ctrl_new_std(&vsen
->hdl
, &vimc_sen_ctrl_ops
,
346 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 128);
347 v4l2_ctrl_new_std(&vsen
->hdl
, &vimc_sen_ctrl_ops
,
348 V4L2_CID_CONTRAST
, 0, 255, 1, 128);
349 v4l2_ctrl_new_std(&vsen
->hdl
, &vimc_sen_ctrl_ops
,
350 V4L2_CID_HUE
, -128, 127, 1, 0);
351 v4l2_ctrl_new_std(&vsen
->hdl
, &vimc_sen_ctrl_ops
,
352 V4L2_CID_SATURATION
, 0, 255, 1, 128);
353 vsen
->sd
.ctrl_handler
= &vsen
->hdl
;
354 if (vsen
->hdl
.error
) {
355 ret
= vsen
->hdl
.error
;
359 /* Initialize ved and sd */
360 ret
= vimc_ent_sd_register(&vsen
->ved
, &vsen
->sd
, v4l2_dev
,
362 MEDIA_ENT_F_CAM_SENSOR
, 1,
363 (const unsigned long[1]) {MEDIA_PAD_FL_SOURCE
},
364 &vimc_sen_int_ops
, &vimc_sen_ops
);
368 vsen
->ved
.process_frame
= vimc_sen_process_frame
;
369 dev_set_drvdata(comp
, &vsen
->ved
);
372 /* Initialize the frame format */
373 vsen
->mbus_format
= fmt_default
;
375 /* Initialize the test pattern generator */
376 tpg_init(&vsen
->tpg
, vsen
->mbus_format
.width
,
377 vsen
->mbus_format
.height
);
378 ret
= tpg_alloc(&vsen
->tpg
, VIMC_FRAME_MAX_WIDTH
);
380 goto err_unregister_ent_sd
;
384 err_unregister_ent_sd
:
385 vimc_ent_sd_unregister(&vsen
->ved
, &vsen
->sd
);
387 v4l2_ctrl_handler_free(&vsen
->hdl
);
394 static const struct component_ops vimc_sen_comp_ops
= {
395 .bind
= vimc_sen_comp_bind
,
396 .unbind
= vimc_sen_comp_unbind
,
399 static int vimc_sen_probe(struct platform_device
*pdev
)
401 return component_add(&pdev
->dev
, &vimc_sen_comp_ops
);
404 static int vimc_sen_remove(struct platform_device
*pdev
)
406 component_del(&pdev
->dev
, &vimc_sen_comp_ops
);
411 static const struct platform_device_id vimc_sen_driver_ids
[] = {
413 .name
= VIMC_SEN_DRV_NAME
,
418 static struct platform_driver vimc_sen_pdrv
= {
419 .probe
= vimc_sen_probe
,
420 .remove
= vimc_sen_remove
,
421 .id_table
= vimc_sen_driver_ids
,
423 .name
= VIMC_SEN_DRV_NAME
,
427 module_platform_driver(vimc_sen_pdrv
);
429 MODULE_DEVICE_TABLE(platform
, vimc_sen_driver_ids
);
431 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Sensor");
432 MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
433 MODULE_LICENSE("GPL");