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,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