This source file includes following definitions.
- vimc_sen_init_cfg
- vimc_sen_enum_mbus_code
- vimc_sen_enum_frame_size
- vimc_sen_get_fmt
- vimc_sen_tpg_s_format
- vimc_sen_adjust_fmt
- vimc_sen_set_fmt
- vimc_sen_process_frame
- vimc_sen_s_stream
- vimc_sen_s_ctrl
- vimc_sen_release
- vimc_sen_comp_unbind
- vimc_sen_comp_bind
- vimc_sen_probe
- vimc_sen_remove
1
2
3
4
5
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
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
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
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
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
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
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
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
170 mf->width, mf->height, mf->code,
171 mf->colorspace, mf->quantization,
172 mf->xfer_func, mf->ycbcr_enc,
173
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
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
217
218
219 vsen->frame = vmalloc(frame_size);
220 if (!vsen->frame)
221 return -ENOMEM;
222
223
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 vimc_pads_cleanup(vsen->ved.pads);
296 kfree(vsen);
297 }
298
299 static const struct v4l2_subdev_internal_ops vimc_sen_int_ops = {
300 .release = vimc_sen_release,
301 };
302
303 static void vimc_sen_comp_unbind(struct device *comp, struct device *master,
304 void *master_data)
305 {
306 struct vimc_ent_device *ved = dev_get_drvdata(comp);
307 struct vimc_sen_device *vsen =
308 container_of(ved, struct vimc_sen_device, ved);
309
310 vimc_ent_sd_unregister(ved, &vsen->sd);
311 }
312
313
314 static const struct v4l2_ctrl_config vimc_sen_ctrl_class = {
315 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY,
316 .id = VIMC_CID_VIMC_CLASS,
317 .name = "VIMC Controls",
318 .type = V4L2_CTRL_TYPE_CTRL_CLASS,
319 };
320
321 static const struct v4l2_ctrl_config vimc_sen_ctrl_test_pattern = {
322 .ops = &vimc_sen_ctrl_ops,
323 .id = VIMC_CID_TEST_PATTERN,
324 .name = "Test Pattern",
325 .type = V4L2_CTRL_TYPE_MENU,
326 .max = TPG_PAT_NOISE,
327 .qmenu = tpg_pattern_strings,
328 };
329
330 static int vimc_sen_comp_bind(struct device *comp, struct device *master,
331 void *master_data)
332 {
333 struct v4l2_device *v4l2_dev = master_data;
334 struct vimc_platform_data *pdata = comp->platform_data;
335 struct vimc_sen_device *vsen;
336 int ret;
337
338
339 vsen = kzalloc(sizeof(*vsen), GFP_KERNEL);
340 if (!vsen)
341 return -ENOMEM;
342
343 v4l2_ctrl_handler_init(&vsen->hdl, 4);
344
345 v4l2_ctrl_new_custom(&vsen->hdl, &vimc_sen_ctrl_class, NULL);
346 v4l2_ctrl_new_custom(&vsen->hdl, &vimc_sen_ctrl_test_pattern, NULL);
347 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
348 V4L2_CID_VFLIP, 0, 1, 1, 0);
349 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
350 V4L2_CID_HFLIP, 0, 1, 1, 0);
351 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
352 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
353 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
354 V4L2_CID_CONTRAST, 0, 255, 1, 128);
355 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
356 V4L2_CID_HUE, -128, 127, 1, 0);
357 v4l2_ctrl_new_std(&vsen->hdl, &vimc_sen_ctrl_ops,
358 V4L2_CID_SATURATION, 0, 255, 1, 128);
359 vsen->sd.ctrl_handler = &vsen->hdl;
360 if (vsen->hdl.error) {
361 ret = vsen->hdl.error;
362 goto err_free_vsen;
363 }
364
365
366 ret = vimc_ent_sd_register(&vsen->ved, &vsen->sd, v4l2_dev,
367 pdata->entity_name,
368 MEDIA_ENT_F_CAM_SENSOR, 1,
369 (const unsigned long[1]) {MEDIA_PAD_FL_SOURCE},
370 &vimc_sen_int_ops, &vimc_sen_ops);
371 if (ret)
372 goto err_free_hdl;
373
374 vsen->ved.process_frame = vimc_sen_process_frame;
375 dev_set_drvdata(comp, &vsen->ved);
376 vsen->dev = comp;
377
378
379 vsen->mbus_format = fmt_default;
380
381
382 tpg_init(&vsen->tpg, vsen->mbus_format.width,
383 vsen->mbus_format.height);
384 ret = tpg_alloc(&vsen->tpg, VIMC_FRAME_MAX_WIDTH);
385 if (ret)
386 goto err_unregister_ent_sd;
387
388 return 0;
389
390 err_unregister_ent_sd:
391 vimc_ent_sd_unregister(&vsen->ved, &vsen->sd);
392 err_free_hdl:
393 v4l2_ctrl_handler_free(&vsen->hdl);
394 err_free_vsen:
395 kfree(vsen);
396
397 return ret;
398 }
399
400 static const struct component_ops vimc_sen_comp_ops = {
401 .bind = vimc_sen_comp_bind,
402 .unbind = vimc_sen_comp_unbind,
403 };
404
405 static int vimc_sen_probe(struct platform_device *pdev)
406 {
407 return component_add(&pdev->dev, &vimc_sen_comp_ops);
408 }
409
410 static int vimc_sen_remove(struct platform_device *pdev)
411 {
412 component_del(&pdev->dev, &vimc_sen_comp_ops);
413
414 return 0;
415 }
416
417 static const struct platform_device_id vimc_sen_driver_ids[] = {
418 {
419 .name = VIMC_SEN_DRV_NAME,
420 },
421 { }
422 };
423
424 static struct platform_driver vimc_sen_pdrv = {
425 .probe = vimc_sen_probe,
426 .remove = vimc_sen_remove,
427 .id_table = vimc_sen_driver_ids,
428 .driver = {
429 .name = VIMC_SEN_DRV_NAME,
430 },
431 };
432
433 module_platform_driver(vimc_sen_pdrv);
434
435 MODULE_DEVICE_TABLE(platform, vimc_sen_driver_ids);
436
437 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Sensor");
438 MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
439 MODULE_LICENSE("GPL");