Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
@@ -0,0 +1,10 @@
# SPDX-License-Identifier: GPL-2.0
comment "Sunxi media platform drivers"
source "drivers/media/platform/sunxi/sun4i-csi/Kconfig"
source "drivers/media/platform/sunxi/sun6i-csi/Kconfig"
source "drivers/media/platform/sunxi/sun6i-mipi-csi2/Kconfig"
source "drivers/media/platform/sunxi/sun8i-a83t-mipi-csi2/Kconfig"
source "drivers/media/platform/sunxi/sun8i-di/Kconfig"
source "drivers/media/platform/sunxi/sun8i-rotate/Kconfig"
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: GPL-2.0
obj-y += sun4i-csi/
obj-y += sun6i-csi/
obj-y += sun6i-mipi-csi2/
obj-y += sun8i-a83t-mipi-csi2/
obj-y += sun8i-di/
obj-y += sun8i-rotate/
@@ -0,0 +1,16 @@
# SPDX-License-Identifier: GPL-2.0
config VIDEO_SUN4I_CSI
tristate "Allwinner A10 CMOS Sensor Interface Support"
depends on V4L_PLATFORM_DRIVERS
depends on VIDEO_DEV && COMMON_CLK && RESET_CONTROLLER && HAS_DMA
depends on ARCH_SUNXI || COMPILE_TEST
select MEDIA_CONTROLLER
select VIDEO_V4L2_SUBDEV_API
select VIDEOBUF2_DMA_CONTIG
select V4L2_FWNODE
help
This is a V4L2 driver for the Allwinner A10 CSI
To compile this driver as a module, choose M here: the module
will be called sun4i_csi.
@@ -0,0 +1,7 @@
# SPDX-License-Identifier: GPL-2.0
sun4i-csi-y += sun4i_csi.o
sun4i-csi-y += sun4i_dma.o
sun4i-csi-y += sun4i_v4l2.o
obj-$(CONFIG_VIDEO_SUN4I_CSI) += sun4i-csi.o
@@ -0,0 +1,354 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2016 NextThing Co
* Copyright (C) 2016-2019 Bootlin
*
* Author: Maxime Ripard <maxime.ripard@bootlin.com>
*/
#include <linux/clk.h>
#include <linux/dma-mapping.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/of_graph.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/reset.h>
#include <linux/videodev2.h>
#include <media/v4l2-dev.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-mediabus.h>
#include <media/videobuf2-core.h>
#include <media/videobuf2-dma-contig.h>
#include "sun4i_csi.h"
struct sun4i_csi_traits {
unsigned int channels;
unsigned int max_width;
bool has_isp;
};
static int sun4i_csi_video_link_validate(struct media_link *link)
{
dev_warn_once(link->graph_obj.mdev->dev,
"Driver bug: link validation not implemented\n");
return 0;
}
static const struct media_entity_operations sun4i_csi_video_entity_ops = {
.link_validate = sun4i_csi_video_link_validate,
};
static const struct media_entity_operations sun4i_csi_subdev_entity_ops = {
.link_validate = v4l2_subdev_link_validate,
};
static int sun4i_csi_notify_bound(struct v4l2_async_notifier *notifier,
struct v4l2_subdev *subdev,
struct v4l2_async_connection *asd)
{
struct sun4i_csi *csi = container_of(notifier, struct sun4i_csi,
notifier);
csi->src_subdev = subdev;
csi->src_pad = media_entity_get_fwnode_pad(&subdev->entity,
subdev->fwnode,
MEDIA_PAD_FL_SOURCE);
if (csi->src_pad < 0) {
dev_err(csi->dev, "Couldn't find output pad for subdev %s\n",
subdev->name);
return csi->src_pad;
}
dev_dbg(csi->dev, "Bound %s pad: %d\n", subdev->name, csi->src_pad);
return 0;
}
static int sun4i_csi_notify_complete(struct v4l2_async_notifier *notifier)
{
struct sun4i_csi *csi = container_of(notifier, struct sun4i_csi,
notifier);
struct v4l2_subdev *subdev = &csi->subdev;
struct video_device *vdev = &csi->vdev;
int ret;
ret = v4l2_device_register_subdev(&csi->v4l, subdev);
if (ret < 0)
return ret;
ret = sun4i_csi_v4l2_register(csi);
if (ret < 0)
return ret;
ret = media_device_register(&csi->mdev);
if (ret)
return ret;
/* Create link from subdev to main device */
ret = media_create_pad_link(&subdev->entity, CSI_SUBDEV_SOURCE,
&vdev->entity, 0,
MEDIA_LNK_FL_ENABLED |
MEDIA_LNK_FL_IMMUTABLE);
if (ret)
goto err_clean_media;
ret = media_create_pad_link(&csi->src_subdev->entity, csi->src_pad,
&subdev->entity, CSI_SUBDEV_SINK,
MEDIA_LNK_FL_ENABLED |
MEDIA_LNK_FL_IMMUTABLE);
if (ret)
goto err_clean_media;
ret = v4l2_device_register_subdev_nodes(&csi->v4l);
if (ret < 0)
goto err_clean_media;
return 0;
err_clean_media:
media_device_unregister(&csi->mdev);
return ret;
}
static const struct v4l2_async_notifier_operations sun4i_csi_notify_ops = {
.bound = sun4i_csi_notify_bound,
.complete = sun4i_csi_notify_complete,
};
static int sun4i_csi_notifier_init(struct sun4i_csi *csi)
{
struct v4l2_fwnode_endpoint vep = {
.bus_type = V4L2_MBUS_PARALLEL,
};
struct v4l2_async_connection *asd;
struct fwnode_handle *ep;
int ret;
v4l2_async_nf_init(&csi->notifier, &csi->v4l);
ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(csi->dev), 0, 0,
FWNODE_GRAPH_ENDPOINT_NEXT);
if (!ep)
return -EINVAL;
ret = v4l2_fwnode_endpoint_parse(ep, &vep);
if (ret)
goto out;
csi->bus = vep.bus.parallel;
asd = v4l2_async_nf_add_fwnode_remote(&csi->notifier, ep,
struct v4l2_async_connection);
if (IS_ERR(asd)) {
ret = PTR_ERR(asd);
goto out;
}
csi->notifier.ops = &sun4i_csi_notify_ops;
out:
fwnode_handle_put(ep);
return ret;
}
static int sun4i_csi_probe(struct platform_device *pdev)
{
struct v4l2_subdev *subdev;
struct video_device *vdev;
struct sun4i_csi *csi;
int ret;
int irq;
csi = devm_kzalloc(&pdev->dev, sizeof(*csi), GFP_KERNEL);
if (!csi)
return -ENOMEM;
platform_set_drvdata(pdev, csi);
csi->dev = &pdev->dev;
subdev = &csi->subdev;
vdev = &csi->vdev;
csi->traits = of_device_get_match_data(&pdev->dev);
if (!csi->traits)
return -EINVAL;
csi->mdev.dev = csi->dev;
strscpy(csi->mdev.model, "Allwinner Video Capture Device",
sizeof(csi->mdev.model));
csi->mdev.hw_revision = 0;
media_device_init(&csi->mdev);
csi->v4l.mdev = &csi->mdev;
csi->regs = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(csi->regs))
return PTR_ERR(csi->regs);
irq = platform_get_irq(pdev, 0);
if (irq < 0)
return irq;
csi->bus_clk = devm_clk_get(&pdev->dev, "bus");
if (IS_ERR(csi->bus_clk)) {
dev_err(&pdev->dev, "Couldn't get our bus clock\n");
return PTR_ERR(csi->bus_clk);
}
if (csi->traits->has_isp) {
csi->isp_clk = devm_clk_get(&pdev->dev, "isp");
if (IS_ERR(csi->isp_clk)) {
dev_err(&pdev->dev, "Couldn't get our ISP clock\n");
return PTR_ERR(csi->isp_clk);
}
}
csi->ram_clk = devm_clk_get(&pdev->dev, "ram");
if (IS_ERR(csi->ram_clk)) {
dev_err(&pdev->dev, "Couldn't get our ram clock\n");
return PTR_ERR(csi->ram_clk);
}
csi->rst = devm_reset_control_get(&pdev->dev, NULL);
if (IS_ERR(csi->rst)) {
dev_err(&pdev->dev, "Couldn't get our reset line\n");
return PTR_ERR(csi->rst);
}
/* Initialize subdev */
v4l2_subdev_init(subdev, &sun4i_csi_subdev_ops);
subdev->internal_ops = &sun4i_csi_subdev_internal_ops;
subdev->flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
subdev->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
subdev->entity.ops = &sun4i_csi_subdev_entity_ops;
subdev->owner = THIS_MODULE;
snprintf(subdev->name, sizeof(subdev->name), "sun4i-csi-0");
v4l2_set_subdevdata(subdev, csi);
csi->subdev_pads[CSI_SUBDEV_SINK].flags =
MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
csi->subdev_pads[CSI_SUBDEV_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
ret = media_entity_pads_init(&subdev->entity, CSI_SUBDEV_PADS,
csi->subdev_pads);
if (ret < 0)
return ret;
csi->vdev_pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
vdev->entity.ops = &sun4i_csi_video_entity_ops;
ret = media_entity_pads_init(&vdev->entity, 1, &csi->vdev_pad);
if (ret < 0)
return ret;
ret = sun4i_csi_dma_register(csi, irq);
if (ret)
goto err_clean_pad;
ret = sun4i_csi_notifier_init(csi);
if (ret)
goto err_unregister_media;
ret = v4l2_async_nf_register(&csi->notifier);
if (ret) {
dev_err(csi->dev, "Couldn't register our notifier.\n");
goto err_unregister_media;
}
pm_runtime_enable(&pdev->dev);
return 0;
err_unregister_media:
media_device_unregister(&csi->mdev);
sun4i_csi_dma_unregister(csi);
err_clean_pad:
media_device_cleanup(&csi->mdev);
return ret;
}
static void sun4i_csi_remove(struct platform_device *pdev)
{
struct sun4i_csi *csi = platform_get_drvdata(pdev);
pm_runtime_disable(&pdev->dev);
v4l2_async_nf_unregister(&csi->notifier);
v4l2_async_nf_cleanup(&csi->notifier);
vb2_video_unregister_device(&csi->vdev);
media_device_unregister(&csi->mdev);
sun4i_csi_dma_unregister(csi);
media_device_cleanup(&csi->mdev);
}
static const struct sun4i_csi_traits sun4i_a10_csi1_traits = {
.channels = 1,
.max_width = 24,
.has_isp = false,
};
static const struct sun4i_csi_traits sun7i_a20_csi0_traits = {
.channels = 4,
.max_width = 16,
.has_isp = true,
};
static const struct of_device_id sun4i_csi_of_match[] = {
{ .compatible = "allwinner,sun4i-a10-csi1", .data = &sun4i_a10_csi1_traits },
{ .compatible = "allwinner,sun7i-a20-csi0", .data = &sun7i_a20_csi0_traits },
{ /* Sentinel */ }
};
MODULE_DEVICE_TABLE(of, sun4i_csi_of_match);
static int __maybe_unused sun4i_csi_runtime_resume(struct device *dev)
{
struct sun4i_csi *csi = dev_get_drvdata(dev);
reset_control_deassert(csi->rst);
clk_prepare_enable(csi->bus_clk);
clk_prepare_enable(csi->ram_clk);
clk_set_rate(csi->isp_clk, 80000000);
clk_prepare_enable(csi->isp_clk);
writel(1, csi->regs + CSI_EN_REG);
return 0;
}
static int __maybe_unused sun4i_csi_runtime_suspend(struct device *dev)
{
struct sun4i_csi *csi = dev_get_drvdata(dev);
clk_disable_unprepare(csi->isp_clk);
clk_disable_unprepare(csi->ram_clk);
clk_disable_unprepare(csi->bus_clk);
reset_control_assert(csi->rst);
return 0;
}
static const struct dev_pm_ops sun4i_csi_pm_ops = {
SET_RUNTIME_PM_OPS(sun4i_csi_runtime_suspend,
sun4i_csi_runtime_resume,
NULL)
};
static struct platform_driver sun4i_csi_driver = {
.probe = sun4i_csi_probe,
.remove = sun4i_csi_remove,
.driver = {
.name = "sun4i-csi",
.of_match_table = sun4i_csi_of_match,
.pm = &sun4i_csi_pm_ops,
},
};
module_platform_driver(sun4i_csi_driver);
MODULE_DESCRIPTION("Allwinner A10 Camera Sensor Interface driver");
MODULE_AUTHOR("Maxime Ripard <mripard@kernel.org>");
MODULE_LICENSE("GPL");
@@ -0,0 +1,162 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright (C) 2016 NextThing Co
* Copyright (C) 2016-2019 Bootlin
*
* Author: Maxime Ripard <maxime.ripard@bootlin.com>
*/
#ifndef _SUN4I_CSI_H_
#define _SUN4I_CSI_H_
#include <media/media-device.h>
#include <media/v4l2-async.h>
#include <media/v4l2-dev.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#include <media/videobuf2-core.h>
#define CSI_EN_REG 0x00
#define CSI_CFG_REG 0x04
#define CSI_CFG_INPUT_FMT(fmt) ((fmt) << 20)
#define CSI_CFG_OUTPUT_FMT(fmt) ((fmt) << 16)
#define CSI_CFG_YUV_DATA_SEQ(seq) ((seq) << 8)
#define CSI_CFG_VREF_POL(pol) ((pol) << 2)
#define CSI_CFG_HREF_POL(pol) ((pol) << 1)
#define CSI_CFG_PCLK_POL(pol) ((pol) << 0)
#define CSI_CPT_CTRL_REG 0x08
#define CSI_CPT_CTRL_VIDEO_START BIT(1)
#define CSI_CPT_CTRL_IMAGE_START BIT(0)
#define CSI_BUF_ADDR_REG(fifo, buf) (0x10 + (0x8 * (fifo)) + (0x4 * (buf)))
#define CSI_BUF_CTRL_REG 0x28
#define CSI_BUF_CTRL_DBN BIT(2)
#define CSI_BUF_CTRL_DBS BIT(1)
#define CSI_BUF_CTRL_DBE BIT(0)
#define CSI_INT_EN_REG 0x30
#define CSI_INT_FRM_DONE BIT(1)
#define CSI_INT_CPT_DONE BIT(0)
#define CSI_INT_STA_REG 0x34
#define CSI_WIN_CTRL_W_REG 0x40
#define CSI_WIN_CTRL_W_ACTIVE(w) ((w) << 16)
#define CSI_WIN_CTRL_H_REG 0x44
#define CSI_WIN_CTRL_H_ACTIVE(h) ((h) << 16)
#define CSI_BUF_LEN_REG 0x48
#define CSI_MAX_BUFFER 2
#define CSI_MAX_HEIGHT 8192U
#define CSI_MAX_WIDTH 8192U
enum csi_input {
CSI_INPUT_RAW = 0,
CSI_INPUT_BT656 = 2,
CSI_INPUT_YUV = 3,
};
enum csi_output_raw {
CSI_OUTPUT_RAW_PASSTHROUGH = 0,
};
enum csi_output_yuv {
CSI_OUTPUT_YUV_422_PLANAR = 0,
CSI_OUTPUT_YUV_420_PLANAR = 1,
CSI_OUTPUT_YUV_422_UV = 4,
CSI_OUTPUT_YUV_420_UV = 5,
CSI_OUTPUT_YUV_422_MACRO = 8,
CSI_OUTPUT_YUV_420_MACRO = 9,
};
enum csi_yuv_data_seq {
CSI_YUV_DATA_SEQ_YUYV = 0,
CSI_YUV_DATA_SEQ_YVYU = 1,
CSI_YUV_DATA_SEQ_UYVY = 2,
CSI_YUV_DATA_SEQ_VYUY = 3,
};
enum csi_subdev_pads {
CSI_SUBDEV_SINK,
CSI_SUBDEV_SOURCE,
CSI_SUBDEV_PADS,
};
extern const struct v4l2_subdev_ops sun4i_csi_subdev_ops;
extern const struct v4l2_subdev_internal_ops sun4i_csi_subdev_internal_ops;
struct sun4i_csi_format {
u32 mbus;
u32 fourcc;
enum csi_input input;
u32 output;
unsigned int num_planes;
u8 bpp[3];
unsigned int hsub;
unsigned int vsub;
};
const struct sun4i_csi_format *sun4i_csi_find_format(const u32 *fourcc,
const u32 *mbus);
struct sun4i_csi {
/* Device resources */
struct device *dev;
const struct sun4i_csi_traits *traits;
void __iomem *regs;
struct clk *bus_clk;
struct clk *isp_clk;
struct clk *ram_clk;
struct reset_control *rst;
struct vb2_v4l2_buffer *current_buf[CSI_MAX_BUFFER];
struct {
size_t size;
void *vaddr;
dma_addr_t paddr;
} scratch;
struct v4l2_mbus_config_parallel bus;
/* Main Device */
struct v4l2_device v4l;
struct media_device mdev;
struct video_device vdev;
struct media_pad vdev_pad;
struct v4l2_pix_format_mplane fmt;
/* Local subdev */
struct v4l2_subdev subdev;
struct media_pad subdev_pads[CSI_SUBDEV_PADS];
struct v4l2_mbus_framefmt subdev_fmt;
/* V4L2 Async variables */
struct v4l2_async_notifier notifier;
struct v4l2_subdev *src_subdev;
int src_pad;
/* V4L2 variables */
struct mutex lock;
/* Videobuf2 */
struct vb2_queue queue;
struct list_head buf_list;
spinlock_t qlock;
unsigned int sequence;
};
int sun4i_csi_dma_register(struct sun4i_csi *csi, int irq);
void sun4i_csi_dma_unregister(struct sun4i_csi *csi);
int sun4i_csi_v4l2_register(struct sun4i_csi *csi);
#endif /* _SUN4I_CSI_H_ */
@@ -0,0 +1,458 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2016 NextThing Co
* Copyright (C) 2016-2019 Bootlin
*
* Author: Maxime Ripard <maxime.ripard@bootlin.com>
*/
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <media/videobuf2-dma-contig.h>
#include <media/videobuf2-v4l2.h>
#include "sun4i_csi.h"
struct sun4i_csi_buffer {
struct vb2_v4l2_buffer vb;
struct list_head list;
};
static inline struct sun4i_csi_buffer *
vb2_v4l2_to_csi_buffer(const struct vb2_v4l2_buffer *p)
{
return container_of(p, struct sun4i_csi_buffer, vb);
}
static inline struct sun4i_csi_buffer *
vb2_to_csi_buffer(const struct vb2_buffer *p)
{
return vb2_v4l2_to_csi_buffer(to_vb2_v4l2_buffer(p));
}
static void sun4i_csi_capture_start(struct sun4i_csi *csi)
{
writel(CSI_CPT_CTRL_VIDEO_START, csi->regs + CSI_CPT_CTRL_REG);
}
static void sun4i_csi_capture_stop(struct sun4i_csi *csi)
{
writel(0, csi->regs + CSI_CPT_CTRL_REG);
}
static int sun4i_csi_queue_setup(struct vb2_queue *vq,
unsigned int *nbuffers,
unsigned int *nplanes,
unsigned int sizes[],
struct device *alloc_devs[])
{
struct sun4i_csi *csi = vb2_get_drv_priv(vq);
unsigned int num_planes = csi->fmt.num_planes;
unsigned int i;
if (*nplanes) {
if (*nplanes != num_planes)
return -EINVAL;
for (i = 0; i < num_planes; i++)
if (sizes[i] < csi->fmt.plane_fmt[i].sizeimage)
return -EINVAL;
return 0;
}
*nplanes = num_planes;
for (i = 0; i < num_planes; i++)
sizes[i] = csi->fmt.plane_fmt[i].sizeimage;
return 0;
};
static int sun4i_csi_buffer_prepare(struct vb2_buffer *vb)
{
struct sun4i_csi *csi = vb2_get_drv_priv(vb->vb2_queue);
unsigned int i;
for (i = 0; i < csi->fmt.num_planes; i++) {
unsigned long size = csi->fmt.plane_fmt[i].sizeimage;
if (vb2_plane_size(vb, i) < size) {
dev_err(csi->dev, "buffer too small (%lu < %lu)\n",
vb2_plane_size(vb, i), size);
return -EINVAL;
}
vb2_set_plane_payload(vb, i, size);
}
return 0;
}
static int sun4i_csi_setup_scratch_buffer(struct sun4i_csi *csi,
unsigned int slot)
{
dma_addr_t addr = csi->scratch.paddr;
unsigned int plane;
dev_dbg(csi->dev,
"No more available buffer, using the scratch buffer\n");
for (plane = 0; plane < csi->fmt.num_planes; plane++) {
writel(addr, csi->regs + CSI_BUF_ADDR_REG(plane, slot));
addr += csi->fmt.plane_fmt[plane].sizeimage;
}
csi->current_buf[slot] = NULL;
return 0;
}
static int sun4i_csi_buffer_fill_slot(struct sun4i_csi *csi, unsigned int slot)
{
struct sun4i_csi_buffer *c_buf;
struct vb2_v4l2_buffer *v_buf;
unsigned int plane;
/*
* We should never end up in a situation where we overwrite an
* already filled slot.
*/
if (WARN_ON(csi->current_buf[slot]))
return -EINVAL;
if (list_empty(&csi->buf_list))
return sun4i_csi_setup_scratch_buffer(csi, slot);
c_buf = list_first_entry(&csi->buf_list, struct sun4i_csi_buffer, list);
list_del_init(&c_buf->list);
v_buf = &c_buf->vb;
csi->current_buf[slot] = v_buf;
for (plane = 0; plane < csi->fmt.num_planes; plane++) {
dma_addr_t buf_addr;
buf_addr = vb2_dma_contig_plane_dma_addr(&v_buf->vb2_buf,
plane);
writel(buf_addr, csi->regs + CSI_BUF_ADDR_REG(plane, slot));
}
return 0;
}
static int sun4i_csi_buffer_fill_all(struct sun4i_csi *csi)
{
unsigned int slot;
int ret;
for (slot = 0; slot < CSI_MAX_BUFFER; slot++) {
ret = sun4i_csi_buffer_fill_slot(csi, slot);
if (ret)
return ret;
}
return 0;
}
static void sun4i_csi_buffer_mark_done(struct sun4i_csi *csi,
unsigned int slot,
unsigned int sequence)
{
struct vb2_v4l2_buffer *v_buf;
if (!csi->current_buf[slot]) {
dev_dbg(csi->dev, "Scratch buffer was used, ignoring..\n");
return;
}
v_buf = csi->current_buf[slot];
v_buf->field = csi->fmt.field;
v_buf->sequence = sequence;
v_buf->vb2_buf.timestamp = ktime_get_ns();
vb2_buffer_done(&v_buf->vb2_buf, VB2_BUF_STATE_DONE);
csi->current_buf[slot] = NULL;
}
static int sun4i_csi_buffer_flip(struct sun4i_csi *csi, unsigned int sequence)
{
u32 reg = readl(csi->regs + CSI_BUF_CTRL_REG);
unsigned int next;
/* Our next buffer is not the current buffer */
next = !(reg & CSI_BUF_CTRL_DBS);
/* Report the previous buffer as done */
sun4i_csi_buffer_mark_done(csi, next, sequence);
/* Put a new buffer in there */
return sun4i_csi_buffer_fill_slot(csi, next);
}
static void sun4i_csi_buffer_queue(struct vb2_buffer *vb)
{
struct sun4i_csi *csi = vb2_get_drv_priv(vb->vb2_queue);
struct sun4i_csi_buffer *buf = vb2_to_csi_buffer(vb);
unsigned long flags;
spin_lock_irqsave(&csi->qlock, flags);
list_add_tail(&buf->list, &csi->buf_list);
spin_unlock_irqrestore(&csi->qlock, flags);
}
static void return_all_buffers(struct sun4i_csi *csi,
enum vb2_buffer_state state)
{
struct sun4i_csi_buffer *buf, *node;
unsigned int slot;
list_for_each_entry_safe(buf, node, &csi->buf_list, list) {
vb2_buffer_done(&buf->vb.vb2_buf, state);
list_del(&buf->list);
}
for (slot = 0; slot < CSI_MAX_BUFFER; slot++) {
struct vb2_v4l2_buffer *v_buf = csi->current_buf[slot];
if (!v_buf)
continue;
vb2_buffer_done(&v_buf->vb2_buf, state);
csi->current_buf[slot] = NULL;
}
}
static int sun4i_csi_start_streaming(struct vb2_queue *vq, unsigned int count)
{
struct sun4i_csi *csi = vb2_get_drv_priv(vq);
struct v4l2_mbus_config_parallel *bus = &csi->bus;
const struct sun4i_csi_format *csi_fmt;
unsigned long href_pol, pclk_pol, vref_pol;
unsigned long flags;
unsigned int i;
int ret;
csi_fmt = sun4i_csi_find_format(&csi->fmt.pixelformat, NULL);
if (!csi_fmt) {
ret = -EINVAL;
goto err_clear_dma_queue;
}
dev_dbg(csi->dev, "Starting capture\n");
csi->sequence = 0;
/*
* We need a scratch buffer in case where we'll not have any
* more buffer queued so that we don't error out. One of those
* cases is when you end up at the last frame to capture, you
* don't have any buffer queued any more, and yet it doesn't
* really matter since you'll never reach the next buffer.
*
* Since we support the multi-planar API, we need to have a
* buffer for each plane. Allocating a single one large enough
* to hold all the buffers is simpler, so let's go for that.
*/
csi->scratch.size = 0;
for (i = 0; i < csi->fmt.num_planes; i++)
csi->scratch.size += csi->fmt.plane_fmt[i].sizeimage;
csi->scratch.vaddr = dma_alloc_coherent(csi->dev,
csi->scratch.size,
&csi->scratch.paddr,
GFP_KERNEL);
if (!csi->scratch.vaddr) {
dev_err(csi->dev, "Failed to allocate scratch buffer\n");
ret = -ENOMEM;
goto err_clear_dma_queue;
}
ret = video_device_pipeline_alloc_start(&csi->vdev);
if (ret < 0)
goto err_free_scratch_buffer;
spin_lock_irqsave(&csi->qlock, flags);
/* Setup timings */
writel(CSI_WIN_CTRL_W_ACTIVE(csi->fmt.width * 2),
csi->regs + CSI_WIN_CTRL_W_REG);
writel(CSI_WIN_CTRL_H_ACTIVE(csi->fmt.height),
csi->regs + CSI_WIN_CTRL_H_REG);
/*
* This hardware uses [HV]REF instead of [HV]SYNC. Based on the
* provided timing diagrams in the manual, positive polarity
* equals active high [HV]REF.
*
* When the back porch is 0, [HV]REF is more or less equivalent
* to [HV]SYNC inverted.
*/
href_pol = !!(bus->flags & V4L2_MBUS_HSYNC_ACTIVE_LOW);
vref_pol = !!(bus->flags & V4L2_MBUS_VSYNC_ACTIVE_LOW);
pclk_pol = !!(bus->flags & V4L2_MBUS_PCLK_SAMPLE_RISING);
writel(CSI_CFG_INPUT_FMT(csi_fmt->input) |
CSI_CFG_OUTPUT_FMT(csi_fmt->output) |
CSI_CFG_VREF_POL(vref_pol) |
CSI_CFG_HREF_POL(href_pol) |
CSI_CFG_PCLK_POL(pclk_pol),
csi->regs + CSI_CFG_REG);
/* Setup buffer length */
writel(csi->fmt.plane_fmt[0].bytesperline,
csi->regs + CSI_BUF_LEN_REG);
/* Prepare our buffers in hardware */
ret = sun4i_csi_buffer_fill_all(csi);
if (ret) {
spin_unlock_irqrestore(&csi->qlock, flags);
goto err_disable_pipeline;
}
/* Enable double buffering */
writel(CSI_BUF_CTRL_DBE, csi->regs + CSI_BUF_CTRL_REG);
/* Clear the pending interrupts */
writel(CSI_INT_FRM_DONE, csi->regs + CSI_INT_STA_REG);
/* Enable frame done interrupt */
writel(CSI_INT_FRM_DONE, csi->regs + CSI_INT_EN_REG);
sun4i_csi_capture_start(csi);
spin_unlock_irqrestore(&csi->qlock, flags);
ret = v4l2_subdev_call(csi->src_subdev, video, s_stream, 1);
if (ret < 0 && ret != -ENOIOCTLCMD)
goto err_disable_device;
return 0;
err_disable_device:
sun4i_csi_capture_stop(csi);
err_disable_pipeline:
video_device_pipeline_stop(&csi->vdev);
err_free_scratch_buffer:
dma_free_coherent(csi->dev, csi->scratch.size, csi->scratch.vaddr,
csi->scratch.paddr);
err_clear_dma_queue:
spin_lock_irqsave(&csi->qlock, flags);
return_all_buffers(csi, VB2_BUF_STATE_QUEUED);
spin_unlock_irqrestore(&csi->qlock, flags);
return ret;
}
static void sun4i_csi_stop_streaming(struct vb2_queue *vq)
{
struct sun4i_csi *csi = vb2_get_drv_priv(vq);
unsigned long flags;
dev_dbg(csi->dev, "Stopping capture\n");
v4l2_subdev_call(csi->src_subdev, video, s_stream, 0);
sun4i_csi_capture_stop(csi);
/* Release all active buffers */
spin_lock_irqsave(&csi->qlock, flags);
return_all_buffers(csi, VB2_BUF_STATE_ERROR);
spin_unlock_irqrestore(&csi->qlock, flags);
video_device_pipeline_stop(&csi->vdev);
dma_free_coherent(csi->dev, csi->scratch.size, csi->scratch.vaddr,
csi->scratch.paddr);
}
static const struct vb2_ops sun4i_csi_qops = {
.queue_setup = sun4i_csi_queue_setup,
.buf_prepare = sun4i_csi_buffer_prepare,
.buf_queue = sun4i_csi_buffer_queue,
.start_streaming = sun4i_csi_start_streaming,
.stop_streaming = sun4i_csi_stop_streaming,
};
static irqreturn_t sun4i_csi_irq(int irq, void *data)
{
struct sun4i_csi *csi = data;
u32 reg;
reg = readl(csi->regs + CSI_INT_STA_REG);
/* Acknowledge the interrupts */
writel(reg, csi->regs + CSI_INT_STA_REG);
if (!(reg & CSI_INT_FRM_DONE))
return IRQ_HANDLED;
spin_lock(&csi->qlock);
if (sun4i_csi_buffer_flip(csi, csi->sequence++)) {
dev_warn(csi->dev, "%s: Flip failed\n", __func__);
sun4i_csi_capture_stop(csi);
}
spin_unlock(&csi->qlock);
return IRQ_HANDLED;
}
int sun4i_csi_dma_register(struct sun4i_csi *csi, int irq)
{
struct vb2_queue *q = &csi->queue;
int ret;
int i;
spin_lock_init(&csi->qlock);
mutex_init(&csi->lock);
INIT_LIST_HEAD(&csi->buf_list);
for (i = 0; i < CSI_MAX_BUFFER; i++)
csi->current_buf[i] = NULL;
q->min_queued_buffers = 3;
q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
q->io_modes = VB2_MMAP | VB2_DMABUF;
q->lock = &csi->lock;
q->drv_priv = csi;
q->buf_struct_size = sizeof(struct sun4i_csi_buffer);
q->ops = &sun4i_csi_qops;
q->mem_ops = &vb2_dma_contig_memops;
q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
q->dev = csi->dev;
ret = vb2_queue_init(q);
if (ret < 0) {
dev_err(csi->dev, "failed to initialize VB2 queue\n");
goto err_free_mutex;
}
ret = v4l2_device_register(csi->dev, &csi->v4l);
if (ret) {
dev_err(csi->dev, "Couldn't register the v4l2 device\n");
goto err_free_mutex;
}
ret = devm_request_irq(csi->dev, irq, sun4i_csi_irq, 0,
dev_name(csi->dev), csi);
if (ret) {
dev_err(csi->dev, "Couldn't register our interrupt\n");
goto err_unregister_device;
}
return 0;
err_unregister_device:
v4l2_device_unregister(&csi->v4l);
err_free_mutex:
mutex_destroy(&csi->lock);
return ret;
}
void sun4i_csi_dma_unregister(struct sun4i_csi *csi)
{
v4l2_device_unregister(&csi->v4l);
mutex_destroy(&csi->lock);
}
@@ -0,0 +1,381 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2016 NextThing Co
* Copyright (C) 2016-2019 Bootlin
*
* Author: Maxime Ripard <maxime.ripard@bootlin.com>
*/
#include <linux/device.h>
#include <linux/pm_runtime.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-mc.h>
#include <media/videobuf2-v4l2.h>
#include "sun4i_csi.h"
#define CSI_DEFAULT_WIDTH 640
#define CSI_DEFAULT_HEIGHT 480
static const struct sun4i_csi_format sun4i_csi_formats[] = {
/* YUV422 inputs */
{
.mbus = MEDIA_BUS_FMT_YUYV8_2X8,
.fourcc = V4L2_PIX_FMT_YUV420M,
.input = CSI_INPUT_YUV,
.output = CSI_OUTPUT_YUV_420_PLANAR,
.num_planes = 3,
.bpp = { 8, 8, 8 },
.hsub = 2,
.vsub = 2,
},
};
const struct sun4i_csi_format *sun4i_csi_find_format(const u32 *fourcc,
const u32 *mbus)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(sun4i_csi_formats); i++) {
if (fourcc && *fourcc != sun4i_csi_formats[i].fourcc)
continue;
if (mbus && *mbus != sun4i_csi_formats[i].mbus)
continue;
return &sun4i_csi_formats[i];
}
return NULL;
}
static int sun4i_csi_querycap(struct file *file, void *priv,
struct v4l2_capability *cap)
{
strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
strscpy(cap->card, "sun4i-csi", sizeof(cap->card));
return 0;
}
static int sun4i_csi_enum_input(struct file *file, void *priv,
struct v4l2_input *inp)
{
if (inp->index != 0)
return -EINVAL;
inp->type = V4L2_INPUT_TYPE_CAMERA;
strscpy(inp->name, "Camera", sizeof(inp->name));
return 0;
}
static int sun4i_csi_g_input(struct file *file, void *fh,
unsigned int *i)
{
*i = 0;
return 0;
}
static int sun4i_csi_s_input(struct file *file, void *fh,
unsigned int i)
{
if (i != 0)
return -EINVAL;
return 0;
}
static void _sun4i_csi_try_fmt(struct sun4i_csi *csi,
struct v4l2_pix_format_mplane *pix)
{
const struct sun4i_csi_format *_fmt;
unsigned int height, width;
unsigned int i;
_fmt = sun4i_csi_find_format(&pix->pixelformat, NULL);
if (!_fmt)
_fmt = &sun4i_csi_formats[0];
pix->field = V4L2_FIELD_NONE;
pix->colorspace = V4L2_COLORSPACE_SRGB;
pix->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix->colorspace);
pix->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix->colorspace);
pix->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, pix->colorspace,
pix->ycbcr_enc);
pix->num_planes = _fmt->num_planes;
pix->pixelformat = _fmt->fourcc;
/* Align the width and height on the subsampling */
width = ALIGN(pix->width, _fmt->hsub);
height = ALIGN(pix->height, _fmt->vsub);
/* Clamp the width and height to our capabilities */
pix->width = clamp(width, _fmt->hsub, CSI_MAX_WIDTH);
pix->height = clamp(height, _fmt->vsub, CSI_MAX_HEIGHT);
for (i = 0; i < _fmt->num_planes; i++) {
unsigned int hsub = i > 0 ? _fmt->hsub : 1;
unsigned int vsub = i > 0 ? _fmt->vsub : 1;
unsigned int bpl;
bpl = pix->width / hsub * _fmt->bpp[i] / 8;
pix->plane_fmt[i].bytesperline = bpl;
pix->plane_fmt[i].sizeimage = bpl * pix->height / vsub;
}
}
static int sun4i_csi_try_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct sun4i_csi *csi = video_drvdata(file);
_sun4i_csi_try_fmt(csi, &f->fmt.pix_mp);
return 0;
}
static int sun4i_csi_s_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct sun4i_csi *csi = video_drvdata(file);
_sun4i_csi_try_fmt(csi, &f->fmt.pix_mp);
csi->fmt = f->fmt.pix_mp;
return 0;
}
static int sun4i_csi_g_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct sun4i_csi *csi = video_drvdata(file);
f->fmt.pix_mp = csi->fmt;
return 0;
}
static int sun4i_csi_enum_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_fmtdesc *f)
{
if (f->index >= ARRAY_SIZE(sun4i_csi_formats))
return -EINVAL;
f->pixelformat = sun4i_csi_formats[f->index].fourcc;
return 0;
}
static const struct v4l2_ioctl_ops sun4i_csi_ioctl_ops = {
.vidioc_querycap = sun4i_csi_querycap,
.vidioc_enum_fmt_vid_cap = sun4i_csi_enum_fmt_vid_cap,
.vidioc_g_fmt_vid_cap_mplane = sun4i_csi_g_fmt_vid_cap,
.vidioc_s_fmt_vid_cap_mplane = sun4i_csi_s_fmt_vid_cap,
.vidioc_try_fmt_vid_cap_mplane = sun4i_csi_try_fmt_vid_cap,
.vidioc_enum_input = sun4i_csi_enum_input,
.vidioc_g_input = sun4i_csi_g_input,
.vidioc_s_input = sun4i_csi_s_input,
.vidioc_reqbufs = vb2_ioctl_reqbufs,
.vidioc_create_bufs = vb2_ioctl_create_bufs,
.vidioc_querybuf = vb2_ioctl_querybuf,
.vidioc_qbuf = vb2_ioctl_qbuf,
.vidioc_dqbuf = vb2_ioctl_dqbuf,
.vidioc_expbuf = vb2_ioctl_expbuf,
.vidioc_prepare_buf = vb2_ioctl_prepare_buf,
.vidioc_streamon = vb2_ioctl_streamon,
.vidioc_streamoff = vb2_ioctl_streamoff,
};
static int sun4i_csi_open(struct file *file)
{
struct sun4i_csi *csi = video_drvdata(file);
int ret;
ret = mutex_lock_interruptible(&csi->lock);
if (ret)
return ret;
ret = pm_runtime_resume_and_get(csi->dev);
if (ret < 0)
goto err_unlock;
ret = v4l2_pipeline_pm_get(&csi->vdev.entity);
if (ret)
goto err_pm_put;
ret = v4l2_fh_open(file);
if (ret)
goto err_pipeline_pm_put;
mutex_unlock(&csi->lock);
return 0;
err_pipeline_pm_put:
v4l2_pipeline_pm_put(&csi->vdev.entity);
err_pm_put:
pm_runtime_put(csi->dev);
err_unlock:
mutex_unlock(&csi->lock);
return ret;
}
static int sun4i_csi_release(struct file *file)
{
struct sun4i_csi *csi = video_drvdata(file);
mutex_lock(&csi->lock);
_vb2_fop_release(file, NULL);
v4l2_pipeline_pm_put(&csi->vdev.entity);
pm_runtime_put(csi->dev);
mutex_unlock(&csi->lock);
return 0;
}
static const struct v4l2_file_operations sun4i_csi_fops = {
.owner = THIS_MODULE,
.open = sun4i_csi_open,
.release = sun4i_csi_release,
.unlocked_ioctl = video_ioctl2,
.poll = vb2_fop_poll,
.mmap = vb2_fop_mmap,
};
static const struct v4l2_mbus_framefmt sun4i_csi_pad_fmt_default = {
.width = CSI_DEFAULT_WIDTH,
.height = CSI_DEFAULT_HEIGHT,
.code = MEDIA_BUS_FMT_YUYV8_2X8,
.field = V4L2_FIELD_NONE,
.colorspace = V4L2_COLORSPACE_RAW,
.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT,
.quantization = V4L2_QUANTIZATION_DEFAULT,
.xfer_func = V4L2_XFER_FUNC_DEFAULT,
};
static int sun4i_csi_subdev_init_state(struct v4l2_subdev *subdev,
struct v4l2_subdev_state *sd_state)
{
struct v4l2_mbus_framefmt *fmt;
fmt = v4l2_subdev_state_get_format(sd_state, CSI_SUBDEV_SINK);
*fmt = sun4i_csi_pad_fmt_default;
return 0;
}
static int sun4i_csi_subdev_get_fmt(struct v4l2_subdev *subdev,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_format *fmt)
{
struct sun4i_csi *csi = container_of(subdev, struct sun4i_csi, subdev);
struct v4l2_mbus_framefmt *subdev_fmt;
if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
subdev_fmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
else
subdev_fmt = &csi->subdev_fmt;
fmt->format = *subdev_fmt;
return 0;
}
static int sun4i_csi_subdev_set_fmt(struct v4l2_subdev *subdev,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_format *fmt)
{
struct sun4i_csi *csi = container_of(subdev, struct sun4i_csi, subdev);
struct v4l2_mbus_framefmt *subdev_fmt;
if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
subdev_fmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
else
subdev_fmt = &csi->subdev_fmt;
/* We can only set the format on the sink pad */
if (fmt->pad == CSI_SUBDEV_SINK) {
/* It's the sink, only allow changing the frame size */
subdev_fmt->width = fmt->format.width;
subdev_fmt->height = fmt->format.height;
subdev_fmt->code = fmt->format.code;
}
fmt->format = *subdev_fmt;
return 0;
}
static int
sun4i_csi_subdev_enum_mbus_code(struct v4l2_subdev *subdev,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_mbus_code_enum *mbus)
{
if (mbus->index >= ARRAY_SIZE(sun4i_csi_formats))
return -EINVAL;
mbus->code = sun4i_csi_formats[mbus->index].mbus;
return 0;
}
static const struct v4l2_subdev_pad_ops sun4i_csi_subdev_pad_ops = {
.link_validate = v4l2_subdev_link_validate_default,
.get_fmt = sun4i_csi_subdev_get_fmt,
.set_fmt = sun4i_csi_subdev_set_fmt,
.enum_mbus_code = sun4i_csi_subdev_enum_mbus_code,
};
const struct v4l2_subdev_ops sun4i_csi_subdev_ops = {
.pad = &sun4i_csi_subdev_pad_ops,
};
const struct v4l2_subdev_internal_ops sun4i_csi_subdev_internal_ops = {
.init_state = sun4i_csi_subdev_init_state,
};
int sun4i_csi_v4l2_register(struct sun4i_csi *csi)
{
struct video_device *vdev = &csi->vdev;
int ret;
vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_STREAMING;
vdev->v4l2_dev = &csi->v4l;
vdev->queue = &csi->queue;
strscpy(vdev->name, KBUILD_MODNAME, sizeof(vdev->name));
vdev->release = video_device_release_empty;
vdev->lock = &csi->lock;
/* Set a default format */
csi->fmt.pixelformat = sun4i_csi_formats[0].fourcc;
csi->fmt.width = CSI_DEFAULT_WIDTH;
csi->fmt.height = CSI_DEFAULT_HEIGHT;
_sun4i_csi_try_fmt(csi, &csi->fmt);
csi->subdev_fmt = sun4i_csi_pad_fmt_default;
vdev->fops = &sun4i_csi_fops;
vdev->ioctl_ops = &sun4i_csi_ioctl_ops;
video_set_drvdata(vdev, csi);
ret = video_register_device(&csi->vdev, VFL_TYPE_VIDEO, -1);
if (ret)
return ret;
dev_info(csi->dev, "Device registered as %s\n",
video_device_node_name(vdev));
return 0;
}
@@ -0,0 +1,15 @@
# SPDX-License-Identifier: GPL-2.0-only
config VIDEO_SUN6I_CSI
tristate "Allwinner A31 Camera Sensor Interface (CSI) Driver"
depends on V4L_PLATFORM_DRIVERS && VIDEO_DEV
depends on ARCH_SUNXI || COMPILE_TEST
depends on PM && COMMON_CLK && RESET_CONTROLLER && HAS_DMA
select MEDIA_CONTROLLER
select VIDEO_V4L2_SUBDEV_API
select VIDEOBUF2_DMA_CONTIG
select V4L2_FWNODE
select REGMAP_MMIO
help
Support for the Allwinner A31 Camera Sensor Interface (CSI)
controller, also found on other platforms such as the A83T, H3,
V3/V3s or A64.
@@ -0,0 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
sun6i-csi-y += sun6i_csi.o sun6i_csi_bridge.o sun6i_csi_capture.o
obj-$(CONFIG_VIDEO_SUN6I_CSI) += sun6i-csi.o
@@ -0,0 +1,439 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2011-2018 Magewell Electronics Co., Ltd. (Nanjing)
* Author: Yong Deng <yong.deng@magewell.com>
* Copyright 2021-2022 Bootlin
* Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
*/
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/reset.h>
#include <media/v4l2-device.h>
#include <media/v4l2-mc.h>
#include "sun6i_csi.h"
#include "sun6i_csi_bridge.h"
#include "sun6i_csi_capture.h"
#include "sun6i_csi_reg.h"
/* ISP */
int sun6i_csi_isp_complete(struct sun6i_csi_device *csi_dev,
struct v4l2_device *v4l2_dev)
{
if (csi_dev->v4l2_dev && csi_dev->v4l2_dev != v4l2_dev)
return -EINVAL;
csi_dev->v4l2_dev = v4l2_dev;
csi_dev->media_dev = v4l2_dev->mdev;
return sun6i_csi_capture_setup(csi_dev);
}
static int sun6i_csi_isp_detect(struct sun6i_csi_device *csi_dev)
{
struct device *dev = csi_dev->dev;
struct fwnode_handle *handle;
/*
* ISP is not available if not connected via fwnode graph.
* This will also check that the remote parent node is available.
*/
handle = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev),
SUN6I_CSI_PORT_ISP, 0,
FWNODE_GRAPH_ENDPOINT_NEXT);
if (!handle)
return 0;
fwnode_handle_put(handle);
if (!IS_ENABLED(CONFIG_VIDEO_SUN6I_ISP)) {
dev_warn(dev,
"ISP link is detected but not enabled in kernel config!");
return 0;
}
csi_dev->isp_available = true;
return 0;
}
/* Media */
static const struct media_device_ops sun6i_csi_media_ops = {
.link_notify = v4l2_pipeline_link_notify,
};
/* V4L2 */
static int sun6i_csi_v4l2_setup(struct sun6i_csi_device *csi_dev)
{
struct sun6i_csi_v4l2 *v4l2 = &csi_dev->v4l2;
struct media_device *media_dev = &v4l2->media_dev;
struct v4l2_device *v4l2_dev = &v4l2->v4l2_dev;
struct device *dev = csi_dev->dev;
int ret;
/* Media Device */
strscpy(media_dev->model, SUN6I_CSI_DESCRIPTION,
sizeof(media_dev->model));
media_dev->hw_revision = 0;
media_dev->ops = &sun6i_csi_media_ops;
media_dev->dev = dev;
media_device_init(media_dev);
ret = media_device_register(media_dev);
if (ret) {
dev_err(dev, "failed to register media device: %d\n", ret);
goto error_media;
}
/* V4L2 Device */
v4l2_dev->mdev = media_dev;
ret = v4l2_device_register(dev, v4l2_dev);
if (ret) {
dev_err(dev, "failed to register v4l2 device: %d\n", ret);
goto error_media;
}
csi_dev->v4l2_dev = v4l2_dev;
csi_dev->media_dev = media_dev;
return 0;
error_media:
media_device_unregister(media_dev);
media_device_cleanup(media_dev);
return ret;
}
static void sun6i_csi_v4l2_cleanup(struct sun6i_csi_device *csi_dev)
{
struct sun6i_csi_v4l2 *v4l2 = &csi_dev->v4l2;
media_device_unregister(&v4l2->media_dev);
v4l2_device_unregister(&v4l2->v4l2_dev);
media_device_cleanup(&v4l2->media_dev);
}
/* Platform */
static irqreturn_t sun6i_csi_interrupt(int irq, void *private)
{
struct sun6i_csi_device *csi_dev = private;
bool capture_streaming = csi_dev->capture.state.streaming;
struct regmap *regmap = csi_dev->regmap;
u32 status = 0, enable = 0;
regmap_read(regmap, SUN6I_CSI_CH_INT_STA_REG, &status);
regmap_read(regmap, SUN6I_CSI_CH_INT_EN_REG, &enable);
if (!status)
return IRQ_NONE;
else if (!(status & enable) || !capture_streaming)
goto complete;
if ((status & SUN6I_CSI_CH_INT_STA_FIFO0_OF) ||
(status & SUN6I_CSI_CH_INT_STA_FIFO1_OF) ||
(status & SUN6I_CSI_CH_INT_STA_FIFO2_OF) ||
(status & SUN6I_CSI_CH_INT_STA_HB_OF)) {
regmap_write(regmap, SUN6I_CSI_CH_INT_STA_REG, status);
regmap_update_bits(regmap, SUN6I_CSI_EN_REG,
SUN6I_CSI_EN_CSI_EN, 0);
regmap_update_bits(regmap, SUN6I_CSI_EN_REG,
SUN6I_CSI_EN_CSI_EN, SUN6I_CSI_EN_CSI_EN);
return IRQ_HANDLED;
}
if (status & SUN6I_CSI_CH_INT_STA_FD)
sun6i_csi_capture_frame_done(csi_dev);
if (status & SUN6I_CSI_CH_INT_STA_VS)
sun6i_csi_capture_sync(csi_dev);
complete:
regmap_write(regmap, SUN6I_CSI_CH_INT_STA_REG, status);
return IRQ_HANDLED;
}
static int sun6i_csi_suspend(struct device *dev)
{
struct sun6i_csi_device *csi_dev = dev_get_drvdata(dev);
reset_control_assert(csi_dev->reset);
clk_disable_unprepare(csi_dev->clock_ram);
clk_disable_unprepare(csi_dev->clock_mod);
return 0;
}
static int sun6i_csi_resume(struct device *dev)
{
struct sun6i_csi_device *csi_dev = dev_get_drvdata(dev);
int ret;
ret = reset_control_deassert(csi_dev->reset);
if (ret) {
dev_err(dev, "failed to deassert reset\n");
return ret;
}
ret = clk_prepare_enable(csi_dev->clock_mod);
if (ret) {
dev_err(dev, "failed to enable module clock\n");
goto error_reset;
}
ret = clk_prepare_enable(csi_dev->clock_ram);
if (ret) {
dev_err(dev, "failed to enable ram clock\n");
goto error_clock_mod;
}
return 0;
error_clock_mod:
clk_disable_unprepare(csi_dev->clock_mod);
error_reset:
reset_control_assert(csi_dev->reset);
return ret;
}
static const struct dev_pm_ops sun6i_csi_pm_ops = {
.runtime_suspend = sun6i_csi_suspend,
.runtime_resume = sun6i_csi_resume,
};
static const struct regmap_config sun6i_csi_regmap_config = {
.reg_bits = 32,
.reg_stride = 4,
.val_bits = 32,
.max_register = 0x9c,
};
static int sun6i_csi_resources_setup(struct sun6i_csi_device *csi_dev,
struct platform_device *platform_dev)
{
struct device *dev = csi_dev->dev;
const struct sun6i_csi_variant *variant;
void __iomem *io_base;
int ret;
int irq;
variant = of_device_get_match_data(dev);
if (!variant)
return -EINVAL;
/* Registers */
io_base = devm_platform_ioremap_resource(platform_dev, 0);
if (IS_ERR(io_base))
return PTR_ERR(io_base);
csi_dev->regmap = devm_regmap_init_mmio_clk(dev, "bus", io_base,
&sun6i_csi_regmap_config);
if (IS_ERR(csi_dev->regmap)) {
dev_err(dev, "failed to init register map\n");
return PTR_ERR(csi_dev->regmap);
}
/* Clocks */
csi_dev->clock_mod = devm_clk_get(dev, "mod");
if (IS_ERR(csi_dev->clock_mod)) {
dev_err(dev, "failed to acquire module clock\n");
return PTR_ERR(csi_dev->clock_mod);
}
csi_dev->clock_ram = devm_clk_get(dev, "ram");
if (IS_ERR(csi_dev->clock_ram)) {
dev_err(dev, "failed to acquire ram clock\n");
return PTR_ERR(csi_dev->clock_ram);
}
ret = clk_set_rate_exclusive(csi_dev->clock_mod,
variant->clock_mod_rate);
if (ret) {
dev_err(dev, "failed to set mod clock rate\n");
return ret;
}
/* Reset */
csi_dev->reset = devm_reset_control_get_shared(dev, NULL);
if (IS_ERR(csi_dev->reset)) {
dev_err(dev, "failed to acquire reset\n");
ret = PTR_ERR(csi_dev->reset);
goto error_clock_rate_exclusive;
}
/* Interrupt */
irq = platform_get_irq(platform_dev, 0);
if (irq < 0) {
ret = -ENXIO;
goto error_clock_rate_exclusive;
}
ret = devm_request_irq(dev, irq, sun6i_csi_interrupt, IRQF_SHARED,
SUN6I_CSI_NAME, csi_dev);
if (ret) {
dev_err(dev, "failed to request interrupt\n");
goto error_clock_rate_exclusive;
}
/* Runtime PM */
pm_runtime_enable(dev);
return 0;
error_clock_rate_exclusive:
clk_rate_exclusive_put(csi_dev->clock_mod);
return ret;
}
static void sun6i_csi_resources_cleanup(struct sun6i_csi_device *csi_dev)
{
pm_runtime_disable(csi_dev->dev);
clk_rate_exclusive_put(csi_dev->clock_mod);
}
static int sun6i_csi_probe(struct platform_device *platform_dev)
{
struct sun6i_csi_device *csi_dev;
struct device *dev = &platform_dev->dev;
int ret;
csi_dev = devm_kzalloc(dev, sizeof(*csi_dev), GFP_KERNEL);
if (!csi_dev)
return -ENOMEM;
csi_dev->dev = &platform_dev->dev;
platform_set_drvdata(platform_dev, csi_dev);
ret = sun6i_csi_resources_setup(csi_dev, platform_dev);
if (ret)
return ret;
ret = sun6i_csi_isp_detect(csi_dev);
if (ret)
goto error_resources;
/*
* Register our own v4l2 and media devices when there is no ISP around.
* Otherwise the ISP will use async subdev registration with our bridge,
* which will provide v4l2 and media devices that are used to register
* the video interface.
*/
if (!csi_dev->isp_available) {
ret = sun6i_csi_v4l2_setup(csi_dev);
if (ret)
goto error_resources;
}
ret = sun6i_csi_bridge_setup(csi_dev);
if (ret)
goto error_v4l2;
if (!csi_dev->isp_available) {
ret = sun6i_csi_capture_setup(csi_dev);
if (ret)
goto error_bridge;
}
return 0;
error_bridge:
sun6i_csi_bridge_cleanup(csi_dev);
error_v4l2:
if (!csi_dev->isp_available)
sun6i_csi_v4l2_cleanup(csi_dev);
error_resources:
sun6i_csi_resources_cleanup(csi_dev);
return ret;
}
static void sun6i_csi_remove(struct platform_device *pdev)
{
struct sun6i_csi_device *csi_dev = platform_get_drvdata(pdev);
sun6i_csi_capture_cleanup(csi_dev);
sun6i_csi_bridge_cleanup(csi_dev);
if (!csi_dev->isp_available)
sun6i_csi_v4l2_cleanup(csi_dev);
sun6i_csi_resources_cleanup(csi_dev);
}
static const struct sun6i_csi_variant sun6i_a31_csi_variant = {
.clock_mod_rate = 297000000,
};
static const struct sun6i_csi_variant sun50i_a64_csi_variant = {
.clock_mod_rate = 300000000,
};
static const struct of_device_id sun6i_csi_of_match[] = {
{
.compatible = "allwinner,sun6i-a31-csi",
.data = &sun6i_a31_csi_variant,
},
{
.compatible = "allwinner,sun8i-a83t-csi",
.data = &sun6i_a31_csi_variant,
},
{
.compatible = "allwinner,sun8i-h3-csi",
.data = &sun6i_a31_csi_variant,
},
{
.compatible = "allwinner,sun8i-v3s-csi",
.data = &sun6i_a31_csi_variant,
},
{
.compatible = "allwinner,sun50i-a64-csi",
.data = &sun50i_a64_csi_variant,
},
{},
};
MODULE_DEVICE_TABLE(of, sun6i_csi_of_match);
static struct platform_driver sun6i_csi_platform_driver = {
.probe = sun6i_csi_probe,
.remove = sun6i_csi_remove,
.driver = {
.name = SUN6I_CSI_NAME,
.of_match_table = sun6i_csi_of_match,
.pm = &sun6i_csi_pm_ops,
},
};
module_platform_driver(sun6i_csi_platform_driver);
MODULE_DESCRIPTION("Allwinner A31 Camera Sensor Interface driver");
MODULE_AUTHOR("Yong Deng <yong.deng@magewell.com>");
MODULE_AUTHOR("Paul Kocialkowski <paul.kocialkowski@bootlin.com>");
MODULE_LICENSE("GPL");
@@ -0,0 +1,63 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright (c) 2011-2018 Magewell Electronics Co., Ltd. (Nanjing)
* Author: Yong Deng <yong.deng@magewell.com>
* Copyright 2021-2022 Bootlin
* Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
*/
#ifndef _SUN6I_CSI_H_
#define _SUN6I_CSI_H_
#include <media/v4l2-device.h>
#include <media/videobuf2-v4l2.h>
#include "sun6i_csi_bridge.h"
#include "sun6i_csi_capture.h"
#define SUN6I_CSI_NAME "sun6i-csi"
#define SUN6I_CSI_DESCRIPTION "Allwinner A31 CSI Device"
enum sun6i_csi_port {
SUN6I_CSI_PORT_PARALLEL = 0,
SUN6I_CSI_PORT_MIPI_CSI2 = 1,
SUN6I_CSI_PORT_ISP = 2,
};
struct sun6i_csi_buffer {
struct vb2_v4l2_buffer v4l2_buffer;
struct list_head list;
};
struct sun6i_csi_v4l2 {
struct v4l2_device v4l2_dev;
struct media_device media_dev;
};
struct sun6i_csi_device {
struct device *dev;
struct v4l2_device *v4l2_dev;
struct media_device *media_dev;
struct sun6i_csi_v4l2 v4l2;
struct sun6i_csi_bridge bridge;
struct sun6i_csi_capture capture;
struct regmap *regmap;
struct clk *clock_mod;
struct clk *clock_ram;
struct reset_control *reset;
bool isp_available;
};
struct sun6i_csi_variant {
unsigned long clock_mod_rate;
};
/* ISP */
int sun6i_csi_isp_complete(struct sun6i_csi_device *csi_dev,
struct v4l2_device *v4l2_dev);
#endif
@@ -0,0 +1,872 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright 2021-2022 Bootlin
* Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
*/
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#include "sun6i_csi.h"
#include "sun6i_csi_bridge.h"
#include "sun6i_csi_reg.h"
/* Helpers */
void sun6i_csi_bridge_dimensions(struct sun6i_csi_device *csi_dev,
unsigned int *width, unsigned int *height)
{
if (width)
*width = csi_dev->bridge.mbus_format.width;
if (height)
*height = csi_dev->bridge.mbus_format.height;
}
void sun6i_csi_bridge_format(struct sun6i_csi_device *csi_dev,
u32 *mbus_code, u32 *field)
{
if (mbus_code)
*mbus_code = csi_dev->bridge.mbus_format.code;
if (field)
*field = csi_dev->bridge.mbus_format.field;
}
/* Format */
static const struct sun6i_csi_bridge_format sun6i_csi_bridge_formats[] = {
/* Bayer */
{
.mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
.input_format = SUN6I_CSI_INPUT_FMT_RAW,
},
{
.mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8,
.input_format = SUN6I_CSI_INPUT_FMT_RAW,
},
{
.mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8,
.input_format = SUN6I_CSI_INPUT_FMT_RAW,
},
{
.mbus_code = MEDIA_BUS_FMT_SRGGB8_1X8,
.input_format = SUN6I_CSI_INPUT_FMT_RAW,
},
{
.mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10,
.input_format = SUN6I_CSI_INPUT_FMT_RAW,
},
{
.mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10,
.input_format = SUN6I_CSI_INPUT_FMT_RAW,
},
{
.mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
.input_format = SUN6I_CSI_INPUT_FMT_RAW,
},
{
.mbus_code = MEDIA_BUS_FMT_SRGGB10_1X10,
.input_format = SUN6I_CSI_INPUT_FMT_RAW,
},
{
.mbus_code = MEDIA_BUS_FMT_SBGGR12_1X12,
.input_format = SUN6I_CSI_INPUT_FMT_RAW,
},
{
.mbus_code = MEDIA_BUS_FMT_SGBRG12_1X12,
.input_format = SUN6I_CSI_INPUT_FMT_RAW,
},
{
.mbus_code = MEDIA_BUS_FMT_SGRBG12_1X12,
.input_format = SUN6I_CSI_INPUT_FMT_RAW,
},
{
.mbus_code = MEDIA_BUS_FMT_SRGGB12_1X12,
.input_format = SUN6I_CSI_INPUT_FMT_RAW,
},
/* RGB */
{
.mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
.input_format = SUN6I_CSI_INPUT_FMT_RAW,
},
{
.mbus_code = MEDIA_BUS_FMT_RGB565_2X8_BE,
.input_format = SUN6I_CSI_INPUT_FMT_RAW,
},
/* YUV422 */
{
.mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
.input_format = SUN6I_CSI_INPUT_FMT_YUV422,
.input_yuv_seq = SUN6I_CSI_INPUT_YUV_SEQ_YUYV,
.input_yuv_seq_invert = SUN6I_CSI_INPUT_YUV_SEQ_YVYU,
},
{
.mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
.input_format = SUN6I_CSI_INPUT_FMT_YUV422,
.input_yuv_seq = SUN6I_CSI_INPUT_YUV_SEQ_UYVY,
.input_yuv_seq_invert = SUN6I_CSI_INPUT_YUV_SEQ_VYUY,
},
{
.mbus_code = MEDIA_BUS_FMT_YVYU8_2X8,
.input_format = SUN6I_CSI_INPUT_FMT_YUV422,
.input_yuv_seq = SUN6I_CSI_INPUT_YUV_SEQ_YVYU,
.input_yuv_seq_invert = SUN6I_CSI_INPUT_YUV_SEQ_YUYV,
},
{
.mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
.input_format = SUN6I_CSI_INPUT_FMT_YUV422,
.input_yuv_seq = SUN6I_CSI_INPUT_YUV_SEQ_UYVY,
.input_yuv_seq_invert = SUN6I_CSI_INPUT_YUV_SEQ_VYUY,
},
{
.mbus_code = MEDIA_BUS_FMT_VYUY8_2X8,
.input_format = SUN6I_CSI_INPUT_FMT_YUV422,
.input_yuv_seq = SUN6I_CSI_INPUT_YUV_SEQ_VYUY,
.input_yuv_seq_invert = SUN6I_CSI_INPUT_YUV_SEQ_UYVY,
},
{
.mbus_code = MEDIA_BUS_FMT_YUYV8_1X16,
.input_format = SUN6I_CSI_INPUT_FMT_YUV422,
.input_yuv_seq = SUN6I_CSI_INPUT_YUV_SEQ_YUYV,
.input_yuv_seq_invert = SUN6I_CSI_INPUT_YUV_SEQ_YVYU,
},
{
.mbus_code = MEDIA_BUS_FMT_UYVY8_1X16,
.input_format = SUN6I_CSI_INPUT_FMT_YUV422,
.input_yuv_seq = SUN6I_CSI_INPUT_YUV_SEQ_UYVY,
.input_yuv_seq_invert = SUN6I_CSI_INPUT_YUV_SEQ_VYUY,
},
{
.mbus_code = MEDIA_BUS_FMT_YVYU8_1X16,
.input_format = SUN6I_CSI_INPUT_FMT_YUV422,
.input_yuv_seq = SUN6I_CSI_INPUT_YUV_SEQ_YVYU,
.input_yuv_seq_invert = SUN6I_CSI_INPUT_YUV_SEQ_YUYV,
},
{
.mbus_code = MEDIA_BUS_FMT_UYVY8_1X16,
.input_format = SUN6I_CSI_INPUT_FMT_YUV422,
.input_yuv_seq = SUN6I_CSI_INPUT_YUV_SEQ_UYVY,
.input_yuv_seq_invert = SUN6I_CSI_INPUT_YUV_SEQ_VYUY,
},
{
.mbus_code = MEDIA_BUS_FMT_VYUY8_1X16,
.input_format = SUN6I_CSI_INPUT_FMT_YUV422,
.input_yuv_seq = SUN6I_CSI_INPUT_YUV_SEQ_VYUY,
.input_yuv_seq_invert = SUN6I_CSI_INPUT_YUV_SEQ_UYVY,
},
/* Compressed */
{
.mbus_code = MEDIA_BUS_FMT_JPEG_1X8,
.input_format = SUN6I_CSI_INPUT_FMT_RAW,
},
};
const struct sun6i_csi_bridge_format *
sun6i_csi_bridge_format_find(u32 mbus_code)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(sun6i_csi_bridge_formats); i++)
if (sun6i_csi_bridge_formats[i].mbus_code == mbus_code)
return &sun6i_csi_bridge_formats[i];
return NULL;
}
/* Bridge */
static void sun6i_csi_bridge_irq_enable(struct sun6i_csi_device *csi_dev)
{
struct regmap *regmap = csi_dev->regmap;
regmap_write(regmap, SUN6I_CSI_CH_INT_EN_REG,
SUN6I_CSI_CH_INT_EN_VS |
SUN6I_CSI_CH_INT_EN_HB_OF |
SUN6I_CSI_CH_INT_EN_FIFO2_OF |
SUN6I_CSI_CH_INT_EN_FIFO1_OF |
SUN6I_CSI_CH_INT_EN_FIFO0_OF |
SUN6I_CSI_CH_INT_EN_FD |
SUN6I_CSI_CH_INT_EN_CD);
}
static void sun6i_csi_bridge_irq_disable(struct sun6i_csi_device *csi_dev)
{
struct regmap *regmap = csi_dev->regmap;
regmap_write(regmap, SUN6I_CSI_CH_INT_EN_REG, 0);
}
static void sun6i_csi_bridge_irq_clear(struct sun6i_csi_device *csi_dev)
{
struct regmap *regmap = csi_dev->regmap;
regmap_write(regmap, SUN6I_CSI_CH_INT_EN_REG, 0);
regmap_write(regmap, SUN6I_CSI_CH_INT_STA_REG,
SUN6I_CSI_CH_INT_STA_CLEAR);
}
static void sun6i_csi_bridge_enable(struct sun6i_csi_device *csi_dev)
{
struct regmap *regmap = csi_dev->regmap;
regmap_update_bits(regmap, SUN6I_CSI_EN_REG, SUN6I_CSI_EN_CSI_EN,
SUN6I_CSI_EN_CSI_EN);
regmap_update_bits(regmap, SUN6I_CSI_CAP_REG, SUN6I_CSI_CAP_VCAP_ON,
SUN6I_CSI_CAP_VCAP_ON);
}
static void sun6i_csi_bridge_disable(struct sun6i_csi_device *csi_dev)
{
struct regmap *regmap = csi_dev->regmap;
regmap_update_bits(regmap, SUN6I_CSI_CAP_REG, SUN6I_CSI_CAP_VCAP_ON, 0);
regmap_update_bits(regmap, SUN6I_CSI_EN_REG, SUN6I_CSI_EN_CSI_EN, 0);
}
static void
sun6i_csi_bridge_configure_parallel(struct sun6i_csi_device *csi_dev)
{
struct device *dev = csi_dev->dev;
struct regmap *regmap = csi_dev->regmap;
struct v4l2_fwnode_endpoint *endpoint =
&csi_dev->bridge.source_parallel.endpoint;
unsigned char bus_width = endpoint->bus.parallel.bus_width;
unsigned int flags = endpoint->bus.parallel.flags;
u32 field;
u32 value = SUN6I_CSI_IF_CFG_IF_CSI;
sun6i_csi_bridge_format(csi_dev, NULL, &field);
if (field == V4L2_FIELD_INTERLACED ||
field == V4L2_FIELD_INTERLACED_TB ||
field == V4L2_FIELD_INTERLACED_BT)
value |= SUN6I_CSI_IF_CFG_SRC_TYPE_INTERLACED |
SUN6I_CSI_IF_CFG_FIELD_DT_PCLK_SHIFT(1) |
SUN6I_CSI_IF_CFG_FIELD_DT_FIELD_VSYNC;
else
value |= SUN6I_CSI_IF_CFG_SRC_TYPE_PROGRESSIVE;
switch (endpoint->bus_type) {
case V4L2_MBUS_PARALLEL:
if (bus_width == 16)
value |= SUN6I_CSI_IF_CFG_IF_CSI_YUV_COMBINED;
else
value |= SUN6I_CSI_IF_CFG_IF_CSI_YUV_RAW;
if (flags & V4L2_MBUS_FIELD_EVEN_LOW)
value |= SUN6I_CSI_IF_CFG_FIELD_NEGATIVE;
else
value |= SUN6I_CSI_IF_CFG_FIELD_POSITIVE;
if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
value |= SUN6I_CSI_IF_CFG_VREF_POL_NEGATIVE;
else
value |= SUN6I_CSI_IF_CFG_VREF_POL_POSITIVE;
if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
value |= SUN6I_CSI_IF_CFG_HREF_POL_NEGATIVE;
else
value |= SUN6I_CSI_IF_CFG_HREF_POL_POSITIVE;
if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
value |= SUN6I_CSI_IF_CFG_CLK_POL_RISING;
else
value |= SUN6I_CSI_IF_CFG_CLK_POL_FALLING;
break;
case V4L2_MBUS_BT656:
if (bus_width == 16)
value |= SUN6I_CSI_IF_CFG_IF_CSI_BT1120;
else
value |= SUN6I_CSI_IF_CFG_IF_CSI_BT656;
if (flags & V4L2_MBUS_FIELD_EVEN_LOW)
value |= SUN6I_CSI_IF_CFG_FIELD_NEGATIVE;
else
value |= SUN6I_CSI_IF_CFG_FIELD_POSITIVE;
if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
value |= SUN6I_CSI_IF_CFG_CLK_POL_RISING;
else
value |= SUN6I_CSI_IF_CFG_CLK_POL_FALLING;
break;
default:
dev_warn(dev, "unsupported bus type: %d\n", endpoint->bus_type);
break;
}
switch (bus_width) {
case 8:
/* 16-bit YUV formats use a doubled width in 8-bit mode. */
case 16:
value |= SUN6I_CSI_IF_CFG_DATA_WIDTH_8;
break;
case 10:
value |= SUN6I_CSI_IF_CFG_DATA_WIDTH_10;
break;
case 12:
value |= SUN6I_CSI_IF_CFG_DATA_WIDTH_12;
break;
default:
dev_warn(dev, "unsupported bus width: %u\n", bus_width);
break;
}
regmap_write(regmap, SUN6I_CSI_IF_CFG_REG, value);
}
static void
sun6i_csi_bridge_configure_mipi_csi2(struct sun6i_csi_device *csi_dev)
{
struct regmap *regmap = csi_dev->regmap;
u32 value = SUN6I_CSI_IF_CFG_IF_MIPI;
u32 field;
sun6i_csi_bridge_format(csi_dev, NULL, &field);
if (field == V4L2_FIELD_INTERLACED ||
field == V4L2_FIELD_INTERLACED_TB ||
field == V4L2_FIELD_INTERLACED_BT)
value |= SUN6I_CSI_IF_CFG_SRC_TYPE_INTERLACED;
else
value |= SUN6I_CSI_IF_CFG_SRC_TYPE_PROGRESSIVE;
regmap_write(regmap, SUN6I_CSI_IF_CFG_REG, value);
}
static void sun6i_csi_bridge_configure_format(struct sun6i_csi_device *csi_dev)
{
struct regmap *regmap = csi_dev->regmap;
bool capture_streaming = csi_dev->capture.state.streaming;
const struct sun6i_csi_bridge_format *bridge_format;
const struct sun6i_csi_capture_format *capture_format;
u32 mbus_code, field, pixelformat;
u8 input_format, input_yuv_seq, output_format;
u32 value = 0;
sun6i_csi_bridge_format(csi_dev, &mbus_code, &field);
bridge_format = sun6i_csi_bridge_format_find(mbus_code);
if (WARN_ON(!bridge_format))
return;
input_format = bridge_format->input_format;
input_yuv_seq = bridge_format->input_yuv_seq;
if (capture_streaming) {
sun6i_csi_capture_format(csi_dev, &pixelformat, NULL);
capture_format = sun6i_csi_capture_format_find(pixelformat);
if (WARN_ON(!capture_format))
return;
if (capture_format->input_format_raw)
input_format = SUN6I_CSI_INPUT_FMT_RAW;
if (capture_format->input_yuv_seq_invert)
input_yuv_seq = bridge_format->input_yuv_seq_invert;
if (field == V4L2_FIELD_INTERLACED ||
field == V4L2_FIELD_INTERLACED_TB ||
field == V4L2_FIELD_INTERLACED_BT)
output_format = capture_format->output_format_field;
else
output_format = capture_format->output_format_frame;
value |= SUN6I_CSI_CH_CFG_OUTPUT_FMT(output_format);
}
value |= SUN6I_CSI_CH_CFG_INPUT_FMT(input_format);
value |= SUN6I_CSI_CH_CFG_INPUT_YUV_SEQ(input_yuv_seq);
if (field == V4L2_FIELD_TOP)
value |= SUN6I_CSI_CH_CFG_FIELD_SEL_FIELD0;
else if (field == V4L2_FIELD_BOTTOM)
value |= SUN6I_CSI_CH_CFG_FIELD_SEL_FIELD1;
else
value |= SUN6I_CSI_CH_CFG_FIELD_SEL_EITHER;
regmap_write(regmap, SUN6I_CSI_CH_CFG_REG, value);
}
static void sun6i_csi_bridge_configure(struct sun6i_csi_device *csi_dev,
struct sun6i_csi_bridge_source *source)
{
struct sun6i_csi_bridge *bridge = &csi_dev->bridge;
if (source == &bridge->source_parallel)
sun6i_csi_bridge_configure_parallel(csi_dev);
else
sun6i_csi_bridge_configure_mipi_csi2(csi_dev);
sun6i_csi_bridge_configure_format(csi_dev);
}
/* V4L2 Subdev */
static int sun6i_csi_bridge_s_stream(struct v4l2_subdev *subdev, int on)
{
struct sun6i_csi_device *csi_dev = v4l2_get_subdevdata(subdev);
struct sun6i_csi_bridge *bridge = &csi_dev->bridge;
struct media_pad *local_pad = &bridge->pads[SUN6I_CSI_BRIDGE_PAD_SINK];
bool capture_streaming = csi_dev->capture.state.streaming;
struct device *dev = csi_dev->dev;
struct sun6i_csi_bridge_source *source;
struct v4l2_subdev *source_subdev;
struct media_pad *remote_pad;
int ret;
/* Source */
remote_pad = media_pad_remote_pad_unique(local_pad);
if (IS_ERR(remote_pad)) {
dev_err(dev,
"zero or more than a single source connected to the bridge\n");
return PTR_ERR(remote_pad);
}
source_subdev = media_entity_to_v4l2_subdev(remote_pad->entity);
if (source_subdev == bridge->source_parallel.subdev)
source = &bridge->source_parallel;
else
source = &bridge->source_mipi_csi2;
if (!on) {
v4l2_subdev_call(source_subdev, video, s_stream, 0);
ret = 0;
goto disable;
}
/* PM */
ret = pm_runtime_resume_and_get(dev);
if (ret < 0)
return ret;
/* Clear */
sun6i_csi_bridge_irq_clear(csi_dev);
/* Configure */
sun6i_csi_bridge_configure(csi_dev, source);
if (capture_streaming)
sun6i_csi_capture_configure(csi_dev);
/* State Update */
if (capture_streaming)
sun6i_csi_capture_state_update(csi_dev);
/* Enable */
if (capture_streaming)
sun6i_csi_bridge_irq_enable(csi_dev);
sun6i_csi_bridge_enable(csi_dev);
ret = v4l2_subdev_call(source_subdev, video, s_stream, 1);
if (ret && ret != -ENOIOCTLCMD)
goto disable;
return 0;
disable:
if (capture_streaming)
sun6i_csi_bridge_irq_disable(csi_dev);
sun6i_csi_bridge_disable(csi_dev);
pm_runtime_put(dev);
return ret;
}
static const struct v4l2_subdev_video_ops sun6i_csi_bridge_video_ops = {
.s_stream = sun6i_csi_bridge_s_stream,
};
static void
sun6i_csi_bridge_mbus_format_prepare(struct v4l2_mbus_framefmt *mbus_format)
{
if (!sun6i_csi_bridge_format_find(mbus_format->code))
mbus_format->code = sun6i_csi_bridge_formats[0].mbus_code;
mbus_format->field = V4L2_FIELD_NONE;
mbus_format->colorspace = V4L2_COLORSPACE_RAW;
mbus_format->quantization = V4L2_QUANTIZATION_DEFAULT;
mbus_format->xfer_func = V4L2_XFER_FUNC_DEFAULT;
}
static int sun6i_csi_bridge_init_state(struct v4l2_subdev *subdev,
struct v4l2_subdev_state *state)
{
struct sun6i_csi_device *csi_dev = v4l2_get_subdevdata(subdev);
unsigned int pad = SUN6I_CSI_BRIDGE_PAD_SINK;
struct v4l2_mbus_framefmt *mbus_format =
v4l2_subdev_state_get_format(state, pad);
struct mutex *lock = &csi_dev->bridge.lock;
mutex_lock(lock);
mbus_format->code = sun6i_csi_bridge_formats[0].mbus_code;
mbus_format->width = 1280;
mbus_format->height = 720;
sun6i_csi_bridge_mbus_format_prepare(mbus_format);
mutex_unlock(lock);
return 0;
}
static int
sun6i_csi_bridge_enum_mbus_code(struct v4l2_subdev *subdev,
struct v4l2_subdev_state *state,
struct v4l2_subdev_mbus_code_enum *code_enum)
{
if (code_enum->index >= ARRAY_SIZE(sun6i_csi_bridge_formats))
return -EINVAL;
code_enum->code = sun6i_csi_bridge_formats[code_enum->index].mbus_code;
return 0;
}
static int sun6i_csi_bridge_get_fmt(struct v4l2_subdev *subdev,
struct v4l2_subdev_state *state,
struct v4l2_subdev_format *format)
{
struct sun6i_csi_device *csi_dev = v4l2_get_subdevdata(subdev);
struct v4l2_mbus_framefmt *mbus_format = &format->format;
struct mutex *lock = &csi_dev->bridge.lock;
mutex_lock(lock);
if (format->which == V4L2_SUBDEV_FORMAT_TRY)
*mbus_format = *v4l2_subdev_state_get_format(state,
format->pad);
else
*mbus_format = csi_dev->bridge.mbus_format;
mutex_unlock(lock);
return 0;
}
static int sun6i_csi_bridge_set_fmt(struct v4l2_subdev *subdev,
struct v4l2_subdev_state *state,
struct v4l2_subdev_format *format)
{
struct sun6i_csi_device *csi_dev = v4l2_get_subdevdata(subdev);
struct v4l2_mbus_framefmt *mbus_format = &format->format;
struct mutex *lock = &csi_dev->bridge.lock;
mutex_lock(lock);
sun6i_csi_bridge_mbus_format_prepare(mbus_format);
if (format->which == V4L2_SUBDEV_FORMAT_TRY)
*v4l2_subdev_state_get_format(state, format->pad) =
*mbus_format;
else
csi_dev->bridge.mbus_format = *mbus_format;
mutex_unlock(lock);
return 0;
}
static const struct v4l2_subdev_pad_ops sun6i_csi_bridge_pad_ops = {
.enum_mbus_code = sun6i_csi_bridge_enum_mbus_code,
.get_fmt = sun6i_csi_bridge_get_fmt,
.set_fmt = sun6i_csi_bridge_set_fmt,
};
static const struct v4l2_subdev_ops sun6i_csi_bridge_subdev_ops = {
.video = &sun6i_csi_bridge_video_ops,
.pad = &sun6i_csi_bridge_pad_ops,
};
static const struct v4l2_subdev_internal_ops sun6i_csi_bridge_internal_ops = {
.init_state = sun6i_csi_bridge_init_state,
};
/* Media Entity */
static const struct media_entity_operations sun6i_csi_bridge_entity_ops = {
.link_validate = v4l2_subdev_link_validate,
};
/* V4L2 Async */
static int sun6i_csi_bridge_link(struct sun6i_csi_device *csi_dev,
int sink_pad_index,
struct v4l2_subdev *remote_subdev,
bool enabled)
{
struct device *dev = csi_dev->dev;
struct v4l2_subdev *subdev = &csi_dev->bridge.subdev;
struct media_entity *sink_entity = &subdev->entity;
struct media_entity *source_entity = &remote_subdev->entity;
int source_pad_index;
int ret;
/* Get the first remote source pad. */
ret = media_entity_get_fwnode_pad(source_entity, remote_subdev->fwnode,
MEDIA_PAD_FL_SOURCE);
if (ret < 0) {
dev_err(dev, "missing source pad in external entity %s\n",
source_entity->name);
return -EINVAL;
}
source_pad_index = ret;
dev_dbg(dev, "creating %s:%u -> %s:%u link\n", source_entity->name,
source_pad_index, sink_entity->name, sink_pad_index);
ret = media_create_pad_link(source_entity, source_pad_index,
sink_entity, sink_pad_index,
enabled ? MEDIA_LNK_FL_ENABLED : 0);
if (ret < 0) {
dev_err(dev, "failed to create %s:%u -> %s:%u link\n",
source_entity->name, source_pad_index,
sink_entity->name, sink_pad_index);
return ret;
}
return 0;
}
static int
sun6i_csi_bridge_notifier_bound(struct v4l2_async_notifier *notifier,
struct v4l2_subdev *remote_subdev,
struct v4l2_async_connection *async_subdev)
{
struct sun6i_csi_device *csi_dev =
container_of(notifier, struct sun6i_csi_device,
bridge.notifier);
struct sun6i_csi_bridge_async_subdev *bridge_async_subdev =
container_of(async_subdev, struct sun6i_csi_bridge_async_subdev,
async_subdev);
struct sun6i_csi_bridge *bridge = &csi_dev->bridge;
struct sun6i_csi_bridge_source *source = bridge_async_subdev->source;
bool enabled = false;
int ret;
switch (source->endpoint.base.port) {
case SUN6I_CSI_PORT_PARALLEL:
enabled = true;
break;
case SUN6I_CSI_PORT_MIPI_CSI2:
enabled = !bridge->source_parallel.expected;
break;
default:
return -EINVAL;
}
source->subdev = remote_subdev;
if (csi_dev->isp_available) {
/*
* Hook to the first available remote subdev to get v4l2 and
* media devices and register the capture device then.
*/
ret = sun6i_csi_isp_complete(csi_dev, remote_subdev->v4l2_dev);
if (ret)
return ret;
}
return sun6i_csi_bridge_link(csi_dev, SUN6I_CSI_BRIDGE_PAD_SINK,
remote_subdev, enabled);
}
static int
sun6i_csi_bridge_notifier_complete(struct v4l2_async_notifier *notifier)
{
struct sun6i_csi_device *csi_dev =
container_of(notifier, struct sun6i_csi_device,
bridge.notifier);
struct v4l2_device *v4l2_dev = &csi_dev->v4l2.v4l2_dev;
if (csi_dev->isp_available)
return 0;
return v4l2_device_register_subdev_nodes(v4l2_dev);
}
static const struct v4l2_async_notifier_operations
sun6i_csi_bridge_notifier_ops = {
.bound = sun6i_csi_bridge_notifier_bound,
.complete = sun6i_csi_bridge_notifier_complete,
};
/* Bridge */
static int sun6i_csi_bridge_source_setup(struct sun6i_csi_device *csi_dev,
struct sun6i_csi_bridge_source *source,
u32 port,
enum v4l2_mbus_type *bus_types)
{
struct device *dev = csi_dev->dev;
struct v4l2_async_notifier *notifier = &csi_dev->bridge.notifier;
struct v4l2_fwnode_endpoint *endpoint = &source->endpoint;
struct sun6i_csi_bridge_async_subdev *bridge_async_subdev;
struct fwnode_handle *handle;
int ret;
handle = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev), port, 0, 0);
if (!handle)
return -ENODEV;
ret = v4l2_fwnode_endpoint_parse(handle, endpoint);
if (ret)
goto complete;
if (bus_types) {
bool valid = false;
unsigned int i;
for (i = 0; bus_types[i] != V4L2_MBUS_INVALID; i++) {
if (endpoint->bus_type == bus_types[i]) {
valid = true;
break;
}
}
if (!valid) {
dev_err(dev, "unsupported bus type for port %d\n",
port);
ret = -EINVAL;
goto complete;
}
}
bridge_async_subdev =
v4l2_async_nf_add_fwnode_remote(notifier, handle,
struct
sun6i_csi_bridge_async_subdev);
if (IS_ERR(bridge_async_subdev)) {
ret = PTR_ERR(bridge_async_subdev);
goto complete;
}
bridge_async_subdev->source = source;
source->expected = true;
complete:
fwnode_handle_put(handle);
return ret;
}
int sun6i_csi_bridge_setup(struct sun6i_csi_device *csi_dev)
{
struct device *dev = csi_dev->dev;
struct sun6i_csi_bridge *bridge = &csi_dev->bridge;
struct v4l2_device *v4l2_dev = csi_dev->v4l2_dev;
struct v4l2_subdev *subdev = &bridge->subdev;
struct v4l2_async_notifier *notifier = &bridge->notifier;
struct media_pad *pads = bridge->pads;
enum v4l2_mbus_type parallel_mbus_types[] = {
V4L2_MBUS_PARALLEL,
V4L2_MBUS_BT656,
V4L2_MBUS_INVALID
};
int ret;
mutex_init(&bridge->lock);
/* V4L2 Subdev */
v4l2_subdev_init(subdev, &sun6i_csi_bridge_subdev_ops);
subdev->internal_ops = &sun6i_csi_bridge_internal_ops;
strscpy(subdev->name, SUN6I_CSI_BRIDGE_NAME, sizeof(subdev->name));
subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
subdev->owner = THIS_MODULE;
subdev->dev = dev;
v4l2_set_subdevdata(subdev, csi_dev);
/* Media Entity */
subdev->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
subdev->entity.ops = &sun6i_csi_bridge_entity_ops;
/* Media Pads */
pads[SUN6I_CSI_BRIDGE_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
pads[SUN6I_CSI_BRIDGE_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE |
MEDIA_PAD_FL_MUST_CONNECT;
ret = media_entity_pads_init(&subdev->entity,
SUN6I_CSI_BRIDGE_PAD_COUNT, pads);
if (ret < 0)
return ret;
/* V4L2 Subdev */
if (csi_dev->isp_available)
ret = v4l2_async_register_subdev(subdev);
else
ret = v4l2_device_register_subdev(v4l2_dev, subdev);
if (ret) {
dev_err(dev, "failed to register v4l2 subdev: %d\n", ret);
goto error_media_entity;
}
/* V4L2 Async */
if (csi_dev->isp_available)
v4l2_async_subdev_nf_init(notifier, subdev);
else
v4l2_async_nf_init(notifier, v4l2_dev);
notifier->ops = &sun6i_csi_bridge_notifier_ops;
sun6i_csi_bridge_source_setup(csi_dev, &bridge->source_parallel,
SUN6I_CSI_PORT_PARALLEL,
parallel_mbus_types);
sun6i_csi_bridge_source_setup(csi_dev, &bridge->source_mipi_csi2,
SUN6I_CSI_PORT_MIPI_CSI2, NULL);
ret = v4l2_async_nf_register(notifier);
if (ret) {
dev_err(dev, "failed to register v4l2 async notifier: %d\n",
ret);
goto error_v4l2_async_notifier;
}
return 0;
error_v4l2_async_notifier:
v4l2_async_nf_cleanup(notifier);
if (csi_dev->isp_available)
v4l2_async_unregister_subdev(subdev);
else
v4l2_device_unregister_subdev(subdev);
error_media_entity:
media_entity_cleanup(&subdev->entity);
return ret;
}
void sun6i_csi_bridge_cleanup(struct sun6i_csi_device *csi_dev)
{
struct v4l2_subdev *subdev = &csi_dev->bridge.subdev;
struct v4l2_async_notifier *notifier = &csi_dev->bridge.notifier;
v4l2_async_nf_unregister(notifier);
v4l2_async_nf_cleanup(notifier);
v4l2_device_unregister_subdev(subdev);
media_entity_cleanup(&subdev->entity);
}
@@ -0,0 +1,69 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright 2021-2022 Bootlin
* Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
*/
#ifndef _SUN6I_CSI_BRIDGE_H_
#define _SUN6I_CSI_BRIDGE_H_
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#define SUN6I_CSI_BRIDGE_NAME "sun6i-csi-bridge"
enum sun6i_csi_bridge_pad {
SUN6I_CSI_BRIDGE_PAD_SINK = 0,
SUN6I_CSI_BRIDGE_PAD_SOURCE = 1,
SUN6I_CSI_BRIDGE_PAD_COUNT = 2,
};
struct sun6i_csi_device;
struct sun6i_csi_bridge_format {
u32 mbus_code;
u8 input_format;
u8 input_yuv_seq;
u8 input_yuv_seq_invert;
};
struct sun6i_csi_bridge_source {
struct v4l2_subdev *subdev;
struct v4l2_fwnode_endpoint endpoint;
bool expected;
};
struct sun6i_csi_bridge_async_subdev {
struct v4l2_async_connection async_subdev;
struct sun6i_csi_bridge_source *source;
};
struct sun6i_csi_bridge {
struct v4l2_subdev subdev;
struct v4l2_async_notifier notifier;
struct media_pad pads[2];
struct v4l2_mbus_framefmt mbus_format;
struct mutex lock; /* Mbus format lock. */
struct sun6i_csi_bridge_source source_parallel;
struct sun6i_csi_bridge_source source_mipi_csi2;
};
/* Helpers */
void sun6i_csi_bridge_dimensions(struct sun6i_csi_device *csi_dev,
unsigned int *width, unsigned int *height);
void sun6i_csi_bridge_format(struct sun6i_csi_device *csi_dev,
u32 *mbus_code, u32 *field);
/* Format */
const struct sun6i_csi_bridge_format *
sun6i_csi_bridge_format_find(u32 mbus_code);
/* Bridge */
int sun6i_csi_bridge_setup(struct sun6i_csi_device *csi_dev);
void sun6i_csi_bridge_cleanup(struct sun6i_csi_device *csi_dev);
#endif
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,89 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright (c) 2011-2018 Magewell Electronics Co., Ltd. (Nanjing)
* Author: Yong Deng <yong.deng@magewell.com>
* Copyright 2021-2022 Bootlin
* Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
*/
#ifndef _SUN6I_CAPTURE_H_
#define _SUN6I_CAPTURE_H_
#include <media/v4l2-device.h>
#define SUN6I_CSI_CAPTURE_NAME "sun6i-csi-capture"
#define SUN6I_CSI_CAPTURE_WIDTH_MIN 32
#define SUN6I_CSI_CAPTURE_WIDTH_MAX 4800
#define SUN6I_CSI_CAPTURE_HEIGHT_MIN 32
#define SUN6I_CSI_CAPTURE_HEIGHT_MAX 4800
struct sun6i_csi_device;
struct sun6i_csi_capture_format {
u32 pixelformat;
u8 output_format_field;
u8 output_format_frame;
bool input_yuv_seq_invert;
bool input_format_raw;
u32 hsize_len_factor;
};
struct sun6i_csi_capture_format_match {
u32 pixelformat;
u32 mbus_code;
};
#undef current
struct sun6i_csi_capture_state {
struct list_head queue;
spinlock_t lock; /* Queue and buffers lock. */
struct sun6i_csi_buffer *pending;
struct sun6i_csi_buffer *current;
struct sun6i_csi_buffer *complete;
unsigned int sequence;
bool streaming;
bool setup;
};
struct sun6i_csi_capture {
struct sun6i_csi_capture_state state;
struct video_device video_dev;
struct vb2_queue queue;
struct mutex lock; /* Queue lock. */
struct media_pad pad;
struct v4l2_format format;
};
/* Helpers */
void sun6i_csi_capture_dimensions(struct sun6i_csi_device *csi_dev,
unsigned int *width, unsigned int *height);
void sun6i_csi_capture_format(struct sun6i_csi_device *csi_dev,
u32 *pixelformat, u32 *field);
/* Format */
const
struct sun6i_csi_capture_format *sun6i_csi_capture_format_find(u32 pixelformat);
/* Capture */
void sun6i_csi_capture_configure(struct sun6i_csi_device *csi_dev);
void sun6i_csi_capture_state_update(struct sun6i_csi_device *csi_dev);
/* State */
void sun6i_csi_capture_sync(struct sun6i_csi_device *csi_dev);
void sun6i_csi_capture_frame_done(struct sun6i_csi_device *csi_dev);
/* Capture */
int sun6i_csi_capture_setup(struct sun6i_csi_device *csi_dev);
void sun6i_csi_capture_cleanup(struct sun6i_csi_device *csi_dev);
#endif
@@ -0,0 +1,184 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright (c) 2011-2018 Magewell Electronics Co., Ltd. (Nanjing)
* Author: Yong Deng <yong.deng@magewell.com>
* Copyright 2021-2022 Bootlin
* Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
*/
#ifndef _SUN6I_CSI_REG_H_
#define _SUN6I_CSI_REG_H_
#include <linux/kernel.h>
#define SUN6I_CSI_ADDR_VALUE(a) ((a) >> 2)
#define SUN6I_CSI_EN_REG 0x0
#define SUN6I_CSI_EN_VER_EN BIT(30)
#define SUN6I_CSI_EN_PTN_CYCLE(v) (((v) << 16) & GENMASK(23, 16))
#define SUN6I_CSI_EN_SRAM_PWDN BIT(8)
#define SUN6I_CSI_EN_PTN_START BIT(4)
#define SUN6I_CSI_EN_CLK_CNT_SPL_VSYNC BIT(3)
#define SUN6I_CSI_EN_CLK_CNT_EN BIT(2)
#define SUN6I_CSI_EN_PTN_GEN_EN BIT(1)
#define SUN6I_CSI_EN_CSI_EN BIT(0)
/* Note that Allwinner manuals and code invert positive/negative definitions. */
#define SUN6I_CSI_IF_CFG_REG 0x4
#define SUN6I_CSI_IF_CFG_FIELD_DT_PCLK_SHIFT(v) (((v) << 24) & GENMASK(27, 24))
#define SUN6I_CSI_IF_CFG_SRC_TYPE_PROGRESSIVE (0 << 21)
#define SUN6I_CSI_IF_CFG_SRC_TYPE_INTERLACED (1 << 21)
#define SUN6I_CSI_IF_CFG_FPS_DS BIT(20)
#define SUN6I_CSI_IF_CFG_FIELD_POSITIVE (0 << 19)
#define SUN6I_CSI_IF_CFG_FIELD_NEGATIVE (1 << 19)
#define SUN6I_CSI_IF_CFG_VREF_POL_POSITIVE (0 << 18)
#define SUN6I_CSI_IF_CFG_VREF_POL_NEGATIVE (1 << 18)
#define SUN6I_CSI_IF_CFG_HREF_POL_POSITIVE (0 << 17)
#define SUN6I_CSI_IF_CFG_HREF_POL_NEGATIVE (1 << 17)
#define SUN6I_CSI_IF_CFG_CLK_POL_FALLING (0 << 16)
#define SUN6I_CSI_IF_CFG_CLK_POL_RISING (1 << 16)
#define SUN6I_CSI_IF_CFG_FIELD_DT_FIELD_VSYNC (0 << 14)
#define SUN6I_CSI_IF_CFG_FIELD_DT_FIELD (1 << 14)
#define SUN6I_CSI_IF_CFG_FIELD_DT_VSYNC (2 << 14)
#define SUN6I_CSI_IF_CFG_DATA_WIDTH_8 (0 << 8)
#define SUN6I_CSI_IF_CFG_DATA_WIDTH_10 (1 << 8)
#define SUN6I_CSI_IF_CFG_DATA_WIDTH_12 (2 << 8)
#define SUN6I_CSI_IF_CFG_DATA_WIDTH_8_PLUS_2 (3 << 8)
#define SUN6I_CSI_IF_CFG_DATA_WIDTH_2_TIMES_8 (4 << 8)
#define SUN6I_CSI_IF_CFG_IF_CSI (0 << 7)
#define SUN6I_CSI_IF_CFG_IF_MIPI (1 << 7)
#define SUN6I_CSI_IF_CFG_IF_CSI_YUV_RAW (0 << 0)
#define SUN6I_CSI_IF_CFG_IF_CSI_YUV_COMBINED (1 << 0)
#define SUN6I_CSI_IF_CFG_IF_CSI_BT656 (4 << 0)
#define SUN6I_CSI_IF_CFG_IF_CSI_BT1120 (5 << 0)
#define SUN6I_CSI_CAP_REG 0x8
#define SUN6I_CSI_CAP_MASK(v) (((v) << 2) & GENMASK(5, 2))
#define SUN6I_CSI_CAP_VCAP_ON BIT(1)
#define SUN6I_CSI_CAP_SCAP_ON BIT(0)
#define SUN6I_CSI_SYNC_CNT_REG 0xc
#define SUN6I_CSI_FIFO_THRS_REG 0x10
#define SUN6I_CSI_BT656_HEAD_CFG_REG 0x14
#define SUN6I_CSI_PTN_LEN_REG 0x30
#define SUN6I_CSI_PTN_ADDR_REG 0x34
#define SUN6I_CSI_VER_REG 0x3c
#define SUN6I_CSI_CH_CFG_REG 0x44
#define SUN6I_CSI_CH_CFG_PAD_VAL(v) (((v) << 24) & GENMASK(31, 24))
#define SUN6I_CSI_CH_CFG_INPUT_FMT(v) (((v) << 20) & GENMASK(23, 20))
#define SUN6I_CSI_CH_CFG_OUTPUT_FMT(v) (((v) << 16) & GENMASK(19, 16))
#define SUN6I_CSI_CH_CFG_VFLIP_EN BIT(13)
#define SUN6I_CSI_CH_CFG_HFLIP_EN BIT(12)
#define SUN6I_CSI_CH_CFG_FIELD_SEL_FIELD0 (0 << 10)
#define SUN6I_CSI_CH_CFG_FIELD_SEL_FIELD1 (1 << 10)
#define SUN6I_CSI_CH_CFG_FIELD_SEL_EITHER (2 << 10)
#define SUN6I_CSI_CH_CFG_INPUT_YUV_SEQ(v) (((v) << 8) & GENMASK(9, 8))
#define SUN6I_CSI_INPUT_FMT_RAW 0
#define SUN6I_CSI_INPUT_FMT_YUV422 3
#define SUN6I_CSI_INPUT_FMT_YUV420 4
/* Note that Allwinner manuals and code invert frame/field definitions. */
/* RAW */
#define SUN6I_CSI_OUTPUT_FMT_FRAME_RAW_8 0
#define SUN6I_CSI_OUTPUT_FMT_FRAME_RAW_10 1
#define SUN6I_CSI_OUTPUT_FMT_FRAME_RAW_12 2
#define SUN6I_CSI_OUTPUT_FMT_FRAME_RGB565 4
#define SUN6I_CSI_OUTPUT_FMT_FRAME_RGB888 5
#define SUN6I_CSI_OUTPUT_FMT_FRAME_PRGB888 6
#define SUN6I_CSI_OUTPUT_FMT_FIELD_RAW_8 8
#define SUN6I_CSI_OUTPUT_FMT_FIELD_RAW_10 9
#define SUN6I_CSI_OUTPUT_FMT_FIELD_RAW_12 10
#define SUN6I_CSI_OUTPUT_FMT_FIELD_RGB565 12
#define SUN6I_CSI_OUTPUT_FMT_FIELD_RGB888 13
#define SUN6I_CSI_OUTPUT_FMT_FIELD_PRGB888 14
/* YUV */
#define SUN6I_CSI_OUTPUT_FMT_FRAME_YUV422P 0
#define SUN6I_CSI_OUTPUT_FMT_FRAME_YUV420P 1
#define SUN6I_CSI_OUTPUT_FMT_FIELD_YUV420P 2
#define SUN6I_CSI_OUTPUT_FMT_FIELD_YUV422P 3
#define SUN6I_CSI_OUTPUT_FMT_FRAME_YUV422SP 4
#define SUN6I_CSI_OUTPUT_FMT_FRAME_YUV420SP 5
#define SUN6I_CSI_OUTPUT_FMT_FIELD_YUV420SP 6
#define SUN6I_CSI_OUTPUT_FMT_FIELD_YUV422SP 7
#define SUN6I_CSI_OUTPUT_FMT_FRAME_YUV422MB 8
#define SUN6I_CSI_OUTPUT_FMT_FRAME_YUV420MB 9
#define SUN6I_CSI_OUTPUT_FMT_FIELD_YUV420MB 10
#define SUN6I_CSI_OUTPUT_FMT_FIELD_YUV422MB 11
#define SUN6I_CSI_OUTPUT_FMT_FRAME_YUV422SP_10 12
#define SUN6I_CSI_OUTPUT_FMT_FRAME_YUV420SP_10 13
/* YUV Planar */
#define SUN6I_CSI_INPUT_YUV_SEQ_YUYV 0
#define SUN6I_CSI_INPUT_YUV_SEQ_YVYU 1
#define SUN6I_CSI_INPUT_YUV_SEQ_UYVY 2
#define SUN6I_CSI_INPUT_YUV_SEQ_VYUY 3
/* YUV Semi-planar */
#define SUN6I_CSI_INPUT_YUV_SEQ_UV 0
#define SUN6I_CSI_INPUT_YUV_SEQ_VU 1
#define SUN6I_CSI_CH_SCALE_REG 0x4c
#define SUN6I_CSI_CH_SCALE_QUART_EN BIT(0)
#define SUN6I_CSI_CH_FIFO0_ADDR_REG 0x50
#define SUN6I_CSI_CH_FIFO1_ADDR_REG 0x58
#define SUN6I_CSI_CH_FIFO2_ADDR_REG 0x60
#define SUN6I_CSI_CH_STA_REG 0x6c
#define SUN6I_CSI_CH_STA_FIELD BIT(2)
#define SUN6I_CSI_CH_STA_VCAP BIT(1)
#define SUN6I_CSI_CH_STA_SCAP BIT(0)
#define SUN6I_CSI_CH_INT_EN_REG 0x70
#define SUN6I_CSI_CH_INT_EN_VS BIT(7)
#define SUN6I_CSI_CH_INT_EN_HB_OF BIT(6)
#define SUN6I_CSI_CH_INT_EN_MUL_ERR BIT(5)
#define SUN6I_CSI_CH_INT_EN_FIFO2_OF BIT(4)
#define SUN6I_CSI_CH_INT_EN_FIFO1_OF BIT(3)
#define SUN6I_CSI_CH_INT_EN_FIFO0_OF BIT(2)
#define SUN6I_CSI_CH_INT_EN_FD BIT(1)
#define SUN6I_CSI_CH_INT_EN_CD BIT(0)
#define SUN6I_CSI_CH_INT_STA_REG 0x74
#define SUN6I_CSI_CH_INT_STA_CLEAR 0xff
#define SUN6I_CSI_CH_INT_STA_VS BIT(7)
#define SUN6I_CSI_CH_INT_STA_HB_OF BIT(6)
#define SUN6I_CSI_CH_INT_STA_MUL_ERR BIT(5)
#define SUN6I_CSI_CH_INT_STA_FIFO2_OF BIT(4)
#define SUN6I_CSI_CH_INT_STA_FIFO1_OF BIT(3)
#define SUN6I_CSI_CH_INT_STA_FIFO0_OF BIT(2)
#define SUN6I_CSI_CH_INT_STA_FD BIT(1)
#define SUN6I_CSI_CH_INT_STA_CD BIT(0)
#define SUN6I_CSI_CH_FLD1_VSIZE_REG 0x78
#define SUN6I_CSI_CH_FLD1_VSIZE_VER_LEN(v) (((v) << 16) & GENMASK(28, 16))
#define SUN6I_CSI_CH_FLD1_VSIZE_VER_START(v) ((v) & GENMASK(12, 0))
#define SUN6I_CSI_CH_HSIZE_REG 0x80
#define SUN6I_CSI_CH_HSIZE_LEN(v) (((v) << 16) & GENMASK(28, 16))
#define SUN6I_CSI_CH_HSIZE_START(v) ((v) & GENMASK(12, 0))
#define SUN6I_CSI_CH_VSIZE_REG 0x84
#define SUN6I_CSI_CH_VSIZE_LEN(v) (((v) << 16) & GENMASK(28, 16))
#define SUN6I_CSI_CH_VSIZE_START(v) ((v) & GENMASK(12, 0))
#define SUN6I_CSI_CH_BUF_LEN_REG 0x88
#define SUN6I_CSI_CH_BUF_LEN_CHROMA_LINE(v) (((v) << 16) & GENMASK(29, 16))
#define SUN6I_CSI_CH_BUF_LEN_LUMA_LINE(v) ((v) & GENMASK(13, 0))
#define SUN6I_CSI_CH_FLIP_SIZE_REG 0x8c
#define SUN6I_CSI_CH_FLIP_SIZE_VER_LEN(v) (((v) << 16) & GENMASK(28, 16))
#define SUN6I_CSI_CH_FLIP_SIZE_VALID_LEN(v) ((v) & GENMASK(12, 0))
#define SUN6I_CSI_CH_FRM_CLK_CNT_REG 0x90
#define SUN6I_CSI_CH_ACC_ITNL_CLK_CNT_REG 0x94
#define SUN6I_CSI_CH_FIFO_STAT_REG 0x98
#define SUN6I_CSI_CH_PCLK_STAT_REG 0x9c
#endif
@@ -0,0 +1,15 @@
# SPDX-License-Identifier: GPL-2.0-only
config VIDEO_SUN6I_MIPI_CSI2
tristate "Allwinner A31 MIPI CSI-2 Controller Driver"
depends on V4L_PLATFORM_DRIVERS && VIDEO_DEV
depends on ARCH_SUNXI || COMPILE_TEST
depends on PM && COMMON_CLK && RESET_CONTROLLER
depends on PHY_SUN6I_MIPI_DPHY
select MEDIA_CONTROLLER
select VIDEO_V4L2_SUBDEV_API
select V4L2_FWNODE
select GENERIC_PHY_MIPI_DPHY
select REGMAP_MMIO
help
Support for the Allwinner A31 MIPI CSI-2 controller, also found on
other platforms such as the V3/V3s.
@@ -0,0 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
sun6i-mipi-csi2-y += sun6i_mipi_csi2.o
obj-$(CONFIG_VIDEO_SUN6I_MIPI_CSI2) += sun6i-mipi-csi2.o
@@ -0,0 +1,771 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright 2020-2022 Bootlin
* Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
*/
#include <linux/clk.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/phy/phy.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/reset.h>
#include <media/mipi-csi2.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#include "sun6i_mipi_csi2.h"
#include "sun6i_mipi_csi2_reg.h"
/* Format */
static const struct sun6i_mipi_csi2_format sun6i_mipi_csi2_formats[] = {
{
.mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
.data_type = MIPI_CSI2_DT_RAW8,
.bpp = 8,
},
{
.mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8,
.data_type = MIPI_CSI2_DT_RAW8,
.bpp = 8,
},
{
.mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8,
.data_type = MIPI_CSI2_DT_RAW8,
.bpp = 8,
},
{
.mbus_code = MEDIA_BUS_FMT_SRGGB8_1X8,
.data_type = MIPI_CSI2_DT_RAW8,
.bpp = 8,
},
{
.mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10,
.data_type = MIPI_CSI2_DT_RAW10,
.bpp = 10,
},
{
.mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10,
.data_type = MIPI_CSI2_DT_RAW10,
.bpp = 10,
},
{
.mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
.data_type = MIPI_CSI2_DT_RAW10,
.bpp = 10,
},
{
.mbus_code = MEDIA_BUS_FMT_SRGGB10_1X10,
.data_type = MIPI_CSI2_DT_RAW10,
.bpp = 10,
},
};
static const struct sun6i_mipi_csi2_format *
sun6i_mipi_csi2_format_find(u32 mbus_code)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(sun6i_mipi_csi2_formats); i++)
if (sun6i_mipi_csi2_formats[i].mbus_code == mbus_code)
return &sun6i_mipi_csi2_formats[i];
return NULL;
}
/* Controller */
static void sun6i_mipi_csi2_enable(struct sun6i_mipi_csi2_device *csi2_dev)
{
struct regmap *regmap = csi2_dev->regmap;
regmap_update_bits(regmap, SUN6I_MIPI_CSI2_CTL_REG,
SUN6I_MIPI_CSI2_CTL_EN, SUN6I_MIPI_CSI2_CTL_EN);
}
static void sun6i_mipi_csi2_disable(struct sun6i_mipi_csi2_device *csi2_dev)
{
struct regmap *regmap = csi2_dev->regmap;
regmap_update_bits(regmap, SUN6I_MIPI_CSI2_CTL_REG,
SUN6I_MIPI_CSI2_CTL_EN, 0);
}
static void sun6i_mipi_csi2_configure(struct sun6i_mipi_csi2_device *csi2_dev)
{
struct regmap *regmap = csi2_dev->regmap;
unsigned int lanes_count =
csi2_dev->bridge.endpoint.bus.mipi_csi2.num_data_lanes;
struct v4l2_mbus_framefmt *mbus_format = &csi2_dev->bridge.mbus_format;
const struct sun6i_mipi_csi2_format *format;
struct device *dev = csi2_dev->dev;
u32 version = 0;
format = sun6i_mipi_csi2_format_find(mbus_format->code);
if (WARN_ON(!format))
return;
/*
* The enable flow in the Allwinner BSP is a bit different: the enable
* and reset bits are set together before starting the CSI controller.
*
* In mainline we enable the CSI controller first (due to subdev logic).
* One reliable way to make this work is to deassert reset, configure
* registers and enable the controller when everything's ready.
*
* However, setting the version enable bit and removing it afterwards
* appears necessary for capture to work reliably, while replacing it
* with a delay doesn't do the trick.
*/
regmap_write(regmap, SUN6I_MIPI_CSI2_CTL_REG,
SUN6I_MIPI_CSI2_CTL_RESET_N |
SUN6I_MIPI_CSI2_CTL_VERSION_EN |
SUN6I_MIPI_CSI2_CTL_UNPK_EN);
regmap_read(regmap, SUN6I_MIPI_CSI2_VERSION_REG, &version);
regmap_update_bits(regmap, SUN6I_MIPI_CSI2_CTL_REG,
SUN6I_MIPI_CSI2_CTL_VERSION_EN, 0);
dev_dbg(dev, "A31 MIPI CSI-2 version: %04x\n", version);
regmap_write(regmap, SUN6I_MIPI_CSI2_CFG_REG,
SUN6I_MIPI_CSI2_CFG_CHANNEL_MODE(1) |
SUN6I_MIPI_CSI2_CFG_LANE_COUNT(lanes_count));
/*
* Only a single virtual channel (index 0) is currently supported.
* While the registers do mention multiple physical channels being
* available (which can be configured to match a specific virtual
* channel or data type), it's unclear whether channels > 0 are actually
* connected and available and the reference source code only makes use
* of channel 0.
*
* Using extra channels would also require matching channels to be
* available on the CSI (and ISP) side, which is also unsure although
* some CSI implementations are said to support multiple channels for
* BT656 time-sharing.
*
* We still configure virtual channel numbers to ensure that virtual
* channel 0 only goes to channel 0.
*/
regmap_write(regmap, SUN6I_MIPI_CSI2_VCDT_RX_REG,
SUN6I_MIPI_CSI2_VCDT_RX_CH_VC(3, 3) |
SUN6I_MIPI_CSI2_VCDT_RX_CH_VC(2, 2) |
SUN6I_MIPI_CSI2_VCDT_RX_CH_VC(1, 1) |
SUN6I_MIPI_CSI2_VCDT_RX_CH_VC(0, 0) |
SUN6I_MIPI_CSI2_VCDT_RX_CH_DT(0, format->data_type));
regmap_write(regmap, SUN6I_MIPI_CSI2_CH_INT_PD_REG,
SUN6I_MIPI_CSI2_CH_INT_PD_CLEAR);
}
/* V4L2 Subdev */
static int sun6i_mipi_csi2_s_stream(struct v4l2_subdev *subdev, int on)
{
struct sun6i_mipi_csi2_device *csi2_dev = v4l2_get_subdevdata(subdev);
struct v4l2_subdev *source_subdev = csi2_dev->bridge.source_subdev;
union phy_configure_opts dphy_opts = { 0 };
struct phy_configure_opts_mipi_dphy *dphy_cfg = &dphy_opts.mipi_dphy;
struct v4l2_mbus_framefmt *mbus_format = &csi2_dev->bridge.mbus_format;
const struct sun6i_mipi_csi2_format *format;
struct phy *dphy = csi2_dev->dphy;
struct device *dev = csi2_dev->dev;
struct v4l2_ctrl *ctrl;
unsigned int lanes_count =
csi2_dev->bridge.endpoint.bus.mipi_csi2.num_data_lanes;
unsigned long pixel_rate;
int ret;
if (!source_subdev)
return -ENODEV;
if (!on) {
v4l2_subdev_call(source_subdev, video, s_stream, 0);
ret = 0;
goto disable;
}
/* Runtime PM */
ret = pm_runtime_resume_and_get(dev);
if (ret < 0)
return ret;
/* Sensor Pixel Rate */
ctrl = v4l2_ctrl_find(source_subdev->ctrl_handler, V4L2_CID_PIXEL_RATE);
if (!ctrl) {
dev_err(dev, "missing sensor pixel rate\n");
ret = -ENODEV;
goto error_pm;
}
pixel_rate = (unsigned long)v4l2_ctrl_g_ctrl_int64(ctrl);
if (!pixel_rate) {
dev_err(dev, "missing (zero) sensor pixel rate\n");
ret = -ENODEV;
goto error_pm;
}
/* D-PHY */
if (!lanes_count) {
dev_err(dev, "missing (zero) MIPI CSI-2 lanes count\n");
ret = -ENODEV;
goto error_pm;
}
format = sun6i_mipi_csi2_format_find(mbus_format->code);
if (WARN_ON(!format)) {
ret = -ENODEV;
goto error_pm;
}
phy_mipi_dphy_get_default_config(pixel_rate, format->bpp, lanes_count,
dphy_cfg);
/*
* Note that our hardware is using DDR, which is not taken in account by
* phy_mipi_dphy_get_default_config when calculating hs_clk_rate from
* the pixel rate, lanes count and bpp.
*
* The resulting clock rate is basically the symbol rate over the whole
* link. The actual clock rate is calculated with division by two since
* DDR samples both on rising and falling edges.
*/
dev_dbg(dev, "A31 MIPI CSI-2 config:\n");
dev_dbg(dev, "%ld pixels/s, %u bits/pixel, %u lanes, %lu Hz clock\n",
pixel_rate, format->bpp, lanes_count,
dphy_cfg->hs_clk_rate / 2);
ret = phy_reset(dphy);
if (ret) {
dev_err(dev, "failed to reset MIPI D-PHY\n");
goto error_pm;
}
ret = phy_configure(dphy, &dphy_opts);
if (ret) {
dev_err(dev, "failed to configure MIPI D-PHY\n");
goto error_pm;
}
/* Controller */
sun6i_mipi_csi2_configure(csi2_dev);
sun6i_mipi_csi2_enable(csi2_dev);
/* D-PHY */
ret = phy_power_on(dphy);
if (ret) {
dev_err(dev, "failed to power on MIPI D-PHY\n");
goto error_pm;
}
/* Source */
ret = v4l2_subdev_call(source_subdev, video, s_stream, 1);
if (ret && ret != -ENOIOCTLCMD)
goto disable;
return 0;
disable:
phy_power_off(dphy);
sun6i_mipi_csi2_disable(csi2_dev);
error_pm:
pm_runtime_put(dev);
return ret;
}
static const struct v4l2_subdev_video_ops sun6i_mipi_csi2_video_ops = {
.s_stream = sun6i_mipi_csi2_s_stream,
};
static void
sun6i_mipi_csi2_mbus_format_prepare(struct v4l2_mbus_framefmt *mbus_format)
{
if (!sun6i_mipi_csi2_format_find(mbus_format->code))
mbus_format->code = sun6i_mipi_csi2_formats[0].mbus_code;
mbus_format->field = V4L2_FIELD_NONE;
mbus_format->colorspace = V4L2_COLORSPACE_RAW;
mbus_format->quantization = V4L2_QUANTIZATION_DEFAULT;
mbus_format->xfer_func = V4L2_XFER_FUNC_DEFAULT;
}
static int sun6i_mipi_csi2_init_state(struct v4l2_subdev *subdev,
struct v4l2_subdev_state *state)
{
struct sun6i_mipi_csi2_device *csi2_dev = v4l2_get_subdevdata(subdev);
unsigned int pad = SUN6I_MIPI_CSI2_PAD_SINK;
struct v4l2_mbus_framefmt *mbus_format =
v4l2_subdev_state_get_format(state, pad);
struct mutex *lock = &csi2_dev->bridge.lock;
mutex_lock(lock);
mbus_format->code = sun6i_mipi_csi2_formats[0].mbus_code;
mbus_format->width = 640;
mbus_format->height = 480;
sun6i_mipi_csi2_mbus_format_prepare(mbus_format);
mutex_unlock(lock);
return 0;
}
static int
sun6i_mipi_csi2_enum_mbus_code(struct v4l2_subdev *subdev,
struct v4l2_subdev_state *state,
struct v4l2_subdev_mbus_code_enum *code_enum)
{
if (code_enum->index >= ARRAY_SIZE(sun6i_mipi_csi2_formats))
return -EINVAL;
code_enum->code = sun6i_mipi_csi2_formats[code_enum->index].mbus_code;
return 0;
}
static int sun6i_mipi_csi2_get_fmt(struct v4l2_subdev *subdev,
struct v4l2_subdev_state *state,
struct v4l2_subdev_format *format)
{
struct sun6i_mipi_csi2_device *csi2_dev = v4l2_get_subdevdata(subdev);
struct v4l2_mbus_framefmt *mbus_format = &format->format;
struct mutex *lock = &csi2_dev->bridge.lock;
mutex_lock(lock);
if (format->which == V4L2_SUBDEV_FORMAT_TRY)
*mbus_format = *v4l2_subdev_state_get_format(state,
format->pad);
else
*mbus_format = csi2_dev->bridge.mbus_format;
mutex_unlock(lock);
return 0;
}
static int sun6i_mipi_csi2_set_fmt(struct v4l2_subdev *subdev,
struct v4l2_subdev_state *state,
struct v4l2_subdev_format *format)
{
struct sun6i_mipi_csi2_device *csi2_dev = v4l2_get_subdevdata(subdev);
struct v4l2_mbus_framefmt *mbus_format = &format->format;
struct mutex *lock = &csi2_dev->bridge.lock;
mutex_lock(lock);
sun6i_mipi_csi2_mbus_format_prepare(mbus_format);
if (format->which == V4L2_SUBDEV_FORMAT_TRY)
*v4l2_subdev_state_get_format(state, format->pad) =
*mbus_format;
else
csi2_dev->bridge.mbus_format = *mbus_format;
mutex_unlock(lock);
return 0;
}
static const struct v4l2_subdev_pad_ops sun6i_mipi_csi2_pad_ops = {
.enum_mbus_code = sun6i_mipi_csi2_enum_mbus_code,
.get_fmt = sun6i_mipi_csi2_get_fmt,
.set_fmt = sun6i_mipi_csi2_set_fmt,
};
static const struct v4l2_subdev_ops sun6i_mipi_csi2_subdev_ops = {
.video = &sun6i_mipi_csi2_video_ops,
.pad = &sun6i_mipi_csi2_pad_ops,
};
static const struct v4l2_subdev_internal_ops sun6i_mipi_csi2_internal_ops = {
.init_state = sun6i_mipi_csi2_init_state,
};
/* Media Entity */
static const struct media_entity_operations sun6i_mipi_csi2_entity_ops = {
.link_validate = v4l2_subdev_link_validate,
};
/* V4L2 Async */
static int
sun6i_mipi_csi2_notifier_bound(struct v4l2_async_notifier *notifier,
struct v4l2_subdev *remote_subdev,
struct v4l2_async_connection *async_subdev)
{
struct v4l2_subdev *subdev = notifier->sd;
struct sun6i_mipi_csi2_device *csi2_dev =
container_of(notifier, struct sun6i_mipi_csi2_device,
bridge.notifier);
struct media_entity *sink_entity = &subdev->entity;
struct media_entity *source_entity = &remote_subdev->entity;
struct device *dev = csi2_dev->dev;
int sink_pad_index = 0;
int source_pad_index;
int ret;
ret = media_entity_get_fwnode_pad(source_entity, remote_subdev->fwnode,
MEDIA_PAD_FL_SOURCE);
if (ret < 0) {
dev_err(dev, "missing source pad in external entity %s\n",
source_entity->name);
return -EINVAL;
}
source_pad_index = ret;
dev_dbg(dev, "creating %s:%u -> %s:%u link\n", source_entity->name,
source_pad_index, sink_entity->name, sink_pad_index);
ret = media_create_pad_link(source_entity, source_pad_index,
sink_entity, sink_pad_index,
MEDIA_LNK_FL_ENABLED |
MEDIA_LNK_FL_IMMUTABLE);
if (ret) {
dev_err(dev, "failed to create %s:%u -> %s:%u link\n",
source_entity->name, source_pad_index,
sink_entity->name, sink_pad_index);
return ret;
}
csi2_dev->bridge.source_subdev = remote_subdev;
return 0;
}
static const struct v4l2_async_notifier_operations
sun6i_mipi_csi2_notifier_ops = {
.bound = sun6i_mipi_csi2_notifier_bound,
};
/* Bridge */
static int
sun6i_mipi_csi2_bridge_source_setup(struct sun6i_mipi_csi2_device *csi2_dev)
{
struct v4l2_async_notifier *notifier = &csi2_dev->bridge.notifier;
struct v4l2_fwnode_endpoint *endpoint = &csi2_dev->bridge.endpoint;
struct v4l2_async_connection *subdev_async;
struct fwnode_handle *handle;
struct device *dev = csi2_dev->dev;
int ret;
handle = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev), 0, 0,
FWNODE_GRAPH_ENDPOINT_NEXT);
if (!handle)
return -ENODEV;
endpoint->bus_type = V4L2_MBUS_CSI2_DPHY;
ret = v4l2_fwnode_endpoint_parse(handle, endpoint);
if (ret)
goto complete;
subdev_async =
v4l2_async_nf_add_fwnode_remote(notifier, handle,
struct v4l2_async_connection);
if (IS_ERR(subdev_async))
ret = PTR_ERR(subdev_async);
complete:
fwnode_handle_put(handle);
return ret;
}
static int sun6i_mipi_csi2_bridge_setup(struct sun6i_mipi_csi2_device *csi2_dev)
{
struct sun6i_mipi_csi2_bridge *bridge = &csi2_dev->bridge;
struct v4l2_subdev *subdev = &bridge->subdev;
struct v4l2_async_notifier *notifier = &bridge->notifier;
struct media_pad *pads = bridge->pads;
struct device *dev = csi2_dev->dev;
bool notifier_registered = false;
int ret;
mutex_init(&bridge->lock);
/* V4L2 Subdev */
v4l2_subdev_init(subdev, &sun6i_mipi_csi2_subdev_ops);
subdev->internal_ops = &sun6i_mipi_csi2_internal_ops;
strscpy(subdev->name, SUN6I_MIPI_CSI2_NAME, sizeof(subdev->name));
subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
subdev->owner = THIS_MODULE;
subdev->dev = dev;
v4l2_set_subdevdata(subdev, csi2_dev);
/* Media Entity */
subdev->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
subdev->entity.ops = &sun6i_mipi_csi2_entity_ops;
/* Media Pads */
pads[SUN6I_MIPI_CSI2_PAD_SINK].flags = MEDIA_PAD_FL_SINK |
MEDIA_PAD_FL_MUST_CONNECT;
pads[SUN6I_MIPI_CSI2_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE |
MEDIA_PAD_FL_MUST_CONNECT;
ret = media_entity_pads_init(&subdev->entity, SUN6I_MIPI_CSI2_PAD_COUNT,
pads);
if (ret)
return ret;
/* V4L2 Async */
v4l2_async_subdev_nf_init(notifier, subdev);
notifier->ops = &sun6i_mipi_csi2_notifier_ops;
ret = sun6i_mipi_csi2_bridge_source_setup(csi2_dev);
if (ret && ret != -ENODEV)
goto error_v4l2_notifier_cleanup;
/* Only register the notifier when a sensor is connected. */
if (ret != -ENODEV) {
ret = v4l2_async_nf_register(notifier);
if (ret < 0)
goto error_v4l2_notifier_cleanup;
notifier_registered = true;
}
/* V4L2 Subdev */
ret = v4l2_async_register_subdev(subdev);
if (ret < 0)
goto error_v4l2_notifier_unregister;
return 0;
error_v4l2_notifier_unregister:
if (notifier_registered)
v4l2_async_nf_unregister(notifier);
error_v4l2_notifier_cleanup:
v4l2_async_nf_cleanup(notifier);
media_entity_cleanup(&subdev->entity);
return ret;
}
static void
sun6i_mipi_csi2_bridge_cleanup(struct sun6i_mipi_csi2_device *csi2_dev)
{
struct v4l2_subdev *subdev = &csi2_dev->bridge.subdev;
struct v4l2_async_notifier *notifier = &csi2_dev->bridge.notifier;
v4l2_async_unregister_subdev(subdev);
v4l2_async_nf_unregister(notifier);
v4l2_async_nf_cleanup(notifier);
media_entity_cleanup(&subdev->entity);
}
/* Platform */
static int sun6i_mipi_csi2_suspend(struct device *dev)
{
struct sun6i_mipi_csi2_device *csi2_dev = dev_get_drvdata(dev);
clk_disable_unprepare(csi2_dev->clock_mod);
reset_control_assert(csi2_dev->reset);
return 0;
}
static int sun6i_mipi_csi2_resume(struct device *dev)
{
struct sun6i_mipi_csi2_device *csi2_dev = dev_get_drvdata(dev);
int ret;
ret = reset_control_deassert(csi2_dev->reset);
if (ret) {
dev_err(dev, "failed to deassert reset\n");
return ret;
}
ret = clk_prepare_enable(csi2_dev->clock_mod);
if (ret) {
dev_err(dev, "failed to enable module clock\n");
goto error_reset;
}
return 0;
error_reset:
reset_control_assert(csi2_dev->reset);
return ret;
}
static const struct dev_pm_ops sun6i_mipi_csi2_pm_ops = {
.runtime_suspend = sun6i_mipi_csi2_suspend,
.runtime_resume = sun6i_mipi_csi2_resume,
};
static const struct regmap_config sun6i_mipi_csi2_regmap_config = {
.reg_bits = 32,
.reg_stride = 4,
.val_bits = 32,
.max_register = 0x400,
};
static int
sun6i_mipi_csi2_resources_setup(struct sun6i_mipi_csi2_device *csi2_dev,
struct platform_device *platform_dev)
{
struct device *dev = csi2_dev->dev;
void __iomem *io_base;
int ret;
/* Registers */
io_base = devm_platform_ioremap_resource(platform_dev, 0);
if (IS_ERR(io_base))
return PTR_ERR(io_base);
csi2_dev->regmap =
devm_regmap_init_mmio_clk(dev, "bus", io_base,
&sun6i_mipi_csi2_regmap_config);
if (IS_ERR(csi2_dev->regmap)) {
dev_err(dev, "failed to init register map\n");
return PTR_ERR(csi2_dev->regmap);
}
/* Clock */
csi2_dev->clock_mod = devm_clk_get(dev, "mod");
if (IS_ERR(csi2_dev->clock_mod)) {
dev_err(dev, "failed to acquire mod clock\n");
return PTR_ERR(csi2_dev->clock_mod);
}
ret = clk_set_rate_exclusive(csi2_dev->clock_mod, 297000000);
if (ret) {
dev_err(dev, "failed to set mod clock rate\n");
return ret;
}
/* Reset */
csi2_dev->reset = devm_reset_control_get_shared(dev, NULL);
if (IS_ERR(csi2_dev->reset)) {
dev_err(dev, "failed to get reset controller\n");
ret = PTR_ERR(csi2_dev->reset);
goto error_clock_rate_exclusive;
}
/* D-PHY */
csi2_dev->dphy = devm_phy_get(dev, "dphy");
if (IS_ERR(csi2_dev->dphy)) {
dev_err(dev, "failed to get MIPI D-PHY\n");
ret = PTR_ERR(csi2_dev->dphy);
goto error_clock_rate_exclusive;
}
ret = phy_init(csi2_dev->dphy);
if (ret) {
dev_err(dev, "failed to initialize MIPI D-PHY\n");
goto error_clock_rate_exclusive;
}
/* Runtime PM */
pm_runtime_enable(dev);
return 0;
error_clock_rate_exclusive:
clk_rate_exclusive_put(csi2_dev->clock_mod);
return ret;
}
static void
sun6i_mipi_csi2_resources_cleanup(struct sun6i_mipi_csi2_device *csi2_dev)
{
pm_runtime_disable(csi2_dev->dev);
phy_exit(csi2_dev->dphy);
clk_rate_exclusive_put(csi2_dev->clock_mod);
}
static int sun6i_mipi_csi2_probe(struct platform_device *platform_dev)
{
struct sun6i_mipi_csi2_device *csi2_dev;
struct device *dev = &platform_dev->dev;
int ret;
csi2_dev = devm_kzalloc(dev, sizeof(*csi2_dev), GFP_KERNEL);
if (!csi2_dev)
return -ENOMEM;
csi2_dev->dev = dev;
platform_set_drvdata(platform_dev, csi2_dev);
ret = sun6i_mipi_csi2_resources_setup(csi2_dev, platform_dev);
if (ret)
return ret;
ret = sun6i_mipi_csi2_bridge_setup(csi2_dev);
if (ret)
goto error_resources;
return 0;
error_resources:
sun6i_mipi_csi2_resources_cleanup(csi2_dev);
return ret;
}
static void sun6i_mipi_csi2_remove(struct platform_device *platform_dev)
{
struct sun6i_mipi_csi2_device *csi2_dev =
platform_get_drvdata(platform_dev);
sun6i_mipi_csi2_bridge_cleanup(csi2_dev);
sun6i_mipi_csi2_resources_cleanup(csi2_dev);
}
static const struct of_device_id sun6i_mipi_csi2_of_match[] = {
{ .compatible = "allwinner,sun6i-a31-mipi-csi2" },
{},
};
MODULE_DEVICE_TABLE(of, sun6i_mipi_csi2_of_match);
static struct platform_driver sun6i_mipi_csi2_platform_driver = {
.probe = sun6i_mipi_csi2_probe,
.remove = sun6i_mipi_csi2_remove,
.driver = {
.name = SUN6I_MIPI_CSI2_NAME,
.of_match_table = sun6i_mipi_csi2_of_match,
.pm = &sun6i_mipi_csi2_pm_ops,
},
};
module_platform_driver(sun6i_mipi_csi2_platform_driver);
MODULE_DESCRIPTION("Allwinner A31 MIPI CSI-2 Controller Driver");
MODULE_AUTHOR("Paul Kocialkowski <paul.kocialkowski@bootlin.com>");
MODULE_LICENSE("GPL");
@@ -0,0 +1,52 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright 2020-2022 Bootlin
* Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
*/
#ifndef _SUN6I_MIPI_CSI2_H_
#define _SUN6I_MIPI_CSI2_H_
#include <linux/phy/phy.h>
#include <linux/regmap.h>
#include <linux/reset.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#define SUN6I_MIPI_CSI2_NAME "sun6i-mipi-csi2"
enum sun6i_mipi_csi2_pad {
SUN6I_MIPI_CSI2_PAD_SINK = 0,
SUN6I_MIPI_CSI2_PAD_SOURCE = 1,
SUN6I_MIPI_CSI2_PAD_COUNT = 2,
};
struct sun6i_mipi_csi2_format {
u32 mbus_code;
u8 data_type;
u32 bpp;
};
struct sun6i_mipi_csi2_bridge {
struct v4l2_subdev subdev;
struct media_pad pads[SUN6I_MIPI_CSI2_PAD_COUNT];
struct v4l2_fwnode_endpoint endpoint;
struct v4l2_async_notifier notifier;
struct v4l2_mbus_framefmt mbus_format;
struct mutex lock; /* Mbus format lock. */
struct v4l2_subdev *source_subdev;
};
struct sun6i_mipi_csi2_device {
struct device *dev;
struct regmap *regmap;
struct clk *clock_mod;
struct reset_control *reset;
struct phy *dphy;
struct sun6i_mipi_csi2_bridge bridge;
};
#endif
@@ -0,0 +1,76 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright 2020-2022 Bootlin
* Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
*/
#ifndef _SUN6I_MIPI_CSI2_REG_H_
#define _SUN6I_MIPI_CSI2_REG_H_
#define SUN6I_MIPI_CSI2_CTL_REG 0x0
#define SUN6I_MIPI_CSI2_CTL_RESET_N BIT(31)
#define SUN6I_MIPI_CSI2_CTL_VERSION_EN BIT(30)
#define SUN6I_MIPI_CSI2_CTL_UNPK_EN BIT(1)
#define SUN6I_MIPI_CSI2_CTL_EN BIT(0)
#define SUN6I_MIPI_CSI2_CFG_REG 0x4
#define SUN6I_MIPI_CSI2_CFG_CHANNEL_MODE(v) ((((v) - 1) << 8) & \
GENMASK(9, 8))
#define SUN6I_MIPI_CSI2_CFG_LANE_COUNT(v) (((v) - 1) & GENMASK(1, 0))
#define SUN6I_MIPI_CSI2_VCDT_RX_REG 0x8
#define SUN6I_MIPI_CSI2_VCDT_RX_CH_VC(ch, vc) (((vc) & GENMASK(1, 0)) << \
((ch) * 8 + 6))
#define SUN6I_MIPI_CSI2_VCDT_RX_CH_DT(ch, t) (((t) & GENMASK(5, 0)) << \
((ch) * 8))
#define SUN6I_MIPI_CSI2_RX_PKT_NUM_REG 0xc
#define SUN6I_MIPI_CSI2_VERSION_REG 0x3c
#define SUN6I_MIPI_CSI2_CH_CFG_REG 0x40
#define SUN6I_MIPI_CSI2_CH_INT_EN_REG 0x50
#define SUN6I_MIPI_CSI2_CH_INT_EN_EOT_ERR BIT(29)
#define SUN6I_MIPI_CSI2_CH_INT_EN_CHKSUM_ERR BIT(28)
#define SUN6I_MIPI_CSI2_CH_INT_EN_ECC_WRN BIT(27)
#define SUN6I_MIPI_CSI2_CH_INT_EN_ECC_ERR BIT(26)
#define SUN6I_MIPI_CSI2_CH_INT_EN_LINE_SYNC_ERR BIT(25)
#define SUN6I_MIPI_CSI2_CH_INT_EN_FRAME_SYNC_ERR BIT(24)
#define SUN6I_MIPI_CSI2_CH_INT_EN_EMB_DATA BIT(18)
#define SUN6I_MIPI_CSI2_CH_INT_EN_PF BIT(17)
#define SUN6I_MIPI_CSI2_CH_INT_EN_PH_UPDATE BIT(16)
#define SUN6I_MIPI_CSI2_CH_INT_EN_LINE_START_SYNC BIT(11)
#define SUN6I_MIPI_CSI2_CH_INT_EN_LINE_END_SYNC BIT(10)
#define SUN6I_MIPI_CSI2_CH_INT_EN_FRAME_START_SYNC BIT(9)
#define SUN6I_MIPI_CSI2_CH_INT_EN_FRAME_END_SYNC BIT(8)
#define SUN6I_MIPI_CSI2_CH_INT_EN_FIFO_OVER BIT(0)
#define SUN6I_MIPI_CSI2_CH_INT_PD_REG 0x58
#define SUN6I_MIPI_CSI2_CH_INT_PD_CLEAR 0xff
#define SUN6I_MIPI_CSI2_CH_INT_PD_EOT_ERR BIT(29)
#define SUN6I_MIPI_CSI2_CH_INT_PD_CHKSUM_ERR BIT(28)
#define SUN6I_MIPI_CSI2_CH_INT_PD_ECC_WRN BIT(27)
#define SUN6I_MIPI_CSI2_CH_INT_PD_ECC_ERR BIT(26)
#define SUN6I_MIPI_CSI2_CH_INT_PD_LINE_SYNC_ERR BIT(25)
#define SUN6I_MIPI_CSI2_CH_INT_PD_FRAME_SYNC_ERR BIT(24)
#define SUN6I_MIPI_CSI2_CH_INT_PD_EMB_DATA BIT(18)
#define SUN6I_MIPI_CSI2_CH_INT_PD_PF BIT(17)
#define SUN6I_MIPI_CSI2_CH_INT_PD_PH_UPDATE BIT(16)
#define SUN6I_MIPI_CSI2_CH_INT_PD_LINE_START_SYNC BIT(11)
#define SUN6I_MIPI_CSI2_CH_INT_PD_LINE_END_SYNC BIT(10)
#define SUN6I_MIPI_CSI2_CH_INT_PD_FRAME_START_SYNC BIT(9)
#define SUN6I_MIPI_CSI2_CH_INT_PD_FRAME_END_SYNC BIT(8)
#define SUN6I_MIPI_CSI2_CH_INT_PD_FIFO_OVER BIT(0)
#define SUN6I_MIPI_CSI2_CH_DT_TRIGGER_REG 0x60
#define SUN6I_MIPI_CSI2_CH_CUR_PH_REG 0x70
#define SUN6I_MIPI_CSI2_CH_ECC_REG 0x74
#define SUN6I_MIPI_CSI2_CH_CKS_REG 0x78
#define SUN6I_MIPI_CSI2_CH_FRAME_NUM_REG 0x7c
#define SUN6I_MIPI_CSI2_CH_LINE_NUM_REG 0x80
#define SUN6I_MIPI_CSI2_CH_OFFSET 0x100
#define SUN6I_MIPI_CSI2_CH_REG(reg, ch) \
(SUN6I_MIPI_CSI2_CH_OFFSET * (ch) + (reg))
#endif
@@ -0,0 +1,14 @@
# SPDX-License-Identifier: GPL-2.0-only
config VIDEO_SUN8I_A83T_MIPI_CSI2
tristate "Allwinner A83T MIPI CSI-2 Controller and D-PHY Driver"
depends on V4L_PLATFORM_DRIVERS && VIDEO_DEV
depends on ARCH_SUNXI || COMPILE_TEST
depends on PM && COMMON_CLK && RESET_CONTROLLER
select MEDIA_CONTROLLER
select VIDEO_V4L2_SUBDEV_API
select V4L2_FWNODE
select REGMAP_MMIO
select GENERIC_PHY
select GENERIC_PHY_MIPI_DPHY
help
Support for the Allwinner A83T MIPI CSI-2 controller and D-PHY.
@@ -0,0 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
sun8i-a83t-mipi-csi2-y += sun8i_a83t_mipi_csi2.o sun8i_a83t_dphy.o
obj-$(CONFIG_VIDEO_SUN8I_A83T_MIPI_CSI2) += sun8i-a83t-mipi-csi2.o
@@ -0,0 +1,72 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright 2020-2022 Bootlin
* Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
*/
#include <linux/phy/phy.h>
#include <linux/regmap.h>
#include "sun8i_a83t_dphy.h"
#include "sun8i_a83t_mipi_csi2.h"
static int sun8i_a83t_dphy_configure(struct phy *dphy,
union phy_configure_opts *opts)
{
return phy_mipi_dphy_config_validate(&opts->mipi_dphy);
}
static int sun8i_a83t_dphy_power_on(struct phy *dphy)
{
struct sun8i_a83t_mipi_csi2_device *csi2_dev = phy_get_drvdata(dphy);
struct regmap *regmap = csi2_dev->regmap;
regmap_write(regmap, SUN8I_A83T_DPHY_CTRL_REG,
SUN8I_A83T_DPHY_CTRL_RESET_N |
SUN8I_A83T_DPHY_CTRL_SHUTDOWN_N);
regmap_write(regmap, SUN8I_A83T_DPHY_ANA0_REG,
SUN8I_A83T_DPHY_ANA0_REXT_EN |
SUN8I_A83T_DPHY_ANA0_RINT(2) |
SUN8I_A83T_DPHY_ANA0_SNK(2));
return 0;
};
static int sun8i_a83t_dphy_power_off(struct phy *dphy)
{
struct sun8i_a83t_mipi_csi2_device *csi2_dev = phy_get_drvdata(dphy);
struct regmap *regmap = csi2_dev->regmap;
regmap_write(regmap, SUN8I_A83T_DPHY_CTRL_REG, 0);
return 0;
};
static const struct phy_ops sun8i_a83t_dphy_ops = {
.configure = sun8i_a83t_dphy_configure,
.power_on = sun8i_a83t_dphy_power_on,
.power_off = sun8i_a83t_dphy_power_off,
};
int sun8i_a83t_dphy_register(struct sun8i_a83t_mipi_csi2_device *csi2_dev)
{
struct device *dev = csi2_dev->dev;
struct phy_provider *phy_provider;
csi2_dev->dphy = devm_phy_create(dev, NULL, &sun8i_a83t_dphy_ops);
if (IS_ERR(csi2_dev->dphy)) {
dev_err(dev, "failed to create D-PHY\n");
return PTR_ERR(csi2_dev->dphy);
}
phy_set_drvdata(csi2_dev->dphy, csi2_dev);
phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
if (IS_ERR(phy_provider)) {
dev_err(dev, "failed to register D-PHY provider\n");
return PTR_ERR(phy_provider);
}
return 0;
}
@@ -0,0 +1,39 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright 2020 Kévin L'hôpital <kevin.lhopital@bootlin.com>
* Copyright 2020-2022 Bootlin
* Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
*/
#ifndef _SUN8I_A83T_DPHY_H_
#define _SUN8I_A83T_DPHY_H_
#include "sun8i_a83t_mipi_csi2.h"
#define SUN8I_A83T_DPHY_CTRL_REG 0x10
#define SUN8I_A83T_DPHY_CTRL_INIT_VALUE 0xb8df698e
#define SUN8I_A83T_DPHY_CTRL_RESET_N BIT(31)
#define SUN8I_A83T_DPHY_CTRL_SHUTDOWN_N BIT(15)
#define SUN8I_A83T_DPHY_CTRL_DEBUG BIT(8)
#define SUN8I_A83T_DPHY_STATUS_REG 0x14
#define SUN8I_A83T_DPHY_STATUS_CLK_STOP BIT(10)
#define SUN8I_A83T_DPHY_STATUS_CLK_ULPS BIT(9)
#define SUN8I_A83T_DPHY_STATUS_HSCLK BIT(8)
#define SUN8I_A83T_DPHY_STATUS_D3_STOP BIT(7)
#define SUN8I_A83T_DPHY_STATUS_D2_STOP BIT(6)
#define SUN8I_A83T_DPHY_STATUS_D1_STOP BIT(5)
#define SUN8I_A83T_DPHY_STATUS_D0_STOP BIT(4)
#define SUN8I_A83T_DPHY_STATUS_D3_ULPS BIT(3)
#define SUN8I_A83T_DPHY_STATUS_D2_ULPS BIT(2)
#define SUN8I_A83T_DPHY_STATUS_D1_ULPS BIT(1)
#define SUN8I_A83T_DPHY_STATUS_D0_ULPS BIT(0)
#define SUN8I_A83T_DPHY_ANA0_REG 0x30
#define SUN8I_A83T_DPHY_ANA0_REXT_EN BIT(31)
#define SUN8I_A83T_DPHY_ANA0_REXT BIT(30)
#define SUN8I_A83T_DPHY_ANA0_RINT(v) (((v) << 28) & GENMASK(29, 28))
#define SUN8I_A83T_DPHY_ANA0_SNK(v) (((v) << 20) & GENMASK(22, 20))
int sun8i_a83t_dphy_register(struct sun8i_a83t_mipi_csi2_device *csi2_dev);
#endif
@@ -0,0 +1,838 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright 2020 Kévin L'hôpital <kevin.lhopital@bootlin.com>
* Copyright 2020-2022 Bootlin
* Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
*/
#include <linux/clk.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/phy/phy.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/reset.h>
#include <media/mipi-csi2.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#include "sun8i_a83t_dphy.h"
#include "sun8i_a83t_mipi_csi2.h"
#include "sun8i_a83t_mipi_csi2_reg.h"
/* Format */
static const struct sun8i_a83t_mipi_csi2_format
sun8i_a83t_mipi_csi2_formats[] = {
{
.mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
.data_type = MIPI_CSI2_DT_RAW8,
.bpp = 8,
},
{
.mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8,
.data_type = MIPI_CSI2_DT_RAW8,
.bpp = 8,
},
{
.mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8,
.data_type = MIPI_CSI2_DT_RAW8,
.bpp = 8,
},
{
.mbus_code = MEDIA_BUS_FMT_SRGGB8_1X8,
.data_type = MIPI_CSI2_DT_RAW8,
.bpp = 8,
},
{
.mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10,
.data_type = MIPI_CSI2_DT_RAW10,
.bpp = 10,
},
{
.mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10,
.data_type = MIPI_CSI2_DT_RAW10,
.bpp = 10,
},
{
.mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
.data_type = MIPI_CSI2_DT_RAW10,
.bpp = 10,
},
{
.mbus_code = MEDIA_BUS_FMT_SRGGB10_1X10,
.data_type = MIPI_CSI2_DT_RAW10,
.bpp = 10,
},
};
static const struct sun8i_a83t_mipi_csi2_format *
sun8i_a83t_mipi_csi2_format_find(u32 mbus_code)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(sun8i_a83t_mipi_csi2_formats); i++)
if (sun8i_a83t_mipi_csi2_formats[i].mbus_code == mbus_code)
return &sun8i_a83t_mipi_csi2_formats[i];
return NULL;
}
/* Controller */
static void
sun8i_a83t_mipi_csi2_init(struct sun8i_a83t_mipi_csi2_device *csi2_dev)
{
struct regmap *regmap = csi2_dev->regmap;
/*
* The Allwinner BSP sets various magic values on a bunch of registers.
* This is apparently a necessary initialization process that will cause
* the capture to fail with unsolicited interrupts hitting if skipped.
*
* Most of the registers are set to proper values later, except for the
* two reserved registers. They are said to hold a "hardware lock"
* value, without more information available.
*/
regmap_write(regmap, SUN8I_A83T_MIPI_CSI2_CTRL_REG, 0);
regmap_write(regmap, SUN8I_A83T_MIPI_CSI2_CTRL_REG,
SUN8I_A83T_MIPI_CSI2_CTRL_INIT_VALUE);
regmap_write(regmap, SUN8I_A83T_MIPI_CSI2_RX_PKT_NUM_REG, 0);
regmap_write(regmap, SUN8I_A83T_MIPI_CSI2_RX_PKT_NUM_REG,
SUN8I_A83T_MIPI_CSI2_RX_PKT_NUM_INIT_VALUE);
regmap_write(regmap, SUN8I_A83T_DPHY_CTRL_REG, 0);
regmap_write(regmap, SUN8I_A83T_DPHY_CTRL_REG,
SUN8I_A83T_DPHY_CTRL_INIT_VALUE);
regmap_write(regmap, SUN8I_A83T_MIPI_CSI2_RSVD1_REG, 0);
regmap_write(regmap, SUN8I_A83T_MIPI_CSI2_RSVD1_REG,
SUN8I_A83T_MIPI_CSI2_RSVD1_HW_LOCK_VALUE);
regmap_write(regmap, SUN8I_A83T_MIPI_CSI2_RSVD2_REG, 0);
regmap_write(regmap, SUN8I_A83T_MIPI_CSI2_RSVD2_REG,
SUN8I_A83T_MIPI_CSI2_RSVD2_HW_LOCK_VALUE);
regmap_write(regmap, SUN8I_A83T_MIPI_CSI2_CFG_REG, 0);
regmap_write(regmap, SUN8I_A83T_MIPI_CSI2_CFG_REG,
SUN8I_A83T_MIPI_CSI2_CFG_INIT_VALUE);
}
static void
sun8i_a83t_mipi_csi2_enable(struct sun8i_a83t_mipi_csi2_device *csi2_dev)
{
struct regmap *regmap = csi2_dev->regmap;
regmap_update_bits(regmap, SUN8I_A83T_MIPI_CSI2_CFG_REG,
SUN8I_A83T_MIPI_CSI2_CFG_SYNC_EN,
SUN8I_A83T_MIPI_CSI2_CFG_SYNC_EN);
}
static void
sun8i_a83t_mipi_csi2_disable(struct sun8i_a83t_mipi_csi2_device *csi2_dev)
{
struct regmap *regmap = csi2_dev->regmap;
regmap_update_bits(regmap, SUN8I_A83T_MIPI_CSI2_CFG_REG,
SUN8I_A83T_MIPI_CSI2_CFG_SYNC_EN, 0);
regmap_write(regmap, SUN8I_A83T_MIPI_CSI2_CTRL_REG, 0);
}
static void
sun8i_a83t_mipi_csi2_configure(struct sun8i_a83t_mipi_csi2_device *csi2_dev)
{
struct regmap *regmap = csi2_dev->regmap;
unsigned int lanes_count =
csi2_dev->bridge.endpoint.bus.mipi_csi2.num_data_lanes;
struct v4l2_mbus_framefmt *mbus_format = &csi2_dev->bridge.mbus_format;
const struct sun8i_a83t_mipi_csi2_format *format;
struct device *dev = csi2_dev->dev;
u32 version = 0;
format = sun8i_a83t_mipi_csi2_format_find(mbus_format->code);
if (WARN_ON(!format))
return;
regmap_write(regmap, SUN8I_A83T_MIPI_CSI2_CTRL_REG,
SUN8I_A83T_MIPI_CSI2_CTRL_RESET_N);
regmap_read(regmap, SUN8I_A83T_MIPI_CSI2_VERSION_REG, &version);
dev_dbg(dev, "A83T MIPI CSI-2 version: %04x\n", version);
regmap_write(regmap, SUN8I_A83T_MIPI_CSI2_CFG_REG,
SUN8I_A83T_MIPI_CSI2_CFG_UNPKT_EN |
SUN8I_A83T_MIPI_CSI2_CFG_SYNC_DLY_CYCLE(8) |
SUN8I_A83T_MIPI_CSI2_CFG_N_CHANNEL(1) |
SUN8I_A83T_MIPI_CSI2_CFG_N_LANE(lanes_count));
/*
* Only a single virtual channel (index 0) is currently supported.
* While the registers do mention multiple physical channels being
* available (which can be configured to match a specific virtual
* channel or data type), it's unclear whether channels > 0 are actually
* connected and available and the reference source code only makes use
* of channel 0.
*
* Using extra channels would also require matching channels to be
* available on the CSI (and ISP) side, which is also unsure although
* some CSI implementations are said to support multiple channels for
* BT656 time-sharing.
*
* We still configure virtual channel numbers to ensure that virtual
* channel 0 only goes to channel 0.
*/
regmap_write(regmap, SUN8I_A83T_MIPI_CSI2_VCDT0_REG,
SUN8I_A83T_MIPI_CSI2_VCDT0_CH_VC(3, 3) |
SUN8I_A83T_MIPI_CSI2_VCDT0_CH_VC(2, 2) |
SUN8I_A83T_MIPI_CSI2_VCDT0_CH_VC(1, 1) |
SUN8I_A83T_MIPI_CSI2_VCDT0_CH_VC(0, 0) |
SUN8I_A83T_MIPI_CSI2_VCDT0_CH_DT(0, format->data_type));
}
/* V4L2 Subdev */
static int sun8i_a83t_mipi_csi2_s_stream(struct v4l2_subdev *subdev, int on)
{
struct sun8i_a83t_mipi_csi2_device *csi2_dev =
v4l2_get_subdevdata(subdev);
struct v4l2_subdev *source_subdev = csi2_dev->bridge.source_subdev;
union phy_configure_opts dphy_opts = { 0 };
struct phy_configure_opts_mipi_dphy *dphy_cfg = &dphy_opts.mipi_dphy;
struct v4l2_mbus_framefmt *mbus_format = &csi2_dev->bridge.mbus_format;
const struct sun8i_a83t_mipi_csi2_format *format;
struct phy *dphy = csi2_dev->dphy;
struct device *dev = csi2_dev->dev;
struct v4l2_ctrl *ctrl;
unsigned int lanes_count =
csi2_dev->bridge.endpoint.bus.mipi_csi2.num_data_lanes;
unsigned long pixel_rate;
int ret;
if (!source_subdev)
return -ENODEV;
if (!on) {
v4l2_subdev_call(source_subdev, video, s_stream, 0);
ret = 0;
goto disable;
}
/* Runtime PM */
ret = pm_runtime_resume_and_get(dev);
if (ret < 0)
return ret;
/* Sensor pixel rate */
ctrl = v4l2_ctrl_find(source_subdev->ctrl_handler, V4L2_CID_PIXEL_RATE);
if (!ctrl) {
dev_err(dev, "missing sensor pixel rate\n");
ret = -ENODEV;
goto error_pm;
}
pixel_rate = (unsigned long)v4l2_ctrl_g_ctrl_int64(ctrl);
if (!pixel_rate) {
dev_err(dev, "missing (zero) sensor pixel rate\n");
ret = -ENODEV;
goto error_pm;
}
/* D-PHY */
if (!lanes_count) {
dev_err(dev, "missing (zero) MIPI CSI-2 lanes count\n");
ret = -ENODEV;
goto error_pm;
}
format = sun8i_a83t_mipi_csi2_format_find(mbus_format->code);
if (WARN_ON(!format)) {
ret = -ENODEV;
goto error_pm;
}
phy_mipi_dphy_get_default_config(pixel_rate, format->bpp, lanes_count,
dphy_cfg);
/*
* Note that our hardware is using DDR, which is not taken in account by
* phy_mipi_dphy_get_default_config when calculating hs_clk_rate from
* the pixel rate, lanes count and bpp.
*
* The resulting clock rate is basically the symbol rate over the whole
* link. The actual clock rate is calculated with division by two since
* DDR samples both on rising and falling edges.
*/
dev_dbg(dev, "A83T MIPI CSI-2 config:\n");
dev_dbg(dev, "%ld pixels/s, %u bits/pixel, %u lanes, %lu Hz clock\n",
pixel_rate, format->bpp, lanes_count,
dphy_cfg->hs_clk_rate / 2);
ret = phy_reset(dphy);
if (ret) {
dev_err(dev, "failed to reset MIPI D-PHY\n");
goto error_pm;
}
ret = phy_configure(dphy, &dphy_opts);
if (ret) {
dev_err(dev, "failed to configure MIPI D-PHY\n");
goto error_pm;
}
/* Controller */
sun8i_a83t_mipi_csi2_configure(csi2_dev);
sun8i_a83t_mipi_csi2_enable(csi2_dev);
/* D-PHY */
ret = phy_power_on(dphy);
if (ret) {
dev_err(dev, "failed to power on MIPI D-PHY\n");
goto error_pm;
}
/* Source */
ret = v4l2_subdev_call(source_subdev, video, s_stream, 1);
if (ret && ret != -ENOIOCTLCMD)
goto disable;
return 0;
disable:
phy_power_off(dphy);
sun8i_a83t_mipi_csi2_disable(csi2_dev);
error_pm:
pm_runtime_put(dev);
return ret;
}
static const struct v4l2_subdev_video_ops
sun8i_a83t_mipi_csi2_video_ops = {
.s_stream = sun8i_a83t_mipi_csi2_s_stream,
};
static void
sun8i_a83t_mipi_csi2_mbus_format_prepare(struct v4l2_mbus_framefmt *mbus_format)
{
if (!sun8i_a83t_mipi_csi2_format_find(mbus_format->code))
mbus_format->code = sun8i_a83t_mipi_csi2_formats[0].mbus_code;
mbus_format->field = V4L2_FIELD_NONE;
mbus_format->colorspace = V4L2_COLORSPACE_RAW;
mbus_format->quantization = V4L2_QUANTIZATION_DEFAULT;
mbus_format->xfer_func = V4L2_XFER_FUNC_DEFAULT;
}
static int sun8i_a83t_mipi_csi2_init_state(struct v4l2_subdev *subdev,
struct v4l2_subdev_state *state)
{
struct sun8i_a83t_mipi_csi2_device *csi2_dev =
v4l2_get_subdevdata(subdev);
unsigned int pad = SUN8I_A83T_MIPI_CSI2_PAD_SINK;
struct v4l2_mbus_framefmt *mbus_format =
v4l2_subdev_state_get_format(state, pad);
struct mutex *lock = &csi2_dev->bridge.lock;
mutex_lock(lock);
mbus_format->code = sun8i_a83t_mipi_csi2_formats[0].mbus_code;
mbus_format->width = 640;
mbus_format->height = 480;
sun8i_a83t_mipi_csi2_mbus_format_prepare(mbus_format);
mutex_unlock(lock);
return 0;
}
static int
sun8i_a83t_mipi_csi2_enum_mbus_code(struct v4l2_subdev *subdev,
struct v4l2_subdev_state *state,
struct v4l2_subdev_mbus_code_enum *code_enum)
{
if (code_enum->index >= ARRAY_SIZE(sun8i_a83t_mipi_csi2_formats))
return -EINVAL;
code_enum->code =
sun8i_a83t_mipi_csi2_formats[code_enum->index].mbus_code;
return 0;
}
static int sun8i_a83t_mipi_csi2_get_fmt(struct v4l2_subdev *subdev,
struct v4l2_subdev_state *state,
struct v4l2_subdev_format *format)
{
struct sun8i_a83t_mipi_csi2_device *csi2_dev =
v4l2_get_subdevdata(subdev);
struct v4l2_mbus_framefmt *mbus_format = &format->format;
struct mutex *lock = &csi2_dev->bridge.lock;
mutex_lock(lock);
if (format->which == V4L2_SUBDEV_FORMAT_TRY)
*mbus_format = *v4l2_subdev_state_get_format(state,
format->pad);
else
*mbus_format = csi2_dev->bridge.mbus_format;
mutex_unlock(lock);
return 0;
}
static int sun8i_a83t_mipi_csi2_set_fmt(struct v4l2_subdev *subdev,
struct v4l2_subdev_state *state,
struct v4l2_subdev_format *format)
{
struct sun8i_a83t_mipi_csi2_device *csi2_dev =
v4l2_get_subdevdata(subdev);
struct v4l2_mbus_framefmt *mbus_format = &format->format;
struct mutex *lock = &csi2_dev->bridge.lock;
mutex_lock(lock);
sun8i_a83t_mipi_csi2_mbus_format_prepare(mbus_format);
if (format->which == V4L2_SUBDEV_FORMAT_TRY)
*v4l2_subdev_state_get_format(state, format->pad) =
*mbus_format;
else
csi2_dev->bridge.mbus_format = *mbus_format;
mutex_unlock(lock);
return 0;
}
static const struct v4l2_subdev_pad_ops sun8i_a83t_mipi_csi2_pad_ops = {
.enum_mbus_code = sun8i_a83t_mipi_csi2_enum_mbus_code,
.get_fmt = sun8i_a83t_mipi_csi2_get_fmt,
.set_fmt = sun8i_a83t_mipi_csi2_set_fmt,
};
static const struct v4l2_subdev_ops sun8i_a83t_mipi_csi2_subdev_ops = {
.video = &sun8i_a83t_mipi_csi2_video_ops,
.pad = &sun8i_a83t_mipi_csi2_pad_ops,
};
static const struct v4l2_subdev_internal_ops sun8i_a83t_mipi_csi2_internal_ops = {
.init_state = sun8i_a83t_mipi_csi2_init_state,
};
/* Media Entity */
static const struct media_entity_operations sun8i_a83t_mipi_csi2_entity_ops = {
.link_validate = v4l2_subdev_link_validate,
};
/* V4L2 Async */
static int
sun8i_a83t_mipi_csi2_notifier_bound(struct v4l2_async_notifier *notifier,
struct v4l2_subdev *remote_subdev,
struct v4l2_async_connection *async_subdev)
{
struct v4l2_subdev *subdev = notifier->sd;
struct sun8i_a83t_mipi_csi2_device *csi2_dev =
container_of(notifier, struct sun8i_a83t_mipi_csi2_device,
bridge.notifier);
struct media_entity *sink_entity = &subdev->entity;
struct media_entity *source_entity = &remote_subdev->entity;
struct device *dev = csi2_dev->dev;
int sink_pad_index = 0;
int source_pad_index;
int ret;
ret = media_entity_get_fwnode_pad(source_entity, remote_subdev->fwnode,
MEDIA_PAD_FL_SOURCE);
if (ret < 0) {
dev_err(dev, "missing source pad in external entity %s\n",
source_entity->name);
return -EINVAL;
}
source_pad_index = ret;
dev_dbg(dev, "creating %s:%u -> %s:%u link\n", source_entity->name,
source_pad_index, sink_entity->name, sink_pad_index);
ret = media_create_pad_link(source_entity, source_pad_index,
sink_entity, sink_pad_index,
MEDIA_LNK_FL_ENABLED |
MEDIA_LNK_FL_IMMUTABLE);
if (ret) {
dev_err(dev, "failed to create %s:%u -> %s:%u link\n",
source_entity->name, source_pad_index,
sink_entity->name, sink_pad_index);
return ret;
}
csi2_dev->bridge.source_subdev = remote_subdev;
return 0;
}
static const struct v4l2_async_notifier_operations
sun8i_a83t_mipi_csi2_notifier_ops = {
.bound = sun8i_a83t_mipi_csi2_notifier_bound,
};
/* Bridge */
static int
sun8i_a83t_mipi_csi2_bridge_source_setup(struct sun8i_a83t_mipi_csi2_device *csi2_dev)
{
struct v4l2_async_notifier *notifier = &csi2_dev->bridge.notifier;
struct v4l2_fwnode_endpoint *endpoint = &csi2_dev->bridge.endpoint;
struct v4l2_async_connection *subdev_async;
struct fwnode_handle *handle;
struct device *dev = csi2_dev->dev;
int ret;
handle = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev), 0, 0,
FWNODE_GRAPH_ENDPOINT_NEXT);
if (!handle)
return -ENODEV;
endpoint->bus_type = V4L2_MBUS_CSI2_DPHY;
ret = v4l2_fwnode_endpoint_parse(handle, endpoint);
if (ret)
goto complete;
subdev_async =
v4l2_async_nf_add_fwnode_remote(notifier, handle,
struct v4l2_async_connection);
if (IS_ERR(subdev_async))
ret = PTR_ERR(subdev_async);
complete:
fwnode_handle_put(handle);
return ret;
}
static int
sun8i_a83t_mipi_csi2_bridge_setup(struct sun8i_a83t_mipi_csi2_device *csi2_dev)
{
struct sun8i_a83t_mipi_csi2_bridge *bridge = &csi2_dev->bridge;
struct v4l2_subdev *subdev = &bridge->subdev;
struct v4l2_async_notifier *notifier = &bridge->notifier;
struct media_pad *pads = bridge->pads;
struct device *dev = csi2_dev->dev;
bool notifier_registered = false;
int ret;
mutex_init(&bridge->lock);
/* V4L2 Subdev */
v4l2_subdev_init(subdev, &sun8i_a83t_mipi_csi2_subdev_ops);
subdev->internal_ops = &sun8i_a83t_mipi_csi2_internal_ops;
strscpy(subdev->name, SUN8I_A83T_MIPI_CSI2_NAME, sizeof(subdev->name));
subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
subdev->owner = THIS_MODULE;
subdev->dev = dev;
v4l2_set_subdevdata(subdev, csi2_dev);
/* Media Entity */
subdev->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
subdev->entity.ops = &sun8i_a83t_mipi_csi2_entity_ops;
/* Media Pads */
pads[SUN8I_A83T_MIPI_CSI2_PAD_SINK].flags = MEDIA_PAD_FL_SINK |
MEDIA_PAD_FL_MUST_CONNECT;
pads[SUN8I_A83T_MIPI_CSI2_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE |
MEDIA_PAD_FL_MUST_CONNECT;
ret = media_entity_pads_init(&subdev->entity,
SUN8I_A83T_MIPI_CSI2_PAD_COUNT, pads);
if (ret)
return ret;
/* V4L2 Async */
v4l2_async_subdev_nf_init(notifier, subdev);
notifier->ops = &sun8i_a83t_mipi_csi2_notifier_ops;
ret = sun8i_a83t_mipi_csi2_bridge_source_setup(csi2_dev);
if (ret && ret != -ENODEV)
goto error_v4l2_notifier_cleanup;
/* Only register the notifier when a sensor is connected. */
if (ret != -ENODEV) {
ret = v4l2_async_nf_register(notifier);
if (ret < 0)
goto error_v4l2_notifier_cleanup;
notifier_registered = true;
}
/* V4L2 Subdev */
ret = v4l2_async_register_subdev(subdev);
if (ret < 0)
goto error_v4l2_notifier_unregister;
return 0;
error_v4l2_notifier_unregister:
if (notifier_registered)
v4l2_async_nf_unregister(notifier);
error_v4l2_notifier_cleanup:
v4l2_async_nf_cleanup(notifier);
media_entity_cleanup(&subdev->entity);
return ret;
}
static void
sun8i_a83t_mipi_csi2_bridge_cleanup(struct sun8i_a83t_mipi_csi2_device *csi2_dev)
{
struct v4l2_subdev *subdev = &csi2_dev->bridge.subdev;
struct v4l2_async_notifier *notifier = &csi2_dev->bridge.notifier;
v4l2_async_unregister_subdev(subdev);
v4l2_async_nf_unregister(notifier);
v4l2_async_nf_cleanup(notifier);
media_entity_cleanup(&subdev->entity);
}
/* Platform */
static int sun8i_a83t_mipi_csi2_suspend(struct device *dev)
{
struct sun8i_a83t_mipi_csi2_device *csi2_dev = dev_get_drvdata(dev);
clk_disable_unprepare(csi2_dev->clock_misc);
clk_disable_unprepare(csi2_dev->clock_mipi);
clk_disable_unprepare(csi2_dev->clock_mod);
reset_control_assert(csi2_dev->reset);
return 0;
}
static int sun8i_a83t_mipi_csi2_resume(struct device *dev)
{
struct sun8i_a83t_mipi_csi2_device *csi2_dev = dev_get_drvdata(dev);
int ret;
ret = reset_control_deassert(csi2_dev->reset);
if (ret) {
dev_err(dev, "failed to deassert reset\n");
return ret;
}
ret = clk_prepare_enable(csi2_dev->clock_mod);
if (ret) {
dev_err(dev, "failed to enable module clock\n");
goto error_reset;
}
ret = clk_prepare_enable(csi2_dev->clock_mipi);
if (ret) {
dev_err(dev, "failed to enable MIPI clock\n");
goto error_clock_mod;
}
ret = clk_prepare_enable(csi2_dev->clock_misc);
if (ret) {
dev_err(dev, "failed to enable CSI misc clock\n");
goto error_clock_mipi;
}
sun8i_a83t_mipi_csi2_init(csi2_dev);
return 0;
error_clock_mipi:
clk_disable_unprepare(csi2_dev->clock_mipi);
error_clock_mod:
clk_disable_unprepare(csi2_dev->clock_mod);
error_reset:
reset_control_assert(csi2_dev->reset);
return ret;
}
static const struct dev_pm_ops sun8i_a83t_mipi_csi2_pm_ops = {
.runtime_suspend = sun8i_a83t_mipi_csi2_suspend,
.runtime_resume = sun8i_a83t_mipi_csi2_resume,
};
static const struct regmap_config sun8i_a83t_mipi_csi2_regmap_config = {
.reg_bits = 32,
.reg_stride = 4,
.val_bits = 32,
.max_register = 0x120,
};
static int
sun8i_a83t_mipi_csi2_resources_setup(struct sun8i_a83t_mipi_csi2_device *csi2_dev,
struct platform_device *platform_dev)
{
struct device *dev = csi2_dev->dev;
void __iomem *io_base;
int ret;
/* Registers */
io_base = devm_platform_ioremap_resource(platform_dev, 0);
if (IS_ERR(io_base))
return PTR_ERR(io_base);
csi2_dev->regmap =
devm_regmap_init_mmio_clk(dev, "bus", io_base,
&sun8i_a83t_mipi_csi2_regmap_config);
if (IS_ERR(csi2_dev->regmap)) {
dev_err(dev, "failed to init register map\n");
return PTR_ERR(csi2_dev->regmap);
}
/* Clocks */
csi2_dev->clock_mod = devm_clk_get(dev, "mod");
if (IS_ERR(csi2_dev->clock_mod)) {
dev_err(dev, "failed to acquire mod clock\n");
return PTR_ERR(csi2_dev->clock_mod);
}
ret = clk_set_rate_exclusive(csi2_dev->clock_mod, 297000000);
if (ret) {
dev_err(dev, "failed to set mod clock rate\n");
return ret;
}
csi2_dev->clock_mipi = devm_clk_get(dev, "mipi");
if (IS_ERR(csi2_dev->clock_mipi)) {
dev_err(dev, "failed to acquire mipi clock\n");
ret = PTR_ERR(csi2_dev->clock_mipi);
goto error_clock_rate_exclusive;
}
csi2_dev->clock_misc = devm_clk_get(dev, "misc");
if (IS_ERR(csi2_dev->clock_misc)) {
dev_err(dev, "failed to acquire misc clock\n");
ret = PTR_ERR(csi2_dev->clock_misc);
goto error_clock_rate_exclusive;
}
/* Reset */
csi2_dev->reset = devm_reset_control_get_shared(dev, NULL);
if (IS_ERR(csi2_dev->reset)) {
dev_err(dev, "failed to get reset controller\n");
ret = PTR_ERR(csi2_dev->reset);
goto error_clock_rate_exclusive;
}
/* D-PHY */
ret = sun8i_a83t_dphy_register(csi2_dev);
if (ret) {
dev_err(dev, "failed to initialize MIPI D-PHY\n");
goto error_clock_rate_exclusive;
}
/* Runtime PM */
pm_runtime_enable(dev);
return 0;
error_clock_rate_exclusive:
clk_rate_exclusive_put(csi2_dev->clock_mod);
return ret;
}
static void
sun8i_a83t_mipi_csi2_resources_cleanup(struct sun8i_a83t_mipi_csi2_device *csi2_dev)
{
pm_runtime_disable(csi2_dev->dev);
phy_exit(csi2_dev->dphy);
clk_rate_exclusive_put(csi2_dev->clock_mod);
}
static int sun8i_a83t_mipi_csi2_probe(struct platform_device *platform_dev)
{
struct sun8i_a83t_mipi_csi2_device *csi2_dev;
struct device *dev = &platform_dev->dev;
int ret;
csi2_dev = devm_kzalloc(dev, sizeof(*csi2_dev), GFP_KERNEL);
if (!csi2_dev)
return -ENOMEM;
csi2_dev->dev = dev;
platform_set_drvdata(platform_dev, csi2_dev);
ret = sun8i_a83t_mipi_csi2_resources_setup(csi2_dev, platform_dev);
if (ret)
return ret;
ret = sun8i_a83t_mipi_csi2_bridge_setup(csi2_dev);
if (ret)
goto error_resources;
return 0;
error_resources:
sun8i_a83t_mipi_csi2_resources_cleanup(csi2_dev);
return ret;
}
static void sun8i_a83t_mipi_csi2_remove(struct platform_device *platform_dev)
{
struct sun8i_a83t_mipi_csi2_device *csi2_dev =
platform_get_drvdata(platform_dev);
sun8i_a83t_mipi_csi2_bridge_cleanup(csi2_dev);
sun8i_a83t_mipi_csi2_resources_cleanup(csi2_dev);
}
static const struct of_device_id sun8i_a83t_mipi_csi2_of_match[] = {
{ .compatible = "allwinner,sun8i-a83t-mipi-csi2" },
{},
};
MODULE_DEVICE_TABLE(of, sun8i_a83t_mipi_csi2_of_match);
static struct platform_driver sun8i_a83t_mipi_csi2_platform_driver = {
.probe = sun8i_a83t_mipi_csi2_probe,
.remove = sun8i_a83t_mipi_csi2_remove,
.driver = {
.name = SUN8I_A83T_MIPI_CSI2_NAME,
.of_match_table = sun8i_a83t_mipi_csi2_of_match,
.pm = &sun8i_a83t_mipi_csi2_pm_ops,
},
};
module_platform_driver(sun8i_a83t_mipi_csi2_platform_driver);
MODULE_DESCRIPTION("Allwinner A83T MIPI CSI-2 and D-PHY Controller Driver");
MODULE_AUTHOR("Paul Kocialkowski <paul.kocialkowski@bootlin.com>");
MODULE_LICENSE("GPL");
@@ -0,0 +1,55 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright 2020 Kévin L'hôpital <kevin.lhopital@bootlin.com>
* Copyright 2020-2022 Bootlin
* Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
*/
#ifndef _SUN8I_A83T_MIPI_CSI2_H_
#define _SUN8I_A83T_MIPI_CSI2_H_
#include <linux/phy/phy.h>
#include <linux/regmap.h>
#include <linux/reset.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#define SUN8I_A83T_MIPI_CSI2_NAME "sun8i-a83t-mipi-csi2"
enum sun8i_a83t_mipi_csi2_pad {
SUN8I_A83T_MIPI_CSI2_PAD_SINK = 0,
SUN8I_A83T_MIPI_CSI2_PAD_SOURCE = 1,
SUN8I_A83T_MIPI_CSI2_PAD_COUNT = 2,
};
struct sun8i_a83t_mipi_csi2_format {
u32 mbus_code;
u8 data_type;
u32 bpp;
};
struct sun8i_a83t_mipi_csi2_bridge {
struct v4l2_subdev subdev;
struct media_pad pads[SUN8I_A83T_MIPI_CSI2_PAD_COUNT];
struct v4l2_fwnode_endpoint endpoint;
struct v4l2_async_notifier notifier;
struct v4l2_mbus_framefmt mbus_format;
struct mutex lock; /* Mbus format lock. */
struct v4l2_subdev *source_subdev;
};
struct sun8i_a83t_mipi_csi2_device {
struct device *dev;
struct regmap *regmap;
struct clk *clock_mod;
struct clk *clock_mipi;
struct clk *clock_misc;
struct reset_control *reset;
struct phy *dphy;
struct sun8i_a83t_mipi_csi2_bridge bridge;
};
#endif
@@ -0,0 +1,151 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright 2020 Kévin L'hôpital <kevin.lhopital@bootlin.com>
* Copyright 2020-2022 Bootlin
* Author: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
*/
#ifndef _SUN8I_A83T_MIPI_CSI2_REG_H_
#define _SUN8I_A83T_MIPI_CSI2_REG_H_
#define SUN8I_A83T_MIPI_CSI2_VERSION_REG 0x0
#define SUN8I_A83T_MIPI_CSI2_CTRL_REG 0x4
#define SUN8I_A83T_MIPI_CSI2_CTRL_INIT_VALUE 0xb8c39bec
#define SUN8I_A83T_MIPI_CSI2_CTRL_RESET_N BIT(31)
#define SUN8I_A83T_MIPI_CSI2_RX_PKT_NUM_REG 0x8
#define SUN8I_A83T_MIPI_CSI2_RX_PKT_NUM_INIT_VALUE 0xb8d257f8
#define SUN8I_A83T_MIPI_CSI2_RSVD0_REG 0xc
#define SUN8I_A83T_MIPI_CSI2_RSVD1_REG 0x18
#define SUN8I_A83T_MIPI_CSI2_RSVD1_HW_LOCK_VALUE 0xb8c8a30c
#define SUN8I_A83T_MIPI_CSI2_RSVD2_REG 0x1c
#define SUN8I_A83T_MIPI_CSI2_RSVD2_HW_LOCK_VALUE 0xb8df8ad7
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_REG 0x20
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_ECC_ERR_DBL BIT(28)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_LINE_CKSM_ERR_VC3 BIT(27)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_LINE_CKSM_ERR_VC2 BIT(26)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_LINE_CKSM_ERR_VC1 BIT(25)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_LINE_CKSM_ERR_VC0 BIT(24)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_LINE_SEQ_ERR_DT3 BIT(23)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_LINE_SEQ_ERR_DT2 BIT(22)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_LINE_SEQ_ERR_DT1 BIT(21)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_LINE_SEQ_ERR_DT0 BIT(20)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_LS_LE_ERR_DT3 BIT(19)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_LS_LE_ERR_DT2 BIT(18)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_LS_LE_ERR_DT1 BIT(17)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_LS_LE_ERR_DT0 BIT(16)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_CRC_ERR_VC3 BIT(15)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_CRC_ERR_VC2 BIT(14)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_CRC_ERR_VC1 BIT(13)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_CRC_ERR_VC0 BIT(12)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_FRM_SEQ_ERR_VC3 BIT(11)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_FRM_SEQ_ERR_VC2 BIT(10)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_FRM_SEQ_ERR_VC1 BIT(9)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_FRM_SEQ_ERR_VC0 BIT(8)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_FS_FE_ERR_VC3 BIT(7)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_FS_FE_ERR_VC2 BIT(6)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_FS_FE_ERR_VC1 BIT(5)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_FS_FE_ERR_VC0 BIT(4)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_SOT_SYNC_ERR_3 BIT(3)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_SOT_SYNC_ERR_2 BIT(2)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_SOT_SYNC_ERR_1 BIT(1)
#define SUN8I_A83T_MIPI_CSI2_INT_STA0_SOT_SYNC_ERR_0 BIT(0)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_REG 0x24
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_LINE_SEQ_ERR_DT7 BIT(23)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_LINE_SEQ_ERR_DT6 BIT(22)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_LINE_SEQ_ERR_DT5 BIT(21)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_LINE_SEQ_ERR_DT4 BIT(20)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_LS_LE_ERR_DT7 BIT(19)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_LS_LE_ERR_DT6 BIT(18)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_LS_LE_ERR_DT5 BIT(17)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_LS_LE_ERR_DT4 BIT(16)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_DT_ERR_VC3 BIT(15)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_DT_ERR_VC2 BIT(14)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_DT_ERR_VC1 BIT(13)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_DT_ERR_VC0 BIT(12)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_ECC_ERR1_VC3 BIT(11)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_ECC_ERR1_VC2 BIT(10)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_ECC_ERR1_VC1 BIT(9)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_ECC_ERR1_VC0 BIT(8)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_SOT_ERR_3 BIT(7)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_SOT_ERR_2 BIT(6)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_SOT_ERR_1 BIT(5)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_SOT_ERR_0 BIT(4)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_ESC_ENTRY_ERR_3 BIT(3)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_ESC_ENTRY_ERR_2 BIT(2)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_ESC_ENTRY_ERR_1 BIT(1)
#define SUN8I_A83T_MIPI_CSI2_INT_STA1_ESC_ENTRY_ERR_0 BIT(0)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_REG 0x28
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_ECC_ERR_DBL BIT(28)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_CKSM_ERR_VC3 BIT(27)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_CKSM_ERR_VC2 BIT(26)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_CKSM_ERR_VC1 BIT(25)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_CKSM_ERR_VC0 BIT(24)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_LINE_SEQ_ERR_DT3 BIT(23)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_LINE_SEQ_ERR_DT2 BIT(22)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_LINE_SEQ_ERR_DT1 BIT(21)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_LINE_SEQ_ERR_DT0 BIT(20)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_LS_LE_ERR_DT3 BIT(19)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_LS_LE_ERR_DT2 BIT(18)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_LS_LE_ERR_DT1 BIT(17)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_LS_LE_ERR_DT0 BIT(16)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_CRC_ERR_VC3 BIT(15)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_CRC_ERR_VC2 BIT(14)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_CRC_ERR_VC1 BIT(13)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_CRC_ERR_VC0 BIT(12)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_FRM_SEQ_ERR_VC3 BIT(11)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_FRM_SEQ_ERR_VC2 BIT(10)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_FRM_SEQ_ERR_VC1 BIT(9)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_FRM_SEQ_ERR_VC0 BIT(8)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_FS_FE_ERR_VC3 BIT(7)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_FS_FE_ERR_VC2 BIT(6)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_FS_FE_ERR_VC1 BIT(5)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_FS_FE_ERR_VC0 BIT(4)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_SOT_SYNC_ERR_3 BIT(3)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_SOT_SYNC_ERR_2 BIT(2)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_SOT_SYNC_ERR_1 BIT(1)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK0_SOT_SYNC_ERR_0 BIT(0)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK1_REG 0x2c
#define SUN8I_A83T_MIPI_CSI2_INT_MSK1_DT_ERR_VC3 BIT(15)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK1_DT_ERR_VC2 BIT(14)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK1_DT_ERR_VC1 BIT(13)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK1_DT_ERR_VC0 BIT(12)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK1_ECC_ERR1_VC3 BIT(11)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK1_ECC_ERR1_VC2 BIT(10)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK1_ECC_ERR1_VC1 BIT(9)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK1_ECC_ERR1_VC0 BIT(8)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK1_SOT_ERR_3 BIT(7)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK1_SOT_ERR_2 BIT(6)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK1_SOT_ERR_1 BIT(5)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK1_SOT_ERR_0 BIT(4)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK1_ESC_ENTRY_ERR_3 BIT(3)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK1_ESC_ENTRY_ERR_2 BIT(2)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK1_ESC_ENTRY_ERR_1 BIT(1)
#define SUN8I_A83T_MIPI_CSI2_INT_MSK1_ESC_ENTRY_ERR_0 BIT(0)
#define SUN8I_A83T_MIPI_CSI2_CFG_REG 0x100
#define SUN8I_A83T_MIPI_CSI2_CFG_INIT_VALUE 0xb8c64f24
#define SUN8I_A83T_MIPI_CSI2_CFG_SYNC_EN BIT(31)
#define SUN8I_A83T_MIPI_CSI2_CFG_BYPASS_ECC_EN BIT(29)
#define SUN8I_A83T_MIPI_CSI2_CFG_UNPKT_EN BIT(28)
#define SUN8I_A83T_MIPI_CSI2_CFG_NONE_UNPKT_RX_MODE BIT(27)
#define SUN8I_A83T_MIPI_CSI2_CFG_YC_SWAB BIT(26)
#define SUN8I_A83T_MIPI_CSI2_CFG_N_BYTE BIT(24)
#define SUN8I_A83T_MIPI_CSI2_CFG_SYNC_DLY_CYCLE(v) (((v) << 18) & \
GENMASK(22, 18))
#define SUN8I_A83T_MIPI_CSI2_CFG_N_CHANNEL(v) ((((v) - 1) << 16) & \
GENMASK(17, 16))
#define SUN8I_A83T_MIPI_CSI2_CFG_N_LANE(v) ((((v) - 1) << 4) & \
GENMASK(5, 4))
#define SUN8I_A83T_MIPI_CSI2_VCDT0_REG 0x104
#define SUN8I_A83T_MIPI_CSI2_VCDT0_CH_VC(ch, vc) (((vc) & GENMASK(1, 0)) << \
((ch) * 8 + 6))
#define SUN8I_A83T_MIPI_CSI2_VCDT0_CH_DT(ch, t) (((t) & GENMASK(5, 0)) << \
((ch) * 8))
#define SUN8I_A83T_MIPI_CSI2_VCDT1_REG 0x108
#define SUN8I_A83T_MIPI_CSI2_VCDT1_CH_VC(ch, vc) (((vc) & GENMASK(1, 0)) << \
(((ch) - 4) * 8 + 6))
#define SUN8I_A83T_MIPI_CSI2_VCDT1_CH_DT(ch, t) (((t) & GENMASK(5, 0)) << \
(((ch) - 4) * 8))
#endif
@@ -0,0 +1,14 @@
# SPDX-License-Identifier: GPL-2.0-only
config VIDEO_SUN8I_DEINTERLACE
tristate "Allwinner Deinterlace driver"
depends on V4L_MEM2MEM_DRIVERS
depends on VIDEO_DEV
depends on ARCH_SUNXI || COMPILE_TEST
depends on COMMON_CLK && RESET_CONTROLLER && OF
depends on PM
select VIDEOBUF2_DMA_CONTIG
select V4L2_MEM2MEM_DEV
help
Support for the Allwinner deinterlace unit with scaling
capability found on some SoCs, like H3.
To compile this driver as a module choose m here.
@@ -0,0 +1,2 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_VIDEO_SUN8I_DEINTERLACE) += sun8i-di.o
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,237 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Allwinner Deinterlace driver
*
* Copyright (C) 2019 Jernej Skrabec <jernej.skrabec@siol.net>
*/
#ifndef _SUN8I_DEINTERLACE_H_
#define _SUN8I_DEINTERLACE_H_
#include <media/v4l2-device.h>
#include <media/v4l2-mem2mem.h>
#include <media/videobuf2-v4l2.h>
#include <media/videobuf2-dma-contig.h>
#include <linux/platform_device.h>
#define DEINTERLACE_NAME "sun8i-di"
#define DEINTERLACE_MOD_ENABLE 0x00
#define DEINTERLACE_MOD_ENABLE_EN BIT(0)
#define DEINTERLACE_FRM_CTRL 0x04
#define DEINTERLACE_FRM_CTRL_REG_READY BIT(0)
#define DEINTERLACE_FRM_CTRL_WB_EN BIT(2)
#define DEINTERLACE_FRM_CTRL_OUT_CTRL BIT(11)
#define DEINTERLACE_FRM_CTRL_START BIT(16)
#define DEINTERLACE_FRM_CTRL_COEF_ACCESS BIT(23)
#define DEINTERLACE_BYPASS 0x08
#define DEINTERLACE_BYPASS_CSC BIT(1)
#define DEINTERLACE_AGTH_SEL 0x0c
#define DEINTERLACE_AGTH_SEL_LINEBUF BIT(8)
#define DEINTERLACE_LINT_CTRL 0x10
#define DEINTERLACE_TRD_PRELUMA 0x1c
#define DEINTERLACE_BUF_ADDR0 0x20
#define DEINTERLACE_BUF_ADDR1 0x24
#define DEINTERLACE_BUF_ADDR2 0x28
#define DEINTERLACE_FIELD_CTRL 0x2c
#define DEINTERLACE_FIELD_CTRL_FIELD_CNT(v) ((v) & 0xff)
#define DEINTERLACE_FIELD_CTRL_FIELD_CNT_MSK (0xff)
#define DEINTERLACE_TB_OFFSET0 0x30
#define DEINTERLACE_TB_OFFSET1 0x34
#define DEINTERLACE_TB_OFFSET2 0x38
#define DEINTERLACE_TRD_PRECHROMA 0x3c
#define DEINTERLACE_LINE_STRIDE0 0x40
#define DEINTERLACE_LINE_STRIDE1 0x44
#define DEINTERLACE_LINE_STRIDE2 0x48
#define DEINTERLACE_IN_FMT 0x4c
#define DEINTERLACE_IN_FMT_PS(v) ((v) & 3)
#define DEINTERLACE_IN_FMT_FMT(v) (((v) & 7) << 4)
#define DEINTERLACE_IN_FMT_MOD(v) (((v) & 7) << 8)
#define DEINTERLACE_WB_ADDR0 0x50
#define DEINTERLACE_WB_ADDR1 0x54
#define DEINTERLACE_WB_ADDR2 0x58
#define DEINTERLACE_OUT_FMT 0x5c
#define DEINTERLACE_OUT_FMT_FMT(v) ((v) & 0xf)
#define DEINTERLACE_OUT_FMT_PS(v) (((v) & 3) << 5)
#define DEINTERLACE_INT_ENABLE 0x60
#define DEINTERLACE_INT_ENABLE_WB_EN BIT(7)
#define DEINTERLACE_INT_STATUS 0x64
#define DEINTERLACE_INT_STATUS_WRITEBACK BIT(7)
#define DEINTERLACE_STATUS 0x68
#define DEINTERLACE_STATUS_COEF_STATUS BIT(11)
#define DEINTERLACE_STATUS_WB_ERROR BIT(12)
#define DEINTERLACE_CSC_COEF 0x70 /* 12 registers */
#define DEINTERLACE_CTRL 0xa0
#define DEINTERLACE_CTRL_EN BIT(0)
#define DEINTERLACE_CTRL_FLAG_OUT_EN BIT(8)
#define DEINTERLACE_CTRL_MODE_PASSTROUGH (0 << 16)
#define DEINTERLACE_CTRL_MODE_WEAVE (1 << 16)
#define DEINTERLACE_CTRL_MODE_BOB (2 << 16)
#define DEINTERLACE_CTRL_MODE_MIXED (3 << 16)
#define DEINTERLACE_CTRL_DIAG_INTP_EN BIT(24)
#define DEINTERLACE_CTRL_TEMP_DIFF_EN BIT(25)
#define DEINTERLACE_DIAG_INTP 0xa4
#define DEINTERLACE_DIAG_INTP_TH0(v) ((v) & 0x7f)
#define DEINTERLACE_DIAG_INTP_TH0_MSK (0x7f)
#define DEINTERLACE_DIAG_INTP_TH1(v) (((v) & 0x7f) << 8)
#define DEINTERLACE_DIAG_INTP_TH1_MSK (0x7f << 8)
#define DEINTERLACE_DIAG_INTP_TH3(v) (((v) & 0xff) << 24)
#define DEINTERLACE_DIAG_INTP_TH3_MSK (0xff << 24)
#define DEINTERLACE_TEMP_DIFF 0xa8
#define DEINTERLACE_TEMP_DIFF_SAD_CENTRAL_TH(v) ((v) & 0x7f)
#define DEINTERLACE_TEMP_DIFF_SAD_CENTRAL_TH_MSK (0x7f)
#define DEINTERLACE_TEMP_DIFF_AMBIGUITY_TH(v) (((v) & 0x7f) << 8)
#define DEINTERLACE_TEMP_DIFF_AMBIGUITY_TH_MSK (0x7f << 8)
#define DEINTERLACE_TEMP_DIFF_DIRECT_DITHER_TH(v) (((v) & 0x7ff) << 16)
#define DEINTERLACE_TEMP_DIFF_DIRECT_DITHER_TH_MSK (0x7ff << 16)
#define DEINTERLACE_LUMA_TH 0xac
#define DEINTERLACE_LUMA_TH_MIN_LUMA(v) ((v) & 0xff)
#define DEINTERLACE_LUMA_TH_MIN_LUMA_MSK (0xff)
#define DEINTERLACE_LUMA_TH_MAX_LUMA(v) (((v) & 0xff) << 8)
#define DEINTERLACE_LUMA_TH_MAX_LUMA_MSK (0xff << 8)
#define DEINTERLACE_LUMA_TH_AVG_LUMA_SHIFT(v) (((v) & 0xff) << 16)
#define DEINTERLACE_LUMA_TH_AVG_LUMA_SHIFT_MSK (0xff << 16)
#define DEINTERLACE_LUMA_TH_PIXEL_STATIC(v) (((v) & 3) << 24)
#define DEINTERLACE_LUMA_TH_PIXEL_STATIC_MSK (3 << 24)
#define DEINTERLACE_SPAT_COMP 0xb0
#define DEINTERLACE_SPAT_COMP_TH2(v) ((v) & 0xff)
#define DEINTERLACE_SPAT_COMP_TH2_MSK (0xff)
#define DEINTERLACE_SPAT_COMP_TH3(v) (((v) & 0xff) << 16)
#define DEINTERLACE_SPAT_COMP_TH3_MSK (0xff << 16)
#define DEINTERLACE_CHROMA_DIFF 0xb4
#define DEINTERLACE_CHROMA_DIFF_TH(v) ((v) & 0xff)
#define DEINTERLACE_CHROMA_DIFF_TH_MSK (0xff)
#define DEINTERLACE_CHROMA_DIFF_LUMA(v) (((v) & 0x3f) << 16)
#define DEINTERLACE_CHROMA_DIFF_LUMA_MSK (0x3f << 16)
#define DEINTERLACE_CHROMA_DIFF_CHROMA(v) (((v) & 0x3f) << 24)
#define DEINTERLACE_CHROMA_DIFF_CHROMA_MSK (0x3f << 24)
#define DEINTERLACE_PRELUMA 0xb8
#define DEINTERLACE_PRECHROMA 0xbc
#define DEINTERLACE_TILE_FLAG0 0xc0
#define DEINTERLACE_TILE_FLAG1 0xc4
#define DEINTERLACE_FLAG_LINE_STRIDE 0xc8
#define DEINTERLACE_FLAG_SEQ 0xcc
#define DEINTERLACE_WB_LINE_STRIDE_CTRL 0xd0
#define DEINTERLACE_WB_LINE_STRIDE_CTRL_EN BIT(0)
#define DEINTERLACE_WB_LINE_STRIDE0 0xd4
#define DEINTERLACE_WB_LINE_STRIDE1 0xd8
#define DEINTERLACE_WB_LINE_STRIDE2 0xdc
#define DEINTERLACE_TRD_CTRL 0xe0
#define DEINTERLACE_TRD_BUF_ADDR0 0xe4
#define DEINTERLACE_TRD_BUF_ADDR1 0xe8
#define DEINTERLACE_TRD_BUF_ADDR2 0xec
#define DEINTERLACE_TRD_TB_OFF0 0xf0
#define DEINTERLACE_TRD_TB_OFF1 0xf4
#define DEINTERLACE_TRD_TB_OFF2 0xf8
#define DEINTERLACE_TRD_WB_STRIDE 0xfc
#define DEINTERLACE_CH0_IN_SIZE 0x100
#define DEINTERLACE_CH0_OUT_SIZE 0x104
#define DEINTERLACE_CH0_HORZ_FACT 0x108
#define DEINTERLACE_CH0_VERT_FACT 0x10c
#define DEINTERLACE_CH0_HORZ_PHASE 0x110
#define DEINTERLACE_CH0_VERT_PHASE0 0x114
#define DEINTERLACE_CH0_VERT_PHASE1 0x118
#define DEINTERLACE_CH0_HORZ_TAP0 0x120
#define DEINTERLACE_CH0_HORZ_TAP1 0x124
#define DEINTERLACE_CH0_VERT_TAP 0x128
#define DEINTERLACE_CH1_IN_SIZE 0x200
#define DEINTERLACE_CH1_OUT_SIZE 0x204
#define DEINTERLACE_CH1_HORZ_FACT 0x208
#define DEINTERLACE_CH1_VERT_FACT 0x20c
#define DEINTERLACE_CH1_HORZ_PHASE 0x210
#define DEINTERLACE_CH1_VERT_PHASE0 0x214
#define DEINTERLACE_CH1_VERT_PHASE1 0x218
#define DEINTERLACE_CH1_HORZ_TAP0 0x220
#define DEINTERLACE_CH1_HORZ_TAP1 0x224
#define DEINTERLACE_CH1_VERT_TAP 0x228
#define DEINTERLACE_CH0_HORZ_COEF0 0x400 /* 32 registers */
#define DEINTERLACE_CH0_HORZ_COEF1 0x480 /* 32 registers */
#define DEINTERLACE_CH0_VERT_COEF 0x500 /* 32 registers */
#define DEINTERLACE_CH1_HORZ_COEF0 0x600 /* 32 registers */
#define DEINTERLACE_CH1_HORZ_COEF1 0x680 /* 32 registers */
#define DEINTERLACE_CH1_VERT_COEF 0x700 /* 32 registers */
#define DEINTERLACE_CH3_HORZ_COEF0 0x800 /* 32 registers */
#define DEINTERLACE_CH3_HORZ_COEF1 0x880 /* 32 registers */
#define DEINTERLACE_CH3_VERT_COEF 0x900 /* 32 registers */
#define DEINTERLACE_MIN_WIDTH 2U
#define DEINTERLACE_MIN_HEIGHT 2U
#define DEINTERLACE_MAX_WIDTH 2048U
#define DEINTERLACE_MAX_HEIGHT 1100U
#define DEINTERLACE_MODE_UV_COMBINED 2
#define DEINTERLACE_IN_FMT_YUV420 2
#define DEINTERLACE_OUT_FMT_YUV420SP 13
#define DEINTERLACE_PS_UVUV 0
#define DEINTERLACE_PS_VUVU 1
#define DEINTERLACE_IDENTITY_COEF 0x4000
#define DEINTERLACE_SIZE(w, h) (((h) - 1) << 16 | ((w) - 1))
struct deinterlace_ctx {
struct v4l2_fh fh;
struct deinterlace_dev *dev;
struct v4l2_pix_format src_fmt;
struct v4l2_pix_format dst_fmt;
void *flag1_buf;
dma_addr_t flag1_buf_dma;
void *flag2_buf;
dma_addr_t flag2_buf_dma;
struct vb2_v4l2_buffer *prev;
unsigned int first_field;
unsigned int field;
int aborting;
};
struct deinterlace_dev {
struct v4l2_device v4l2_dev;
struct video_device vfd;
struct device *dev;
struct v4l2_m2m_dev *m2m_dev;
/* Device file mutex */
struct mutex dev_mutex;
void __iomem *base;
struct clk *bus_clk;
struct clk *mod_clk;
struct clk *ram_clk;
struct reset_control *rstc;
};
#endif
@@ -0,0 +1,14 @@
# SPDX-License-Identifier: GPL-2.0-only
config VIDEO_SUN8I_ROTATE
tristate "Allwinner DE2 rotation driver"
depends on V4L_MEM2MEM_DRIVERS
depends on VIDEO_DEV
depends on ARCH_SUNXI || COMPILE_TEST
depends on COMMON_CLK && RESET_CONTROLLER && OF
depends on PM
select VIDEOBUF2_DMA_CONTIG
select V4L2_MEM2MEM_DEV
help
Support for the Allwinner DE2 rotation unit.
To compile this driver as a module choose m here.
@@ -0,0 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
sun8i-rotate-y += sun8i_rotate.o
sun8i-rotate-y += sun8i_formats.o
obj-$(CONFIG_VIDEO_SUN8I_ROTATE) += sun8i-rotate.o
@@ -0,0 +1,25 @@
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (C) 2020 Jernej Skrabec <jernej.skrabec@siol.net> */
#ifndef _SUN8I_FORMATS_H_
#define _SUN8I_FORMATS_H_
#include <linux/videodev2.h>
#define ROTATE_FLAG_YUV BIT(0)
#define ROTATE_FLAG_OUTPUT BIT(1)
struct rotate_format {
u32 fourcc;
u32 hw_format;
int planes;
int bpp[3];
int hsub;
int vsub;
unsigned int flags;
};
const struct rotate_format *rotate_find_format(u32 pixelformat);
int rotate_enum_fmt(struct v4l2_fmtdesc *f, bool dst);
#endif
@@ -0,0 +1,135 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Allwinner DE2 rotation driver
*
* Copyright (C) 2020 Jernej Skrabec <jernej.skrabec@siol.net>
*/
#ifndef _SUN8I_ROTATE_H_
#define _SUN8I_ROTATE_H_
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-mem2mem.h>
#include <media/videobuf2-v4l2.h>
#include <media/videobuf2-dma-contig.h>
#include <linux/platform_device.h>
#define ROTATE_NAME "sun8i-rotate"
#define ROTATE_GLB_CTL 0x00
#define ROTATE_GLB_CTL_START BIT(31)
#define ROTATE_GLB_CTL_RESET BIT(30)
#define ROTATE_GLB_CTL_BURST_LEN(x) ((x) << 16)
#define ROTATE_GLB_CTL_HFLIP BIT(7)
#define ROTATE_GLB_CTL_VFLIP BIT(6)
#define ROTATE_GLB_CTL_ROTATION(x) ((x) << 4)
#define ROTATE_GLB_CTL_MODE(x) ((x) << 0)
#define ROTATE_INT 0x04
#define ROTATE_INT_FINISH_IRQ_EN BIT(16)
#define ROTATE_INT_FINISH_IRQ BIT(0)
#define ROTATE_IN_FMT 0x20
#define ROTATE_IN_FMT_FORMAT(x) ((x) << 0)
#define ROTATE_IN_SIZE 0x24
#define ROTATE_IN_PITCH0 0x30
#define ROTATE_IN_PITCH1 0x34
#define ROTATE_IN_PITCH2 0x38
#define ROTATE_IN_ADDRL0 0x40
#define ROTATE_IN_ADDRH0 0x44
#define ROTATE_IN_ADDRL1 0x48
#define ROTATE_IN_ADDRH1 0x4c
#define ROTATE_IN_ADDRL2 0x50
#define ROTATE_IN_ADDRH2 0x54
#define ROTATE_OUT_SIZE 0x84
#define ROTATE_OUT_PITCH0 0x90
#define ROTATE_OUT_PITCH1 0x94
#define ROTATE_OUT_PITCH2 0x98
#define ROTATE_OUT_ADDRL0 0xA0
#define ROTATE_OUT_ADDRH0 0xA4
#define ROTATE_OUT_ADDRL1 0xA8
#define ROTATE_OUT_ADDRH1 0xAC
#define ROTATE_OUT_ADDRL2 0xB0
#define ROTATE_OUT_ADDRH2 0xB4
#define ROTATE_BURST_8 0x07
#define ROTATE_BURST_16 0x0f
#define ROTATE_BURST_32 0x1f
#define ROTATE_BURST_64 0x3f
#define ROTATE_MODE_COPY_ROTATE 0x01
#define ROTATE_FORMAT_ARGB32 0x00
#define ROTATE_FORMAT_ABGR32 0x01
#define ROTATE_FORMAT_RGBA32 0x02
#define ROTATE_FORMAT_BGRA32 0x03
#define ROTATE_FORMAT_XRGB32 0x04
#define ROTATE_FORMAT_XBGR32 0x05
#define ROTATE_FORMAT_RGBX32 0x06
#define ROTATE_FORMAT_BGRX32 0x07
#define ROTATE_FORMAT_RGB24 0x08
#define ROTATE_FORMAT_BGR24 0x09
#define ROTATE_FORMAT_RGB565 0x0a
#define ROTATE_FORMAT_BGR565 0x0b
#define ROTATE_FORMAT_ARGB4444 0x0c
#define ROTATE_FORMAT_ABGR4444 0x0d
#define ROTATE_FORMAT_RGBA4444 0x0e
#define ROTATE_FORMAT_BGRA4444 0x0f
#define ROTATE_FORMAT_ARGB1555 0x10
#define ROTATE_FORMAT_ABGR1555 0x11
#define ROTATE_FORMAT_RGBA5551 0x12
#define ROTATE_FORMAT_BGRA5551 0x13
#define ROTATE_FORMAT_YUYV 0x20
#define ROTATE_FORMAT_UYVY 0x21
#define ROTATE_FORMAT_YVYU 0x22
#define ROTATE_FORMAT_VYUV 0x23
#define ROTATE_FORMAT_NV61 0x24
#define ROTATE_FORMAT_NV16 0x25
#define ROTATE_FORMAT_YUV422P 0x26
#define ROTATE_FORMAT_NV21 0x28
#define ROTATE_FORMAT_NV12 0x29
#define ROTATE_FORMAT_YUV420P 0x2A
#define ROTATE_SIZE(w, h) (((h) - 1) << 16 | ((w) - 1))
#define ROTATE_MIN_WIDTH 8U
#define ROTATE_MIN_HEIGHT 8U
#define ROTATE_MAX_WIDTH 4096U
#define ROTATE_MAX_HEIGHT 4096U
struct rotate_ctx {
struct v4l2_fh fh;
struct rotate_dev *dev;
struct v4l2_pix_format src_fmt;
struct v4l2_pix_format dst_fmt;
struct v4l2_ctrl_handler ctrl_handler;
u32 hflip;
u32 vflip;
u32 rotate;
};
struct rotate_dev {
struct v4l2_device v4l2_dev;
struct video_device vfd;
struct device *dev;
struct v4l2_m2m_dev *m2m_dev;
/* Device file mutex */
struct mutex dev_mutex;
void __iomem *base;
struct clk *bus_clk;
struct clk *mod_clk;
struct reset_control *rstc;
};
#endif
@@ -0,0 +1,273 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2020 Jernej Skrabec <jernej.skrabec@siol.net> */
#include "sun8i-formats.h"
#include "sun8i-rotate.h"
/*
* Formats not included in array:
* ROTATE_FORMAT_BGR565
* ROTATE_FORMAT_VYUV
*/
static const struct rotate_format rotate_formats[] = {
{
.fourcc = V4L2_PIX_FMT_ARGB32,
.hw_format = ROTATE_FORMAT_ARGB32,
.planes = 1,
.bpp = { 4, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_ABGR32,
.hw_format = ROTATE_FORMAT_ABGR32,
.planes = 1,
.bpp = { 4, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_RGBA32,
.hw_format = ROTATE_FORMAT_RGBA32,
.planes = 1,
.bpp = { 4, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_BGRA32,
.hw_format = ROTATE_FORMAT_BGRA32,
.planes = 1,
.bpp = { 4, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_XRGB32,
.hw_format = ROTATE_FORMAT_XRGB32,
.planes = 1,
.bpp = { 4, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_XBGR32,
.hw_format = ROTATE_FORMAT_XBGR32,
.planes = 1,
.bpp = { 4, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_RGB32,
.hw_format = ROTATE_FORMAT_RGBX32,
.planes = 1,
.bpp = { 4, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_BGR32,
.hw_format = ROTATE_FORMAT_BGRX32,
.planes = 1,
.bpp = { 4, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_RGB24,
.hw_format = ROTATE_FORMAT_RGB24,
.planes = 1,
.bpp = { 3, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_BGR24,
.hw_format = ROTATE_FORMAT_BGR24,
.planes = 1,
.bpp = { 3, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_RGB565,
.hw_format = ROTATE_FORMAT_RGB565,
.planes = 1,
.bpp = { 2, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_ARGB444,
.hw_format = ROTATE_FORMAT_ARGB4444,
.planes = 1,
.bpp = { 2, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_ABGR444,
.hw_format = ROTATE_FORMAT_ABGR4444,
.planes = 1,
.bpp = { 2, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_RGBA444,
.hw_format = ROTATE_FORMAT_RGBA4444,
.planes = 1,
.bpp = { 2, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_BGRA444,
.hw_format = ROTATE_FORMAT_BGRA4444,
.planes = 1,
.bpp = { 2, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_ARGB555,
.hw_format = ROTATE_FORMAT_ARGB1555,
.planes = 1,
.bpp = { 2, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_ABGR555,
.hw_format = ROTATE_FORMAT_ABGR1555,
.planes = 1,
.bpp = { 2, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_RGBA555,
.hw_format = ROTATE_FORMAT_RGBA5551,
.planes = 1,
.bpp = { 2, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_BGRA555,
.hw_format = ROTATE_FORMAT_BGRA5551,
.planes = 1,
.bpp = { 2, 0, 0 },
.hsub = 1,
.vsub = 1,
.flags = ROTATE_FLAG_OUTPUT
}, {
.fourcc = V4L2_PIX_FMT_YVYU,
.hw_format = ROTATE_FORMAT_YVYU,
.planes = 1,
.bpp = { 2, 0, 0 },
.hsub = 2,
.vsub = 1,
.flags = ROTATE_FLAG_YUV
}, {
.fourcc = V4L2_PIX_FMT_UYVY,
.hw_format = ROTATE_FORMAT_UYVY,
.planes = 1,
.bpp = { 2, 0, 0 },
.hsub = 2,
.vsub = 1,
.flags = ROTATE_FLAG_YUV
}, {
.fourcc = V4L2_PIX_FMT_YUYV,
.hw_format = ROTATE_FORMAT_YUYV,
.planes = 1,
.bpp = { 2, 0, 0 },
.hsub = 2,
.vsub = 1,
.flags = ROTATE_FLAG_YUV
}, {
.fourcc = V4L2_PIX_FMT_NV61,
.hw_format = ROTATE_FORMAT_NV61,
.planes = 2,
.bpp = { 1, 2, 0 },
.hsub = 2,
.vsub = 1,
.flags = ROTATE_FLAG_YUV
}, {
.fourcc = V4L2_PIX_FMT_NV16,
.hw_format = ROTATE_FORMAT_NV16,
.planes = 2,
.bpp = { 1, 2, 0 },
.hsub = 2,
.vsub = 1,
.flags = ROTATE_FLAG_YUV
}, {
.fourcc = V4L2_PIX_FMT_YUV422P,
.hw_format = ROTATE_FORMAT_YUV422P,
.planes = 3,
.bpp = { 1, 1, 1 },
.hsub = 2,
.vsub = 1,
.flags = ROTATE_FLAG_YUV
}, {
.fourcc = V4L2_PIX_FMT_NV21,
.hw_format = ROTATE_FORMAT_NV21,
.planes = 2,
.bpp = { 1, 2, 0 },
.hsub = 2,
.vsub = 2,
.flags = ROTATE_FLAG_YUV
}, {
.fourcc = V4L2_PIX_FMT_NV12,
.hw_format = ROTATE_FORMAT_NV12,
.planes = 2,
.bpp = { 1, 2, 0 },
.hsub = 2,
.vsub = 2,
.flags = ROTATE_FLAG_YUV
}, {
.fourcc = V4L2_PIX_FMT_YUV420,
.hw_format = ROTATE_FORMAT_YUV420P,
.planes = 3,
.bpp = { 1, 1, 1 },
.hsub = 2,
.vsub = 2,
.flags = ROTATE_FLAG_YUV | ROTATE_FLAG_OUTPUT
},
};
const struct rotate_format *rotate_find_format(u32 pixelformat)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(rotate_formats); i++)
if (rotate_formats[i].fourcc == pixelformat)
return &rotate_formats[i];
return NULL;
}
int rotate_enum_fmt(struct v4l2_fmtdesc *f, bool dst)
{
int i, index;
index = 0;
for (i = 0; i < ARRAY_SIZE(rotate_formats); i++) {
/* not all formats can be used for capture buffers */
if (dst && !(rotate_formats[i].flags & ROTATE_FLAG_OUTPUT))
continue;
if (index == f->index) {
f->pixelformat = rotate_formats[i].fourcc;
return 0;
}
index++;
}
return -EINVAL;
}
@@ -0,0 +1,914 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Allwinner sun8i DE2 rotation driver
*
* Copyright (C) 2020 Jernej Skrabec <jernej.skrabec@siol.net>
*/
#include <linux/clk.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/iopoll.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/reset.h>
#include <media/v4l2-device.h>
#include <media/v4l2-event.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-mem2mem.h>
#include "sun8i-formats.h"
#include "sun8i-rotate.h"
static inline u32 rotate_read(struct rotate_dev *dev, u32 reg)
{
return readl(dev->base + reg);
}
static inline void rotate_write(struct rotate_dev *dev, u32 reg, u32 value)
{
writel(value, dev->base + reg);
}
static inline void rotate_set_bits(struct rotate_dev *dev, u32 reg, u32 bits)
{
writel(readl(dev->base + reg) | bits, dev->base + reg);
}
static void rotate_calc_addr_pitch(dma_addr_t buffer,
u32 bytesperline, u32 height,
const struct rotate_format *fmt,
dma_addr_t *addr, u32 *pitch)
{
u32 size;
int i;
for (i = 0; i < fmt->planes; i++) {
pitch[i] = bytesperline;
addr[i] = buffer;
if (i > 0)
pitch[i] /= fmt->hsub / fmt->bpp[i];
size = pitch[i] * height;
if (i > 0)
size /= fmt->vsub;
buffer += size;
}
}
static void rotate_device_run(void *priv)
{
struct rotate_ctx *ctx = priv;
struct rotate_dev *dev = ctx->dev;
struct vb2_v4l2_buffer *src, *dst;
const struct rotate_format *fmt;
dma_addr_t addr[3];
u32 val, pitch[3];
src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
v4l2_m2m_buf_copy_metadata(src, dst);
val = ROTATE_GLB_CTL_MODE(ROTATE_MODE_COPY_ROTATE);
if (ctx->hflip)
val |= ROTATE_GLB_CTL_HFLIP;
if (ctx->vflip)
val |= ROTATE_GLB_CTL_VFLIP;
val |= ROTATE_GLB_CTL_ROTATION(ctx->rotate / 90);
if (ctx->rotate != 90 && ctx->rotate != 270)
val |= ROTATE_GLB_CTL_BURST_LEN(ROTATE_BURST_64);
else
val |= ROTATE_GLB_CTL_BURST_LEN(ROTATE_BURST_8);
rotate_write(dev, ROTATE_GLB_CTL, val);
fmt = rotate_find_format(ctx->src_fmt.pixelformat);
if (!fmt)
return;
rotate_write(dev, ROTATE_IN_FMT, ROTATE_IN_FMT_FORMAT(fmt->hw_format));
rotate_calc_addr_pitch(vb2_dma_contig_plane_dma_addr(&src->vb2_buf, 0),
ctx->src_fmt.bytesperline, ctx->src_fmt.height,
fmt, addr, pitch);
rotate_write(dev, ROTATE_IN_SIZE,
ROTATE_SIZE(ctx->src_fmt.width, ctx->src_fmt.height));
rotate_write(dev, ROTATE_IN_PITCH0, pitch[0]);
rotate_write(dev, ROTATE_IN_PITCH1, pitch[1]);
rotate_write(dev, ROTATE_IN_PITCH2, pitch[2]);
rotate_write(dev, ROTATE_IN_ADDRL0, addr[0]);
rotate_write(dev, ROTATE_IN_ADDRL1, addr[1]);
rotate_write(dev, ROTATE_IN_ADDRL2, addr[2]);
rotate_write(dev, ROTATE_IN_ADDRH0, 0);
rotate_write(dev, ROTATE_IN_ADDRH1, 0);
rotate_write(dev, ROTATE_IN_ADDRH2, 0);
fmt = rotate_find_format(ctx->dst_fmt.pixelformat);
if (!fmt)
return;
rotate_calc_addr_pitch(vb2_dma_contig_plane_dma_addr(&dst->vb2_buf, 0),
ctx->dst_fmt.bytesperline, ctx->dst_fmt.height,
fmt, addr, pitch);
rotate_write(dev, ROTATE_OUT_SIZE,
ROTATE_SIZE(ctx->dst_fmt.width, ctx->dst_fmt.height));
rotate_write(dev, ROTATE_OUT_PITCH0, pitch[0]);
rotate_write(dev, ROTATE_OUT_PITCH1, pitch[1]);
rotate_write(dev, ROTATE_OUT_PITCH2, pitch[2]);
rotate_write(dev, ROTATE_OUT_ADDRL0, addr[0]);
rotate_write(dev, ROTATE_OUT_ADDRL1, addr[1]);
rotate_write(dev, ROTATE_OUT_ADDRL2, addr[2]);
rotate_write(dev, ROTATE_OUT_ADDRH0, 0);
rotate_write(dev, ROTATE_OUT_ADDRH1, 0);
rotate_write(dev, ROTATE_OUT_ADDRH2, 0);
rotate_set_bits(dev, ROTATE_INT, ROTATE_INT_FINISH_IRQ_EN);
rotate_set_bits(dev, ROTATE_GLB_CTL, ROTATE_GLB_CTL_START);
}
static irqreturn_t rotate_irq(int irq, void *data)
{
struct vb2_v4l2_buffer *buffer;
struct rotate_dev *dev = data;
struct rotate_ctx *ctx;
unsigned int val;
ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
if (!ctx) {
v4l2_err(&dev->v4l2_dev,
"Instance released before the end of transaction\n");
return IRQ_NONE;
}
val = rotate_read(dev, ROTATE_INT);
if (!(val & ROTATE_INT_FINISH_IRQ))
return IRQ_NONE;
/* clear flag and disable irq */
rotate_write(dev, ROTATE_INT, ROTATE_INT_FINISH_IRQ);
buffer = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
v4l2_m2m_buf_done(buffer, VB2_BUF_STATE_DONE);
buffer = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
v4l2_m2m_buf_done(buffer, VB2_BUF_STATE_DONE);
v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
return IRQ_HANDLED;
}
static inline struct rotate_ctx *rotate_file2ctx(struct file *file)
{
return container_of(file_to_v4l2_fh(file), struct rotate_ctx, fh);
}
static void rotate_prepare_format(struct v4l2_pix_format *pix_fmt)
{
unsigned int height, width, alignment, sizeimage, size, bpl;
const struct rotate_format *fmt;
int i;
fmt = rotate_find_format(pix_fmt->pixelformat);
if (!fmt)
return;
width = ALIGN(pix_fmt->width, fmt->hsub);
height = ALIGN(pix_fmt->height, fmt->vsub);
/* all pitches have to be 16 byte aligned */
alignment = 16;
if (fmt->planes > 1)
alignment *= fmt->hsub / fmt->bpp[1];
bpl = ALIGN(width * fmt->bpp[0], alignment);
sizeimage = 0;
for (i = 0; i < fmt->planes; i++) {
size = bpl * height;
if (i > 0) {
size *= fmt->bpp[i];
size /= fmt->hsub;
size /= fmt->vsub;
}
sizeimage += size;
}
pix_fmt->width = width;
pix_fmt->height = height;
pix_fmt->bytesperline = bpl;
pix_fmt->sizeimage = sizeimage;
}
static int rotate_querycap(struct file *file, void *priv,
struct v4l2_capability *cap)
{
strscpy(cap->driver, ROTATE_NAME, sizeof(cap->driver));
strscpy(cap->card, ROTATE_NAME, sizeof(cap->card));
snprintf(cap->bus_info, sizeof(cap->bus_info),
"platform:%s", ROTATE_NAME);
return 0;
}
static int rotate_enum_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_fmtdesc *f)
{
return rotate_enum_fmt(f, true);
}
static int rotate_enum_fmt_vid_out(struct file *file, void *priv,
struct v4l2_fmtdesc *f)
{
return rotate_enum_fmt(f, false);
}
static int rotate_enum_framesizes(struct file *file, void *priv,
struct v4l2_frmsizeenum *fsize)
{
const struct rotate_format *fmt;
if (fsize->index != 0)
return -EINVAL;
fmt = rotate_find_format(fsize->pixel_format);
if (!fmt)
return -EINVAL;
fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
fsize->stepwise.min_width = ROTATE_MIN_WIDTH;
fsize->stepwise.min_height = ROTATE_MIN_HEIGHT;
fsize->stepwise.max_width = ROTATE_MAX_WIDTH;
fsize->stepwise.max_height = ROTATE_MAX_HEIGHT;
fsize->stepwise.step_width = fmt->hsub;
fsize->stepwise.step_height = fmt->vsub;
return 0;
}
static int rotate_set_cap_format(struct rotate_ctx *ctx,
struct v4l2_pix_format *f,
u32 rotate)
{
const struct rotate_format *fmt;
fmt = rotate_find_format(ctx->src_fmt.pixelformat);
if (!fmt)
return -EINVAL;
if (fmt->flags & ROTATE_FLAG_YUV)
f->pixelformat = V4L2_PIX_FMT_YUV420;
else
f->pixelformat = ctx->src_fmt.pixelformat;
f->field = V4L2_FIELD_NONE;
if (rotate == 90 || rotate == 270) {
f->width = ctx->src_fmt.height;
f->height = ctx->src_fmt.width;
} else {
f->width = ctx->src_fmt.width;
f->height = ctx->src_fmt.height;
}
rotate_prepare_format(f);
return 0;
}
static int rotate_g_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct rotate_ctx *ctx = rotate_file2ctx(file);
f->fmt.pix = ctx->dst_fmt;
return 0;
}
static int rotate_g_fmt_vid_out(struct file *file, void *priv,
struct v4l2_format *f)
{
struct rotate_ctx *ctx = rotate_file2ctx(file);
f->fmt.pix = ctx->src_fmt;
return 0;
}
static int rotate_try_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct rotate_ctx *ctx = rotate_file2ctx(file);
return rotate_set_cap_format(ctx, &f->fmt.pix, ctx->rotate);
}
static int rotate_try_fmt_vid_out(struct file *file, void *priv,
struct v4l2_format *f)
{
if (!rotate_find_format(f->fmt.pix.pixelformat))
f->fmt.pix.pixelformat = V4L2_PIX_FMT_ARGB32;
if (f->fmt.pix.width < ROTATE_MIN_WIDTH)
f->fmt.pix.width = ROTATE_MIN_WIDTH;
if (f->fmt.pix.height < ROTATE_MIN_HEIGHT)
f->fmt.pix.height = ROTATE_MIN_HEIGHT;
if (f->fmt.pix.width > ROTATE_MAX_WIDTH)
f->fmt.pix.width = ROTATE_MAX_WIDTH;
if (f->fmt.pix.height > ROTATE_MAX_HEIGHT)
f->fmt.pix.height = ROTATE_MAX_HEIGHT;
f->fmt.pix.field = V4L2_FIELD_NONE;
rotate_prepare_format(&f->fmt.pix);
return 0;
}
static int rotate_s_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct rotate_ctx *ctx = rotate_file2ctx(file);
struct vb2_queue *vq;
int ret;
ret = rotate_try_fmt_vid_cap(file, priv, f);
if (ret)
return ret;
vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
if (vb2_is_busy(vq))
return -EBUSY;
ctx->dst_fmt = f->fmt.pix;
return 0;
}
static int rotate_s_fmt_vid_out(struct file *file, void *priv,
struct v4l2_format *f)
{
struct rotate_ctx *ctx = rotate_file2ctx(file);
struct vb2_queue *vq;
int ret;
ret = rotate_try_fmt_vid_out(file, priv, f);
if (ret)
return ret;
vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
if (vb2_is_busy(vq))
return -EBUSY;
/*
* Capture queue has to be also checked, because format and size
* depends on output format and size.
*/
vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
if (vb2_is_busy(vq))
return -EBUSY;
ctx->src_fmt = f->fmt.pix;
/* Propagate colorspace information to capture. */
ctx->dst_fmt.colorspace = f->fmt.pix.colorspace;
ctx->dst_fmt.xfer_func = f->fmt.pix.xfer_func;
ctx->dst_fmt.ycbcr_enc = f->fmt.pix.ycbcr_enc;
ctx->dst_fmt.quantization = f->fmt.pix.quantization;
return rotate_set_cap_format(ctx, &ctx->dst_fmt, ctx->rotate);
}
static const struct v4l2_ioctl_ops rotate_ioctl_ops = {
.vidioc_querycap = rotate_querycap,
.vidioc_enum_framesizes = rotate_enum_framesizes,
.vidioc_enum_fmt_vid_cap = rotate_enum_fmt_vid_cap,
.vidioc_g_fmt_vid_cap = rotate_g_fmt_vid_cap,
.vidioc_try_fmt_vid_cap = rotate_try_fmt_vid_cap,
.vidioc_s_fmt_vid_cap = rotate_s_fmt_vid_cap,
.vidioc_enum_fmt_vid_out = rotate_enum_fmt_vid_out,
.vidioc_g_fmt_vid_out = rotate_g_fmt_vid_out,
.vidioc_try_fmt_vid_out = rotate_try_fmt_vid_out,
.vidioc_s_fmt_vid_out = rotate_s_fmt_vid_out,
.vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
.vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
.vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
.vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
.vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
.vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
.vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
.vidioc_streamon = v4l2_m2m_ioctl_streamon,
.vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
.vidioc_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
static int rotate_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
unsigned int *nplanes, unsigned int sizes[],
struct device *alloc_devs[])
{
struct rotate_ctx *ctx = vb2_get_drv_priv(vq);
struct v4l2_pix_format *pix_fmt;
if (V4L2_TYPE_IS_OUTPUT(vq->type))
pix_fmt = &ctx->src_fmt;
else
pix_fmt = &ctx->dst_fmt;
if (*nplanes) {
if (sizes[0] < pix_fmt->sizeimage)
return -EINVAL;
} else {
sizes[0] = pix_fmt->sizeimage;
*nplanes = 1;
}
return 0;
}
static int rotate_buf_prepare(struct vb2_buffer *vb)
{
struct vb2_queue *vq = vb->vb2_queue;
struct rotate_ctx *ctx = vb2_get_drv_priv(vq);
struct v4l2_pix_format *pix_fmt;
if (V4L2_TYPE_IS_OUTPUT(vq->type))
pix_fmt = &ctx->src_fmt;
else
pix_fmt = &ctx->dst_fmt;
if (vb2_plane_size(vb, 0) < pix_fmt->sizeimage)
return -EINVAL;
vb2_set_plane_payload(vb, 0, pix_fmt->sizeimage);
return 0;
}
static void rotate_buf_queue(struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct rotate_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
}
static void rotate_queue_cleanup(struct vb2_queue *vq, u32 state)
{
struct rotate_ctx *ctx = vb2_get_drv_priv(vq);
struct vb2_v4l2_buffer *vbuf;
do {
if (V4L2_TYPE_IS_OUTPUT(vq->type))
vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
else
vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
if (vbuf)
v4l2_m2m_buf_done(vbuf, state);
} while (vbuf);
}
static int rotate_start_streaming(struct vb2_queue *vq, unsigned int count)
{
if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
struct rotate_ctx *ctx = vb2_get_drv_priv(vq);
struct device *dev = ctx->dev->dev;
int ret;
ret = pm_runtime_resume_and_get(dev);
if (ret < 0) {
dev_err(dev, "Failed to enable module\n");
return ret;
}
}
return 0;
}
static void rotate_stop_streaming(struct vb2_queue *vq)
{
if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
struct rotate_ctx *ctx = vb2_get_drv_priv(vq);
pm_runtime_put(ctx->dev->dev);
}
rotate_queue_cleanup(vq, VB2_BUF_STATE_ERROR);
}
static const struct vb2_ops rotate_qops = {
.queue_setup = rotate_queue_setup,
.buf_prepare = rotate_buf_prepare,
.buf_queue = rotate_buf_queue,
.start_streaming = rotate_start_streaming,
.stop_streaming = rotate_stop_streaming,
};
static int rotate_queue_init(void *priv, struct vb2_queue *src_vq,
struct vb2_queue *dst_vq)
{
struct rotate_ctx *ctx = priv;
int ret;
src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
src_vq->drv_priv = ctx;
src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
src_vq->min_queued_buffers = 1;
src_vq->ops = &rotate_qops;
src_vq->mem_ops = &vb2_dma_contig_memops;
src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
src_vq->lock = &ctx->dev->dev_mutex;
src_vq->dev = ctx->dev->dev;
ret = vb2_queue_init(src_vq);
if (ret)
return ret;
dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
dst_vq->drv_priv = ctx;
dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
dst_vq->min_queued_buffers = 2;
dst_vq->ops = &rotate_qops;
dst_vq->mem_ops = &vb2_dma_contig_memops;
dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
dst_vq->lock = &ctx->dev->dev_mutex;
dst_vq->dev = ctx->dev->dev;
ret = vb2_queue_init(dst_vq);
if (ret)
return ret;
return 0;
}
static int rotate_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct rotate_ctx *ctx = container_of(ctrl->handler,
struct rotate_ctx,
ctrl_handler);
struct v4l2_pix_format fmt;
switch (ctrl->id) {
case V4L2_CID_HFLIP:
ctx->hflip = ctrl->val;
break;
case V4L2_CID_VFLIP:
ctx->vflip = ctrl->val;
break;
case V4L2_CID_ROTATE:
rotate_set_cap_format(ctx, &fmt, ctrl->val);
/* Check if capture format needs to be changed */
if (fmt.width != ctx->dst_fmt.width ||
fmt.height != ctx->dst_fmt.height ||
fmt.bytesperline != ctx->dst_fmt.bytesperline ||
fmt.sizeimage != ctx->dst_fmt.sizeimage) {
struct vb2_queue *vq;
vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
V4L2_BUF_TYPE_VIDEO_CAPTURE);
if (vb2_is_busy(vq))
return -EBUSY;
rotate_set_cap_format(ctx, &ctx->dst_fmt, ctrl->val);
}
ctx->rotate = ctrl->val;
break;
default:
return -EINVAL;
}
return 0;
}
static const struct v4l2_ctrl_ops rotate_ctrl_ops = {
.s_ctrl = rotate_s_ctrl,
};
static int rotate_setup_ctrls(struct rotate_ctx *ctx)
{
v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
v4l2_ctrl_new_std(&ctx->ctrl_handler, &rotate_ctrl_ops,
V4L2_CID_HFLIP, 0, 1, 1, 0);
v4l2_ctrl_new_std(&ctx->ctrl_handler, &rotate_ctrl_ops,
V4L2_CID_VFLIP, 0, 1, 1, 0);
v4l2_ctrl_new_std(&ctx->ctrl_handler, &rotate_ctrl_ops,
V4L2_CID_ROTATE, 0, 270, 90, 0);
if (ctx->ctrl_handler.error) {
int err = ctx->ctrl_handler.error;
v4l2_err(&ctx->dev->v4l2_dev, "control setup failed!\n");
v4l2_ctrl_handler_free(&ctx->ctrl_handler);
return err;
}
return v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
}
static int rotate_open(struct file *file)
{
struct rotate_dev *dev = video_drvdata(file);
struct rotate_ctx *ctx = NULL;
int ret;
if (mutex_lock_interruptible(&dev->dev_mutex))
return -ERESTARTSYS;
ctx = kzalloc_obj(*ctx);
if (!ctx) {
mutex_unlock(&dev->dev_mutex);
return -ENOMEM;
}
/* default output format */
ctx->src_fmt.pixelformat = V4L2_PIX_FMT_ARGB32;
ctx->src_fmt.field = V4L2_FIELD_NONE;
ctx->src_fmt.width = 640;
ctx->src_fmt.height = 480;
rotate_prepare_format(&ctx->src_fmt);
/* default capture format */
rotate_set_cap_format(ctx, &ctx->dst_fmt, ctx->rotate);
v4l2_fh_init(&ctx->fh, video_devdata(file));
ctx->dev = dev;
ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
&rotate_queue_init);
if (IS_ERR(ctx->fh.m2m_ctx)) {
ret = PTR_ERR(ctx->fh.m2m_ctx);
goto err_free;
}
v4l2_fh_add(&ctx->fh, file);
ret = rotate_setup_ctrls(ctx);
if (ret)
goto err_free;
ctx->fh.ctrl_handler = &ctx->ctrl_handler;
mutex_unlock(&dev->dev_mutex);
return 0;
err_free:
kfree(ctx);
mutex_unlock(&dev->dev_mutex);
return ret;
}
static int rotate_release(struct file *file)
{
struct rotate_dev *dev = video_drvdata(file);
struct rotate_ctx *ctx = rotate_file2ctx(file);
mutex_lock(&dev->dev_mutex);
v4l2_ctrl_handler_free(&ctx->ctrl_handler);
v4l2_fh_del(&ctx->fh, file);
v4l2_fh_exit(&ctx->fh);
v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
kfree(ctx);
mutex_unlock(&dev->dev_mutex);
return 0;
}
static const struct v4l2_file_operations rotate_fops = {
.owner = THIS_MODULE,
.open = rotate_open,
.release = rotate_release,
.poll = v4l2_m2m_fop_poll,
.unlocked_ioctl = video_ioctl2,
.mmap = v4l2_m2m_fop_mmap,
};
static const struct video_device rotate_video_device = {
.name = ROTATE_NAME,
.vfl_dir = VFL_DIR_M2M,
.fops = &rotate_fops,
.ioctl_ops = &rotate_ioctl_ops,
.minor = -1,
.release = video_device_release_empty,
.device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING,
};
static const struct v4l2_m2m_ops rotate_m2m_ops = {
.device_run = rotate_device_run,
};
static int rotate_probe(struct platform_device *pdev)
{
struct rotate_dev *dev;
struct video_device *vfd;
int irq, ret;
dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
if (!dev)
return -ENOMEM;
dev->vfd = rotate_video_device;
dev->dev = &pdev->dev;
irq = platform_get_irq(pdev, 0);
if (irq <= 0)
return irq;
ret = devm_request_irq(dev->dev, irq, rotate_irq,
0, dev_name(dev->dev), dev);
if (ret) {
dev_err(dev->dev, "Failed to request IRQ\n");
return ret;
}
dev->base = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(dev->base))
return PTR_ERR(dev->base);
dev->bus_clk = devm_clk_get(dev->dev, "bus");
if (IS_ERR(dev->bus_clk)) {
dev_err(dev->dev, "Failed to get bus clock\n");
return PTR_ERR(dev->bus_clk);
}
dev->mod_clk = devm_clk_get(dev->dev, "mod");
if (IS_ERR(dev->mod_clk)) {
dev_err(dev->dev, "Failed to get mod clock\n");
return PTR_ERR(dev->mod_clk);
}
dev->rstc = devm_reset_control_get(dev->dev, NULL);
if (IS_ERR(dev->rstc)) {
dev_err(dev->dev, "Failed to get reset control\n");
return PTR_ERR(dev->rstc);
}
mutex_init(&dev->dev_mutex);
ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
if (ret) {
dev_err(dev->dev, "Failed to register V4L2 device\n");
return ret;
}
vfd = &dev->vfd;
vfd->lock = &dev->dev_mutex;
vfd->v4l2_dev = &dev->v4l2_dev;
snprintf(vfd->name, sizeof(vfd->name), "%s",
rotate_video_device.name);
video_set_drvdata(vfd, dev);
ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
if (ret) {
v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
goto err_v4l2;
}
v4l2_info(&dev->v4l2_dev,
"Device registered as /dev/video%d\n", vfd->num);
dev->m2m_dev = v4l2_m2m_init(&rotate_m2m_ops);
if (IS_ERR(dev->m2m_dev)) {
v4l2_err(&dev->v4l2_dev,
"Failed to initialize V4L2 M2M device\n");
ret = PTR_ERR(dev->m2m_dev);
goto err_video;
}
platform_set_drvdata(pdev, dev);
pm_runtime_enable(dev->dev);
return 0;
err_video:
video_unregister_device(&dev->vfd);
err_v4l2:
v4l2_device_unregister(&dev->v4l2_dev);
return ret;
}
static void rotate_remove(struct platform_device *pdev)
{
struct rotate_dev *dev = platform_get_drvdata(pdev);
v4l2_m2m_release(dev->m2m_dev);
video_unregister_device(&dev->vfd);
v4l2_device_unregister(&dev->v4l2_dev);
pm_runtime_force_suspend(&pdev->dev);
}
static int rotate_runtime_resume(struct device *device)
{
struct rotate_dev *dev = dev_get_drvdata(device);
int ret;
ret = clk_prepare_enable(dev->bus_clk);
if (ret) {
dev_err(dev->dev, "Failed to enable bus clock\n");
return ret;
}
ret = clk_prepare_enable(dev->mod_clk);
if (ret) {
dev_err(dev->dev, "Failed to enable mod clock\n");
goto err_bus_clk;
}
ret = reset_control_deassert(dev->rstc);
if (ret) {
dev_err(dev->dev, "Failed to apply reset\n");
goto err_mod_clk;
}
return 0;
err_mod_clk:
clk_disable_unprepare(dev->mod_clk);
err_bus_clk:
clk_disable_unprepare(dev->bus_clk);
return ret;
}
static int rotate_runtime_suspend(struct device *device)
{
struct rotate_dev *dev = dev_get_drvdata(device);
reset_control_assert(dev->rstc);
clk_disable_unprepare(dev->mod_clk);
clk_disable_unprepare(dev->bus_clk);
return 0;
}
static const struct of_device_id rotate_dt_match[] = {
{ .compatible = "allwinner,sun8i-a83t-de2-rotate" },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, rotate_dt_match);
static const struct dev_pm_ops rotate_pm_ops = {
.runtime_resume = rotate_runtime_resume,
.runtime_suspend = rotate_runtime_suspend,
};
static struct platform_driver rotate_driver = {
.probe = rotate_probe,
.remove = rotate_remove,
.driver = {
.name = ROTATE_NAME,
.of_match_table = rotate_dt_match,
.pm = &rotate_pm_ops,
},
};
module_platform_driver(rotate_driver);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Jernej Skrabec <jernej.skrabec@siol.net>");
MODULE_DESCRIPTION("Allwinner DE2 rotate driver");