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/freezer.h>
20 #include <linux/kthread.h>
21 #include <linux/module.h>
22 #include <linux/mod_devicetable.h>
23 #include <linux/platform_device.h>
24 #include <linux/v4l2-mediabus.h>
25 #include <linux/vmalloc.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-event.h>
28 #include <media/v4l2-subdev.h>
29 #include <media/tpg/v4l2-tpg.h>
31 #include "vimc-common.h"
33 #define VIMC_SEN_DRV_NAME "vimc-sensor"
35 struct vimc_sen_device
{
36 struct vimc_ent_device ved
;
37 struct v4l2_subdev sd
;
40 struct task_struct
*kthread_sen
;
42 /* The active format */
43 struct v4l2_mbus_framefmt mbus_format
;
44 struct v4l2_ctrl_handler hdl
;
47 static const struct v4l2_mbus_framefmt fmt_default
= {
50 .code
= MEDIA_BUS_FMT_RGB888_1X24
,
51 .field
= V4L2_FIELD_NONE
,
52 .colorspace
= V4L2_COLORSPACE_DEFAULT
,
55 static int vimc_sen_init_cfg(struct v4l2_subdev
*sd
,
56 struct v4l2_subdev_pad_config
*cfg
)
60 for (i
= 0; i
< sd
->entity
.num_pads
; i
++) {
61 struct v4l2_mbus_framefmt
*mf
;
63 mf
= v4l2_subdev_get_try_format(sd
, cfg
, i
);
70 static int vimc_sen_enum_mbus_code(struct v4l2_subdev
*sd
,
71 struct v4l2_subdev_pad_config
*cfg
,
72 struct v4l2_subdev_mbus_code_enum
*code
)
74 const struct vimc_pix_map
*vpix
= vimc_pix_map_by_index(code
->index
);
79 code
->code
= vpix
->code
;
84 static int vimc_sen_enum_frame_size(struct v4l2_subdev
*sd
,
85 struct v4l2_subdev_pad_config
*cfg
,
86 struct v4l2_subdev_frame_size_enum
*fse
)
88 const struct vimc_pix_map
*vpix
;
93 /* Only accept code in the pix map table */
94 vpix
= vimc_pix_map_by_code(fse
->code
);
98 fse
->min_width
= VIMC_FRAME_MIN_WIDTH
;
99 fse
->max_width
= VIMC_FRAME_MAX_WIDTH
;
100 fse
->min_height
= VIMC_FRAME_MIN_HEIGHT
;
101 fse
->max_height
= VIMC_FRAME_MAX_HEIGHT
;
106 static int vimc_sen_get_fmt(struct v4l2_subdev
*sd
,
107 struct v4l2_subdev_pad_config
*cfg
,
108 struct v4l2_subdev_format
*fmt
)
110 struct vimc_sen_device
*vsen
=
111 container_of(sd
, struct vimc_sen_device
, sd
);
113 fmt
->format
= fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
?
114 *v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
) :
120 static void vimc_sen_tpg_s_format(struct vimc_sen_device
*vsen
)
122 const struct vimc_pix_map
*vpix
=
123 vimc_pix_map_by_code(vsen
->mbus_format
.code
);
125 tpg_reset_source(&vsen
->tpg
, vsen
->mbus_format
.width
,
126 vsen
->mbus_format
.height
, vsen
->mbus_format
.field
);
127 tpg_s_bytesperline(&vsen
->tpg
, 0, vsen
->mbus_format
.width
* vpix
->bpp
);
128 tpg_s_buf_height(&vsen
->tpg
, vsen
->mbus_format
.height
);
129 tpg_s_fourcc(&vsen
->tpg
, vpix
->pixelformat
);
130 /* TODO: add support for V4L2_FIELD_ALTERNATE */
131 tpg_s_field(&vsen
->tpg
, vsen
->mbus_format
.field
, false);
132 tpg_s_colorspace(&vsen
->tpg
, vsen
->mbus_format
.colorspace
);
133 tpg_s_ycbcr_enc(&vsen
->tpg
, vsen
->mbus_format
.ycbcr_enc
);
134 tpg_s_quantization(&vsen
->tpg
, vsen
->mbus_format
.quantization
);
135 tpg_s_xfer_func(&vsen
->tpg
, vsen
->mbus_format
.xfer_func
);
138 static void vimc_sen_adjust_fmt(struct v4l2_mbus_framefmt
*fmt
)
140 const struct vimc_pix_map
*vpix
;
142 /* Only accept code in the pix map table */
143 vpix
= vimc_pix_map_by_code(fmt
->code
);
145 fmt
->code
= fmt_default
.code
;
147 fmt
->width
= clamp_t(u32
, fmt
->width
, VIMC_FRAME_MIN_WIDTH
,
148 VIMC_FRAME_MAX_WIDTH
) & ~1;
149 fmt
->height
= clamp_t(u32
, fmt
->height
, VIMC_FRAME_MIN_HEIGHT
,
150 VIMC_FRAME_MAX_HEIGHT
) & ~1;
152 /* TODO: add support for V4L2_FIELD_ALTERNATE */
153 if (fmt
->field
== V4L2_FIELD_ANY
|| fmt
->field
== V4L2_FIELD_ALTERNATE
)
154 fmt
->field
= fmt_default
.field
;
156 vimc_colorimetry_clamp(fmt
);
159 static int vimc_sen_set_fmt(struct v4l2_subdev
*sd
,
160 struct v4l2_subdev_pad_config
*cfg
,
161 struct v4l2_subdev_format
*fmt
)
163 struct vimc_sen_device
*vsen
= v4l2_get_subdevdata(sd
);
164 struct v4l2_mbus_framefmt
*mf
;
166 if (fmt
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
167 /* Do not change the format while stream is on */
171 mf
= &vsen
->mbus_format
;
173 mf
= v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
);
176 /* Set the new format */
177 vimc_sen_adjust_fmt(&fmt
->format
);
179 dev_dbg(vsen
->dev
, "%s: format update: "
180 "old:%dx%d (0x%x, %d, %d, %d, %d) "
181 "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsen
->sd
.name
,
183 mf
->width
, mf
->height
, mf
->code
,
184 mf
->colorspace
, mf
->quantization
,
185 mf
->xfer_func
, mf
->ycbcr_enc
,
187 fmt
->format
.width
, fmt
->format
.height
, fmt
->format
.code
,
188 fmt
->format
.colorspace
, fmt
->format
.quantization
,
189 fmt
->format
.xfer_func
, fmt
->format
.ycbcr_enc
);
196 static const struct v4l2_subdev_pad_ops vimc_sen_pad_ops
= {
197 .init_cfg
= vimc_sen_init_cfg
,
198 .enum_mbus_code
= vimc_sen_enum_mbus_code
,
199 .enum_frame_size
= vimc_sen_enum_frame_size
,
200 .get_fmt
= vimc_sen_get_fmt
,
201 .set_fmt
= vimc_sen_set_fmt
,
204 static int vimc_sen_tpg_thread(void *data
)
206 struct vimc_sen_device
*vsen
= data
;
210 set_current_state(TASK_UNINTERRUPTIBLE
);
214 if (kthread_should_stop())
217 tpg_fill_plane_buffer(&vsen
->tpg
, 0, 0, vsen
->frame
);
219 /* Send the frame to all source pads */
220 for (i
= 0; i
< vsen
->sd
.entity
.num_pads
; i
++)
221 vimc_propagate_frame(&vsen
->sd
.entity
.pads
[i
],
224 /* 60 frames per second */
225 schedule_timeout(HZ
/60);
231 static int vimc_sen_s_stream(struct v4l2_subdev
*sd
, int enable
)
233 struct vimc_sen_device
*vsen
=
234 container_of(sd
, struct vimc_sen_device
, sd
);
238 const struct vimc_pix_map
*vpix
;
239 unsigned int frame_size
;
241 if (vsen
->kthread_sen
)
242 /* tpg is already executing */
245 /* Calculate the frame size */
246 vpix
= vimc_pix_map_by_code(vsen
->mbus_format
.code
);
247 frame_size
= vsen
->mbus_format
.width
* vpix
->bpp
*
248 vsen
->mbus_format
.height
;
251 * Allocate the frame buffer. Use vmalloc to be able to
252 * allocate a large amount of memory
254 vsen
->frame
= vmalloc(frame_size
);
258 /* configure the test pattern generator */
259 vimc_sen_tpg_s_format(vsen
);
261 /* Initialize the image generator thread */
262 vsen
->kthread_sen
= kthread_run(vimc_sen_tpg_thread
, vsen
,
263 "%s-sen", vsen
->sd
.v4l2_dev
->name
);
264 if (IS_ERR(vsen
->kthread_sen
)) {
265 dev_err(vsen
->dev
, "%s: kernel_thread() failed\n",
269 return PTR_ERR(vsen
->kthread_sen
);
272 if (!vsen
->kthread_sen
)
275 /* Stop image generator */
276 ret
= kthread_stop(vsen
->kthread_sen
);
280 vsen
->kthread_sen
= NULL
;
289 static struct v4l2_subdev_core_ops vimc_sen_core_ops
= {
290 .log_status
= v4l2_ctrl_subdev_log_status
,
291 .subscribe_event
= v4l2_ctrl_subdev_subscribe_event
,
292 .unsubscribe_event
= v4l2_event_subdev_unsubscribe
,
295 static const struct v4l2_subdev_video_ops vimc_sen_video_ops
= {
296 .s_stream
= vimc_sen_s_stream
,
299 static const struct v4l2_subdev_ops vimc_sen_ops
= {
300 .core
= &vimc_sen_core_ops
,
301 .pad
= &vimc_sen_pad_ops
,
302 .video
= &vimc_sen_video_ops
,
305 static int vimc_sen_s_ctrl(struct v4l2_ctrl
*ctrl
)
307 struct vimc_sen_device
*vsen
=
308 container_of(ctrl
->handler
, struct vimc_sen_device
, hdl
);
311 case VIMC_CID_TEST_PATTERN
:
312 tpg_s_pattern(&vsen
->tpg
, ctrl
->val
);
315 tpg_s_hflip(&vsen
->tpg
, ctrl
->val
);
318 tpg_s_vflip(&vsen
->tpg
, ctrl
->val
);
326 static const struct v4l2_ctrl_ops vimc_sen_ctrl_ops
= {
327 .s_ctrl
= vimc_sen_s_ctrl
,
330 static void vimc_sen_comp_unbind(struct device
*comp
, struct device
*master
,
333 struct vimc_ent_device
*ved
= dev_get_drvdata(comp
);
334 struct vimc_sen_device
*vsen
=
335 container_of(ved
, struct vimc_sen_device
, ved
);
337 vimc_ent_sd_unregister(ved
, &vsen
->sd
);
338 v4l2_ctrl_handler_free(&vsen
->hdl
);
339 tpg_free(&vsen
->tpg
);
343 /* Image Processing Controls */
344 static const struct v4l2_ctrl_config vimc_sen_ctrl_class
= {
345 .flags
= V4L2_CTRL_FLAG_READ_ONLY
| V4L2_CTRL_FLAG_WRITE_ONLY
,
346 .id
= VIMC_CID_VIMC_CLASS
,
347 .name
= "VIMC Controls",
348 .type
= V4L2_CTRL_TYPE_CTRL_CLASS
,
351 static const struct v4l2_ctrl_config vimc_sen_ctrl_test_pattern
= {
352 .ops
= &vimc_sen_ctrl_ops
,
353 .id
= VIMC_CID_TEST_PATTERN
,
354 .name
= "Test Pattern",
355 .type
= V4L2_CTRL_TYPE_MENU
,
356 .max
= TPG_PAT_NOISE
,
357 .qmenu
= tpg_pattern_strings
,
360 static int vimc_sen_comp_bind(struct device
*comp
, struct device
*master
,
363 struct v4l2_device
*v4l2_dev
= master_data
;
364 struct vimc_platform_data
*pdata
= comp
->platform_data
;
365 struct vimc_sen_device
*vsen
;
368 /* Allocate the vsen struct */
369 vsen
= kzalloc(sizeof(*vsen
), GFP_KERNEL
);
373 v4l2_ctrl_handler_init(&vsen
->hdl
, 4);
375 v4l2_ctrl_new_custom(&vsen
->hdl
, &vimc_sen_ctrl_class
, NULL
);
376 v4l2_ctrl_new_custom(&vsen
->hdl
, &vimc_sen_ctrl_test_pattern
, NULL
);
377 v4l2_ctrl_new_std(&vsen
->hdl
, &vimc_sen_ctrl_ops
,
378 V4L2_CID_VFLIP
, 0, 1, 1, 0);
379 v4l2_ctrl_new_std(&vsen
->hdl
, &vimc_sen_ctrl_ops
,
380 V4L2_CID_HFLIP
, 0, 1, 1, 0);
381 vsen
->sd
.ctrl_handler
= &vsen
->hdl
;
382 if (vsen
->hdl
.error
) {
383 ret
= vsen
->hdl
.error
;
387 /* Initialize ved and sd */
388 ret
= vimc_ent_sd_register(&vsen
->ved
, &vsen
->sd
, v4l2_dev
,
390 MEDIA_ENT_F_CAM_SENSOR
, 1,
391 (const unsigned long[1]) {MEDIA_PAD_FL_SOURCE
},
396 dev_set_drvdata(comp
, &vsen
->ved
);
399 /* Initialize the frame format */
400 vsen
->mbus_format
= fmt_default
;
402 /* Initialize the test pattern generator */
403 tpg_init(&vsen
->tpg
, vsen
->mbus_format
.width
,
404 vsen
->mbus_format
.height
);
405 ret
= tpg_alloc(&vsen
->tpg
, VIMC_FRAME_MAX_WIDTH
);
407 goto err_unregister_ent_sd
;
411 err_unregister_ent_sd
:
412 vimc_ent_sd_unregister(&vsen
->ved
, &vsen
->sd
);
414 v4l2_ctrl_handler_free(&vsen
->hdl
);
421 static const struct component_ops vimc_sen_comp_ops
= {
422 .bind
= vimc_sen_comp_bind
,
423 .unbind
= vimc_sen_comp_unbind
,
426 static int vimc_sen_probe(struct platform_device
*pdev
)
428 return component_add(&pdev
->dev
, &vimc_sen_comp_ops
);
431 static int vimc_sen_remove(struct platform_device
*pdev
)
433 component_del(&pdev
->dev
, &vimc_sen_comp_ops
);
438 static const struct platform_device_id vimc_sen_driver_ids
[] = {
440 .name
= VIMC_SEN_DRV_NAME
,
445 static struct platform_driver vimc_sen_pdrv
= {
446 .probe
= vimc_sen_probe
,
447 .remove
= vimc_sen_remove
,
448 .id_table
= vimc_sen_driver_ids
,
450 .name
= VIMC_SEN_DRV_NAME
,
454 module_platform_driver(vimc_sen_pdrv
);
456 MODULE_DEVICE_TABLE(platform
, vimc_sen_driver_ids
);
458 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Sensor");
459 MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
460 MODULE_LICENSE("GPL");