20 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/videodev2.h>
37 #define CODA_NAME "coda"
39 #define CODA_MAX_INSTANCES 4
41 #define CODA_FMO_BUF_SIZE 32
42 #define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
43 #define CODA7_WORK_BUF_SIZE (512 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
44 #define CODA_PARA_BUF_SIZE (10 * 1024)
45 #define CODA_ISRAM_SIZE (2048 * 2)
46 #define CODA7_IRAM_SIZE 0x14000
48 #define CODA_MAX_FRAMEBUFFERS 2
52 #define CODA_MAX_FRAME_SIZE 0x90000
53 #define FMO_SLICE_SAVE_BUF_SIZE (32)
54 #define CODA_DEFAULT_GAMMA 4096
65 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
67 static int coda_debug;
184 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
193 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
197 static inline unsigned long coda_isbusy(
struct coda_dev *
dev)
202 static inline int coda_is_initialized(
struct coda_dev *
dev)
207 static int coda_wait_timeout(
struct coda_dev *dev)
211 while (coda_isbusy(dev)) {
232 coda_command_async(ctx, cmd);
233 return coda_wait_timeout(dev);
256 static struct coda_fmt codadx6_formats[] = {
258 .name =
"YUV 4:2:0 Planar",
263 .name =
"H264 Encoded Stream",
268 .name =
"MPEG4 Encoded Stream",
274 static struct coda_fmt coda7_formats[] = {
276 .name =
"YUV 4:2:0 Planar",
281 .name =
"H264 Encoded Stream",
286 .name =
"MPEG4 Encoded Stream",
295 int num_formats = dev->
devtype->num_formats;
298 for (k = 0; k < num_formats; k++) {
303 if (k == num_formats)
312 static int vidioc_querycap(
struct file *
file,
void *
priv,
337 int num_formats = dev->
devtype->num_formats;
340 for (i = 0; i < num_formats; i++) {
341 if (formats[i].type == type) {
348 if (i < num_formats) {
359 static int vidioc_enum_fmt_vid_cap(
struct file *
file,
void *priv,
365 static int vidioc_enum_fmt_vid_out(
struct file *
file,
void *priv,
381 q_data = get_q_data(ctx, f->
type);
384 f->
fmt.
pix.pixelformat = q_data->
fmt->fourcc;
390 f->
fmt.
pix.bytesperline = 0;
418 f->
fmt.
pix.height * 3 / 2;
420 f->
fmt.
pix.bytesperline = 0;
427 static int vidioc_try_fmt_vid_cap(
struct file *file,
void *priv,
434 fmt = find_format(ctx->
dev, f);
444 ret = vidioc_try_fmt(ctx->
dev, f);
451 static int vidioc_try_fmt_vid_out(
struct file *file,
void *priv,
458 fmt = find_format(ctx->
dev, f);
466 if (!f->
fmt.
pix.colorspace)
469 ret = vidioc_try_fmt(ctx->
dev, f);
486 q_data = get_q_data(ctx, f->
type);
490 if (vb2_is_busy(vq)) {
491 v4l2_err(&ctx->
dev->v4l2_dev,
"%s queue busy\n", __func__);
495 ret = vidioc_try_fmt(ctx->
dev, f);
499 q_data->
fmt = find_format(ctx->
dev, f);
505 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
511 static int vidioc_s_fmt_vid_cap(
struct file *file,
void *priv,
516 ret = vidioc_try_fmt_vid_cap(file, priv, f);
523 static int vidioc_s_fmt_vid_out(
struct file *file,
void *priv,
529 ret = vidioc_try_fmt_vid_out(file, priv, f);
533 ret = vidioc_s_fmt(ctx, f);
540 static int vidioc_reqbufs(
struct file *file,
void *priv,
548 static int vidioc_querybuf(
struct file *file,
void *priv,
556 static int vidioc_qbuf(
struct file *file,
void *priv,
struct v4l2_buffer *
buf)
563 static int vidioc_dqbuf(
struct file *file,
void *priv,
struct v4l2_buffer *
buf)
570 static int vidioc_streamon(
struct file *file,
void *priv,
578 static int vidioc_streamoff(
struct file *file,
void *priv,
590 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
595 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
612 static void coda_device_run(
void *m2m_priv)
620 u32 picture_y, picture_cb, picture_cr;
621 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
624 src_buf = v4l2_m2m_next_src_buf(ctx->
m2m_ctx);
625 dst_buf = v4l2_m2m_next_dst_buf(ctx->
m2m_ctx);
628 dst_fourcc = q_data_dst->
fmt->fourcc;
651 if (src_buf->
v4l2_buf.sequence == 0) {
652 pic_stream_buffer_addr =
653 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
669 pic_stream_buffer_addr =
670 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
676 switch (dst_fourcc) {
678 quant_param = ctx->
params.h264_intra_qp;
681 quant_param = ctx->
params.mpeg4_intra_qp;
685 "cannot set intra qp, fmt not supported\n");
690 switch (dst_fourcc) {
692 quant_param = ctx->
params.h264_inter_qp;
695 quant_param = ctx->
params.mpeg4_inter_qp;
699 "cannot set inter qp, fmt not supported\n");
709 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
710 picture_cb = picture_y + q_data_src->
width * q_data_src->
height;
711 picture_cr = picture_cb + q_data_src->
width / 2 *
717 coda_write(dev, force_ipicture << 1 & 0x2,
721 coda_write(dev, pic_stream_buffer_size / 1024,
737 static int coda_job_ready(
void *m2m_priv)
745 if (!v4l2_m2m_num_src_bufs_ready(ctx->
m2m_ctx) ||
746 !v4l2_m2m_num_dst_bufs_ready(ctx->
m2m_ctx)) {
748 "not ready: not enough video buffers.\n");
757 static void coda_job_abort(
void *priv)
770 static void coda_lock(
void *m2m_priv)
777 static void coda_unlock(
void *m2m_priv)
785 .device_run = coda_device_run,
786 .job_ready = coda_job_ready,
787 .job_abort = coda_job_abort,
789 .unlock = coda_unlock,
792 static void set_default_params(
struct coda_ctx *ctx)
798 ctx->
params.framerate = 30;
815 static int coda_queue_setup(
struct vb2_queue *vq,
817 unsigned int *nbuffers,
unsigned int *nplanes,
818 unsigned int sizes[],
void *alloc_ctxs[])
820 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
824 q_data = get_q_data(ctx, vq->
type);
830 alloc_ctxs[0] = ctx->
dev->alloc_ctx;
833 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
838 static int coda_buf_prepare(
struct vb2_buffer *vb)
843 q_data = get_q_data(ctx, vb->
vb2_queue->type);
845 if (vb2_plane_size(vb, 0) < q_data->
sizeimage) {
847 "%s data will not fit into plane (%lu < %lu)\n",
848 __func__, vb2_plane_size(vb, 0),
853 vb2_set_plane_payload(vb, 0, q_data->
sizeimage);
858 static void coda_buf_queue(
struct vb2_buffer *vb)
864 static void coda_wait_prepare(
struct vb2_queue *
q)
866 struct coda_ctx *ctx = vb2_get_drv_priv(q);
870 static void coda_wait_finish(
struct vb2_queue *
q)
872 struct coda_ctx *ctx = vb2_get_drv_priv(q);
876 static void coda_free_framebuffers(
struct coda_ctx *ctx)
910 coda_free_framebuffers(ctx);
921 p[i * 3 + 1] = p[i * 3] + width *
height;
922 p[i * 3 + 2] = p[i * 3 + 1] + width / 2 * height / 2;
927 p[i * 3] = p[i * 3 + 1] + width *
height;
928 p[i * 3 + 3] = p[i * 3] + (width / 2) * (height / 2);
931 p[96 + i + 1] = p[i * 3 + 3] + (width / 2) * (height / 2);
935 p[i * 3 + 5] = p[i * 3 + 2] + width *
height ;
936 p[i * 3 + 4] = p[i * 3 + 5] + (width / 2) * (height / 2);
939 p[96 +
i] = p[i * 3 + 4] + (width / 2) * (height / 2);
947 static int coda_start_streaming(
struct vb2_queue *
q,
unsigned int count)
949 struct coda_ctx *ctx = vb2_get_drv_priv(q);
951 u32 bitstream_buf, bitstream_size;
971 if (coda_isbusy(dev))
978 buf = v4l2_m2m_next_dst_buf(ctx->
m2m_ctx);
979 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
982 dst_fourcc = q_data_dst->
fmt->fourcc;
991 v4l2_err(v4l2_dev,
"decoding not supported.\n");
994 v4l2_err(v4l2_dev,
"couldn't tell instance type.\n");
998 if (!coda_is_initialized(dev)) {
999 v4l2_err(v4l2_dev,
"coda is not initialized.\n");
1005 switch (dev->
devtype->product) {
1021 switch (dev->
devtype->product) {
1030 coda_write(dev, ctx->
params.framerate,
1033 switch (dst_fourcc) {
1054 "dst format (0x%08x) invalid.\n", dst_fourcc);
1058 switch (ctx->
params.slice_mode) {
1077 if (ctx->
params.bitrate) {
1113 v4l2_err(v4l2_dev,
"CODA_COMMAND_SEQ_INIT timeout\n");
1120 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
1135 v4l2_err(v4l2_dev,
"CODA_COMMAND_SET_FRAME_BUF timeout\n");
1140 buf = v4l2_m2m_next_dst_buf(ctx->
m2m_ctx);
1141 switch (dst_fourcc) {
1151 v4l2_err(v4l2_dev,
"CODA_COMMAND_ENCODE_HEADER timeout\n");
1167 v4l2_err(v4l2_dev,
"CODA_COMMAND_ENCODE_HEADER timeout\n");
1185 v4l2_err(v4l2_dev,
"CODA_COMMAND_ENCODE_HEADER timeout\n");
1197 v4l2_err(v4l2_dev,
"CODA_COMMAND_ENCODE_HEADER failed\n");
1209 v4l2_err(v4l2_dev,
"CODA_COMMAND_ENCODE_HEADER failed\n");
1225 static int coda_stop_streaming(
struct vb2_queue *q)
1227 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1232 "%s: output\n", __func__);
1236 "%s: capture\n", __func__);
1244 if (coda_isbusy(dev)) {
1247 "%s: timeout, sending SEQ_END anyway\n", __func__);
1254 "%s: sent command 'SEQ_END' to coda\n", __func__);
1257 "CODA_COMMAND_SEQ_END failed\n");
1261 coda_free_framebuffers(ctx);
1266 static struct vb2_ops coda_qops = {
1267 .queue_setup = coda_queue_setup,
1268 .buf_prepare = coda_buf_prepare,
1269 .buf_queue = coda_buf_queue,
1270 .wait_prepare = coda_wait_prepare,
1271 .wait_finish = coda_wait_finish,
1272 .start_streaming = coda_start_streaming,
1273 .stop_streaming = coda_stop_streaming,
1282 "s_ctrl: id = %d, val = %d\n", ctrl->
id, ctrl->
val);
1322 ctx->
params.slice_max_bits = ctrl->
val * 8;
1328 "Invalid control, id=%d, val=%d\n",
1329 ctrl->
id, ctrl->
val);
1337 .s_ctrl = coda_s_ctrl,
1340 static int coda_ctrls_setup(
struct coda_ctx *ctx)
1374 if (ctx->
ctrls.error) {
1375 v4l2_err(&ctx->
dev->v4l2_dev,
"control initialization error (%d)",
1383 static int coda_queue_init(
void *priv,
struct vb2_queue *src_vq,
1393 src_vq->
ops = &coda_qops;
1404 dst_vq->
ops = &coda_qops;
1410 static int coda_next_free_instance(
struct coda_dev *dev)
1415 static int coda_open(
struct file *file)
1417 struct coda_dev *dev = video_drvdata(file);
1422 idx = coda_next_free_instance(dev);
1437 set_default_params(ctx);
1441 int ret = PTR_ERR(ctx->
m2m_ctx);
1447 ret = coda_ctrls_setup(ctx);
1453 ctx->
fh.ctrl_handler = &ctx->
ctrls;
1467 clk_prepare_enable(dev->
clk_per);
1468 clk_prepare_enable(dev->
clk_ahb);
1482 static int coda_release(
struct file *file)
1484 struct coda_dev *dev = video_drvdata(file);
1498 clk_disable_unprepare(dev->
clk_per);
1499 clk_disable_unprepare(dev->
clk_ahb);
1508 static unsigned int coda_poll(
struct file *file,
1520 static int coda_mmap(
struct file *file,
struct vm_area_struct *vma)
1530 .release = coda_release,
1540 u32 wr_ptr, start_ptr;
1543 __cancel_delayed_work(&dev->
timeout);
1552 v4l2_err(&dev->
v4l2_dev,
"Instance released before the end of transaction\n");
1558 "task has been aborted\n");
1562 if (coda_isbusy(ctx->
dev)) {
1564 "coda is still busy!!!!\n");
1570 src_buf = v4l2_m2m_src_buf_remove(ctx->
m2m_ctx);
1571 dst_buf = v4l2_m2m_dst_buf_remove(ctx->
m2m_ctx);
1578 if (dst_buf->
v4l2_buf.sequence == 0) {
1579 dst_buf->
v4l2_planes[0].bytesused = (wr_ptr - start_ptr) +
1584 dst_buf->
v4l2_planes[0].bytesused = (wr_ptr - start_ptr);
1587 v4l2_dbg(1, coda_debug, &ctx->
dev->v4l2_dev,
"frame size = %u\n",
1588 wr_ptr - start_ptr);
1609 "job finished: encoding frame (%d) (%s)\n",
1612 "KEYFRAME" :
"PFRAME");
1640 static u32 coda_supported_firmwares[] = {
1645 static bool coda_firmware_supported(
u32 vernum)
1649 for (i = 0; i <
ARRAY_SIZE(coda_supported_firmwares); i++)
1650 if (vernum == coda_supported_firmwares[i])
1655 static char *coda_product_name(
int product)
1665 snprintf(buf,
sizeof(buf),
"(0x%04x)", product);
1670 static int coda_hw_init(
struct coda_dev *dev)
1677 clk_prepare_enable(dev->
clk_per);
1678 clk_prepare_enable(dev->
clk_ahb);
1703 coda_write(dev, dev->
workbuf.paddr,
1705 coda_write(dev, dev->
codebuf.paddr,
1710 switch (dev->
devtype->product) {
1740 if (coda_wait_timeout(dev)) {
1741 clk_disable_unprepare(dev->
clk_per);
1742 clk_disable_unprepare(dev->
clk_ahb);
1754 clk_disable_unprepare(dev->
clk_per);
1755 clk_disable_unprepare(dev->
clk_ahb);
1757 if (product != dev->
devtype->product) {
1759 " Version: %u.%u.%u\n",
1760 coda_product_name(dev->
devtype->product),
1761 coda_product_name(product), major, minor, release);
1766 coda_product_name(product));
1768 if (coda_firmware_supported(data)) {
1770 major, minor, release);
1773 "%u.%u.%u\n", major, minor, release);
1796 dev_err(&pdev->
dev,
"failed to allocate code buffer\n");
1804 ret = coda_hw_init(dev);
1810 dev->
vfd.fops = &coda_fops,
1811 dev->
vfd.ioctl_ops = &coda_ioctl_ops;
1817 video_set_drvdata(&dev->
vfd, dev);
1847 static int coda_firmware_request(
struct coda_dev *dev)
1849 char *fw = dev->
devtype->firmware;
1852 coda_product_name(dev->
devtype->product));
1865 .firmware =
"v4l-codadx6-imx27.bin",
1867 .formats = codadx6_formats,
1871 .firmware =
"v4l-coda7541-imx53.bin",
1873 .formats = coda7_formats,
1879 { .name =
"coda-imx27", .driver_data =
CODA_IMX27 },
1880 { .name =
"coda-imx53", .driver_data =
CODA_7541 },
1888 { .compatible =
"fsl,imx53-vpu", .data = &coda_devdata[
CODA_IMX53] },
1905 dev_err(&pdev->
dev,
"Not enough memory for %s\n",
1913 init_completion(&dev->
done);
1919 dev_err(&pdev->
dev,
"Could not get per clock\n");
1925 dev_err(&pdev->
dev,
"Could not get ahb clock\n");
1932 dev_err(&pdev->
dev,
"failed to get memory region resource\n");
1938 dev_err(&pdev->
dev,
"failed to request memory region\n");
1942 resource_size(res));
1944 dev_err(&pdev->
dev,
"failed to ioremap address region\n");
1951 dev_err(&pdev->
dev,
"failed to get irq resource\n");
1955 if (devm_request_irq(&pdev->
dev, irq, coda_irq_handler,
1957 dev_err(&pdev->
dev,
"failed to request irq\n");
1971 }
else if (pdev_id) {
1972 dev->
devtype = &coda_devdata[pdev_id->driver_data];
1979 switch (dev->
devtype->product) {
1990 dev_err(&pdev->
dev,
"failed to allocate work buffer\n");
2003 dev_err(&pdev->
dev,
"unable to alloc iram\n");
2008 platform_set_drvdata(pdev, dev);
2010 return coda_firmware_request(dev);
2015 struct coda_dev *dev = platform_get_drvdata(pdev);
2035 .probe = coda_probe,
2042 .id_table = coda_platform_ids,