GNU Linux-libre 4.14.254-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/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>
25
26 #include "vimc-common.h"
27
28 #define VIMC_SEN_DRV_NAME "vimc-sensor"
29
30 struct vimc_sen_device {
31         struct vimc_ent_device ved;
32         struct v4l2_subdev sd;
33         struct device *dev;
34         struct tpg_data tpg;
35         struct task_struct *kthread_sen;
36         u8 *frame;
37         /* The active format */
38         struct v4l2_mbus_framefmt mbus_format;
39 };
40
41 static const struct v4l2_mbus_framefmt fmt_default = {
42         .width = 640,
43         .height = 480,
44         .code = MEDIA_BUS_FMT_RGB888_1X24,
45         .field = V4L2_FIELD_NONE,
46         .colorspace = V4L2_COLORSPACE_DEFAULT,
47 };
48
49 static int vimc_sen_init_cfg(struct v4l2_subdev *sd,
50                              struct v4l2_subdev_pad_config *cfg)
51 {
52         unsigned int i;
53
54         for (i = 0; i < sd->entity.num_pads; i++) {
55                 struct v4l2_mbus_framefmt *mf;
56
57                 mf = v4l2_subdev_get_try_format(sd, cfg, i);
58                 *mf = fmt_default;
59         }
60
61         return 0;
62 }
63
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)
67 {
68         const struct vimc_pix_map *vpix = vimc_pix_map_by_index(code->index);
69
70         if (!vpix)
71                 return -EINVAL;
72
73         code->code = vpix->code;
74
75         return 0;
76 }
77
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)
81 {
82         const struct vimc_pix_map *vpix;
83
84         if (fse->index)
85                 return -EINVAL;
86
87         /* Only accept code in the pix map table */
88         vpix = vimc_pix_map_by_code(fse->code);
89         if (!vpix)
90                 return -EINVAL;
91
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;
96
97         return 0;
98 }
99
100 static int vimc_sen_get_fmt(struct v4l2_subdev *sd,
101                             struct v4l2_subdev_pad_config *cfg,
102                             struct v4l2_subdev_format *fmt)
103 {
104         struct vimc_sen_device *vsen =
105                                 container_of(sd, struct vimc_sen_device, sd);
106
107         fmt->format = fmt->which == V4L2_SUBDEV_FORMAT_TRY ?
108                       *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) :
109                       vsen->mbus_format;
110
111         return 0;
112 }
113
114 static void vimc_sen_tpg_s_format(struct vimc_sen_device *vsen)
115 {
116         const struct vimc_pix_map *vpix =
117                                 vimc_pix_map_by_code(vsen->mbus_format.code);
118
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);
130 }
131
132 static void vimc_sen_adjust_fmt(struct v4l2_mbus_framefmt *fmt)
133 {
134         const struct vimc_pix_map *vpix;
135
136         /* Only accept code in the pix map table */
137         vpix = vimc_pix_map_by_code(fmt->code);
138         if (!vpix)
139                 fmt->code = fmt_default.code;
140
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;
145
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;
149
150         vimc_colorimetry_clamp(fmt);
151 }
152
153 static int vimc_sen_set_fmt(struct v4l2_subdev *sd,
154                             struct v4l2_subdev_pad_config *cfg,
155                             struct v4l2_subdev_format *fmt)
156 {
157         struct vimc_sen_device *vsen = v4l2_get_subdevdata(sd);
158         struct v4l2_mbus_framefmt *mf;
159
160         if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
161                 /* Do not change the format while stream is on */
162                 if (vsen->frame)
163                         return -EBUSY;
164
165                 mf = &vsen->mbus_format;
166         } else {
167                 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
168         }
169
170         /* Set the new format */
171         vimc_sen_adjust_fmt(&fmt->format);
172
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,
176                 /* old */
177                 mf->width, mf->height, mf->code,
178                 mf->colorspace, mf->quantization,
179                 mf->xfer_func, mf->ycbcr_enc,
180                 /* new */
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);
184
185         *mf = fmt->format;
186
187         return 0;
188 }
189
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,
196 };
197
198 static void *vimc_sen_process_frame(struct vimc_ent_device *ved,
199                                     const void *sink_frame)
200 {
201         struct vimc_sen_device *vsen = container_of(ved, struct vimc_sen_device,
202                                                     ved);
203
204         tpg_fill_plane_buffer(&vsen->tpg, 0, 0, vsen->frame);
205         return vsen->frame;
206 }
207
208 static int vimc_sen_s_stream(struct v4l2_subdev *sd, int enable)
209 {
210         struct vimc_sen_device *vsen =
211                                 container_of(sd, struct vimc_sen_device, sd);
212
213         if (enable) {
214                 const struct vimc_pix_map *vpix;
215                 unsigned int frame_size;
216
217                 if (vsen->kthread_sen)
218                         /* tpg is already executing */
219                         return 0;
220
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;
225
226                 /*
227                  * Allocate the frame buffer. Use vmalloc to be able to
228                  * allocate a large amount of memory
229                  */
230                 vsen->frame = vmalloc(frame_size);
231                 if (!vsen->frame)
232                         return -ENOMEM;
233
234                 /* configure the test pattern generator */
235                 vimc_sen_tpg_s_format(vsen);
236
237         } else {
238
239                 vfree(vsen->frame);
240                 vsen->frame = NULL;
241                 return 0;
242         }
243
244         return 0;
245 }
246
247 static const struct v4l2_subdev_video_ops vimc_sen_video_ops = {
248         .s_stream = vimc_sen_s_stream,
249 };
250
251 static const struct v4l2_subdev_ops vimc_sen_ops = {
252         .pad = &vimc_sen_pad_ops,
253         .video = &vimc_sen_video_ops,
254 };
255
256 static void vimc_sen_comp_unbind(struct device *comp, struct device *master,
257                                  void *master_data)
258 {
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);
262
263         vimc_ent_sd_unregister(ved, &vsen->sd);
264         tpg_free(&vsen->tpg);
265         kfree(vsen);
266 }
267
268 static int vimc_sen_comp_bind(struct device *comp, struct device *master,
269                               void *master_data)
270 {
271         struct v4l2_device *v4l2_dev = master_data;
272         struct vimc_platform_data *pdata = comp->platform_data;
273         struct vimc_sen_device *vsen;
274         int ret;
275
276         /* Allocate the vsen struct */
277         vsen = kzalloc(sizeof(*vsen), GFP_KERNEL);
278         if (!vsen)
279                 return -ENOMEM;
280
281         /* Initialize ved and sd */
282         ret = vimc_ent_sd_register(&vsen->ved, &vsen->sd, v4l2_dev,
283                                    pdata->entity_name,
284                                    MEDIA_ENT_F_ATV_DECODER, 1,
285                                    (const unsigned long[1]) {MEDIA_PAD_FL_SOURCE},
286                                    &vimc_sen_ops);
287         if (ret)
288                 goto err_free_vsen;
289
290         vsen->ved.process_frame = vimc_sen_process_frame;
291         dev_set_drvdata(comp, &vsen->ved);
292         vsen->dev = comp;
293
294         /* Initialize the frame format */
295         vsen->mbus_format = fmt_default;
296
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);
301         if (ret)
302                 goto err_unregister_ent_sd;
303
304         return 0;
305
306 err_unregister_ent_sd:
307         vimc_ent_sd_unregister(&vsen->ved,  &vsen->sd);
308 err_free_vsen:
309         kfree(vsen);
310
311         return ret;
312 }
313
314 static const struct component_ops vimc_sen_comp_ops = {
315         .bind = vimc_sen_comp_bind,
316         .unbind = vimc_sen_comp_unbind,
317 };
318
319 static int vimc_sen_probe(struct platform_device *pdev)
320 {
321         return component_add(&pdev->dev, &vimc_sen_comp_ops);
322 }
323
324 static int vimc_sen_remove(struct platform_device *pdev)
325 {
326         component_del(&pdev->dev, &vimc_sen_comp_ops);
327
328         return 0;
329 }
330
331 static const struct platform_device_id vimc_sen_driver_ids[] = {
332         {
333                 .name           = VIMC_SEN_DRV_NAME,
334         },
335         { }
336 };
337
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,
342         .driver         = {
343                 .name   = VIMC_SEN_DRV_NAME,
344         },
345 };
346
347 module_platform_driver(vimc_sen_pdrv);
348
349 MODULE_DEVICE_TABLE(platform, vimc_sen_driver_ids);
350
351 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Sensor");
352 MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
353 MODULE_LICENSE("GPL");