]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/media/platform/vimc/vimc-sensor.c
Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[mirror_ubuntu-hirsute-kernel.git] / drivers / media / platform / vimc / vimc-sensor.c
1 /*
2 * vimc-sensor.c Virtual Media Controller Driver
3 *
4 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
5 *
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.
10 *
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.
15 *
16 */
17
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>
28
29 #include "vimc-common.h"
30
31 #define VIMC_SEN_DRV_NAME "vimc-sensor"
32
33 struct vimc_sen_device {
34 struct vimc_ent_device ved;
35 struct v4l2_subdev sd;
36 struct device *dev;
37 struct tpg_data tpg;
38 struct task_struct *kthread_sen;
39 u8 *frame;
40 /* The active format */
41 struct v4l2_mbus_framefmt mbus_format;
42 struct v4l2_ctrl_handler hdl;
43 };
44
45 static const struct v4l2_mbus_framefmt fmt_default = {
46 .width = 640,
47 .height = 480,
48 .code = MEDIA_BUS_FMT_RGB888_1X24,
49 .field = V4L2_FIELD_NONE,
50 .colorspace = V4L2_COLORSPACE_DEFAULT,
51 };
52
53 static int vimc_sen_init_cfg(struct v4l2_subdev *sd,
54 struct v4l2_subdev_pad_config *cfg)
55 {
56 unsigned int i;
57
58 for (i = 0; i < sd->entity.num_pads; i++) {
59 struct v4l2_mbus_framefmt *mf;
60
61 mf = v4l2_subdev_get_try_format(sd, cfg, i);
62 *mf = fmt_default;
63 }
64
65 return 0;
66 }
67
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)
71 {
72 if (fse->index)
73 return -EINVAL;
74
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;
79
80 return 0;
81 }
82
83 static int vimc_sen_get_fmt(struct v4l2_subdev *sd,
84 struct v4l2_subdev_pad_config *cfg,
85 struct v4l2_subdev_format *fmt)
86 {
87 struct vimc_sen_device *vsen =
88 container_of(sd, struct vimc_sen_device, sd);
89
90 fmt->format = fmt->which == V4L2_SUBDEV_FORMAT_TRY ?
91 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) :
92 vsen->mbus_format;
93
94 return 0;
95 }
96
97 static void vimc_sen_tpg_s_format(struct vimc_sen_device *vsen)
98 {
99 u32 pixelformat = vsen->ved.stream->producer_pixfmt;
100 const struct v4l2_format_info *pix_info;
101
102 pix_info = v4l2_format_info(pixelformat);
103
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);
116 }
117
118 static void vimc_sen_adjust_fmt(struct v4l2_mbus_framefmt *fmt)
119 {
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;
124
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;
128
129 vimc_colorimetry_clamp(fmt);
130 }
131
132 static int vimc_sen_set_fmt(struct v4l2_subdev *sd,
133 struct v4l2_subdev_pad_config *cfg,
134 struct v4l2_subdev_format *fmt)
135 {
136 struct vimc_sen_device *vsen = v4l2_get_subdevdata(sd);
137 struct v4l2_mbus_framefmt *mf;
138
139 if (!vimc_mbus_code_supported(fmt->format.code))
140 fmt->format.code = fmt_default.code;
141
142 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
143 /* Do not change the format while stream is on */
144 if (vsen->frame)
145 return -EBUSY;
146
147 mf = &vsen->mbus_format;
148 } else {
149 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
150 }
151
152 /* Set the new format */
153 vimc_sen_adjust_fmt(&fmt->format);
154
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,
158 /* old */
159 mf->width, mf->height, mf->code,
160 mf->colorspace, mf->quantization,
161 mf->xfer_func, mf->ycbcr_enc,
162 /* new */
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);
166
167 *mf = fmt->format;
168
169 return 0;
170 }
171
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,
178 };
179
180 static void *vimc_sen_process_frame(struct vimc_ent_device *ved,
181 const void *sink_frame)
182 {
183 struct vimc_sen_device *vsen = container_of(ved, struct vimc_sen_device,
184 ved);
185
186 tpg_fill_plane_buffer(&vsen->tpg, 0, 0, vsen->frame);
187 return vsen->frame;
188 }
189
190 static int vimc_sen_s_stream(struct v4l2_subdev *sd, int enable)
191 {
192 struct vimc_sen_device *vsen =
193 container_of(sd, struct vimc_sen_device, sd);
194
195 if (enable) {
196 u32 pixelformat = vsen->ved.stream->producer_pixfmt;
197 const struct v4l2_format_info *pix_info;
198 unsigned int frame_size;
199
200 if (vsen->kthread_sen)
201 /* tpg is already executing */
202 return 0;
203
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;
208
209 /*
210 * Allocate the frame buffer. Use vmalloc to be able to
211 * allocate a large amount of memory
212 */
213 vsen->frame = vmalloc(frame_size);
214 if (!vsen->frame)
215 return -ENOMEM;
216
217 /* configure the test pattern generator */
218 vimc_sen_tpg_s_format(vsen);
219
220 } else {
221
222 vfree(vsen->frame);
223 vsen->frame = NULL;
224 return 0;
225 }
226
227 return 0;
228 }
229
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,
234 };
235
236 static const struct v4l2_subdev_video_ops vimc_sen_video_ops = {
237 .s_stream = vimc_sen_s_stream,
238 };
239
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,
244 };
245
246 static int vimc_sen_s_ctrl(struct v4l2_ctrl *ctrl)
247 {
248 struct vimc_sen_device *vsen =
249 container_of(ctrl->handler, struct vimc_sen_device, hdl);
250
251 switch (ctrl->id) {
252 case VIMC_CID_TEST_PATTERN:
253 tpg_s_pattern(&vsen->tpg, ctrl->val);
254 break;
255 case V4L2_CID_HFLIP:
256 tpg_s_hflip(&vsen->tpg, ctrl->val);
257 break;
258 case V4L2_CID_VFLIP:
259 tpg_s_vflip(&vsen->tpg, ctrl->val);
260 break;
261 case V4L2_CID_BRIGHTNESS:
262 tpg_s_brightness(&vsen->tpg, ctrl->val);
263 break;
264 case V4L2_CID_CONTRAST:
265 tpg_s_contrast(&vsen->tpg, ctrl->val);
266 break;
267 case V4L2_CID_HUE:
268 tpg_s_hue(&vsen->tpg, ctrl->val);
269 break;
270 case V4L2_CID_SATURATION:
271 tpg_s_saturation(&vsen->tpg, ctrl->val);
272 break;
273 default:
274 return -EINVAL;
275 }
276 return 0;
277 }
278
279 static const struct v4l2_ctrl_ops vimc_sen_ctrl_ops = {
280 .s_ctrl = vimc_sen_s_ctrl,
281 };
282
283 static void vimc_sen_release(struct v4l2_subdev *sd)
284 {
285 struct vimc_sen_device *vsen =
286 container_of(sd, struct vimc_sen_device, sd);
287
288 v4l2_ctrl_handler_free(&vsen->hdl);
289 tpg_free(&vsen->tpg);
290 kfree(vsen);
291 }
292
293 static const struct v4l2_subdev_internal_ops vimc_sen_int_ops = {
294 .release = vimc_sen_release,
295 };
296
297 static void vimc_sen_comp_unbind(struct device *comp, struct device *master,
298 void *master_data)
299 {
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);
303
304 vimc_ent_sd_unregister(ved, &vsen->sd);
305 }
306
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,
313 };
314
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,
322 };
323
324 static int vimc_sen_comp_bind(struct device *comp, struct device *master,
325 void *master_data)
326 {
327 struct v4l2_device *v4l2_dev = master_data;
328 struct vimc_platform_data *pdata = comp->platform_data;
329 struct vimc_sen_device *vsen;
330 int ret;
331
332 /* Allocate the vsen struct */
333 vsen = kzalloc(sizeof(*vsen), GFP_KERNEL);
334 if (!vsen)
335 return -ENOMEM;
336
337 v4l2_ctrl_handler_init(&vsen->hdl, 4);
338
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;
356 goto err_free_vsen;
357 }
358
359 /* Initialize ved and sd */
360 ret = vimc_ent_sd_register(&vsen->ved, &vsen->sd, v4l2_dev,
361 pdata->entity_name,
362 MEDIA_ENT_F_CAM_SENSOR, 1,
363 (const unsigned long[1]) {MEDIA_PAD_FL_SOURCE},
364 &vimc_sen_int_ops, &vimc_sen_ops);
365 if (ret)
366 goto err_free_hdl;
367
368 vsen->ved.process_frame = vimc_sen_process_frame;
369 dev_set_drvdata(comp, &vsen->ved);
370 vsen->dev = comp;
371
372 /* Initialize the frame format */
373 vsen->mbus_format = fmt_default;
374
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);
379 if (ret)
380 goto err_unregister_ent_sd;
381
382 return 0;
383
384 err_unregister_ent_sd:
385 vimc_ent_sd_unregister(&vsen->ved, &vsen->sd);
386 err_free_hdl:
387 v4l2_ctrl_handler_free(&vsen->hdl);
388 err_free_vsen:
389 kfree(vsen);
390
391 return ret;
392 }
393
394 static const struct component_ops vimc_sen_comp_ops = {
395 .bind = vimc_sen_comp_bind,
396 .unbind = vimc_sen_comp_unbind,
397 };
398
399 static int vimc_sen_probe(struct platform_device *pdev)
400 {
401 return component_add(&pdev->dev, &vimc_sen_comp_ops);
402 }
403
404 static int vimc_sen_remove(struct platform_device *pdev)
405 {
406 component_del(&pdev->dev, &vimc_sen_comp_ops);
407
408 return 0;
409 }
410
411 static const struct platform_device_id vimc_sen_driver_ids[] = {
412 {
413 .name = VIMC_SEN_DRV_NAME,
414 },
415 { }
416 };
417
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,
422 .driver = {
423 .name = VIMC_SEN_DRV_NAME,
424 },
425 };
426
427 module_platform_driver(vimc_sen_pdrv);
428
429 MODULE_DEVICE_TABLE(platform, vimc_sen_driver_ids);
430
431 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Sensor");
432 MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
433 MODULE_LICENSE("GPL");