11 #define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__
14 #include <linux/device.h>
15 #include <linux/errno.h>
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/types.h>
23 #include <linux/slab.h>
24 #include <linux/videodev2.h>
41 static const struct fimc_fmt fimc_lite_formats[] = {
43 .name =
"YUV 4:2:2 packed, YCbYCr",
50 .name =
"YUV 4:2:2 packed, CbYCrY",
57 .name =
"YUV 4:2:2 packed, CrYCbY",
64 .name =
"YUV 4:2:2 packed, YCrYCb",
71 .name =
"RAW8 (GRBG)",
78 .name =
"RAW10 (GRBG)",
85 .name =
"RAW12 (GRBG)",
107 if (index >= (
int)
ARRAY_SIZE(fimc_lite_formats))
110 for (i = 0; i <
ARRAY_SIZE(fimc_lite_formats); ++
i) {
111 fmt = &fimc_lite_formats[
i];
112 if (pixelformat && fmt->
fourcc == *pixelformat)
114 if (mbus_code && fmt->
mbus_code == *mbus_code)
123 static int fimc_lite_hw_init(
struct fimc_lite *fimc)
148 spin_unlock_irqrestore(&fimc->
slock, flags);
179 buf = fimc_lite_pending_queue_pop(fimc);
184 buf = fimc_lite_active_queue_pop(fimc);
186 fimc_lite_pending_queue_add(fimc, buf);
191 spin_unlock_irqrestore(&fimc->
slock, flags);
201 static int fimc_lite_stop_capture(
struct fimc_lite *fimc,
bool suspend)
205 if (!fimc_lite_active(fimc))
211 spin_unlock_irqrestore(&fimc->
slock, flags);
217 return fimc_lite_reinit(fimc, suspend);
221 static void fimc_lite_config_update(
struct fimc_lite *fimc)
250 fimc->
events.data_overflow++;
266 vbuf = fimc_lite_active_queue_pop(fimc);
268 tv = &vbuf->
vb.v4l2_buf.timestamp;
274 vbuf = fimc_lite_pending_queue_pop(fimc);
275 flite_hw_set_output_addr(fimc, vbuf->
paddr);
276 fimc_lite_active_queue_add(fimc, vbuf);
280 fimc_lite_config_update(fimc);
288 spin_unlock_irqrestore(&fimc->
slock, flags);
299 ret = fimc_lite_hw_init(fimc);
301 fimc_lite_reinit(fimc,
false);
321 static int stop_streaming(
struct vb2_queue *q)
325 if (!fimc_lite_active(fimc))
328 return fimc_lite_stop_capture(fimc,
false);
332 unsigned int *num_buffers,
unsigned int *
num_planes,
333 unsigned int sizes[],
void *allocators[])
367 static int buffer_prepare(
struct vb2_buffer *vb)
376 for (i = 0; i < fimc->
fmt->memplanes; i++) {
379 if (vb2_plane_size(vb, i) < size) {
381 "User buffer too small (%ld < %ld)\n",
382 vb2_plane_size(vb, i), size);
385 vb2_set_plane_payload(vb, i, size);
391 static void buffer_queue(
struct vb2_buffer *vb)
399 buf->
paddr = vb2_dma_contig_plane_dma_addr(vb, 0);
404 flite_hw_set_output_addr(fimc, buf->
paddr);
405 fimc_lite_active_queue_add(fimc, buf);
407 fimc_lite_pending_queue_add(fimc, buf);
410 if (vb2_is_streaming(&fimc->
vb_queue) &&
414 spin_unlock_irqrestore(&fimc->
slock, flags);
421 spin_unlock_irqrestore(&fimc->
slock, flags);
424 static void fimc_lock(
struct vb2_queue *vq)
426 struct fimc_lite *fimc = vb2_get_drv_priv(vq);
430 static void fimc_unlock(
struct vb2_queue *vq)
432 struct fimc_lite *fimc = vb2_get_drv_priv(vq);
436 static const struct vb2_ops fimc_lite_qops = {
438 .buf_prepare = buffer_prepare,
439 .buf_queue = buffer_queue,
440 .wait_prepare = fimc_unlock,
441 .wait_finish = fimc_lock,
446 static void fimc_lite_clear_event_counters(
struct fimc_lite *fimc)
452 spin_unlock_irqrestore(&fimc->
slock, flags);
455 static int fimc_lite_open(
struct file *
file)
457 struct fimc_lite *fimc = video_drvdata(file);
464 ret = pm_runtime_get_sync(&fimc->
pdev->dev);
474 &fimc->
vfd.entity,
true);
476 pm_runtime_put_sync(&fimc->
pdev->dev);
482 fimc_lite_clear_event_counters(fimc);
489 static int fimc_lite_close(
struct file *file)
491 struct fimc_lite *fimc = video_drvdata(file);
498 fimc_lite_stop_capture(fimc,
false);
503 pm_runtime_put(&fimc->
pdev->dev);
514 static unsigned int fimc_lite_poll(
struct file *file,
517 struct fimc_lite *fimc = video_drvdata(file);
529 static int fimc_lite_mmap(
struct file *file,
struct vm_area_struct *vma)
531 struct fimc_lite *fimc = video_drvdata(file);
545 .open = fimc_lite_open,
546 .release = fimc_lite_close,
547 .poll = fimc_lite_poll,
549 .mmap = fimc_lite_mmap,
563 fmt = fimc_lite_find_format(fourcc, code, 0);
584 code ? *code : 0, *width, *height);
629 static int fimc_vidioc_querycap_capture(
struct file *file,
void *priv,
639 static int fimc_lite_enum_fmt_mplane(
struct file *file,
void *priv,
647 fmt = &fimc_lite_formats[f->
index];
654 static int fimc_lite_g_fmt_mplane(
struct file *file,
void *fh,
657 struct fimc_lite *fimc = video_drvdata(file);
675 static int fimc_lite_try_fmt(
struct fimc_lite *fimc,
692 if ((bpl == 0 || ((bpl * 8) / fmt->
depth[0]) < pixm->
width))
706 static int fimc_lite_try_fmt_mplane(
struct file *file,
void *fh,
709 struct fimc_lite *fimc = video_drvdata(file);
714 static int fimc_lite_s_fmt_mplane(
struct file *file,
void *priv,
718 struct fimc_lite *fimc = video_drvdata(file);
726 ret = fimc_lite_try_fmt(fimc, &f->
fmt.
pix_mp, &fmt);
739 static int fimc_pipeline_validate(
struct fimc_lite *fimc)
748 pad = &sd->entity.pads[0];
752 if (sd == &fimc->
subdev) {
754 sink_fmt.format.width = ff->
f_width;
755 sink_fmt.format.height = ff->
f_height;
756 sink_fmt.format.code = fimc->
fmt->mbus_code;
758 sink_fmt.pad = pad->
index;
772 src_fmt.pad = pad->
index;
778 if (src_fmt.format.width != sink_fmt.format.width ||
779 src_fmt.format.height != sink_fmt.format.height ||
780 src_fmt.format.code != sink_fmt.format.code)
786 static int fimc_lite_streamon(
struct file *file,
void *priv,
789 struct fimc_lite *fimc = video_drvdata(file);
794 if (fimc_lite_active(fimc))
801 ret = fimc_pipeline_validate(fimc);
810 static int fimc_lite_streamoff(
struct file *file,
void *priv,
813 struct fimc_lite *fimc = video_drvdata(file);
823 static int fimc_lite_reqbufs(
struct file *file,
void *priv,
826 struct fimc_lite *fimc = video_drvdata(file);
837 static int fimc_lite_querybuf(
struct file *file,
void *priv,
840 struct fimc_lite *fimc = video_drvdata(file);
845 static int fimc_lite_qbuf(
struct file *file,
void *priv,
848 struct fimc_lite *fimc = video_drvdata(file);
853 static int fimc_lite_dqbuf(
struct file *file,
void *priv,
856 struct fimc_lite *fimc = video_drvdata(file);
861 static int fimc_lite_create_bufs(
struct file *file,
void *priv,
864 struct fimc_lite *fimc = video_drvdata(file);
869 static int fimc_lite_prepare_buf(
struct file *file,
void *priv,
872 struct fimc_lite *fimc = video_drvdata(file);
890 static int fimc_lite_g_selection(
struct file *file,
void *
fh,
893 struct fimc_lite *fimc = video_drvdata(file);
916 static int fimc_lite_s_selection(
struct file *file,
void *fh,
919 struct fimc_lite *fimc = video_drvdata(file);
928 fimc_lite_try_compose(fimc, &rect);
931 !enclosed_rectangle(&rect, &sel->
r))
935 !enclosed_rectangle(&sel->
r, &rect))
942 spin_unlock_irqrestore(&fimc->
slock, flags);
948 .vidioc_querycap = fimc_vidioc_querycap_capture,
949 .vidioc_enum_fmt_vid_cap_mplane = fimc_lite_enum_fmt_mplane,
950 .vidioc_try_fmt_vid_cap_mplane = fimc_lite_try_fmt_mplane,
951 .vidioc_s_fmt_vid_cap_mplane = fimc_lite_s_fmt_mplane,
952 .vidioc_g_fmt_vid_cap_mplane = fimc_lite_g_fmt_mplane,
953 .vidioc_g_selection = fimc_lite_g_selection,
954 .vidioc_s_selection = fimc_lite_s_selection,
955 .vidioc_reqbufs = fimc_lite_reqbufs,
956 .vidioc_querybuf = fimc_lite_querybuf,
957 .vidioc_prepare_buf = fimc_lite_prepare_buf,
958 .vidioc_create_bufs = fimc_lite_create_bufs,
959 .vidioc_qbuf = fimc_lite_qbuf,
960 .vidioc_dqbuf = fimc_lite_dqbuf,
961 .vidioc_streamon = fimc_lite_streamon,
962 .vidioc_streamoff = fimc_lite_streamoff,
966 static int fimc_lite_link_setup(
struct media_entity *entity,
971 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
972 unsigned int remote_ent_type = media_entity_type(remote->
entity);
977 v4l2_dbg(1,
debug, sd,
"%s: %s --> %s, flags: 0x%x. source_id: 0x%x",
981 switch (local->
index) {
997 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
1008 v4l2_err(sd,
"Invalid pad index\n");
1016 .link_setup = fimc_lite_link_setup,
1019 static int fimc_lite_subdev_enum_mbus_code(
struct v4l2_subdev *sd,
1021 struct v4l2_subdev_mbus_code_enum *code)
1025 fmt = fimc_lite_find_format(
NULL,
NULL, code->index);
1032 static int fimc_lite_subdev_get_fmt(
struct v4l2_subdev *sd,
1036 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1037 struct v4l2_mbus_framefmt *mf = &fmt->
format;
1041 mf = v4l2_subdev_get_try_format(fh, fmt->
pad);
1048 mf->code = fimc->
fmt->mbus_code;
1056 mf->width = f->
rect.width;
1057 mf->height = f->
rect.height;
1063 static int fimc_lite_subdev_set_fmt(
struct v4l2_subdev *sd,
1067 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1068 struct v4l2_mbus_framefmt *mf = &fmt->
format;
1074 fmt->
pad, mf->code, mf->width, mf->height);
1085 ffmt = fimc_lite_try_format(fimc, &mf->width, &mf->height,
1089 mf = v4l2_subdev_get_try_format(fh, fmt->
pad);
1100 sink->
rect.width = mf->width;
1101 sink->
rect.height = mf->height;
1102 sink->
rect.left = 0;
1110 mf->code = fimc->
fmt->mbus_code;
1111 mf->width = sink->
rect.width;
1112 mf->height = sink->
rect.height;
1119 static int fimc_lite_subdev_get_selection(
struct v4l2_subdev *sd,
1121 struct v4l2_subdev_selection *sel)
1123 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1132 sel->r = *v4l2_subdev_get_try_crop(fh, sel->pad);
1147 v4l2_dbg(1,
debug, sd,
"%s: (%d,%d) %dx%d, f_w: %d, f_h: %d",
1154 static int fimc_lite_subdev_set_selection(
struct v4l2_subdev *sd,
1156 struct v4l2_subdev_selection *sel)
1158 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1166 fimc_lite_try_crop(fimc, &sel->r);
1169 *v4l2_subdev_get_try_crop(fh, sel->pad) = sel->r;
1171 unsigned long flags;
1177 spin_unlock_irqrestore(&fimc->
slock, flags);
1181 v4l2_dbg(1,
debug, sd,
"%s: (%d,%d) %dx%d, f_w: %d, f_h: %d",
1188 static int fimc_lite_subdev_s_stream(
struct v4l2_subdev *sd,
int on)
1190 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1200 static int fimc_lite_subdev_s_power(
struct v4l2_subdev *sd,
int on)
1202 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1212 static int fimc_lite_log_status(
struct v4l2_subdev *sd)
1214 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1220 static int fimc_lite_subdev_registered(
struct v4l2_subdev *sd)
1222 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1227 memset(vfd, 0,
sizeof(*vfd));
1229 fimc->
fmt = &fimc_lite_formats[0];
1235 vfd->
fops = &fimc_lite_fops;
1247 memset(q, 0,
sizeof(*q));
1250 q->
ops = &fimc_lite_qops;
1264 video_set_drvdata(vfd, fimc);
1275 vfd->
name, video_device_node_name(vfd));
1279 static void fimc_lite_subdev_unregistered(
struct v4l2_subdev *sd)
1281 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1286 if (video_is_registered(&fimc->
vfd)) {
1294 .registered = fimc_lite_subdev_registered,
1295 .unregistered = fimc_lite_subdev_unregistered,
1299 .enum_mbus_code = fimc_lite_subdev_enum_mbus_code,
1300 .get_selection = fimc_lite_subdev_get_selection,
1301 .set_selection = fimc_lite_subdev_set_selection,
1302 .get_fmt = fimc_lite_subdev_get_fmt,
1303 .set_fmt = fimc_lite_subdev_set_fmt,
1307 .s_stream = fimc_lite_subdev_s_stream,
1311 .s_power = fimc_lite_subdev_s_power,
1312 .log_status = fimc_lite_log_status,
1316 .core = &fimc_lite_core_ops,
1317 .video = &fimc_lite_subdev_video_ops,
1318 .pad = &fimc_lite_subdev_pad_ops,
1330 .s_ctrl = fimc_lite_s_ctrl,
1334 .ops = &fimc_lite_ctrl_ops,
1337 .name =
"Test Pattern 640x480",
1340 static int fimc_lite_create_capture_subdev(
struct fimc_lite *fimc)
1360 if (handler->
error) {
1362 return handler->
error;
1367 sd->entity.
ops = &fimc_lite_subdev_media_ops;
1368 v4l2_set_subdevdata(sd, fimc);
1373 static void fimc_lite_unregister_capture_subdev(
struct fimc_lite *fimc)
1380 v4l2_set_subdevdata(sd,
NULL);
1383 static void fimc_lite_clk_put(
struct fimc_lite *fimc)
1385 if (IS_ERR_OR_NULL(fimc->
clock))
1393 static int fimc_lite_clk_get(
struct fimc_lite *fimc)
1398 if (IS_ERR(fimc->
clock))
1399 return PTR_ERR(fimc->
clock);
1431 dev_err(&pdev->
dev,
"Failed to obtain io memory\n");
1437 dev_err(&pdev->
dev,
"Failed to get IRQ resource\n");
1441 ret = fimc_lite_clk_get(fimc);
1445 ret = devm_request_irq(&pdev->
dev, res->
start, flite_irq_handler,
1446 0, dev_name(&pdev->
dev), fimc);
1448 dev_err(&pdev->
dev,
"Failed to install irq (%d)\n", ret);
1453 ret = fimc_lite_create_capture_subdev(fimc);
1457 platform_set_drvdata(pdev, fimc);
1459 ret = pm_runtime_get_sync(&pdev->
dev);
1468 pm_runtime_put(&pdev->
dev);
1470 dev_dbg(&pdev->
dev,
"FIMC-LITE.%d registered successfully\n",
1474 pm_runtime_put(&pdev->
dev);
1476 fimc_lite_unregister_capture_subdev(fimc);
1478 fimc_lite_clk_put(fimc);
1482 static int fimc_lite_runtime_resume(
struct device *
dev)
1490 static int fimc_lite_runtime_suspend(
struct device *
dev)
1498 #ifdef CONFIG_PM_SLEEP
1499 static int fimc_lite_resume(
struct device *
dev)
1503 unsigned long flags;
1509 spin_unlock_irqrestore(&fimc->
slock, flags);
1513 spin_unlock_irqrestore(&fimc->
slock, flags);
1520 &fimc->
vfd.entity,
false);
1521 fimc_lite_hw_init(fimc);
1527 buf = fimc_lite_pending_queue_pop(fimc);
1528 buffer_queue(&buf->
vb);
1533 static int fimc_lite_suspend(
struct device *dev)
1542 ret = fimc_lite_stop_capture(fimc, suspend);
1543 if (ret < 0 || !fimc_lite_active(fimc))
1552 struct fimc_lite *fimc = platform_get_drvdata(pdev);
1555 pm_runtime_disable(dev);
1556 pm_runtime_set_suspended(dev);
1557 fimc_lite_unregister_capture_subdev(fimc);
1559 fimc_lite_clk_put(fimc);
1561 dev_info(dev,
"Driver unloaded\n");
1568 .out_width_align = 8,
1569 .win_hor_offs_align = 2,
1570 .out_hor_offs_align = 8,
1576 [0] = &fimc_lite0_variant_exynos4,
1577 [1] = &fimc_lite0_variant_exynos4,
1583 .name =
"exynos-fimc-lite",
1584 .driver_data = (
unsigned long)&fimc_lite_drvdata_exynos4,
1590 static const struct dev_pm_ops fimc_lite_pm_ops = {
1597 .
probe = fimc_lite_probe,
1599 .id_table = fimc_lite_driver_ids,
1603 .pm = &fimc_lite_pm_ops,