Merge tag 'tpmdd-next-20151110' of https://github.com/jsakkine/linux-tpmdd into for...
[firefly-linux-kernel-4.4.55.git] / drivers / media / platform / soc_camera / atmel-isi.c
index 90701726a06a2e5c4bcea8a9f011333d7ae1651c..454f68f0cdad36d56acd1110976d0834771ec0fc 100644 (file)
 #include <linux/pm_runtime.h>
 #include <linux/slab.h>
 
-#include <media/atmel-isi.h>
 #include <media/soc_camera.h>
 #include <media/soc_mediabus.h>
 #include <media/v4l2-of.h>
 #include <media/videobuf2-dma-contig.h>
 
+#include "atmel-isi.h"
+
 #define MAX_BUFFER_NUM                 32
 #define MAX_SUPPORT_WIDTH              2048
 #define MAX_SUPPORT_HEIGHT             2048
@@ -59,7 +60,7 @@ struct isi_dma_desc {
 
 /* Frame buffer data */
 struct frame_buffer {
-       struct vb2_buffer vb;
+       struct vb2_v4l2_buffer vb;
        struct isi_dma_desc *p_dma_desc;
        struct list_head list;
 };
@@ -102,62 +103,71 @@ static u32 isi_readl(struct atmel_isi *isi, u32 reg)
        return readl(isi->regs + reg);
 }
 
-static int configure_geometry(struct atmel_isi *isi, u32 width,
+static void configure_geometry(struct atmel_isi *isi, u32 width,
                        u32 height, u32 code)
 {
-       u32 cfg2, cr;
+       u32 cfg2;
 
+       /* According to sensor's output format to set cfg2 */
        switch (code) {
-       /* YUV, including grey */
+       default:
+       /* Grey */
        case MEDIA_BUS_FMT_Y8_1X8:
-               cr = ISI_CFG2_GRAYSCALE;
+               cfg2 = ISI_CFG2_GRAYSCALE | ISI_CFG2_COL_SPACE_YCbCr;
                break;
+       /* YUV */
        case MEDIA_BUS_FMT_VYUY8_2X8:
-               cr = ISI_CFG2_YCC_SWAP_MODE_3;
+               cfg2 = ISI_CFG2_YCC_SWAP_MODE_3 | ISI_CFG2_COL_SPACE_YCbCr;
                break;
        case MEDIA_BUS_FMT_UYVY8_2X8:
-               cr = ISI_CFG2_YCC_SWAP_MODE_2;
+               cfg2 = ISI_CFG2_YCC_SWAP_MODE_2 | ISI_CFG2_COL_SPACE_YCbCr;
                break;
        case MEDIA_BUS_FMT_YVYU8_2X8:
-               cr = ISI_CFG2_YCC_SWAP_MODE_1;
+               cfg2 = ISI_CFG2_YCC_SWAP_MODE_1 | ISI_CFG2_COL_SPACE_YCbCr;
                break;
        case MEDIA_BUS_FMT_YUYV8_2X8:
-               cr = ISI_CFG2_YCC_SWAP_DEFAULT;
+               cfg2 = ISI_CFG2_YCC_SWAP_DEFAULT | ISI_CFG2_COL_SPACE_YCbCr;
                break;
        /* RGB, TODO */
-       default:
-               return -EINVAL;
        }
 
        isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
-
-       cfg2 = isi_readl(isi, ISI_CFG2);
-       /* Set YCC swap mode */
-       cfg2 &= ~ISI_CFG2_YCC_SWAP_MODE_MASK;
-       cfg2 |= cr;
        /* Set width */
-       cfg2 &= ~(ISI_CFG2_IM_HSIZE_MASK);
        cfg2 |= ((width - 1) << ISI_CFG2_IM_HSIZE_OFFSET) &
                        ISI_CFG2_IM_HSIZE_MASK;
        /* Set height */
-       cfg2 &= ~(ISI_CFG2_IM_VSIZE_MASK);
        cfg2 |= ((height - 1) << ISI_CFG2_IM_VSIZE_OFFSET)
                        & ISI_CFG2_IM_VSIZE_MASK;
        isi_writel(isi, ISI_CFG2, cfg2);
+}
 
-       return 0;
+static bool is_supported(struct soc_camera_device *icd,
+               const u32 pixformat)
+{
+       switch (pixformat) {
+       /* YUV, including grey */
+       case V4L2_PIX_FMT_GREY:
+       case V4L2_PIX_FMT_YUYV:
+       case V4L2_PIX_FMT_UYVY:
+       case V4L2_PIX_FMT_YVYU:
+       case V4L2_PIX_FMT_VYUY:
+               return true;
+       /* RGB, TODO */
+       default:
+               return false;
+       }
 }
 
 static irqreturn_t atmel_isi_handle_streaming(struct atmel_isi *isi)
 {
        if (isi->active) {
-               struct vb2_buffer *vb = &isi->active->vb;
+               struct vb2_v4l2_buffer *vbuf = &isi->active->vb;
                struct frame_buffer *buf = isi->active;
 
                list_del_init(&buf->list);
-               v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
-               vb->v4l2_buf.sequence = isi->sequence++;
-               vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
+               v4l2_get_timestamp(&vbuf->timestamp);
+               vbuf->sequence = isi->sequence++;
+               vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_DONE);
        }
 
        if (list_empty(&isi->video_buffer_list)) {
@@ -225,7 +235,7 @@ static int atmel_isi_wait_status(struct atmel_isi *isi, int wait_reset)
        }
 
        timeout = wait_for_completion_timeout(&isi->complete,
-                       msecs_to_jiffies(100));
+                       msecs_to_jiffies(500));
        if (timeout == 0)
                return -ETIMEDOUT;
 
@@ -235,7 +245,7 @@ static int atmel_isi_wait_status(struct atmel_isi *isi, int wait_reset)
 /* ------------------------------------------------------------------
        Videobuf operations
    ------------------------------------------------------------------*/
-static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
+static int queue_setup(struct vb2_queue *vq, const void *parg,
                                unsigned int *nbuffers, unsigned int *nplanes,
                                unsigned int sizes[], void *alloc_ctxs[])
 {
@@ -267,7 +277,8 @@ static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
 
 static int buffer_init(struct vb2_buffer *vb)
 {
-       struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
+       struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+       struct frame_buffer *buf = container_of(vbuf, struct frame_buffer, vb);
 
        buf->p_dma_desc = NULL;
        INIT_LIST_HEAD(&buf->list);
@@ -277,8 +288,9 @@ static int buffer_init(struct vb2_buffer *vb)
 
 static int buffer_prepare(struct vb2_buffer *vb)
 {
+       struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
        struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
-       struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
+       struct frame_buffer *buf = container_of(vbuf, struct frame_buffer, vb);
        struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
        struct atmel_isi *isi = ici->priv;
        unsigned long size;
@@ -292,7 +304,7 @@ static int buffer_prepare(struct vb2_buffer *vb)
                return -EINVAL;
        }
 
-       vb2_set_plane_payload(&buf->vb, 0, size);
+       vb2_set_plane_payload(vb, 0, size);
 
        if (!buf->p_dma_desc) {
                if (list_empty(&isi->dma_desc_head)) {
@@ -319,10 +331,11 @@ static int buffer_prepare(struct vb2_buffer *vb)
 
 static void buffer_cleanup(struct vb2_buffer *vb)
 {
+       struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
        struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
        struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
        struct atmel_isi *isi = ici->priv;
-       struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
+       struct frame_buffer *buf = container_of(vbuf, struct frame_buffer, vb);
 
        /* This descriptor is available now and we add to head list */
        if (buf->p_dma_desc)
@@ -360,10 +373,11 @@ static void start_dma(struct atmel_isi *isi, struct frame_buffer *buffer)
 
 static void buffer_queue(struct vb2_buffer *vb)
 {
+       struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
        struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
        struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
        struct atmel_isi *isi = ici->priv;
-       struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
+       struct frame_buffer *buf = container_of(vbuf, struct frame_buffer, vb);
        unsigned long flags = 0;
 
        spin_lock_irqsave(&isi->lock, flags);
@@ -396,6 +410,9 @@ static int start_streaming(struct vb2_queue *vq, unsigned int count)
        /* Disable all interrupts */
        isi_writel(isi, ISI_INTDIS, (u32)~0UL);
 
+       configure_geometry(isi, icd->user_width, icd->user_height,
+                               icd->current_fmt->code);
+
        spin_lock_irq(&isi->lock);
        /* Clear any pending interrupt */
        isi_readl(isi, ISI_STATUS);
@@ -422,7 +439,7 @@ static void stop_streaming(struct vb2_queue *vq)
        /* Release all active buffers */
        list_for_each_entry_safe(buf, node, &isi->video_buffer_list, list) {
                list_del_init(&buf->list);
-               vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
+               vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
        }
        spin_unlock_irq(&isi->lock);
 
@@ -483,8 +500,6 @@ static int isi_camera_init_videobuf(struct vb2_queue *q,
 static int isi_camera_set_fmt(struct soc_camera_device *icd,
                              struct v4l2_format *f)
 {
-       struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
-       struct atmel_isi *isi = ici->priv;
        struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
        const struct soc_camera_format_xlate *xlate;
        struct v4l2_pix_format *pix = &f->fmt.pix;
@@ -494,6 +509,10 @@ static int isi_camera_set_fmt(struct soc_camera_device *icd,
        struct v4l2_mbus_framefmt *mf = &format.format;
        int ret;
 
+       /* check with atmel-isi support format, if not support use YUYV */
+       if (!is_supported(icd, pix->pixelformat))
+               pix->pixelformat = V4L2_PIX_FMT_YUYV;
+
        xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
        if (!xlate) {
                dev_warn(icd->parent, "Format %x not found\n",
@@ -517,16 +536,6 @@ static int isi_camera_set_fmt(struct soc_camera_device *icd,
        if (mf->code != xlate->code)
                return -EINVAL;
 
-       /* Enable PM and peripheral clock before operate isi registers */
-       pm_runtime_get_sync(ici->v4l2_dev.dev);
-
-       ret = configure_geometry(isi, pix->width, pix->height, xlate->code);
-
-       pm_runtime_put(ici->v4l2_dev.dev);
-
-       if (ret < 0)
-               return ret;
-
        pix->width              = mf->width;
        pix->height             = mf->height;
        pix->field              = mf->field;
@@ -553,6 +562,10 @@ static int isi_camera_try_fmt(struct soc_camera_device *icd,
        u32 pixfmt = pix->pixelformat;
        int ret;
 
+       /* check with atmel-isi support format, if not support use YUYV */
+       if (!is_supported(icd, pix->pixelformat))
+               pix->pixelformat = V4L2_PIX_FMT_YUYV;
+
        xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
        if (pixfmt && !xlate) {
                dev_warn(icd->parent, "Format %x not found\n", pixfmt);
@@ -824,6 +837,11 @@ static int isi_camera_set_bus_param(struct soc_camera_device *icd)
        if (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
                cfg1 |= ISI_CFG1_PIXCLK_POL_ACTIVE_FALLING;
 
+       dev_dbg(icd->parent, "vsync active %s, hsync active %s, sampling on pix clock %s edge\n",
+               common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW ? "low" : "high",
+               common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW ? "low" : "high",
+               common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING ? "falling" : "rising");
+
        if (isi->pdata.has_emb_sync)
                cfg1 |= ISI_CFG1_EMB_SYNC;
        if (isi->pdata.full_mode)
@@ -873,7 +891,7 @@ static int atmel_isi_remove(struct platform_device *pdev)
        return 0;
 }
 
-static int atmel_isi_probe_dt(struct atmel_isi *isi,
+static int atmel_isi_parse_dt(struct atmel_isi *isi,
                        struct platform_device *pdev)
 {
        struct device_node *np= pdev->dev.of_node;
@@ -891,9 +909,10 @@ static int atmel_isi_probe_dt(struct atmel_isi *isi,
        }
 
        err = v4l2_of_parse_endpoint(np, &ep);
+       of_node_put(np);
        if (err) {
                dev_err(&pdev->dev, "Could not parse the endpoint\n");
-               goto err_probe_dt;
+               return err;
        }
 
        switch (ep.bus.parallel.bus_width) {
@@ -907,14 +926,20 @@ static int atmel_isi_probe_dt(struct atmel_isi *isi,
        default:
                dev_err(&pdev->dev, "Unsupported bus width: %d\n",
                                ep.bus.parallel.bus_width);
-               err = -EINVAL;
-               goto err_probe_dt;
+               return -EINVAL;
        }
 
-err_probe_dt:
-       of_node_put(np);
+       if (ep.bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
+               isi->pdata.hsync_act_low = true;
+       if (ep.bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
+               isi->pdata.vsync_act_low = true;
+       if (ep.bus.parallel.flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
+               isi->pdata.pclk_act_falling = true;
 
-       return err;
+       if (ep.bus_type == V4L2_MBUS_BT656)
+               isi->pdata.has_emb_sync = true;
+
+       return 0;
 }
 
 static int atmel_isi_probe(struct platform_device *pdev)
@@ -923,16 +948,7 @@ static int atmel_isi_probe(struct platform_device *pdev)
        struct atmel_isi *isi;
        struct resource *regs;
        int ret, i;
-       struct device *dev = &pdev->dev;
        struct soc_camera_host *soc_host;
-       struct isi_platform_data *pdata;
-
-       pdata = dev->platform_data;
-       if ((!pdata || !pdata->data_width_flags) && !pdev->dev.of_node) {
-               dev_err(&pdev->dev,
-                       "No config available for Atmel ISI\n");
-               return -EINVAL;
-       }
 
        isi = devm_kzalloc(&pdev->dev, sizeof(struct atmel_isi), GFP_KERNEL);
        if (!isi) {
@@ -944,13 +960,9 @@ static int atmel_isi_probe(struct platform_device *pdev)
        if (IS_ERR(isi->pclk))
                return PTR_ERR(isi->pclk);
 
-       if (pdata) {
-               memcpy(&isi->pdata, pdata, sizeof(isi->pdata));
-       } else {
-               ret = atmel_isi_probe_dt(isi, pdev);
-               if (ret)
-                       return ret;
-       }
+       ret = atmel_isi_parse_dt(isi, pdev);
+       if (ret)
+               return ret;
 
        isi->active = NULL;
        spin_lock_init(&isi->lock);
@@ -1014,11 +1026,6 @@ static int atmel_isi_probe(struct platform_device *pdev)
        pm_suspend_ignore_children(&pdev->dev, true);
        pm_runtime_enable(&pdev->dev);
 
-       if (isi->pdata.asd_sizes) {
-               soc_host->asd = isi->pdata.asd;
-               soc_host->asd_sizes = isi->pdata.asd_sizes;
-       }
-
        ret = soc_camera_host_register(soc_host);
        if (ret) {
                dev_err(&pdev->dev, "Unable to register soc camera host\n");
@@ -1040,6 +1047,7 @@ err_alloc_ctx:
        return ret;
 }
 
+#ifdef CONFIG_PM
 static int atmel_isi_runtime_suspend(struct device *dev)
 {
        struct soc_camera_host *soc_host = to_soc_camera_host(dev);
@@ -1058,6 +1066,7 @@ static int atmel_isi_runtime_resume(struct device *dev)
 
        return clk_prepare_enable(isi->pclk);
 }
+#endif /* CONFIG_PM */
 
 static const struct dev_pm_ops atmel_isi_dev_pm_ops = {
        SET_RUNTIME_PM_OPS(atmel_isi_runtime_suspend,