GNU Linux-libre 4.19.286-gnu1
[releases.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_mbus_code(struct v4l2_subdev *sd,
69                                    struct v4l2_subdev_pad_config *cfg,
70                                    struct v4l2_subdev_mbus_code_enum *code)
71 {
72         const struct vimc_pix_map *vpix = vimc_pix_map_by_index(code->index);
73
74         if (!vpix)
75                 return -EINVAL;
76
77         code->code = vpix->code;
78
79         return 0;
80 }
81
82 static int vimc_sen_enum_frame_size(struct v4l2_subdev *sd,
83                                     struct v4l2_subdev_pad_config *cfg,
84                                     struct v4l2_subdev_frame_size_enum *fse)
85 {
86         const struct vimc_pix_map *vpix;
87
88         if (fse->index)
89                 return -EINVAL;
90
91         /* Only accept code in the pix map table */
92         vpix = vimc_pix_map_by_code(fse->code);
93         if (!vpix)
94                 return -EINVAL;
95
96         fse->min_width = VIMC_FRAME_MIN_WIDTH;
97         fse->max_width = VIMC_FRAME_MAX_WIDTH;
98         fse->min_height = VIMC_FRAME_MIN_HEIGHT;
99         fse->max_height = VIMC_FRAME_MAX_HEIGHT;
100
101         return 0;
102 }
103
104 static int vimc_sen_get_fmt(struct v4l2_subdev *sd,
105                             struct v4l2_subdev_pad_config *cfg,
106                             struct v4l2_subdev_format *fmt)
107 {
108         struct vimc_sen_device *vsen =
109                                 container_of(sd, struct vimc_sen_device, sd);
110
111         fmt->format = fmt->which == V4L2_SUBDEV_FORMAT_TRY ?
112                       *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) :
113                       vsen->mbus_format;
114
115         return 0;
116 }
117
118 static void vimc_sen_tpg_s_format(struct vimc_sen_device *vsen)
119 {
120         const struct vimc_pix_map *vpix =
121                                 vimc_pix_map_by_code(vsen->mbus_format.code);
122
123         tpg_reset_source(&vsen->tpg, vsen->mbus_format.width,
124                          vsen->mbus_format.height, vsen->mbus_format.field);
125         tpg_s_bytesperline(&vsen->tpg, 0, vsen->mbus_format.width * vpix->bpp);
126         tpg_s_buf_height(&vsen->tpg, vsen->mbus_format.height);
127         tpg_s_fourcc(&vsen->tpg, vpix->pixelformat);
128         /* TODO: add support for V4L2_FIELD_ALTERNATE */
129         tpg_s_field(&vsen->tpg, vsen->mbus_format.field, false);
130         tpg_s_colorspace(&vsen->tpg, vsen->mbus_format.colorspace);
131         tpg_s_ycbcr_enc(&vsen->tpg, vsen->mbus_format.ycbcr_enc);
132         tpg_s_quantization(&vsen->tpg, vsen->mbus_format.quantization);
133         tpg_s_xfer_func(&vsen->tpg, vsen->mbus_format.xfer_func);
134 }
135
136 static void vimc_sen_adjust_fmt(struct v4l2_mbus_framefmt *fmt)
137 {
138         const struct vimc_pix_map *vpix;
139
140         /* Only accept code in the pix map table */
141         vpix = vimc_pix_map_by_code(fmt->code);
142         if (!vpix)
143                 fmt->code = fmt_default.code;
144
145         fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
146                              VIMC_FRAME_MAX_WIDTH) & ~1;
147         fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
148                               VIMC_FRAME_MAX_HEIGHT) & ~1;
149
150         /* TODO: add support for V4L2_FIELD_ALTERNATE */
151         if (fmt->field == V4L2_FIELD_ANY || fmt->field == V4L2_FIELD_ALTERNATE)
152                 fmt->field = fmt_default.field;
153
154         vimc_colorimetry_clamp(fmt);
155 }
156
157 static int vimc_sen_set_fmt(struct v4l2_subdev *sd,
158                             struct v4l2_subdev_pad_config *cfg,
159                             struct v4l2_subdev_format *fmt)
160 {
161         struct vimc_sen_device *vsen = v4l2_get_subdevdata(sd);
162         struct v4l2_mbus_framefmt *mf;
163
164         if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
165                 /* Do not change the format while stream is on */
166                 if (vsen->frame)
167                         return -EBUSY;
168
169                 mf = &vsen->mbus_format;
170         } else {
171                 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
172         }
173
174         /* Set the new format */
175         vimc_sen_adjust_fmt(&fmt->format);
176
177         dev_dbg(vsen->dev, "%s: format update: "
178                 "old:%dx%d (0x%x, %d, %d, %d, %d) "
179                 "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsen->sd.name,
180                 /* old */
181                 mf->width, mf->height, mf->code,
182                 mf->colorspace, mf->quantization,
183                 mf->xfer_func, mf->ycbcr_enc,
184                 /* new */
185                 fmt->format.width, fmt->format.height, fmt->format.code,
186                 fmt->format.colorspace, fmt->format.quantization,
187                 fmt->format.xfer_func, fmt->format.ycbcr_enc);
188
189         *mf = fmt->format;
190
191         return 0;
192 }
193
194 static const struct v4l2_subdev_pad_ops vimc_sen_pad_ops = {
195         .init_cfg               = vimc_sen_init_cfg,
196         .enum_mbus_code         = vimc_sen_enum_mbus_code,
197         .enum_frame_size        = vimc_sen_enum_frame_size,
198         .get_fmt                = vimc_sen_get_fmt,
199         .set_fmt                = vimc_sen_set_fmt,
200 };
201
202 static void *vimc_sen_process_frame(struct vimc_ent_device *ved,
203                                     const void *sink_frame)
204 {
205         struct vimc_sen_device *vsen = container_of(ved, struct vimc_sen_device,
206                                                     ved);
207
208         tpg_fill_plane_buffer(&vsen->tpg, 0, 0, vsen->frame);
209         return vsen->frame;
210 }
211
212 static int vimc_sen_s_stream(struct v4l2_subdev *sd, int enable)
213 {
214         struct vimc_sen_device *vsen =
215                                 container_of(sd, struct vimc_sen_device, sd);
216
217         if (enable) {
218                 const struct vimc_pix_map *vpix;
219                 unsigned int frame_size;
220
221                 if (vsen->kthread_sen)
222                         /* tpg is already executing */
223                         return 0;
224
225                 /* Calculate the frame size */
226                 vpix = vimc_pix_map_by_code(vsen->mbus_format.code);
227                 frame_size = vsen->mbus_format.width * vpix->bpp *
228                              vsen->mbus_format.height;
229
230                 /*
231                  * Allocate the frame buffer. Use vmalloc to be able to
232                  * allocate a large amount of memory
233                  */
234                 vsen->frame = vmalloc(frame_size);
235                 if (!vsen->frame)
236                         return -ENOMEM;
237
238                 /* configure the test pattern generator */
239                 vimc_sen_tpg_s_format(vsen);
240
241         } else {
242
243                 vfree(vsen->frame);
244                 vsen->frame = NULL;
245                 return 0;
246         }
247
248         return 0;
249 }
250
251 static struct v4l2_subdev_core_ops vimc_sen_core_ops = {
252         .log_status = v4l2_ctrl_subdev_log_status,
253         .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
254         .unsubscribe_event = v4l2_event_subdev_unsubscribe,
255 };
256
257 static const struct v4l2_subdev_video_ops vimc_sen_video_ops = {
258         .s_stream = vimc_sen_s_stream,
259 };
260
261 static const struct v4l2_subdev_ops vimc_sen_ops = {
262         .core = &vimc_sen_core_ops,
263         .pad = &vimc_sen_pad_ops,
264         .video = &vimc_sen_video_ops,
265 };
266
267 static int vimc_sen_s_ctrl(struct v4l2_ctrl *ctrl)
268 {
269         struct vimc_sen_device *vsen =
270                 container_of(ctrl->handler, struct vimc_sen_device, hdl);
271
272         switch (ctrl->id) {
273         case VIMC_CID_TEST_PATTERN:
274                 tpg_s_pattern(&vsen->tpg, ctrl->val);
275                 break;
276         case V4L2_CID_HFLIP:
277                 tpg_s_hflip(&vsen->tpg, ctrl->val);
278                 break;
279         case V4L2_CID_VFLIP:
280                 tpg_s_vflip(&vsen->tpg, ctrl->val);
281                 break;
282         default:
283                 return -EINVAL;
284         }
285         return 0;
286 }
287
288 static const struct v4l2_ctrl_ops vimc_sen_ctrl_ops = {
289         .s_ctrl = vimc_sen_s_ctrl,
290 };
291
292 static void vimc_sen_comp_unbind(struct device *comp, struct device *master,
293                                  void *master_data)
294 {
295         struct vimc_ent_device *ved = dev_get_drvdata(comp);
296         struct vimc_sen_device *vsen =
297                                 container_of(ved, struct vimc_sen_device, ved);
298
299         vimc_ent_sd_unregister(ved, &vsen->sd);
300         v4l2_ctrl_handler_free(&vsen->hdl);
301         tpg_free(&vsen->tpg);
302         kfree(vsen);
303 }
304
305 /* Image Processing Controls */
306 static const struct v4l2_ctrl_config vimc_sen_ctrl_class = {
307         .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY,
308         .id = VIMC_CID_VIMC_CLASS,
309         .name = "VIMC Controls",
310         .type = V4L2_CTRL_TYPE_CTRL_CLASS,
311 };
312
313 static const struct v4l2_ctrl_config vimc_sen_ctrl_test_pattern = {
314         .ops = &vimc_sen_ctrl_ops,
315         .id = VIMC_CID_TEST_PATTERN,
316         .name = "Test Pattern",
317         .type = V4L2_CTRL_TYPE_MENU,
318         .max = TPG_PAT_NOISE,
319         .qmenu = tpg_pattern_strings,
320 };
321
322 static int vimc_sen_comp_bind(struct device *comp, struct device *master,
323                               void *master_data)
324 {
325         struct v4l2_device *v4l2_dev = master_data;
326         struct vimc_platform_data *pdata = comp->platform_data;
327         struct vimc_sen_device *vsen;
328         int ret;
329
330         /* Allocate the vsen struct */
331         vsen = kzalloc(sizeof(*vsen), GFP_KERNEL);
332         if (!vsen)
333                 return -ENOMEM;
334
335         v4l2_ctrl_handler_init(&vsen->hdl, 4);
336
337         v4l2_ctrl_new_custom(&vsen->hdl, &vimc_sen_ctrl_class, NULL);
338         v4l2_ctrl_new_custom(&vsen->hdl, &vimc_sen_ctrl_test_pattern, NULL);
339         v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
340                           V4L2_CID_VFLIP, 0, 1, 1, 0);
341         v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
342                           V4L2_CID_HFLIP, 0, 1, 1, 0);
343         vsen->sd.ctrl_handler = &vsen->hdl;
344         if (vsen->hdl.error) {
345                 ret = vsen->hdl.error;
346                 goto err_free_vsen;
347         }
348
349         /* Initialize ved and sd */
350         ret = vimc_ent_sd_register(&vsen->ved, &vsen->sd, v4l2_dev,
351                                    pdata->entity_name,
352                                    MEDIA_ENT_F_CAM_SENSOR, 1,
353                                    (const unsigned long[1]) {MEDIA_PAD_FL_SOURCE},
354                                    &vimc_sen_ops);
355         if (ret)
356                 goto err_free_hdl;
357
358         vsen->ved.process_frame = vimc_sen_process_frame;
359         dev_set_drvdata(comp, &vsen->ved);
360         vsen->dev = comp;
361
362         /* Initialize the frame format */
363         vsen->mbus_format = fmt_default;
364
365         /* Initialize the test pattern generator */
366         tpg_init(&vsen->tpg, vsen->mbus_format.width,
367                  vsen->mbus_format.height);
368         ret = tpg_alloc(&vsen->tpg, VIMC_FRAME_MAX_WIDTH);
369         if (ret)
370                 goto err_unregister_ent_sd;
371
372         return 0;
373
374 err_unregister_ent_sd:
375         vimc_ent_sd_unregister(&vsen->ved,  &vsen->sd);
376 err_free_hdl:
377         v4l2_ctrl_handler_free(&vsen->hdl);
378 err_free_vsen:
379         kfree(vsen);
380
381         return ret;
382 }
383
384 static const struct component_ops vimc_sen_comp_ops = {
385         .bind = vimc_sen_comp_bind,
386         .unbind = vimc_sen_comp_unbind,
387 };
388
389 static int vimc_sen_probe(struct platform_device *pdev)
390 {
391         return component_add(&pdev->dev, &vimc_sen_comp_ops);
392 }
393
394 static int vimc_sen_remove(struct platform_device *pdev)
395 {
396         component_del(&pdev->dev, &vimc_sen_comp_ops);
397
398         return 0;
399 }
400
401 static const struct platform_device_id vimc_sen_driver_ids[] = {
402         {
403                 .name           = VIMC_SEN_DRV_NAME,
404         },
405         { }
406 };
407
408 static struct platform_driver vimc_sen_pdrv = {
409         .probe          = vimc_sen_probe,
410         .remove         = vimc_sen_remove,
411         .id_table       = vimc_sen_driver_ids,
412         .driver         = {
413                 .name   = VIMC_SEN_DRV_NAME,
414         },
415 };
416
417 module_platform_driver(vimc_sen_pdrv);
418
419 MODULE_DEVICE_TABLE(platform, vimc_sen_driver_ids);
420
421 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Sensor");
422 MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
423 MODULE_LICENSE("GPL");