4a6a7e8e66c27571af4ffa984cff9cc2b9b5e7b7
[platform/kernel/linux-rpi.git] / drivers / media / platform / vimc / vimc-sensor.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * vimc-sensor.c Virtual Media Controller Driver
4  *
5  * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
6  */
7
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>
18
19 #include "vimc-common.h"
20
21 #define VIMC_SEN_DRV_NAME "vimc-sensor"
22
23 struct vimc_sen_device {
24         struct vimc_ent_device ved;
25         struct v4l2_subdev sd;
26         struct device *dev;
27         struct tpg_data tpg;
28         u8 *frame;
29         /* The active format */
30         struct v4l2_mbus_framefmt mbus_format;
31         struct v4l2_ctrl_handler hdl;
32 };
33
34 static const struct v4l2_mbus_framefmt fmt_default = {
35         .width = 640,
36         .height = 480,
37         .code = MEDIA_BUS_FMT_RGB888_1X24,
38         .field = V4L2_FIELD_NONE,
39         .colorspace = V4L2_COLORSPACE_DEFAULT,
40 };
41
42 static int vimc_sen_init_cfg(struct v4l2_subdev *sd,
43                              struct v4l2_subdev_pad_config *cfg)
44 {
45         unsigned int i;
46
47         for (i = 0; i < sd->entity.num_pads; i++) {
48                 struct v4l2_mbus_framefmt *mf;
49
50                 mf = v4l2_subdev_get_try_format(sd, cfg, i);
51                 *mf = fmt_default;
52         }
53
54         return 0;
55 }
56
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)
60 {
61         const struct vimc_pix_map *vpix = vimc_pix_map_by_index(code->index);
62
63         if (!vpix)
64                 return -EINVAL;
65
66         code->code = vpix->code;
67
68         return 0;
69 }
70
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)
74 {
75         const struct vimc_pix_map *vpix;
76
77         if (fse->index)
78                 return -EINVAL;
79
80         /* Only accept code in the pix map table */
81         vpix = vimc_pix_map_by_code(fse->code);
82         if (!vpix)
83                 return -EINVAL;
84
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;
89
90         return 0;
91 }
92
93 static int vimc_sen_get_fmt(struct v4l2_subdev *sd,
94                             struct v4l2_subdev_pad_config *cfg,
95                             struct v4l2_subdev_format *fmt)
96 {
97         struct vimc_sen_device *vsen =
98                                 container_of(sd, struct vimc_sen_device, sd);
99
100         fmt->format = fmt->which == V4L2_SUBDEV_FORMAT_TRY ?
101                       *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) :
102                       vsen->mbus_format;
103
104         return 0;
105 }
106
107 static void vimc_sen_tpg_s_format(struct vimc_sen_device *vsen)
108 {
109         const struct vimc_pix_map *vpix =
110                                 vimc_pix_map_by_code(vsen->mbus_format.code);
111
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);
123 }
124
125 static void vimc_sen_adjust_fmt(struct v4l2_mbus_framefmt *fmt)
126 {
127         const struct vimc_pix_map *vpix;
128
129         /* Only accept code in the pix map table */
130         vpix = vimc_pix_map_by_code(fmt->code);
131         if (!vpix)
132                 fmt->code = fmt_default.code;
133
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;
138
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;
142
143         vimc_colorimetry_clamp(fmt);
144 }
145
146 static int vimc_sen_set_fmt(struct v4l2_subdev *sd,
147                             struct v4l2_subdev_pad_config *cfg,
148                             struct v4l2_subdev_format *fmt)
149 {
150         struct vimc_sen_device *vsen = v4l2_get_subdevdata(sd);
151         struct v4l2_mbus_framefmt *mf;
152
153         if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
154                 /* Do not change the format while stream is on */
155                 if (vsen->frame)
156                         return -EBUSY;
157
158                 mf = &vsen->mbus_format;
159         } else {
160                 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
161         }
162
163         /* Set the new format */
164         vimc_sen_adjust_fmt(&fmt->format);
165
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,
169                 /* old */
170                 mf->width, mf->height, mf->code,
171                 mf->colorspace, mf->quantization,
172                 mf->xfer_func, mf->ycbcr_enc,
173                 /* new */
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);
177
178         *mf = fmt->format;
179
180         return 0;
181 }
182
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,
189 };
190
191 static void *vimc_sen_process_frame(struct vimc_ent_device *ved,
192                                     const void *sink_frame)
193 {
194         struct vimc_sen_device *vsen = container_of(ved, struct vimc_sen_device,
195                                                     ved);
196
197         tpg_fill_plane_buffer(&vsen->tpg, 0, 0, vsen->frame);
198         return vsen->frame;
199 }
200
201 static int vimc_sen_s_stream(struct v4l2_subdev *sd, int enable)
202 {
203         struct vimc_sen_device *vsen =
204                                 container_of(sd, struct vimc_sen_device, sd);
205
206         if (enable) {
207                 const struct vimc_pix_map *vpix;
208                 unsigned int frame_size;
209
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;
214
215                 /*
216                  * Allocate the frame buffer. Use vmalloc to be able to
217                  * allocate a large amount of memory
218                  */
219                 vsen->frame = vmalloc(frame_size);
220                 if (!vsen->frame)
221                         return -ENOMEM;
222
223                 /* configure the test pattern generator */
224                 vimc_sen_tpg_s_format(vsen);
225
226         } else {
227
228                 vfree(vsen->frame);
229                 vsen->frame = NULL;
230         }
231
232         return 0;
233 }
234
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,
239 };
240
241 static const struct v4l2_subdev_video_ops vimc_sen_video_ops = {
242         .s_stream = vimc_sen_s_stream,
243 };
244
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,
249 };
250
251 static int vimc_sen_s_ctrl(struct v4l2_ctrl *ctrl)
252 {
253         struct vimc_sen_device *vsen =
254                 container_of(ctrl->handler, struct vimc_sen_device, hdl);
255
256         switch (ctrl->id) {
257         case VIMC_CID_TEST_PATTERN:
258                 tpg_s_pattern(&vsen->tpg, ctrl->val);
259                 break;
260         case V4L2_CID_HFLIP:
261                 tpg_s_hflip(&vsen->tpg, ctrl->val);
262                 break;
263         case V4L2_CID_VFLIP:
264                 tpg_s_vflip(&vsen->tpg, ctrl->val);
265                 break;
266         case V4L2_CID_BRIGHTNESS:
267                 tpg_s_brightness(&vsen->tpg, ctrl->val);
268                 break;
269         case V4L2_CID_CONTRAST:
270                 tpg_s_contrast(&vsen->tpg, ctrl->val);
271                 break;
272         case V4L2_CID_HUE:
273                 tpg_s_hue(&vsen->tpg, ctrl->val);
274                 break;
275         case V4L2_CID_SATURATION:
276                 tpg_s_saturation(&vsen->tpg, ctrl->val);
277                 break;
278         default:
279                 return -EINVAL;
280         }
281         return 0;
282 }
283
284 static const struct v4l2_ctrl_ops vimc_sen_ctrl_ops = {
285         .s_ctrl = vimc_sen_s_ctrl,
286 };
287
288 static void vimc_sen_release(struct v4l2_subdev *sd)
289 {
290         struct vimc_sen_device *vsen =
291                                 container_of(sd, struct vimc_sen_device, sd);
292
293         v4l2_ctrl_handler_free(&vsen->hdl);
294         tpg_free(&vsen->tpg);
295         kfree(vsen);
296 }
297
298 static const struct v4l2_subdev_internal_ops vimc_sen_int_ops = {
299         .release = vimc_sen_release,
300 };
301
302 static void vimc_sen_comp_unbind(struct device *comp, struct device *master,
303                                  void *master_data)
304 {
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);
308
309         vimc_ent_sd_unregister(ved, &vsen->sd);
310 }
311
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,
318 };
319
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,
327 };
328
329 static int vimc_sen_comp_bind(struct device *comp, struct device *master,
330                               void *master_data)
331 {
332         struct v4l2_device *v4l2_dev = master_data;
333         struct vimc_platform_data *pdata = comp->platform_data;
334         struct vimc_sen_device *vsen;
335         int ret;
336
337         /* Allocate the vsen struct */
338         vsen = kzalloc(sizeof(*vsen), GFP_KERNEL);
339         if (!vsen)
340                 return -ENOMEM;
341
342         v4l2_ctrl_handler_init(&vsen->hdl, 4);
343
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;
361                 goto err_free_vsen;
362         }
363
364         /* Initialize ved and sd */
365         ret = vimc_ent_sd_register(&vsen->ved, &vsen->sd, v4l2_dev,
366                                    pdata->entity_name,
367                                    MEDIA_ENT_F_CAM_SENSOR, 1,
368                                    (const unsigned long[1]) {MEDIA_PAD_FL_SOURCE},
369                                    &vimc_sen_int_ops, &vimc_sen_ops);
370         if (ret)
371                 goto err_free_hdl;
372
373         vsen->ved.process_frame = vimc_sen_process_frame;
374         dev_set_drvdata(comp, &vsen->ved);
375         vsen->dev = comp;
376
377         /* Initialize the frame format */
378         vsen->mbus_format = fmt_default;
379
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);
384         if (ret)
385                 goto err_unregister_ent_sd;
386
387         return 0;
388
389 err_unregister_ent_sd:
390         vimc_ent_sd_unregister(&vsen->ved,  &vsen->sd);
391 err_free_hdl:
392         v4l2_ctrl_handler_free(&vsen->hdl);
393 err_free_vsen:
394         kfree(vsen);
395
396         return ret;
397 }
398
399 static const struct component_ops vimc_sen_comp_ops = {
400         .bind = vimc_sen_comp_bind,
401         .unbind = vimc_sen_comp_unbind,
402 };
403
404 static int vimc_sen_probe(struct platform_device *pdev)
405 {
406         return component_add(&pdev->dev, &vimc_sen_comp_ops);
407 }
408
409 static int vimc_sen_remove(struct platform_device *pdev)
410 {
411         component_del(&pdev->dev, &vimc_sen_comp_ops);
412
413         return 0;
414 }
415
416 static const struct platform_device_id vimc_sen_driver_ids[] = {
417         {
418                 .name           = VIMC_SEN_DRV_NAME,
419         },
420         { }
421 };
422
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,
427         .driver         = {
428                 .name   = VIMC_SEN_DRV_NAME,
429         },
430 };
431
432 module_platform_driver(vimc_sen_pdrv);
433
434 MODULE_DEVICE_TABLE(platform, vimc_sen_driver_ids);
435
436 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Sensor");
437 MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
438 MODULE_LICENSE("GPL");