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/platform_device.h>
21 #include <linux/v4l2-mediabus.h>
22 #include <linux/vmalloc.h>
23 #include <media/v4l2-subdev.h>
24 #include <media/v4l2-tpg.h>
26 #include "vimc-common.h"
28 #define VIMC_SEN_DRV_NAME "vimc-sensor"
30 struct vimc_sen_device {
31 struct vimc_ent_device ved;
32 struct v4l2_subdev sd;
35 struct task_struct *kthread_sen;
37 /* The active format */
38 struct v4l2_mbus_framefmt mbus_format;
41 static const struct v4l2_mbus_framefmt fmt_default = {
44 .code = MEDIA_BUS_FMT_RGB888_1X24,
45 .field = V4L2_FIELD_NONE,
46 .colorspace = V4L2_COLORSPACE_DEFAULT,
49 static int vimc_sen_init_cfg(struct v4l2_subdev *sd,
50 struct v4l2_subdev_pad_config *cfg)
54 for (i = 0; i < sd->entity.num_pads; i++) {
55 struct v4l2_mbus_framefmt *mf;
57 mf = v4l2_subdev_get_try_format(sd, cfg, i);
64 static int vimc_sen_enum_mbus_code(struct v4l2_subdev *sd,
65 struct v4l2_subdev_pad_config *cfg,
66 struct v4l2_subdev_mbus_code_enum *code)
68 const struct vimc_pix_map *vpix = vimc_pix_map_by_index(code->index);
73 code->code = vpix->code;
78 static int vimc_sen_enum_frame_size(struct v4l2_subdev *sd,
79 struct v4l2_subdev_pad_config *cfg,
80 struct v4l2_subdev_frame_size_enum *fse)
82 const struct vimc_pix_map *vpix;
87 /* Only accept code in the pix map table */
88 vpix = vimc_pix_map_by_code(fse->code);
92 fse->min_width = VIMC_FRAME_MIN_WIDTH;
93 fse->max_width = VIMC_FRAME_MAX_WIDTH;
94 fse->min_height = VIMC_FRAME_MIN_HEIGHT;
95 fse->max_height = VIMC_FRAME_MAX_HEIGHT;
100 static int vimc_sen_get_fmt(struct v4l2_subdev *sd,
101 struct v4l2_subdev_pad_config *cfg,
102 struct v4l2_subdev_format *fmt)
104 struct vimc_sen_device *vsen =
105 container_of(sd, struct vimc_sen_device, sd);
107 fmt->format = fmt->which == V4L2_SUBDEV_FORMAT_TRY ?
108 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) :
114 static void vimc_sen_tpg_s_format(struct vimc_sen_device *vsen)
116 const struct vimc_pix_map *vpix =
117 vimc_pix_map_by_code(vsen->mbus_format.code);
119 tpg_reset_source(&vsen->tpg, vsen->mbus_format.width,
120 vsen->mbus_format.height, vsen->mbus_format.field);
121 tpg_s_bytesperline(&vsen->tpg, 0, vsen->mbus_format.width * vpix->bpp);
122 tpg_s_buf_height(&vsen->tpg, vsen->mbus_format.height);
123 tpg_s_fourcc(&vsen->tpg, vpix->pixelformat);
124 /* TODO: add support for V4L2_FIELD_ALTERNATE */
125 tpg_s_field(&vsen->tpg, vsen->mbus_format.field, false);
126 tpg_s_colorspace(&vsen->tpg, vsen->mbus_format.colorspace);
127 tpg_s_ycbcr_enc(&vsen->tpg, vsen->mbus_format.ycbcr_enc);
128 tpg_s_quantization(&vsen->tpg, vsen->mbus_format.quantization);
129 tpg_s_xfer_func(&vsen->tpg, vsen->mbus_format.xfer_func);
132 static void vimc_sen_adjust_fmt(struct v4l2_mbus_framefmt *fmt)
134 const struct vimc_pix_map *vpix;
136 /* Only accept code in the pix map table */
137 vpix = vimc_pix_map_by_code(fmt->code);
139 fmt->code = fmt_default.code;
141 fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
142 VIMC_FRAME_MAX_WIDTH) & ~1;
143 fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
144 VIMC_FRAME_MAX_HEIGHT) & ~1;
146 /* TODO: add support for V4L2_FIELD_ALTERNATE */
147 if (fmt->field == V4L2_FIELD_ANY || fmt->field == V4L2_FIELD_ALTERNATE)
148 fmt->field = fmt_default.field;
150 vimc_colorimetry_clamp(fmt);
153 static int vimc_sen_set_fmt(struct v4l2_subdev *sd,
154 struct v4l2_subdev_pad_config *cfg,
155 struct v4l2_subdev_format *fmt)
157 struct vimc_sen_device *vsen = v4l2_get_subdevdata(sd);
158 struct v4l2_mbus_framefmt *mf;
160 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
161 /* Do not change the format while stream is on */
165 mf = &vsen->mbus_format;
167 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
170 /* Set the new format */
171 vimc_sen_adjust_fmt(&fmt->format);
173 dev_dbg(vsen->dev, "%s: format update: "
174 "old:%dx%d (0x%x, %d, %d, %d, %d) "
175 "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsen->sd.name,
177 mf->width, mf->height, mf->code,
178 mf->colorspace, mf->quantization,
179 mf->xfer_func, mf->ycbcr_enc,
181 fmt->format.width, fmt->format.height, fmt->format.code,
182 fmt->format.colorspace, fmt->format.quantization,
183 fmt->format.xfer_func, fmt->format.ycbcr_enc);
190 static const struct v4l2_subdev_pad_ops vimc_sen_pad_ops = {
191 .init_cfg = vimc_sen_init_cfg,
192 .enum_mbus_code = vimc_sen_enum_mbus_code,
193 .enum_frame_size = vimc_sen_enum_frame_size,
194 .get_fmt = vimc_sen_get_fmt,
195 .set_fmt = vimc_sen_set_fmt,
198 static void *vimc_sen_process_frame(struct vimc_ent_device *ved,
199 const void *sink_frame)
201 struct vimc_sen_device *vsen = container_of(ved, struct vimc_sen_device,
204 tpg_fill_plane_buffer(&vsen->tpg, 0, 0, vsen->frame);
208 static int vimc_sen_s_stream(struct v4l2_subdev *sd, int enable)
210 struct vimc_sen_device *vsen =
211 container_of(sd, struct vimc_sen_device, sd);
214 const struct vimc_pix_map *vpix;
215 unsigned int frame_size;
217 if (vsen->kthread_sen)
218 /* tpg is already executing */
221 /* Calculate the frame size */
222 vpix = vimc_pix_map_by_code(vsen->mbus_format.code);
223 frame_size = vsen->mbus_format.width * vpix->bpp *
224 vsen->mbus_format.height;
227 * Allocate the frame buffer. Use vmalloc to be able to
228 * allocate a large amount of memory
230 vsen->frame = vmalloc(frame_size);
234 /* configure the test pattern generator */
235 vimc_sen_tpg_s_format(vsen);
247 static const struct v4l2_subdev_video_ops vimc_sen_video_ops = {
248 .s_stream = vimc_sen_s_stream,
251 static const struct v4l2_subdev_ops vimc_sen_ops = {
252 .pad = &vimc_sen_pad_ops,
253 .video = &vimc_sen_video_ops,
256 static void vimc_sen_comp_unbind(struct device *comp, struct device *master,
259 struct vimc_ent_device *ved = dev_get_drvdata(comp);
260 struct vimc_sen_device *vsen =
261 container_of(ved, struct vimc_sen_device, ved);
263 vimc_ent_sd_unregister(ved, &vsen->sd);
264 tpg_free(&vsen->tpg);
268 static int vimc_sen_comp_bind(struct device *comp, struct device *master,
271 struct v4l2_device *v4l2_dev = master_data;
272 struct vimc_platform_data *pdata = comp->platform_data;
273 struct vimc_sen_device *vsen;
276 /* Allocate the vsen struct */
277 vsen = kzalloc(sizeof(*vsen), GFP_KERNEL);
281 /* Initialize ved and sd */
282 ret = vimc_ent_sd_register(&vsen->ved, &vsen->sd, v4l2_dev,
284 MEDIA_ENT_F_ATV_DECODER, 1,
285 (const unsigned long[1]) {MEDIA_PAD_FL_SOURCE},
290 vsen->ved.process_frame = vimc_sen_process_frame;
291 dev_set_drvdata(comp, &vsen->ved);
294 /* Initialize the frame format */
295 vsen->mbus_format = fmt_default;
297 /* Initialize the test pattern generator */
298 tpg_init(&vsen->tpg, vsen->mbus_format.width,
299 vsen->mbus_format.height);
300 ret = tpg_alloc(&vsen->tpg, VIMC_FRAME_MAX_WIDTH);
302 goto err_unregister_ent_sd;
306 err_unregister_ent_sd:
307 vimc_ent_sd_unregister(&vsen->ved, &vsen->sd);
314 static const struct component_ops vimc_sen_comp_ops = {
315 .bind = vimc_sen_comp_bind,
316 .unbind = vimc_sen_comp_unbind,
319 static int vimc_sen_probe(struct platform_device *pdev)
321 return component_add(&pdev->dev, &vimc_sen_comp_ops);
324 static int vimc_sen_remove(struct platform_device *pdev)
326 component_del(&pdev->dev, &vimc_sen_comp_ops);
331 static const struct platform_device_id vimc_sen_driver_ids[] = {
333 .name = VIMC_SEN_DRV_NAME,
338 static struct platform_driver vimc_sen_pdrv = {
339 .probe = vimc_sen_probe,
340 .remove = vimc_sen_remove,
341 .id_table = vimc_sen_driver_ids,
343 .name = VIMC_SEN_DRV_NAME,
347 module_platform_driver(vimc_sen_pdrv);
349 MODULE_DEVICE_TABLE(platform, vimc_sen_driver_ids);
351 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Sensor");
352 MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
353 MODULE_LICENSE("GPL");