M7350v1_en_gpl

This commit is contained in:
T
2024-09-09 08:52:07 +00:00
commit f9cc65cfda
65988 changed files with 26357421 additions and 0 deletions
@@ -0,0 +1,7 @@
config MSM_VCAP
tristate "Qualcomm MSM VCAP"
depends on VIDEO_DEV && VIDEO_V4L2
default y
---help---
Enables VCAP driver. This device allows for video capture and
video processing using the v4l2 api
@@ -0,0 +1,3 @@
obj-$(CONFIG_MSM_VCAP) += vcap_v4l2.o
obj-$(CONFIG_MSM_VCAP) += vcap_vc.o
obj-$(CONFIG_MSM_VCAP) += vcap_vp.o
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,573 @@
/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/freezer.h>
#include <mach/camera.h>
#include <linux/io.h>
#include <linux/regulator/consumer.h>
#include <mach/clk.h>
#include <linux/clk.h>
#include <media/v4l2-event.h>
#include <media/vcap_v4l2.h>
#include <media/vcap_fmt.h>
#include "vcap_vc.h"
void config_buffer(struct vcap_client_data *c_data,
struct vcap_buffer *buf,
void __iomem *y_addr,
void __iomem *c_addr)
{
if (c_data->vc_format.color_space == HAL_VCAP_RGB) {
writel_relaxed(buf->paddr, y_addr);
} else {
int size = (c_data->vc_format.hactive_end -
c_data->vc_format.hactive_start);
if (c_data->stride == VC_STRIDE_32)
size = VCAP_STRIDE_CALC(size, VCAP_STRIDE_ALIGN_32);
else
size = VCAP_STRIDE_CALC(size, VCAP_STRIDE_ALIGN_16);
size *= (c_data->vc_format.vactive_end -
c_data->vc_format.vactive_start);
writel_relaxed(buf->paddr, y_addr);
writel_relaxed(buf->paddr + size, c_addr);
}
}
static void mov_buf_to_vp(struct work_struct *work)
{
struct vp_work_t *vp_work = container_of(work, struct vp_work_t, work);
struct v4l2_buffer p;
struct vb2_buffer *vb_vc;
struct vcap_buffer *buf_vc;
struct vb2_buffer *vb_vp;
struct vcap_buffer *buf_vp;
int rc;
p.memory = V4L2_MEMORY_USERPTR;
while (1) {
p.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (!vp_work->cd->streaming)
return;
rc = vcvp_dqbuf(&vp_work->cd->vc_vidq, &p);
if (rc < 0)
return;
vb_vc = vp_work->cd->vc_vidq.bufs[p.index];
if (NULL == vb_vc) {
pr_debug("%s: buffer is NULL\n", __func__);
vcvp_qbuf(&vp_work->cd->vc_vidq, &p);
return;
}
buf_vc = container_of(vb_vc, struct vcap_buffer, vb);
vb_vp = vp_work->cd->vp_in_vidq.bufs[p.index];
if (NULL == vb_vp) {
pr_debug("%s: buffer is NULL\n", __func__);
vcvp_qbuf(&vp_work->cd->vc_vidq, &p);
return;
}
buf_vp = container_of(vb_vp, struct vcap_buffer, vb);
buf_vp->ion_handle = buf_vc->ion_handle;
buf_vp->paddr = buf_vc->paddr;
buf_vc->ion_handle = NULL;
buf_vc->paddr = 0;
p.type = V4L2_BUF_TYPE_INTERLACED_IN_DECODER;
/* This call should not fail */
rc = vcvp_qbuf(&vp_work->cd->vp_in_vidq, &p);
if (rc < 0) {
pr_err("%s: qbuf to vp_in failed\n", __func__);
buf_vc->ion_handle = buf_vp->ion_handle;
buf_vc->paddr = buf_vp->paddr;
buf_vp->ion_handle = NULL;
buf_vp->paddr = 0;
p.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
vcvp_qbuf(&vp_work->cd->vc_vidq, &p);
}
}
}
static uint8_t correct_buf_num(uint32_t reg)
{
int i;
bool block_found = false;
for (i = 0; i < VCAP_VC_MAX_BUF; i++) {
if (reg & (0x2 << i)) {
block_found = true;
continue;
}
if (block_found)
return i;
}
return 0;
}
static struct timeval interpolate_ts(struct timeval tv, uint32_t delta)
{
if (tv.tv_usec < delta) {
tv.tv_sec--;
tv.tv_usec += VCAP_USEC - delta;
} else {
tv.tv_usec -= delta;
}
return tv;
}
inline void vc_isr_error_checking(struct vcap_dev *dev,
struct v4l2_event v4l2_evt, uint32_t irq)
{
if (irq & 0x200) {
if (irq & 0x80000000) {
writel_iowmb(0x00000102, VCAP_VC_NPL_CTRL);
v4l2_evt.type = V4L2_EVENT_PRIVATE_START +
VCAP_VC_PIX_ERR_EVENT;
v4l2_event_queue(dev->vfd, &v4l2_evt);
}
if (irq & 0x40000000) {
writel_iowmb(0x00000102, VCAP_VC_NPL_CTRL);
v4l2_evt.type = V4L2_EVENT_PRIVATE_START +
VCAP_VC_LINE_ERR_EVENT;
v4l2_event_queue(dev->vfd, &v4l2_evt);
}
if (irq & 0x20000000) {
writel_iowmb(0x00000102, VCAP_VC_NPL_CTRL);
v4l2_evt.type = V4L2_EVENT_PRIVATE_START +
VCAP_VC_VSYNC_ERR_EVENT;
v4l2_event_queue(dev->vfd, &v4l2_evt);
}
}
if (irq & 0x00001000) {
writel_iowmb(0x00000102, VCAP_VC_NPL_CTRL);
v4l2_evt.type = V4L2_EVENT_PRIVATE_START +
VCAP_VC_VSYNC_SEQ_ERR;
v4l2_event_queue(dev->vfd, &v4l2_evt);
}
if (irq & 0x00000800) {
writel_iowmb(0x00000102, VCAP_VC_NPL_CTRL);
v4l2_evt.type = V4L2_EVENT_PRIVATE_START +
VCAP_VC_NPL_OFLOW_ERR_EVENT;
v4l2_event_queue(dev->vfd, &v4l2_evt);
}
if (irq & 0x00000400) {
writel_iowmb(0x00000102, VCAP_VC_NPL_CTRL);
v4l2_evt.type = V4L2_EVENT_PRIVATE_START +
VCAP_VC_LBUF_OFLOW_ERR_EVENT;
v4l2_event_queue(dev->vfd, &v4l2_evt);
}
}
inline uint8_t vc_isr_buffer_done_count(struct vcap_dev *dev,
struct vcap_client_data *c_data, uint32_t irq)
{
int i;
uint8_t done_count = 0;
for (i = 0; i < VCAP_VC_MAX_BUF; i++) {
if (0x2 & (irq >> i))
done_count++;
}
return done_count;
}
inline bool vc_isr_verify_expect_buf_rdy(struct vcap_dev *dev,
struct vcap_client_data *c_data, struct v4l2_event v4l2_evt,
uint32_t irq, uint8_t done_count, uint8_t tot, uint8_t buf_num)
{
int i;
/* Double check expected buffers are done */
for (i = 0; i < done_count; i++) {
if (!(irq & (0x1 << (((buf_num + i) % tot) + 1)))) {
v4l2_evt.type = V4L2_EVENT_PRIVATE_START +
VCAP_VC_UNEXPECT_BUF_DONE;
v4l2_event_queue(dev->vfd, &v4l2_evt);
pr_debug("Unexpected buffer done\n");
c_data->vc_action.buf_num =
correct_buf_num(irq) % tot;
return true;
}
}
return false;
}
inline void vc_isr_update_timestamp(struct vcap_dev *dev,
struct vcap_client_data *c_data)
{
uint32_t timestamp;
timestamp = readl_relaxed(VCAP_VC_TIMESTAMP);
if (timestamp < c_data->vc_action.last_ts) {
c_data->vc_action.vc_ts.tv_usec +=
(0xFFFFFFFF - c_data->vc_action.last_ts) +
timestamp + 1;
} else {
c_data->vc_action.vc_ts.tv_usec +=
timestamp - c_data->vc_action.last_ts;
}
c_data->vc_action.vc_ts.tv_sec +=
c_data->vc_action.vc_ts.tv_usec / VCAP_USEC;
c_data->vc_action.vc_ts.tv_usec =
c_data->vc_action.vc_ts.tv_usec % VCAP_USEC;
c_data->vc_action.last_ts = timestamp;
}
inline void vc_isr_no_new_buffer(struct vcap_dev *dev,
struct vcap_client_data *c_data, struct v4l2_event v4l2_evt)
{
v4l2_evt.type = V4L2_EVENT_PRIVATE_START +
VCAP_VC_BUF_OVERWRITE_EVENT;
v4l2_event_queue(dev->vfd, &v4l2_evt);
c_data->vc_action.field_dropped =
!c_data->vc_action.field_dropped;
c_data->vc_action.field1 =
!c_data->vc_action.field1;
atomic_inc(&dev->dbg_p.vc_drop_count);
}
inline void vc_isr_switch_buffers(struct vcap_dev *dev,
struct vcap_client_data *c_data, struct vcap_buffer *buf,
struct vb2_buffer *vb, uint8_t idx, int done_count, int i)
{
/* Config vc with this new buffer */
config_buffer(c_data, buf, VCAP_VC_Y_ADDR_1 + 0x8 * idx,
VCAP_VC_C_ADDR_1 + 0x8 * idx);
vb->v4l2_buf.timestamp = interpolate_ts(
c_data->vc_action.vc_ts,
1000000 / c_data->vc_format.frame_rate *
(done_count - 1 - i));
if (c_data->vc_format.mode == HAL_VCAP_MODE_INT) {
if (c_data->vc_action.field1)
vb->v4l2_buf.field = V4L2_FIELD_TOP;
else
vb->v4l2_buf.field = V4L2_FIELD_BOTTOM;
c_data->vc_action.field1 =
!c_data->vc_action.field1;
}
vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
c_data->vc_action.buf[idx] = buf;
}
inline bool vc_isr_change_buffers(struct vcap_dev *dev,
struct vcap_client_data *c_data, struct v4l2_event v4l2_evt,
int done_count, uint8_t tot, uint8_t buf_num)
{
struct vb2_buffer *vb = NULL;
struct vcap_buffer *buf;
bool schedule_work = false;
uint8_t idx;
int i;
for (i = 0; i < done_count; i++) {
idx = (buf_num + i) % tot;
vb = &c_data->vc_action.buf[idx]->vb;
spin_lock(&c_data->cap_slock);
if (list_empty(&c_data->vc_action.active)) {
spin_unlock(&c_data->cap_slock);
vc_isr_no_new_buffer(dev, c_data, v4l2_evt);
continue;
}
if (c_data->vc_format.mode == HAL_VCAP_MODE_INT &&
c_data->vc_action.field_dropped) {
spin_unlock(&c_data->cap_slock);
vc_isr_no_new_buffer(dev, c_data, v4l2_evt);
continue;
}
buf = list_entry(c_data->vc_action.active.next,
struct vcap_buffer, list);
list_del(&buf->list);
spin_unlock(&c_data->cap_slock);
vc_isr_switch_buffers(dev, c_data, buf, vb, idx, done_count, i);
schedule_work = true;
}
return schedule_work;
}
irqreturn_t vc_handler(struct vcap_dev *dev)
{
uint32_t irq;
struct vcap_client_data *c_data;
struct v4l2_event v4l2_evt;
uint8_t done_count = 0, buf_num, tot;
bool schedule_work = false;
v4l2_evt.id = 0;
irq = readl_relaxed(VCAP_VC_INT_STATUS);
writel_relaxed(irq, VCAP_VC_INT_CLEAR);
pr_debug("%s: irq=0x%08x\n", __func__, irq);
if (dev->vc_client == NULL) {
/* This should never happen */
pr_err("VC: There is no active vc client\n");
return IRQ_HANDLED;
}
c_data = dev->vc_client;
if (!c_data->streaming) {
pr_err("VC no longer streaming\n");
return IRQ_HANDLED;
}
if (irq == VC_VSYNC_MASK) {
if (c_data->vc_format.mode == HAL_VCAP_MODE_INT)
c_data->vc_action.field1 = irq & 0x1;
return IRQ_HANDLED;
}
if (irq & VC_ERR_MASK) {
vc_isr_error_checking(dev, v4l2_evt, irq);
return IRQ_HANDLED;
}
if (!(irq & VC_BUFFER_MASK)) {
pr_debug("No frames done\n");
return IRQ_HANDLED;
}
done_count = vc_isr_buffer_done_count(dev, c_data, irq);
buf_num = c_data->vc_action.buf_num;
tot = c_data->vc_action.tot_buf;
if (vc_isr_verify_expect_buf_rdy(dev, c_data,
v4l2_evt, irq, done_count, tot, buf_num))
return IRQ_HANDLED;
vc_isr_update_timestamp(dev, c_data);
c_data->vc_action.buf_num = (buf_num + done_count) % tot;
schedule_work = vc_isr_change_buffers(dev, c_data, v4l2_evt,
done_count, tot, buf_num);
if (schedule_work && c_data->op_mode == VC_AND_VP_VCAP_OP)
queue_work(dev->vcap_wq, &dev->vc_to_vp_work.work);
return IRQ_HANDLED;
}
int vc_start_capture(struct vcap_client_data *c_data)
{
return 0;
}
int vc_hw_kick_off(struct vcap_client_data *c_data)
{
struct vc_action *vc_action = &c_data->vc_action;
struct vcap_dev *dev;
struct timeval tv;
unsigned long flags = 0;
int rc, i, counter = 0;
struct vcap_buffer *buf;
dev = c_data->dev;
pr_debug("Start Kickoff\n");
if (dev->vc_client == NULL) {
pr_err("No active vc client\n");
return -ENODEV;
}
c_data->vc_action.buf_num = 0;
spin_lock_irqsave(&dev->vc_client->cap_slock, flags);
if (list_empty(&dev->vc_client->vc_action.active)) {
spin_unlock_irqrestore(&dev->vc_client->cap_slock, flags);
pr_err("%s: VC We have no more avilable buffers\n",
__func__);
return -EINVAL;
}
list_for_each_entry(buf, &vc_action->active, list)
counter++;
if (counter < c_data->vc_action.tot_buf) {
/* not enough buffers have been queued */
spin_unlock_irqrestore(&dev->vc_client->cap_slock, flags);
return -EINVAL;
}
for (i = 0; i < c_data->vc_action.tot_buf; i++) {
vc_action->buf[i] = list_entry(vc_action->active.next,
struct vcap_buffer, list);
list_del(&vc_action->buf[i]->list);
}
spin_unlock_irqrestore(&dev->vc_client->cap_slock, flags);
for (i = 0; i < c_data->vc_action.tot_buf; i++) {
config_buffer(c_data, vc_action->buf[i],
VCAP_VC_Y_ADDR_1 + i * 8,
VCAP_VC_C_ADDR_1 + i * 8);
}
c_data->vc_action.last_ts = readl_relaxed(VCAP_VC_TIMESTAMP);
c_data->vc_action.vc_ts.tv_sec =
c_data->vc_action.last_ts / VCAP_USEC;
c_data->vc_action.vc_ts.tv_usec =
c_data->vc_action.last_ts % VCAP_USEC;
atomic_set(&dev->dbg_p.vc_drop_count, 0);
do_gettimeofday(&tv);
dev->dbg_p.vc_timestamp = (uint32_t) (tv.tv_sec * VCAP_USEC +
tv.tv_usec);
rc = 0;
for (i = 0; i < c_data->vc_action.tot_buf; i++)
rc = rc << 1 | 0x2;
rc |= VC_ERR_MASK;
rc |= VC_VSYNC_MASK;
writel_relaxed(rc, VCAP_VC_INT_MASK);
enable_irq(dev->vcirq->start);
rc = readl_relaxed(VCAP_VC_CTRL);
writel_iowmb(rc | 0x1, VCAP_VC_CTRL);
return 0;
}
void vc_stop_capture(struct vcap_client_data *c_data)
{
struct vcap_dev *dev = c_data->dev;
unsigned int reg;
int timeout;
writel_iowmb(0x00000102, VCAP_VC_NPL_CTRL);
writel_iowmb(0x0, VCAP_VC_INT_MASK);
flush_workqueue(dev->vcap_wq);
if (atomic_read(&dev->vc_enabled) == 1)
disable_irq_nosync(dev->vcirq->start);
writel_iowmb(0x00000000, VCAP_VC_CTRL);
writel_iowmb(0x00000001, VCAP_SW_RESET_REQ);
timeout = 10000;
while (1) {
reg = (readl_relaxed(VCAP_SW_RESET_STATUS) & 0x1);
if (!reg)
break;
timeout--;
if (timeout == 0) {
/* This should not happen */
pr_err("VC is not resetting properly\n");
writel_iowmb(0x00000000, VCAP_SW_RESET_REQ);
break;
}
}
reg = readl_relaxed(VCAP_VC_NPL_CTRL);
reg = readl_relaxed(VCAP_VC_NPL_CTRL);
writel_iowmb(0x00000002, VCAP_VC_NPL_CTRL);
}
int config_vc_format(struct vcap_client_data *c_data)
{
struct vcap_dev *dev;
unsigned int rc;
int timeout;
struct v4l2_format_vc_ext *vc_format = &c_data->vc_format;
dev = c_data->dev;
/* restart VC */
writel_iowmb(0x00000102, VCAP_VC_NPL_CTRL);
writel_iowmb(0x00000001, VCAP_SW_RESET_REQ);
timeout = 10000;
while (1) {
if (!(readl_relaxed(VCAP_SW_RESET_STATUS) & 0x1))
break;
timeout--;
if (timeout == 0) {
pr_err("VC is not resetting properly\n");
writel_iowmb(0x00000002, VCAP_VC_NPL_CTRL);
return -EINVAL;
}
}
rc = readl_relaxed(VCAP_VC_NPL_CTRL);
rc = readl_relaxed(VCAP_VC_NPL_CTRL);
writel_iowmb(0x00000002, VCAP_VC_NPL_CTRL);
pr_debug("%s: Starting VC configuration\n", __func__);
writel_iowmb(0x00000002, VCAP_VC_NPL_CTRL);
writel_iowmb(0x00000004 | vc_format->color_space << 1 |
vc_format->mode << 3 |
(c_data->vc_action.tot_buf - 2) << 4 |
vc_format->mode << 10,
VCAP_VC_CTRL);
writel_relaxed(vc_format->d_polar << 8 |
vc_format->h_polar << 4 |
vc_format->v_polar << 0, VCAP_VC_POLARITY);
writel_relaxed(((vc_format->htotal << 16) | vc_format->vtotal),
VCAP_VC_V_H_TOTAL);
writel_relaxed(((vc_format->hactive_end << 16) |
vc_format->hactive_start), VCAP_VC_H_ACTIVE);
writel_relaxed(((vc_format->vactive_end << 16) |
vc_format->vactive_start), VCAP_VC_V_ACTIVE);
writel_relaxed(((vc_format->f2_vactive_end << 16) |
vc_format->f2_vactive_start), VCAP_VC_V_ACTIVE_F2);
writel_relaxed(((vc_format->vsync_end << 16) | vc_format->vsync_start),
VCAP_VC_VSYNC_VPOS);
writel_relaxed(((vc_format->f2_vsync_v_end << 16) |
vc_format->f2_vsync_v_start), VCAP_VC_VSYNC_F2_VPOS);
writel_relaxed(((vc_format->hsync_end << 16) |
vc_format->hsync_start), VCAP_VC_HSYNC_HPOS);
writel_relaxed(((vc_format->f2_vsync_h_end << 16) |
vc_format->f2_vsync_h_start), VCAP_VC_VSYNC_F2_HPOS);
writel_iowmb(0x000033FF, VCAP_VC_BUF_CTRL);
rc = vc_format->hactive_end - vc_format->hactive_start;
if (c_data->stride == VC_STRIDE_32)
rc = VCAP_STRIDE_CALC(rc, VCAP_STRIDE_ALIGN_32);
else
rc = VCAP_STRIDE_CALC(rc, VCAP_STRIDE_ALIGN_16);
if (vc_format->color_space)
rc *= 3;
writel_relaxed(rc, VCAP_VC_Y_STRIDE);
writel_relaxed(rc, VCAP_VC_C_STRIDE);
writel_relaxed(0x00010033 , VCAP_OFFSET(0x0898));
writel_relaxed(0x00010fff , VCAP_OFFSET(0x089c));
writel_relaxed(0x0a418820, VCAP_VC_IN_CTRL1);
writel_relaxed(0x16a4a0e6, VCAP_VC_IN_CTRL2);
writel_relaxed(0x2307b9ac, VCAP_VC_IN_CTRL3);
writel_relaxed(0x2f6ad272, VCAP_VC_IN_CTRL4);
writel_relaxed(0x00006b38, VCAP_VC_IN_CTRL5);
writel_iowmb(0x00000001 , VCAP_OFFSET(0x0d00));
pr_debug("%s: Done VC configuration\n", __func__);
return 0;
}
int detect_vc(struct vcap_dev *dev)
{
int result;
result = readl_relaxed(VCAP_HARDWARE_VERSION_REG);
pr_debug("Hardware version: %08x\n", result);
if (result != VCAP_HARDWARE_VERSION)
return -ENODEV;
INIT_WORK(&dev->vc_to_vp_work.work, mov_buf_to_vp);
return 0;
}
int deinit_vc(void)
{
return 0;
}
@@ -0,0 +1,76 @@
/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#ifndef VCAP_VC_H
#define VCAP_VC_H
#include <linux/interrupt.h>
#include <media/vcap_v4l2.h>
#define VCAP_HARDWARE_VERSION 0x10000000
#define VCAP_HARDWARE_VERSION_REG (VCAP_BASE + 0x0000)
#define VCAP_VC_CTRL (VCAP_BASE + 0x0800)
#define VCAP_VC_NPL_CTRL (VCAP_BASE + 0x0804)
#define VCAP_VC_POLARITY (VCAP_BASE + 0x081c)
#define VCAP_VC_V_H_TOTAL (VCAP_BASE + 0x0820)
#define VCAP_VC_H_ACTIVE (VCAP_BASE + 0x0824)
#define VCAP_VC_V_ACTIVE (VCAP_BASE + 0x0828)
#define VCAP_VC_V_ACTIVE_F2 (VCAP_BASE + 0x0830)
#define VCAP_VC_VSYNC_VPOS (VCAP_BASE + 0x0834)
#define VCAP_VC_VSYNC_F2_VPOS (VCAP_BASE + 0x0838)
#define VCAP_VC_HSYNC_HPOS (VCAP_BASE + 0x0840)
#define VCAP_VC_VSYNC_F2_HPOS (VCAP_BASE + 0x083c)
#define VCAP_VC_BUF_CTRL (VCAP_BASE + 0x0848)
#define VCAP_VC_Y_STRIDE (VCAP_BASE + 0x084c)
#define VCAP_VC_C_STRIDE (VCAP_BASE + 0x0850)
#define VCAP_VC_Y_ADDR_1 (VCAP_BASE + 0x0854)
#define VCAP_VC_C_ADDR_1 (VCAP_BASE + 0x0858)
#define VCAP_VC_Y_ADDR_2 (VCAP_BASE + 0x085c)
#define VCAP_VC_C_ADDR_2 (VCAP_BASE + 0x0860)
#define VCAP_VC_Y_ADDR_3 (VCAP_BASE + 0x0864)
#define VCAP_VC_C_ADDR_3 (VCAP_BASE + 0x0868)
#define VCAP_VC_Y_ADDR_4 (VCAP_BASE + 0x086c)
#define VCAP_VC_C_ADDR_4 (VCAP_BASE + 0x0870)
#define VCAP_VC_Y_ADDR_5 (VCAP_BASE + 0x0874)
#define VCAP_VC_C_ADDR_5 (VCAP_BASE + 0x0878)
#define VCAP_VC_Y_ADDR_6 (VCAP_BASE + 0x087c)
#define VCAP_VC_C_ADDR_6 (VCAP_BASE + 0x0880)
#define VCAP_VC_IN_CTRL1 (VCAP_BASE + 0x0808)
#define VCAP_VC_IN_CTRL2 (VCAP_BASE + 0x080c)
#define VCAP_VC_IN_CTRL3 (VCAP_BASE + 0x0810)
#define VCAP_VC_IN_CTRL4 (VCAP_BASE + 0x0814)
#define VCAP_VC_IN_CTRL5 (VCAP_BASE + 0x0818)
#define VCAP_VC_INT_MASK (VCAP_BASE + 0x0884)
#define VCAP_VC_INT_CLEAR (VCAP_BASE + 0x0888)
#define VCAP_VC_INT_STATUS (VCAP_BASE + 0x088c)
#define VCAP_VC_TIMESTAMP (VCAP_BASE + 0x0034)
#define VC_BUFFER_WRITTEN (0x3 << 1)
#define VC_BUFFER_MASK 0x7E
#define VC_ERR_MASK 0xE0001E00
#define VC_VSYNC_MASK 0x1
int vc_start_capture(struct vcap_client_data *c_data);
int vc_hw_kick_off(struct vcap_client_data *c_data);
void vc_stop_capture(struct vcap_client_data *c_data);
int config_vc_format(struct vcap_client_data *c_data);
int detect_vc(struct vcap_dev *dev);
int deinit_vc(void);
irqreturn_t vc_handler(struct vcap_dev *dev);
#endif
@@ -0,0 +1,897 @@
/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/freezer.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <mach/camera.h>
#include <mach/clk.h>
#include <media/v4l2-event.h>
#include <media/vcap_v4l2.h>
#include <media/vcap_fmt.h>
#include "vcap_vp.h"
void config_nr_buffer(struct vcap_client_data *c_data,
struct vcap_buffer *buf)
{
struct vcap_dev *dev = c_data->dev;
int size = c_data->vp_in_fmt.height * c_data->vp_in_fmt.width;
writel_relaxed(buf->paddr, VCAP_VP_NR_T2_Y_BASE_ADDR);
writel_relaxed(buf->paddr + size, VCAP_VP_NR_T2_C_BASE_ADDR);
}
void config_in_buffer(struct vcap_client_data *c_data,
struct vcap_buffer *buf)
{
struct vcap_dev *dev = c_data->dev;
int size = c_data->vp_in_fmt.height * c_data->vp_in_fmt.width;
writel_relaxed(buf->paddr, VCAP_VP_T2_Y_BASE_ADDR);
writel_relaxed(buf->paddr + size, VCAP_VP_T2_C_BASE_ADDR);
}
void config_out_buffer(struct vcap_client_data *c_data,
struct vcap_buffer *buf)
{
struct vcap_dev *dev = c_data->dev;
int size;
size = c_data->vp_out_fmt.height * c_data->vp_out_fmt.width;
writel_relaxed(buf->paddr, VCAP_VP_OUT_Y_BASE_ADDR);
writel_relaxed(buf->paddr + size, VCAP_VP_OUT_C_BASE_ADDR);
}
int vp_setup_buffers(struct vcap_client_data *c_data)
{
struct vp_action *vp_act;
struct vcap_dev *dev;
unsigned long flags = 0;
if (!c_data->streaming)
return -ENOEXEC;
dev = c_data->dev;
pr_debug("VP: Start setup buffers\n");
if (dev->vp_shutdown) {
pr_debug("%s: VP shutting down, no buf setup\n",
__func__);
return -EPERM;
}
/* No need to verify vp_client is not NULL caller does so */
vp_act = &dev->vp_client->vp_action;
spin_lock_irqsave(&dev->vp_client->cap_slock, flags);
if (list_empty(&vp_act->in_active)) {
spin_unlock_irqrestore(&dev->vp_client->cap_slock, flags);
pr_debug("%s: VP We have no more input buffers\n",
__func__);
return -EAGAIN;
}
if (list_empty(&vp_act->out_active)) {
spin_unlock_irqrestore(&dev->vp_client->cap_slock,
flags);
pr_debug("%s: VP We have no more output buffers\n",
__func__);
return -EAGAIN;
}
vp_act->bufT2 = list_entry(vp_act->in_active.next,
struct vcap_buffer, list);
list_del(&vp_act->bufT2->list);
vp_act->bufOut = list_entry(vp_act->out_active.next,
struct vcap_buffer, list);
list_del(&vp_act->bufOut->list);
spin_unlock_irqrestore(&dev->vp_client->cap_slock, flags);
config_in_buffer(c_data, vp_act->bufT2);
config_out_buffer(c_data, vp_act->bufOut);
return 0;
}
static void mov_buf_to_vc(struct work_struct *work)
{
struct vp_work_t *vp_work = container_of(work, struct vp_work_t, work);
struct v4l2_buffer p;
struct vb2_buffer *vb_vc;
struct vcap_buffer *buf_vc;
struct vb2_buffer *vb_vp;
struct vcap_buffer *buf_vp;
int rc;
p.memory = V4L2_MEMORY_USERPTR;
/* This loop exits when there is no more buffers left */
while (1) {
p.type = V4L2_BUF_TYPE_INTERLACED_IN_DECODER;
if (!vp_work->cd->streaming)
return;
rc = vcvp_dqbuf(&vp_work->cd->vp_in_vidq, &p);
if (rc < 0)
return;
vb_vc = vp_work->cd->vc_vidq.bufs[p.index];
if (NULL == vb_vc) {
pr_debug("%s: buffer is NULL\n", __func__);
vcvp_qbuf(&vp_work->cd->vp_in_vidq, &p);
return;
}
buf_vc = container_of(vb_vc, struct vcap_buffer, vb);
vb_vp = vp_work->cd->vp_in_vidq.bufs[p.index];
if (NULL == vb_vp) {
pr_debug("%s: buffer is NULL\n", __func__);
vcvp_qbuf(&vp_work->cd->vp_in_vidq, &p);
return;
}
buf_vp = container_of(vb_vp, struct vcap_buffer, vb);
buf_vc->ion_handle = buf_vp->ion_handle;
buf_vc->paddr = buf_vp->paddr;
buf_vp->ion_handle = NULL;
buf_vp->paddr = 0;
p.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
/* This call should not fail */
rc = vcvp_qbuf(&vp_work->cd->vc_vidq, &p);
if (rc < 0) {
pr_err("%s: qbuf to vc failed\n", __func__);
buf_vp->ion_handle = buf_vc->ion_handle;
buf_vp->paddr = buf_vc->paddr;
buf_vc->ion_handle = NULL;
buf_vc->paddr = 0;
p.type = V4L2_BUF_TYPE_INTERLACED_IN_DECODER;
vcvp_qbuf(&vp_work->cd->vp_in_vidq, &p);
}
}
}
void update_nr_value(struct vcap_dev *dev)
{
struct nr_param *par;
uint32_t val = 0;
par = &dev->nr_param;
if (par->mode == NR_MANUAL) {
writel_relaxed(par->window << 24 | par->decay_ratio << 20,
VCAP_VP_NR_CONFIG);
if (par->threshold)
val = VP_NR_DYNAMIC_THRESHOLD;
writel_relaxed(val |
par->luma.max_blend_ratio << 24 |
par->luma.scale_diff_ratio << 12 |
par->luma.diff_limit_ratio << 8 |
par->luma.scale_motion_ratio << 4 |
par->luma.blend_limit_ratio << 0,
VCAP_VP_NR_LUMA_CONFIG);
writel_relaxed(val |
par->chroma.max_blend_ratio << 24 |
par->chroma.scale_diff_ratio << 12 |
par->chroma.diff_limit_ratio << 8 |
par->chroma.scale_motion_ratio << 4 |
par->chroma.blend_limit_ratio << 0,
VCAP_VP_NR_CHROMA_CONFIG);
}
dev->nr_update = false;
}
static void vp_wq_fnc(struct work_struct *work)
{
struct vp_work_t *vp_work = container_of(work, struct vp_work_t, work);
struct vcap_dev *dev;
struct vp_action *vp_act;
struct timeval tv;
unsigned long flags = 0;
uint32_t irq;
int rc;
bool top_field = 0;
if (vp_work && vp_work->cd && vp_work->cd->dev)
dev = vp_work->cd->dev;
else
return;
vp_act = &dev->vp_client->vp_action;
rc = readl_relaxed(VCAP_OFFSET(0x048));
while (!(rc & 0x00000100))
rc = readl_relaxed(VCAP_OFFSET(0x048));
irq = readl_relaxed(VCAP_VP_INT_STATUS);
writel_relaxed(0x00000000, VCAP_VP_BAL_VMOTION_STATE);
writel_relaxed(0x40000000, VCAP_VP_REDUCT_AVG_MOTION2);
spin_lock_irqsave(&dev->vp_client->cap_slock, flags);
if (dev->nr_update == true)
update_nr_value(dev);
spin_unlock_irqrestore(&dev->vp_client->cap_slock, flags);
/* Queue the done buffers */
if (vp_act->vp_state == VP_NORMAL &&
vp_act->bufNR.nr_pos != TM1_BUF) {
vb2_buffer_done(&vp_act->bufTm1->vb, VB2_BUF_STATE_DONE);
if (vp_work->cd->op_mode == VC_AND_VP_VCAP_OP)
queue_work(dev->vcap_wq, &dev->vp_to_vc_work.work);
}
if (vp_act->bufT0 != NULL && vp_act->vp_state == VP_NORMAL) {
vp_act->bufOut->vb.v4l2_buf.timestamp =
vp_act->bufT0->vb.v4l2_buf.timestamp;
}
vb2_buffer_done(&vp_act->bufOut->vb, VB2_BUF_STATE_DONE);
/* Cycle to next state */
if (vp_act->vp_state != VP_NORMAL)
vp_act->vp_state++;
/* Cycle Buffers*/
if (dev->nr_param.mode) {
if (vp_act->bufNR.nr_pos == TM1_BUF)
vp_act->bufNR.nr_pos = BUF_NOT_IN_USE;
if (vp_act->bufNR.nr_pos != BUF_NOT_IN_USE)
vp_act->bufNR.nr_pos++;
vp_act->bufTm1 = vp_act->bufT0;
vp_act->bufT0 = vp_act->bufT1;
vp_act->bufT1 = vp_act->bufNRT2;
vp_act->bufNRT2 = vp_act->bufT2;
config_nr_buffer(vp_work->cd, vp_act->bufNRT2);
} else {
vp_act->bufTm1 = vp_act->bufT0;
vp_act->bufT0 = vp_act->bufT1;
vp_act->bufT1 = vp_act->bufT2;
}
rc = vp_setup_buffers(vp_work->cd);
if (rc < 0) {
/* setup_buf failed because we are waiting for buffers */
writel_relaxed(0x00000000, VCAP_VP_INTERRUPT_ENABLE);
writel_iowmb(irq, VCAP_VP_INT_CLEAR);
atomic_set(&dev->vp_enabled, 0);
if (dev->vp_shutdown)
wake_up(&dev->vp_dummy_waitq);
return;
}
/* Config VP */
if (vp_act->bufT2->vb.v4l2_buf.field == V4L2_FIELD_BOTTOM)
top_field = 1;
writel_iowmb(0x00000000 | top_field, VCAP_VP_CTRL);
writel_iowmb(0x00010000 | top_field, VCAP_VP_CTRL);
enable_irq(dev->vpirq->start);
do_gettimeofday(&tv);
dev->dbg_p.vp_timestamp = (uint32_t) (tv.tv_sec * VCAP_USEC +
tv.tv_usec);
writel_iowmb(irq, VCAP_VP_INT_CLEAR);
}
irqreturn_t vp_handler(struct vcap_dev *dev)
{
struct vcap_client_data *c_data;
struct vp_action *vp_act;
struct v4l2_event v4l2_evt;
uint32_t irq;
int rc;
struct timeval tv;
uint32_t new_ts;
irq = readl_relaxed(VCAP_VP_INT_STATUS);
if (dev->vp_dummy_event == true) {
writel_relaxed(irq, VCAP_VP_INT_CLEAR);
dev->vp_dummy_complete = true;
wake_up(&dev->vp_dummy_waitq);
return IRQ_HANDLED;
}
if (irq & 0x02000000) {
v4l2_evt.type = V4L2_EVENT_PRIVATE_START +
VCAP_VP_REG_R_ERR_EVENT;
v4l2_event_queue(dev->vfd, &v4l2_evt);
}
if (irq & 0x01000000) {
v4l2_evt.type = V4L2_EVENT_PRIVATE_START +
VCAP_VP_REG_W_ERR_EVENT;
v4l2_event_queue(dev->vfd, &v4l2_evt);
}
if (irq & 0x00020000) {
v4l2_evt.type = V4L2_EVENT_PRIVATE_START +
VCAP_VP_IN_HEIGHT_ERR_EVENT;
v4l2_event_queue(dev->vfd, &v4l2_evt);
}
if (irq & 0x00010000) {
v4l2_evt.type = V4L2_EVENT_PRIVATE_START +
VCAP_VP_IN_WIDTH_ERR_EVENT;
v4l2_event_queue(dev->vfd, &v4l2_evt);
}
pr_debug("%s: irq=0x%08x\n", __func__, irq);
if (!(irq & (VP_PIC_DONE | VP_MODE_CHANGE))) {
writel_relaxed(irq, VCAP_VP_INT_CLEAR);
pr_err("VP IRQ shows some error\n");
return IRQ_HANDLED;
}
if (dev->vp_client == NULL) {
writel_relaxed(irq, VCAP_VP_INT_CLEAR);
pr_err("VC: There is no active vp client\n");
return IRQ_HANDLED;
}
vp_act = &dev->vp_client->vp_action;
c_data = dev->vp_client;
if (vp_act->vp_state == VP_UNKNOWN) {
writel_relaxed(irq, VCAP_VP_INT_CLEAR);
pr_err("%s: VP is in an unknown state\n",
__func__);
return -EAGAIN;
}
do_gettimeofday(&tv);
new_ts = (uint32_t) (tv.tv_sec * VCAP_USEC +
tv.tv_usec);
if (new_ts > dev->dbg_p.vp_timestamp) {
dev->dbg_p.vp_ewma = ((new_ts - dev->dbg_p.vp_timestamp) /
10 + (dev->dbg_p.vp_ewma / 10 * 9));
}
dev->dbg_p.vp_timestamp = (uint32_t) (tv.tv_sec * VCAP_USEC +
tv.tv_usec);
INIT_WORK(&dev->vp_work.work, vp_wq_fnc);
dev->vp_work.cd = c_data;
rc = queue_work(dev->vcap_wq, &dev->vp_work.work);
disable_irq_nosync(dev->vpirq->start);
return IRQ_HANDLED;
}
int vp_sw_reset(struct vcap_dev *dev)
{
int timeout;
writel_iowmb(0x00000010, VCAP_SW_RESET_REQ);
timeout = 10000;
while (1) {
if (!(readl_relaxed(VCAP_SW_RESET_STATUS) & 0x10))
break;
timeout--;
if (timeout == 0) {
/* This should not happen */
pr_err("VP is not resetting properly\n");
writel_iowmb(0x00000000, VCAP_SW_RESET_REQ);
return -EINVAL;
}
}
return 0;
}
void vp_stop_capture(struct vcap_client_data *c_data)
{
struct vcap_dev *dev = c_data->dev;
int rc;
dev->vp_shutdown = true;
flush_workqueue(dev->vcap_wq);
if (atomic_read(&dev->vp_enabled) == 1) {
rc = wait_event_interruptible_timeout(dev->vp_dummy_waitq,
!atomic_read(&dev->vp_enabled),
msecs_to_jiffies(50));
if (rc == 0 && atomic_read(&dev->vp_enabled) == 1) {
/* This should not happen, if it does hw is stuck */
disable_irq_nosync(dev->vpirq->start);
atomic_set(&dev->vp_enabled, 0);
pr_err("%s: VP Timeout and VP still running\n",
__func__);
}
}
vp_sw_reset(dev);
dev->vp_shutdown = false;
}
int config_vp_format(struct vcap_client_data *c_data)
{
struct vcap_dev *dev = c_data->dev;
int rc;
INIT_WORK(&dev->vp_to_vc_work.work, mov_buf_to_vc);
dev->vp_to_vc_work.cd = c_data;
/* SW restart VP */
rc = vp_sw_reset(dev);
if (rc < 0)
return rc;
/* Film Mode related settings */
writel_iowmb(0x00000000, VCAP_VP_FILM_PROJECTION_T0);
writel_relaxed(0x00000000, VCAP_VP_FILM_PROJECTION_T2);
writel_relaxed(0x00000000, VCAP_VP_FILM_PAST_MAX_PROJ);
writel_relaxed(0x00000000, VCAP_VP_FILM_PAST_MIN_PROJ);
writel_relaxed(0x00000000, VCAP_VP_FILM_SEQUENCE_HIST);
writel_relaxed(0x00000000, VCAP_VP_FILM_MODE_STATE);
writel_relaxed(0x00000000, VCAP_VP_BAL_VMOTION_STATE);
writel_relaxed(0x00000010, VCAP_VP_REDUCT_AVG_MOTION);
writel_relaxed(0x40000000, VCAP_VP_REDUCT_AVG_MOTION2);
writel_relaxed(0x40000000, VCAP_VP_NR_AVG_LUMA);
writel_relaxed(0x40000000, VCAP_VP_NR_AVG_CHROMA);
writel_relaxed(0x40000000, VCAP_VP_NR_CTRL_LUMA);
writel_relaxed(0x40000000, VCAP_VP_NR_CTRL_CHROMA);
writel_relaxed(0x00000000, VCAP_VP_BAL_AVG_BLEND);
writel_relaxed(0x00000000, VCAP_VP_VMOTION_HIST);
writel_relaxed(0x05047D19, VCAP_VP_FILM_ANALYSIS_CONFIG);
writel_relaxed(0x20260200, VCAP_VP_FILM_STATE_CONFIG);
writel_relaxed(0x23A60114, VCAP_VP_FVM_CONFIG);
writel_relaxed(0x03043210, VCAP_VP_FILM_ANALYSIS_CONFIG2);
writel_relaxed(0x04DB7A51, VCAP_VP_MIXED_ANALYSIS_CONFIG);
writel_relaxed(0x14224916, VCAP_VP_SPATIAL_CONFIG);
writel_relaxed(0x83270400, VCAP_VP_SPATIAL_CONFIG2);
writel_relaxed(0x0F000F92, VCAP_VP_SPATIAL_CONFIG3);
writel_relaxed(0x00000000, VCAP_VP_TEMPORAL_CONFIG);
writel_relaxed(0x00000000, VCAP_VP_PIXEL_DIFF_CONFIG);
writel_relaxed(0x0C090511, VCAP_VP_H_FREQ_CONFIG);
writel_relaxed(0x0A000000, VCAP_VP_NR_CONFIG);
writel_relaxed(0x008F4149, VCAP_VP_NR_LUMA_CONFIG);
writel_relaxed(0x008F4149, VCAP_VP_NR_CHROMA_CONFIG);
writel_relaxed(0x43C0FD0C, VCAP_VP_BAL_CONFIG);
writel_relaxed(0x00000255, VCAP_VP_BAL_MOTION_CONFIG);
writel_relaxed(0x24154252, VCAP_VP_BAL_LIGHT_COMB);
writel_relaxed(0x10024414, VCAP_VP_BAL_VMOTION_CONFIG);
writel_relaxed(0x00000002, VCAP_VP_NR_CONFIG2);
writel_relaxed((c_data->vp_out_fmt.height-1)<<16 |
(c_data->vp_out_fmt.width - 1), VCAP_VP_FRAME_SIZE);
writel_relaxed(0x00000000, VCAP_VP_SPLIT_SCRN_CTRL);
return 0;
}
int init_motion_buf(struct vcap_client_data *c_data)
{
int rc;
struct vcap_dev *dev = c_data->dev;
struct ion_handle *handle = NULL;
unsigned long paddr, len;
void *vaddr;
size_t size = ((c_data->vp_out_fmt.width + 63) >> 6) *
((c_data->vp_out_fmt.height + 7) >> 3) * 16;
if (c_data->vp_action.motionHandle) {
pr_err("Motion buffer has already been created");
return -ENOEXEC;
}
handle = ion_alloc(dev->ion_client, size, SZ_4K,
ION_HEAP(ION_CP_MM_HEAP_ID), 0);
if (IS_ERR_OR_NULL(handle)) {
pr_err("%s: ion_alloc failed\n", __func__);
return -ENOMEM;
}
vaddr = ion_map_kernel(dev->ion_client, handle);
if (IS_ERR(vaddr)) {
pr_err("%s: Map motion buffer failed\n", __func__);
ion_free(dev->ion_client, handle);
rc = -ENOMEM;
return rc;
}
memset(vaddr, 0, size);
ion_unmap_kernel(dev->ion_client, handle);
rc = ion_map_iommu(dev->ion_client, handle,
dev->domain_num, 0, SZ_4K, 0, &paddr, &len,
0, 0);
if (rc < 0) {
pr_err("%s: map_iommu failed\n", __func__);
ion_free(dev->ion_client, handle);
return rc;
}
c_data->vp_action.motionHandle = handle;
vaddr = NULL;
writel_iowmb(paddr, VCAP_VP_MOTION_EST_ADDR);
return 0;
}
void deinit_motion_buf(struct vcap_client_data *c_data)
{
struct vcap_dev *dev = c_data->dev;
if (!c_data->vp_action.motionHandle) {
pr_err("Motion buffer has not been created");
return;
}
writel_iowmb(0x00000000, VCAP_VP_MOTION_EST_ADDR);
ion_unmap_iommu(dev->ion_client, c_data->vp_action.motionHandle,
dev->domain_num, 0);
ion_free(dev->ion_client, c_data->vp_action.motionHandle);
c_data->vp_action.motionHandle = NULL;
return;
}
int init_nr_buf(struct vcap_client_data *c_data)
{
struct vcap_dev *dev = c_data->dev;
struct ion_handle *handle = NULL;
size_t frame_size, tot_size;
unsigned long paddr, len;
int rc;
if (c_data->vp_action.bufNR.nr_handle) {
pr_err("NR buffer has already been created");
return -ENOEXEC;
}
frame_size = c_data->vp_in_fmt.width * c_data->vp_in_fmt.height;
if (c_data->vp_in_fmt.pixfmt == V4L2_PIX_FMT_NV16)
tot_size = frame_size * 2;
else
tot_size = frame_size / 2 * 3;
handle = ion_alloc(dev->ion_client, tot_size, SZ_4K,
ION_HEAP(ION_CP_MM_HEAP_ID), 0);
if (IS_ERR_OR_NULL(handle)) {
pr_err("%s: ion_alloc failed\n", __func__);
return -ENOMEM;
}
rc = ion_map_iommu(dev->ion_client, handle,
dev->domain_num, 0, SZ_4K, 0, &paddr, &len,
0, 0);
if (rc < 0) {
pr_err("%s: map_iommu failed\n", __func__);
ion_free(dev->ion_client, handle);
return rc;
}
c_data->vp_action.bufNR.nr_handle = handle;
update_nr_value(dev);
c_data->vp_action.bufNR.paddr = paddr;
rc = readl_relaxed(VCAP_VP_NR_CONFIG2);
rc |= (((c_data->vp_out_fmt.width / 16) << 20) | 0x1);
writel_relaxed(rc, VCAP_VP_NR_CONFIG2);
writel_relaxed(paddr, VCAP_VP_NR_T2_Y_BASE_ADDR);
writel_relaxed(paddr + frame_size, VCAP_VP_NR_T2_C_BASE_ADDR);
c_data->vp_action.bufNR.nr_pos = NRT2_BUF;
return 0;
}
void deinit_nr_buf(struct vcap_client_data *c_data)
{
struct vcap_dev *dev = c_data->dev;
struct nr_buffer *buf;
uint32_t rc;
if (!c_data->vp_action.bufNR.nr_handle) {
pr_err("NR buffer has not been created");
return;
}
buf = &c_data->vp_action.bufNR;
rc = readl_relaxed(VCAP_VP_NR_CONFIG2);
rc &= !(0x0FF00001);
writel_relaxed(rc, VCAP_VP_NR_CONFIG2);
ion_unmap_iommu(dev->ion_client, buf->nr_handle, dev->domain_num, 0);
ion_free(dev->ion_client, buf->nr_handle);
buf->nr_handle = NULL;
buf->paddr = 0;
return;
}
int nr_s_param(struct vcap_client_data *c_data, struct nr_param *param)
{
if (param->mode != NR_MANUAL)
return 0;
/* Verify values in range */
if (param->window > VP_NR_MAX_WINDOW)
return -EINVAL;
if (param->luma.max_blend_ratio > VP_NR_MAX_RATIO)
return -EINVAL;
if (param->luma.scale_diff_ratio > VP_NR_MAX_RATIO)
return -EINVAL;
if (param->luma.diff_limit_ratio > VP_NR_MAX_RATIO)
return -EINVAL;
if (param->luma.scale_motion_ratio > VP_NR_MAX_RATIO)
return -EINVAL;
if (param->luma.blend_limit_ratio > VP_NR_MAX_RATIO)
return -EINVAL;
if (param->chroma.max_blend_ratio > VP_NR_MAX_RATIO)
return -EINVAL;
if (param->chroma.scale_diff_ratio > VP_NR_MAX_RATIO)
return -EINVAL;
if (param->chroma.diff_limit_ratio > VP_NR_MAX_RATIO)
return -EINVAL;
if (param->chroma.scale_motion_ratio > VP_NR_MAX_RATIO)
return -EINVAL;
if (param->chroma.blend_limit_ratio > VP_NR_MAX_RATIO)
return -EINVAL;
return 0;
}
void nr_g_param(struct vcap_client_data *c_data, struct nr_param *param)
{
struct vcap_dev *dev = c_data->dev;
uint32_t rc;
rc = readl_relaxed(VCAP_VP_NR_CONFIG);
param->window = BITS_VALUE(rc, 24, 4);
param->decay_ratio = BITS_VALUE(rc, 20, 3);
rc = readl_relaxed(VCAP_VP_NR_LUMA_CONFIG);
param->threshold = NR_THRESHOLD_STATIC;
if (BITS_VALUE(rc, 16, 1))
param->threshold = NR_THRESHOLD_DYNAMIC;
param->luma.max_blend_ratio = BITS_VALUE(rc, 24, 4);
param->luma.scale_diff_ratio = BITS_VALUE(rc, 12, 4);
param->luma.diff_limit_ratio = BITS_VALUE(rc, 8, 4);
param->luma.scale_motion_ratio = BITS_VALUE(rc, 4, 4);
param->luma.blend_limit_ratio = BITS_VALUE(rc, 0, 4);
rc = readl_relaxed(VCAP_VP_NR_CHROMA_CONFIG);
param->chroma.max_blend_ratio = BITS_VALUE(rc, 24, 4);
param->chroma.scale_diff_ratio = BITS_VALUE(rc, 12, 4);
param->chroma.diff_limit_ratio = BITS_VALUE(rc, 8, 4);
param->chroma.scale_motion_ratio = BITS_VALUE(rc, 4, 4);
param->chroma.blend_limit_ratio = BITS_VALUE(rc, 0, 4);
}
void s_default_nr_val(struct nr_param *param)
{
param->threshold = NR_THRESHOLD_STATIC;
param->window = 10;
param->decay_ratio = 0;
param->luma.max_blend_ratio = 0;
param->luma.scale_diff_ratio = 4;
param->luma.diff_limit_ratio = 1;
param->luma.scale_motion_ratio = 4;
param->luma.blend_limit_ratio = 9;
param->chroma.max_blend_ratio = 0;
param->chroma.scale_diff_ratio = 4;
param->chroma.diff_limit_ratio = 1;
param->chroma.scale_motion_ratio = 4;
param->chroma.blend_limit_ratio = 9;
}
int vp_dummy_event(struct vcap_client_data *c_data)
{
struct vcap_dev *dev = c_data->dev;
unsigned int width, height;
struct ion_handle *handle = NULL;
unsigned long paddr, len;
uint32_t reg;
int rc = 0;
pr_debug("%s: Start VP dummy event\n", __func__);
handle = ion_alloc(dev->ion_client, 0x1200, SZ_4K,
ION_HEAP(ION_CP_MM_HEAP_ID), 0);
if (IS_ERR_OR_NULL(handle)) {
pr_err("%s: ion_alloc failed\n", __func__);
return -ENOMEM;
}
rc = ion_map_iommu(dev->ion_client, handle,
dev->domain_num, 0, SZ_4K, 0, &paddr, &len,
0, 0);
if (rc < 0) {
pr_err("%s: map_iommu failed\n", __func__);
ion_free(dev->ion_client, handle);
return rc;
}
width = c_data->vp_out_fmt.width;
height = c_data->vp_out_fmt.height;
c_data->vp_out_fmt.width = 0x3F;
c_data->vp_out_fmt.height = 0x16;
config_vp_format(c_data);
writel_relaxed(paddr, VCAP_VP_T1_Y_BASE_ADDR);
writel_relaxed(paddr + 0x2C0, VCAP_VP_T1_C_BASE_ADDR);
writel_relaxed(paddr + 0x440, VCAP_VP_T2_Y_BASE_ADDR);
writel_relaxed(paddr + 0x700, VCAP_VP_T2_C_BASE_ADDR);
writel_relaxed(paddr + 0x880, VCAP_VP_OUT_Y_BASE_ADDR);
writel_relaxed(paddr + 0xB40, VCAP_VP_OUT_C_BASE_ADDR);
writel_iowmb(paddr + 0x1100, VCAP_VP_MOTION_EST_ADDR);
writel_relaxed(4 << 20 | 0x2 << 4, VCAP_VP_IN_CONFIG);
writel_relaxed(4 << 20 | 0x1 << 4, VCAP_VP_OUT_CONFIG);
dev->vp_dummy_event = true;
enable_irq(dev->vpirq->start);
writel_relaxed(0x01100101, VCAP_VP_INTERRUPT_ENABLE);
writel_iowmb(0x00000000, VCAP_VP_CTRL);
writel_iowmb(0x00010000, VCAP_VP_CTRL);
rc = wait_event_interruptible_timeout(dev->vp_dummy_waitq,
dev->vp_dummy_complete, msecs_to_jiffies(50));
if (!rc && !dev->vp_dummy_complete) {
pr_err("%s: VP dummy event timeout", __func__);
rc = -ETIME;
vp_sw_reset(dev);
dev->vp_dummy_complete = false;
}
writel_relaxed(0x00000000, VCAP_VP_INTERRUPT_ENABLE);
disable_irq(dev->vpirq->start);
dev->vp_dummy_event = false;
reg = readl_relaxed(VCAP_OFFSET(0x0D94));
writel_relaxed(reg, VCAP_OFFSET(0x0D9C));
c_data->vp_out_fmt.width = width;
c_data->vp_out_fmt.height = height;
ion_unmap_iommu(dev->ion_client, handle, dev->domain_num, 0);
ion_free(dev->ion_client, handle);
pr_debug("%s: Exit VP dummy event\n", __func__);
return rc;
}
int kickoff_vp(struct vcap_client_data *c_data)
{
struct vcap_dev *dev;
struct vp_action *vp_act;
struct timeval tv;
unsigned long flags = 0;
unsigned int chroma_fmt = 0;
int size;
bool top_field = 0;
if (!c_data->streaming)
return -ENOEXEC;
dev = c_data->dev;
pr_debug("Start VP Kickoff\n");
if (dev->vp_client == NULL) {
pr_err("No active vp client\n");
return -ENODEV;
}
vp_act = &dev->vp_client->vp_action;
spin_lock_irqsave(&dev->vp_client->cap_slock, flags);
if (list_empty(&vp_act->in_active)) {
spin_unlock_irqrestore(&dev->vp_client->cap_slock, flags);
pr_err("%s: VP We have no more input buffers\n",
__func__);
return -EAGAIN;
}
vp_act->bufT1 = list_entry(vp_act->in_active.next,
struct vcap_buffer, list);
list_del(&vp_act->bufT1->list);
if (list_empty(&vp_act->in_active)) {
spin_unlock_irqrestore(&dev->vp_client->cap_slock, flags);
list_add(&vp_act->bufT1->list, &vp_act->in_active);
pr_err("%s: VP We have no more input buffers\n",
__func__);
return -EAGAIN;
}
vp_act->bufT2 = list_entry(vp_act->in_active.next,
struct vcap_buffer, list);
list_del(&vp_act->bufT2->list);
if (list_empty(&vp_act->out_active)) {
spin_unlock_irqrestore(&dev->vp_client->cap_slock, flags);
list_add(&vp_act->bufT2->list, &vp_act->in_active);
list_add(&vp_act->bufT1->list, &vp_act->in_active);
pr_err("%s: VP We have no more output buffers\n",
__func__);
return -EAGAIN;
}
vp_act->bufOut = list_entry(vp_act->out_active.next,
struct vcap_buffer, list);
list_del(&vp_act->bufOut->list);
spin_unlock_irqrestore(&dev->vp_client->cap_slock, flags);
size = c_data->vp_in_fmt.height * c_data->vp_in_fmt.width;
writel_relaxed(vp_act->bufT1->paddr, VCAP_VP_T1_Y_BASE_ADDR);
writel_relaxed(vp_act->bufT1->paddr + size, VCAP_VP_T1_C_BASE_ADDR);
config_in_buffer(c_data, vp_act->bufT2);
config_out_buffer(c_data, vp_act->bufOut);
/* Config VP */
if (c_data->vp_in_fmt.pixfmt == V4L2_PIX_FMT_NV16)
chroma_fmt = 1;
writel_relaxed((c_data->vp_in_fmt.width / 16) << 20 |
chroma_fmt << 11 | 0x2 << 4, VCAP_VP_IN_CONFIG);
chroma_fmt = 0;
if (c_data->vp_out_fmt.pixfmt == V4L2_PIX_FMT_NV16)
chroma_fmt = 1;
writel_relaxed((c_data->vp_out_fmt.width / 16) << 20 |
chroma_fmt << 11 | 0x1 << 4, VCAP_VP_OUT_CONFIG);
/* Enable Interrupt */
if (vp_act->bufT2->vb.v4l2_buf.field == V4L2_FIELD_BOTTOM)
top_field = 1;
vp_act->vp_state = VP_FRAME2;
writel_relaxed(0x01100001, VCAP_VP_INTERRUPT_ENABLE);
writel_iowmb(0x00000000 | top_field, VCAP_VP_CTRL);
writel_iowmb(0x00010000 | top_field, VCAP_VP_CTRL);
atomic_set(&c_data->dev->vp_enabled, 1);
enable_irq(dev->vpirq->start);
do_gettimeofday(&tv);
dev->dbg_p.vp_timestamp = (uint32_t) (tv.tv_sec * VCAP_USEC +
tv.tv_usec);
return 0;
}
int continue_vp(struct vcap_client_data *c_data)
{
struct vcap_dev *dev;
struct vp_action *vp_act;
struct timeval tv;
int rc;
bool top_field = 0;
pr_debug("Start VP Continue\n");
dev = c_data->dev;
if (dev->vp_client == NULL) {
pr_err("No active vp client\n");
return -ENODEV;
}
vp_act = &dev->vp_client->vp_action;
if (vp_act->vp_state == VP_UNKNOWN) {
pr_err("%s: VP is in an unknown state\n",
__func__);
return -EAGAIN;
}
rc = vp_setup_buffers(c_data);
if (rc < 0)
return rc;
if (vp_act->bufT2->vb.v4l2_buf.field == V4L2_FIELD_BOTTOM)
top_field = 1;
/* Config VP & Enable Interrupt */
writel_relaxed(0x01100001, VCAP_VP_INTERRUPT_ENABLE);
writel_iowmb(0x00000000 | top_field, VCAP_VP_CTRL);
writel_iowmb(0x00010000 | top_field, VCAP_VP_CTRL);
atomic_set(&c_data->dev->vp_enabled, 1);
enable_irq(dev->vpirq->start);
do_gettimeofday(&tv);
dev->dbg_p.vp_timestamp = (uint32_t) (tv.tv_sec * VCAP_USEC +
tv.tv_usec);
return 0;
}
@@ -0,0 +1,115 @@
/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#ifndef VCAP_VP_H
#define VCAP_VP_H
#include <linux/interrupt.h>
#include <media/vcap_v4l2.h>
#define VCAP_VP_INT_STATUS (VCAP_BASE + 0x404)
#define VCAP_VP_INT_CLEAR (VCAP_BASE + 0x40C)
#define VCAP_VP_SW_RESET (VCAP_BASE + 0x410)
#define VCAP_VP_INTERRUPT_ENABLE (VCAP_BASE + 0x408)
#define VCAP_VP_FILM_PROJECTION_T0 (VCAP_BASE + 0x50C)
#define VCAP_VP_FILM_PROJECTION_T2 (VCAP_BASE + 0x508)
#define VCAP_VP_FILM_PAST_MAX_PROJ (VCAP_BASE + 0x510)
#define VCAP_VP_FILM_PAST_MIN_PROJ (VCAP_BASE + 0x514)
#define VCAP_VP_FILM_SEQUENCE_HIST (VCAP_BASE + 0x504)
#define VCAP_VP_FILM_MODE_STATE (VCAP_BASE + 0x500)
#define VCAP_VP_BAL_VMOTION_STATE (VCAP_BASE + 0x690)
#define VCAP_VP_REDUCT_AVG_MOTION (VCAP_BASE + 0x610)
#define VCAP_VP_REDUCT_AVG_MOTION2 (VCAP_BASE + 0x614)
#define VCAP_VP_NR_AVG_LUMA (VCAP_BASE + 0x608)
#define VCAP_VP_NR_AVG_CHROMA (VCAP_BASE + 0x60C)
#define VCAP_VP_NR_CTRL_LUMA (VCAP_BASE + 0x600)
#define VCAP_VP_NR_CTRL_CHROMA (VCAP_BASE + 0x604)
#define VCAP_VP_BAL_AVG_BLEND (VCAP_BASE + 0x694)
#define VCAP_VP_VMOTION_HIST (VCAP_BASE + 0x6F8)
#define VCAP_VP_MOTION_EST_ADDR (VCAP_BASE + 0x4E0)
#define VCAP_VP_FILM_ANALYSIS_CONFIG (VCAP_BASE + 0x520)
#define VCAP_VP_FILM_STATE_CONFIG (VCAP_BASE + 0x524)
#define VCAP_VP_FVM_CONFIG (VCAP_BASE + 0x550)
#define VCAP_VP_FILM_ANALYSIS_CONFIG2 (VCAP_BASE + 0x52C)
#define VCAP_VP_MIXED_ANALYSIS_CONFIG (VCAP_BASE + 0x530)
#define VCAP_VP_SPATIAL_CONFIG (VCAP_BASE + 0x580)
#define VCAP_VP_SPATIAL_CONFIG2 (VCAP_BASE + 0x584)
#define VCAP_VP_SPATIAL_CONFIG3 (VCAP_BASE + 0x588)
#define VCAP_VP_TEMPORAL_CONFIG (VCAP_BASE + 0x5C0)
#define VCAP_VP_PIXEL_DIFF_CONFIG (VCAP_BASE + 0x6FC)
#define VCAP_VP_H_FREQ_CONFIG (VCAP_BASE + 0x528)
#define VCAP_VP_NR_CONFIG (VCAP_BASE + 0x620)
#define VCAP_VP_NR_LUMA_CONFIG (VCAP_BASE + 0x624)
#define VCAP_VP_NR_CHROMA_CONFIG (VCAP_BASE + 0x628)
#define VCAP_VP_BAL_CONFIG (VCAP_BASE + 0x680)
#define VCAP_VP_BAL_MOTION_CONFIG (VCAP_BASE + 0x684)
#define VCAP_VP_BAL_LIGHT_COMB (VCAP_BASE + 0x688)
#define VCAP_VP_BAL_VMOTION_CONFIG (VCAP_BASE + 0x68C)
#define VCAP_VP_NR_CONFIG2 (VCAP_BASE + 0x484)
#define VCAP_VP_FRAME_SIZE (VCAP_BASE + 0x48C)
#define VCAP_VP_SPLIT_SCRN_CTRL (VCAP_BASE + 0x750)
#define VCAP_VP_IN_CONFIG (VCAP_BASE + 0x480)
#define VCAP_VP_OUT_CONFIG (VCAP_BASE + 0x488)
#define VCAP_VP_T2_Y_BASE_ADDR (VCAP_BASE + 0x4C0)
#define VCAP_VP_T2_C_BASE_ADDR (VCAP_BASE + 0x4C4)
#define VCAP_VP_OUT_Y_BASE_ADDR (VCAP_BASE + 0x4CC)
#define VCAP_VP_OUT_C_BASE_ADDR (VCAP_BASE + 0x4D0)
#define VCAP_VP_OUT_CR_BASE_ADDR (VCAP_BASE + 0x4D4)
#define VCAP_VP_CTRL (VCAP_BASE + 0x4D8)
#define VCAP_VP_T1_Y_BASE_ADDR (VCAP_BASE + 0x4A8)
#define VCAP_VP_T1_C_BASE_ADDR (VCAP_BASE + 0x4Ac)
#define VCAP_VP_NR_T2_Y_BASE_ADDR (VCAP_BASE + 0x4B4)
#define VCAP_VP_NR_T2_C_BASE_ADDR (VCAP_BASE + 0x4B8)
#define VP_PIC_DONE (0x1 << 0)
#define VP_MODE_CHANGE (0x1 << 8)
#define VP_NR_MAX_WINDOW 120
#define VP_NR_MAX_RATIO 16
#define VP_NR_DYNAMIC_THRESHOLD 0x000F0000
#define BITS_MASK(start, num_of_bits) \
(((1 << (num_of_bits)) - 1) << (start))
#define BITS_VALUE(x, start, num_of_bits) \
(((x) & BITS_MASK(start, num_of_bits)) >> (start))
irqreturn_t vp_handler(struct vcap_dev *dev);
int config_vp_format(struct vcap_client_data *c_data);
void vp_stop_capture(struct vcap_client_data *c_data);
int init_motion_buf(struct vcap_client_data *c_data);
void deinit_motion_buf(struct vcap_client_data *c_data);
int init_nr_buf(struct vcap_client_data *c_data);
void deinit_nr_buf(struct vcap_client_data *c_data);
int nr_s_param(struct vcap_client_data *c_data, struct nr_param *param);
void nr_g_param(struct vcap_client_data *c_data, struct nr_param *param);
void s_default_nr_val(struct nr_param *param);
int kickoff_vp(struct vcap_client_data *c_data);
int continue_vp(struct vcap_client_data *c_data);
int vp_dummy_event(struct vcap_client_data *c_data);
#endif