/*
* Copyright (c) 2011 Atmel Corporation
* Josh Wu, <josh.wu@atmel.com>
*
* Based on previous work by Lars Haring, <lars.haring@atmel.com>
* and Sedji Gaouaou
* Based on the bttv driver for Bt848 with respective copyright holders
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/clk.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/slab.h>
#include <media/soc_camera.h>
#include <media/drv-intf/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
#define VID_LIMIT_BYTES (16 * 1024 * 1024)
#define MIN_FRAME_RATE 15
#define FRAME_INTERVAL_MILLI_SEC (1000 / MIN_FRAME_RATE)
/* Frame buffer descriptor */
struct fbd {
/* Physical address of the frame buffer */
u32 fb_address;
/* DMA Control Register(only in HISI2) */
u32 dma_ctrl;
/* Physical address of the next fbd */
u32 next_fbd_address;
};
static void set_dma_ctrl(struct fbd *fb_desc, u32 ctrl)
{
fb_desc->dma_ctrl = ctrl;
}
struct isi_dma_desc {
struct list_head list;
struct fbd *p_fbd;
dma_addr_t fbd_phys;
};
/* Frame buffer data */
struct frame_buffer {
struct vb2_v4l2_buffer vb;
struct isi_dma_desc *p_dma_desc;
struct list_head list;
};
struct atmel_isi {
/* Protects the access of variables shared with the ISR */
spinlock_t lock;
void __iomem *regs;
int sequence;
/* Allocate descriptors for dma buffer use */
struct fbd *p_fb_descriptors;
dma_addr_t fb_descriptors_phys;
struct list_head dma_desc_head;
struct isi_dma_desc dma_desc[MAX_BUFFER_NUM];
bool enable_preview_path;
struct completion complete;
/* ISI peripherial clock */
struct clk *pclk;
unsigned int irq;
struct isi_platform_data pdata;
u16 width_flags; /* max 12 bits */
struct list_head video_buffer_list;
struct frame_buffer *active;
struct soc_camera_host soc_host;
};
static void isi_writel(struct atmel_isi *isi, u32 reg, u32 val)
{
writel(val, isi->regs + reg);
}
static u32 isi_readl(struct atmel_isi *isi, u32 reg)
{
return readl(isi->regs + reg);
}
static u32 setup_cfg2_yuv_swap(struct atmel_isi *isi,
const struct soc_camera_format_xlate *xlate)
{
if (xlate->host_fmt->fourcc == V4L2_PIX_FMT_YUYV) {
/* all convert to YUYV */
switch (xlate->code) {
case MEDIA_BUS_FMT_VYUY8_2X8:
return ISI_CFG2_YCC_SWAP_MODE_3;
case MEDIA_BUS_FMT_UYVY8_2X8:
return ISI_CFG2_YCC_SWAP_MODE_2;
case MEDIA_BUS_FMT_YVYU8_2X8:
return ISI_CFG2_YCC_SWAP_MODE_1;
}
} else if (xlate->host_fmt->fourcc == V4L2_PIX_FMT_RGB565) {
/*
* Preview path is enabled, it will convert UYVY to RGB format.
* But if sensor output format is not UYVY, we need to set
* YCC_SWAP_MODE to convert it as UYVY.
*/
switch (xlate->code) {
case MEDIA_BUS_FMT_VYUY8_2X8:
return ISI_CFG2_YCC_SWAP_MODE_1;
case MEDIA_BUS_FMT_YUYV8_2X8:
return ISI_CFG2_YCC_SWAP_MODE_2;
case MEDIA_BUS_FMT_YVYU8_2X8:
return ISI_CFG2_YCC_SWAP_MODE_3;
}
}
/*
* By default, no swap for the codec path of Atmel ISI. So codec
* output is same as sensor's output.
* For instance, if sensor's output is YUYV, then codec outputs YUYV.
* And if sensor's output is UYVY, then codec outputs UYVY.
*/
return ISI_CFG2_YCC_SWAP_DEFAULT;
}
static