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,33 @@
# SPDX-License-Identifier: GPL-2.0
config VIDEO_RZG2L_CSI2
tristate "RZ/G2L MIPI CSI-2 Receiver"
depends on ARCH_RENESAS || COMPILE_TEST
depends on V4L_PLATFORM_DRIVERS
depends on VIDEO_DEV && OF
select MEDIA_CONTROLLER
select RESET_CONTROLLER
select V4L2_FWNODE
select VIDEO_V4L2_SUBDEV_API
help
Support for Renesas RZ/G2L (and alike SoC's) MIPI CSI-2
Receiver driver.
To compile this driver as a module, choose M here: the
module will be called rzg2l-csi2.
config VIDEO_RZG2L_CRU
tristate "RZ/G2L Camera Receiving Unit (CRU) Driver"
depends on ARCH_RENESAS || COMPILE_TEST
depends on V4L_PLATFORM_DRIVERS
depends on VIDEO_DEV && OF
select MEDIA_CONTROLLER
select V4L2_FWNODE
select VIDEOBUF2_DMA_CONTIG
select VIDEO_V4L2_SUBDEV_API
help
Support for Renesas RZ/G2L (and alike SoC's) Camera Receiving
Unit (CRU) driver.
To compile this driver as a module, choose M here: the
module will be called rzg2l-cru.
@@ -0,0 +1,6 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_VIDEO_RZG2L_CSI2) += rzg2l-csi2.o
rzg2l-cru-objs = rzg2l-core.o rzg2l-ip.o rzg2l-video.o
obj-$(CONFIG_VIDEO_RZG2L_CRU) += rzg2l-cru.o
@@ -0,0 +1,441 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Driver for Renesas RZ/G2L CRU
*
* Copyright (C) 2022 Renesas Electronics Corp.
*
* Based on Renesas R-Car VIN
* Copyright (C) 2011-2013 Renesas Solutions Corp.
* Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
* Copyright (C) 2008 Magnus Damm
*/
#include <linux/clk.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_graph.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <media/v4l2-fwnode.h>
#include <media/v4l2-mc.h>
#include "rzg2l-cru.h"
#include "rzg2l-cru-regs.h"
static inline struct rzg2l_cru_dev *notifier_to_cru(struct v4l2_async_notifier *n)
{
return container_of(n, struct rzg2l_cru_dev, notifier);
}
static const struct media_device_ops rzg2l_cru_media_ops = {
.link_notify = v4l2_pipeline_link_notify,
};
/* -----------------------------------------------------------------------------
* Group async notifier
*/
static int rzg2l_cru_group_notify_complete(struct v4l2_async_notifier *notifier)
{
struct rzg2l_cru_dev *cru = notifier_to_cru(notifier);
struct media_entity *source, *sink;
int ret;
ret = rzg2l_cru_ip_subdev_register(cru);
if (ret)
return ret;
ret = v4l2_device_register_subdev_nodes(&cru->v4l2_dev);
if (ret) {
dev_err(cru->dev, "Failed to register subdev nodes\n");
return ret;
}
ret = rzg2l_cru_video_register(cru);
if (ret)
return ret;
/*
* CRU can be connected either to CSI2 or PARALLEL device
* For now we are only supporting CSI2
*
* Create media device link between CSI-2 <-> CRU IP
*/
source = &cru->csi.subdev->entity;
sink = &cru->ip.subdev.entity;
ret = media_create_pad_link(source, 1, sink, 0,
MEDIA_LNK_FL_ENABLED |
MEDIA_LNK_FL_IMMUTABLE);
if (ret) {
dev_err(cru->dev, "Error creating link from %s to %s\n",
source->name, sink->name);
return ret;
}
cru->ip.remote = cru->csi.subdev;
/* Create media device link between CRU IP <-> CRU OUTPUT */
source = &cru->ip.subdev.entity;
sink = &cru->vdev.entity;
ret = media_create_pad_link(source, 1, sink, 0,
MEDIA_LNK_FL_ENABLED |
MEDIA_LNK_FL_IMMUTABLE);
if (ret) {
dev_err(cru->dev, "Error creating link from %s to %s\n",
source->name, sink->name);
return ret;
}
return 0;
}
static void rzg2l_cru_group_notify_unbind(struct v4l2_async_notifier *notifier,
struct v4l2_subdev *subdev,
struct v4l2_async_connection *asd)
{
struct rzg2l_cru_dev *cru = notifier_to_cru(notifier);
rzg2l_cru_ip_subdev_unregister(cru);
mutex_lock(&cru->mdev_lock);
if (cru->csi.asd == asd) {
cru->csi.subdev = NULL;
dev_dbg(cru->dev, "Unbind CSI-2 %s\n", subdev->name);
}
mutex_unlock(&cru->mdev_lock);
}
static int rzg2l_cru_group_notify_bound(struct v4l2_async_notifier *notifier,
struct v4l2_subdev *subdev,
struct v4l2_async_connection *asd)
{
struct rzg2l_cru_dev *cru = notifier_to_cru(notifier);
mutex_lock(&cru->mdev_lock);
if (cru->csi.asd == asd) {
cru->csi.subdev = subdev;
dev_dbg(cru->dev, "Bound CSI-2 %s\n", subdev->name);
}
mutex_unlock(&cru->mdev_lock);
return 0;
}
static const struct v4l2_async_notifier_operations rzg2l_cru_async_ops = {
.bound = rzg2l_cru_group_notify_bound,
.unbind = rzg2l_cru_group_notify_unbind,
.complete = rzg2l_cru_group_notify_complete,
};
static int rzg2l_cru_mc_parse_of(struct rzg2l_cru_dev *cru)
{
struct v4l2_fwnode_endpoint vep = {
.bus_type = V4L2_MBUS_CSI2_DPHY,
};
struct fwnode_handle *ep, *fwnode;
struct v4l2_async_connection *asd;
int ret;
ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(cru->dev), 1, 0, 0);
if (!ep)
return 0;
fwnode = fwnode_graph_get_remote_endpoint(ep);
ret = v4l2_fwnode_endpoint_parse(ep, &vep);
fwnode_handle_put(ep);
if (ret) {
dev_err(cru->dev, "Failed to parse %pOF\n", to_of_node(fwnode));
ret = -EINVAL;
goto out;
}
if (!of_device_is_available(to_of_node(fwnode))) {
dev_dbg(cru->dev, "OF device %pOF disabled, ignoring\n",
to_of_node(fwnode));
ret = -ENOTCONN;
goto out;
}
asd = v4l2_async_nf_add_fwnode(&cru->notifier, fwnode,
struct v4l2_async_connection);
if (IS_ERR(asd)) {
ret = PTR_ERR(asd);
goto out;
}
cru->csi.asd = asd;
dev_dbg(cru->dev, "Added OF device %pOF to slot %u\n",
to_of_node(fwnode), vep.base.id);
out:
fwnode_handle_put(fwnode);
return ret;
}
static int rzg2l_cru_mc_parse_of_graph(struct rzg2l_cru_dev *cru)
{
int ret;
v4l2_async_nf_init(&cru->notifier, &cru->v4l2_dev);
ret = rzg2l_cru_mc_parse_of(cru);
if (ret)
return ret;
cru->notifier.ops = &rzg2l_cru_async_ops;
if (list_empty(&cru->notifier.waiting_list))
return 0;
ret = v4l2_async_nf_register(&cru->notifier);
if (ret < 0) {
dev_err(cru->dev, "Notifier registration failed\n");
v4l2_async_nf_cleanup(&cru->notifier);
return ret;
}
return 0;
}
static int rzg2l_cru_media_init(struct rzg2l_cru_dev *cru)
{
struct media_device *mdev = NULL;
const struct of_device_id *match;
int ret;
cru->pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
ret = media_entity_pads_init(&cru->vdev.entity, 1, &cru->pad);
if (ret)
return ret;
mutex_init(&cru->mdev_lock);
mdev = &cru->mdev;
mdev->dev = cru->dev;
mdev->ops = &rzg2l_cru_media_ops;
match = of_match_node(cru->dev->driver->of_match_table,
cru->dev->of_node);
strscpy(mdev->driver_name, KBUILD_MODNAME, sizeof(mdev->driver_name));
strscpy(mdev->model, match->compatible, sizeof(mdev->model));
cru->v4l2_dev.mdev = &cru->mdev;
media_device_init(mdev);
ret = rzg2l_cru_mc_parse_of_graph(cru);
if (ret) {
mutex_lock(&cru->mdev_lock);
cru->v4l2_dev.mdev = NULL;
mutex_unlock(&cru->mdev_lock);
}
return 0;
}
static int rzg2l_cru_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct rzg2l_cru_dev *cru;
int irq, ret;
cru = devm_kzalloc(dev, sizeof(*cru), GFP_KERNEL);
if (!cru)
return -ENOMEM;
cru->base = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(cru->base))
return PTR_ERR(cru->base);
cru->presetn = devm_reset_control_get_shared(dev, "presetn");
if (IS_ERR(cru->presetn))
return dev_err_probe(dev, PTR_ERR(cru->presetn),
"Failed to get cpg presetn\n");
cru->aresetn = devm_reset_control_get_exclusive(dev, "aresetn");
if (IS_ERR(cru->aresetn))
return dev_err_probe(dev, PTR_ERR(cru->aresetn),
"Failed to get cpg aresetn\n");
cru->vclk = devm_clk_get(dev, "video");
if (IS_ERR(cru->vclk))
return dev_err_probe(dev, PTR_ERR(cru->vclk),
"Failed to get video clock\n");
cru->dev = dev;
cru->info = of_device_get_match_data(dev);
irq = platform_get_irq(pdev, 0);
if (irq < 0)
return irq;
ret = devm_request_irq(dev, irq, cru->info->irq_handler, 0,
KBUILD_MODNAME, cru);
if (ret)
return dev_err_probe(dev, ret, "failed to request irq\n");
platform_set_drvdata(pdev, cru);
ret = rzg2l_cru_dma_register(cru);
if (ret)
return ret;
cru->num_buf = RZG2L_CRU_HW_BUFFER_DEFAULT;
pm_suspend_ignore_children(dev, true);
ret = devm_pm_runtime_enable(dev);
if (ret)
goto error_dma_unregister;
ret = rzg2l_cru_media_init(cru);
if (ret)
goto error_dma_unregister;
return 0;
error_dma_unregister:
rzg2l_cru_dma_unregister(cru);
return ret;
}
static void rzg2l_cru_remove(struct platform_device *pdev)
{
struct rzg2l_cru_dev *cru = platform_get_drvdata(pdev);
v4l2_async_nf_unregister(&cru->notifier);
v4l2_async_nf_cleanup(&cru->notifier);
rzg2l_cru_video_unregister(cru);
media_device_cleanup(&cru->mdev);
mutex_destroy(&cru->mdev_lock);
rzg2l_cru_dma_unregister(cru);
}
static const u16 rzg3e_cru_regs[] = {
[CRUnCTRL] = 0x0,
[CRUnIE] = 0x4,
[CRUnIE2] = 0x8,
[CRUnINTS] = 0xc,
[CRUnINTS2] = 0x10,
[CRUnRST] = 0x18,
[AMnMB1ADDRL] = 0x40,
[AMnMB1ADDRH] = 0x44,
[AMnMB2ADDRL] = 0x48,
[AMnMB2ADDRH] = 0x4c,
[AMnMB3ADDRL] = 0x50,
[AMnMB3ADDRH] = 0x54,
[AMnMB4ADDRL] = 0x58,
[AMnMB4ADDRH] = 0x5c,
[AMnMB5ADDRL] = 0x60,
[AMnMB5ADDRH] = 0x64,
[AMnMB6ADDRL] = 0x68,
[AMnMB6ADDRH] = 0x6c,
[AMnMB7ADDRL] = 0x70,
[AMnMB7ADDRH] = 0x74,
[AMnMB8ADDRL] = 0x78,
[AMnMB8ADDRH] = 0x7c,
[AMnMBVALID] = 0x88,
[AMnMADRSL] = 0x8c,
[AMnMADRSH] = 0x90,
[AMnAXIATTR] = 0xec,
[AMnFIFOPNTR] = 0xf8,
[AMnAXISTP] = 0x110,
[AMnAXISTPACK] = 0x114,
[AMnIS] = 0x128,
[ICnEN] = 0x1f0,
[ICnSVCNUM] = 0x1f8,
[ICnSVC] = 0x1fc,
[ICnIPMC_C0] = 0x200,
[ICnMS] = 0x2d8,
[ICnDMR] = 0x304,
};
static const struct rzg2l_cru_info rzg3e_cru_info = {
.max_width = 4095,
.max_height = 4095,
.image_conv = ICnIPMC_C0,
.has_stride = true,
.regs = rzg3e_cru_regs,
.irq_handler = rzg3e_cru_irq,
.enable_interrupts = rzg3e_cru_enable_interrupts,
.disable_interrupts = rzg3e_cru_disable_interrupts,
.fifo_empty = rzg3e_fifo_empty,
};
static const u16 rzg2l_cru_regs[] = {
[CRUnCTRL] = 0x0,
[CRUnIE] = 0x4,
[CRUnINTS] = 0x8,
[CRUnRST] = 0xc,
[AMnMB1ADDRL] = 0x100,
[AMnMB1ADDRH] = 0x104,
[AMnMB2ADDRL] = 0x108,
[AMnMB2ADDRH] = 0x10c,
[AMnMB3ADDRL] = 0x110,
[AMnMB3ADDRH] = 0x114,
[AMnMB4ADDRL] = 0x118,
[AMnMB4ADDRH] = 0x11c,
[AMnMB5ADDRL] = 0x120,
[AMnMB5ADDRH] = 0x124,
[AMnMB6ADDRL] = 0x128,
[AMnMB6ADDRH] = 0x12c,
[AMnMB7ADDRL] = 0x130,
[AMnMB7ADDRH] = 0x134,
[AMnMB8ADDRL] = 0x138,
[AMnMB8ADDRH] = 0x13c,
[AMnMBVALID] = 0x148,
[AMnMBS] = 0x14c,
[AMnAXIATTR] = 0x158,
[AMnFIFOPNTR] = 0x168,
[AMnAXISTP] = 0x174,
[AMnAXISTPACK] = 0x178,
[ICnEN] = 0x200,
[ICnMC] = 0x208,
[ICnMS] = 0x254,
[ICnDMR] = 0x26c,
};
static const struct rzg2l_cru_info rzg2l_cru_info = {
.max_width = 2800,
.max_height = 4095,
.image_conv = ICnMC,
.regs = rzg2l_cru_regs,
.irq_handler = rzg2l_cru_irq,
.enable_interrupts = rzg2l_cru_enable_interrupts,
.disable_interrupts = rzg2l_cru_disable_interrupts,
.fifo_empty = rzg2l_fifo_empty,
};
static const struct of_device_id rzg2l_cru_of_id_table[] = {
{
.compatible = "renesas,r9a09g047-cru",
.data = &rzg3e_cru_info,
},
{
.compatible = "renesas,rzg2l-cru",
.data = &rzg2l_cru_info,
},
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, rzg2l_cru_of_id_table);
static struct platform_driver rzg2l_cru_driver = {
.driver = {
.name = "rzg2l-cru",
.of_match_table = rzg2l_cru_of_id_table,
},
.probe = rzg2l_cru_probe,
.remove = rzg2l_cru_remove,
};
module_platform_driver(rzg2l_cru_driver);
MODULE_AUTHOR("Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>");
MODULE_DESCRIPTION("Renesas RZ/G2L CRU driver");
MODULE_LICENSE("GPL");
@@ -0,0 +1,111 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* rzg2l-cru-regs.h--RZ/G2L (and alike SoCs) CRU Registers Definitions
*
* Copyright (C) 2024 Renesas Electronics Corp.
*/
#ifndef __RZG2L_CRU_REGS_H__
#define __RZG2L_CRU_REGS_H__
/* HW CRU Registers Definition */
#define CRUnCTRL_VINSEL(x) ((x) << 0)
#define CRUnIE_EFE BIT(17)
#define CRUnIE2_FExE(x) BIT(((x) * 3) + 1)
#define CRUnINTS_SFS BIT(16)
#define CRUnINTS2_FExS(x) BIT(((x) * 3) + 1)
#define CRUnRST_VRESETN BIT(0)
/* Memory Bank Base Address (Lower) Register for CRU Image Data */
#define AMnMBxADDRL(x) (AMnMB1ADDRL + (x) * 2)
/* Memory Bank Base Address (Higher) Register for CRU Image Data */
#define AMnMBxADDRH(x) (AMnMB1ADDRH + (x) * 2)
#define AMnMBVALID_MBVALID(x) GENMASK(x, 0)
#define AMnMBS_MBSTS 0x7
#define AMnAXIATTR_AXILEN_MASK GENMASK(3, 0)
#define AMnAXIATTR_AXILEN (0xf)
#define AMnFIFOPNTR_FIFOWPNTR GENMASK(7, 0)
#define AMnFIFOPNTR_FIFOWPNTR_B0 AMnFIFOPNTR_FIFOWPNTR
#define AMnFIFOPNTR_FIFOWPNTR_B1 GENMASK(15, 8)
#define AMnFIFOPNTR_FIFORPNTR_Y GENMASK(23, 16)
#define AMnFIFOPNTR_FIFORPNTR_B0 AMnFIFOPNTR_FIFORPNTR_Y
#define AMnFIFOPNTR_FIFORPNTR_B1 GENMASK(31, 24)
#define AMnIS_IS_MASK GENMASK(14, 7)
#define AMnIS_IS(x) ((x) << 7)
#define AMnAXISTP_AXI_STOP BIT(0)
#define AMnAXISTPACK_AXI_STOP_ACK BIT(0)
#define ICnEN_ICEN BIT(0)
#define ICnSVC_SVC0(x) (x)
#define ICnSVC_SVC1(x) ((x) << 4)
#define ICnSVC_SVC2(x) ((x) << 8)
#define ICnSVC_SVC3(x) ((x) << 12)
#define ICnMC_CSCTHR BIT(5)
#define ICnMC_INF(x) ((x) << 16)
#define ICnMC_VCSEL(x) ((x) << 22)
#define ICnMC_VCSEL_MASK GENMASK(23, 22)
#define ICnMC_INF_MASK GENMASK(21, 16)
#define ICnMS_IA BIT(2)
#define ICnDMR_YCMODE_UYVY (1 << 4)
enum rzg2l_cru_common_regs {
CRUnCTRL, /* CRU Control */
CRUnIE, /* CRU Interrupt Enable */
CRUnIE2, /* CRU Interrupt Enable(2) */
CRUnINTS, /* CRU Interrupt Status */
CRUnINTS2, /* CRU Interrupt Status(2) */
CRUnRST, /* CRU Reset */
AMnMB1ADDRL, /* Bank 1 Address (Lower) for CRU Image Data */
AMnMB1ADDRH, /* Bank 1 Address (Higher) for CRU Image Data */
AMnMB2ADDRL, /* Bank 2 Address (Lower) for CRU Image Data */
AMnMB2ADDRH, /* Bank 2 Address (Higher) for CRU Image Data */
AMnMB3ADDRL, /* Bank 3 Address (Lower) for CRU Image Data */
AMnMB3ADDRH, /* Bank 3 Address (Higher) for CRU Image Data */
AMnMB4ADDRL, /* Bank 4 Address (Lower) for CRU Image Data */
AMnMB4ADDRH, /* Bank 4 Address (Higher) for CRU Image Data */
AMnMB5ADDRL, /* Bank 5 Address (Lower) for CRU Image Data */
AMnMB5ADDRH, /* Bank 5 Address (Higher) for CRU Image Data */
AMnMB6ADDRL, /* Bank 6 Address (Lower) for CRU Image Data */
AMnMB6ADDRH, /* Bank 6 Address (Higher) for CRU Image Data */
AMnMB7ADDRL, /* Bank 7 Address (Lower) for CRU Image Data */
AMnMB7ADDRH, /* Bank 7 Address (Higher) for CRU Image Data */
AMnMB8ADDRL, /* Bank 8 Address (Lower) for CRU Image Data */
AMnMB8ADDRH, /* Bank 8 Address (Higher) for CRU Image Data */
AMnMBVALID, /* Memory Bank Enable for CRU Image Data */
AMnMBS, /* Memory Bank Status for CRU Image Data */
AMnMADRSL, /* VD Memory Address Lower Status Register */
AMnMADRSH, /* VD Memory Address Higher Status Register */
AMnAXIATTR, /* AXI Master Transfer Setting Register for CRU Image Data */
AMnFIFOPNTR, /* AXI Master FIFO Pointer for CRU Image Data */
AMnAXISTP, /* AXI Master Transfer Stop for CRU Image Data */
AMnAXISTPACK, /* AXI Master Transfer Stop Status for CRU Image Data */
AMnIS, /* Image Stride Setting Register */
ICnEN, /* CRU Image Processing Enable */
ICnSVCNUM, /* CRU SVC Number Register */
ICnSVC, /* CRU VC Select Register */
ICnMC, /* CRU Image Processing Main Control */
ICnIPMC_C0, /* CRU Image Converter Main Control 0 */
ICnMS, /* CRU Module Status */
ICnDMR, /* CRU Data Output Mode */
RZG2L_CRU_MAX_REG,
};
#endif /* __RZG2L_CRU_REGS_H__ */
@@ -0,0 +1,198 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Driver for Renesas RZ/G2L CRU
*
* Copyright (C) 2022 Renesas Electronics Corp.
*/
#ifndef __RZG2L_CRU__
#define __RZG2L_CRU__
#include <linux/irqreturn.h>
#include <linux/mutex.h>
#include <linux/reset.h>
#include <linux/spinlock.h>
#include <media/v4l2-async.h>
#include <media/v4l2-dev.h>
#include <media/v4l2-device.h>
#include <media/videobuf2-v4l2.h>
/* Number of HW buffers */
#define RZG2L_CRU_HW_BUFFER_MAX 8
#define RZG2L_CRU_HW_BUFFER_DEFAULT 3
/* Address alignment mask for HW buffers */
#define RZG2L_CRU_HW_BUFFER_MASK 0x1ff
/* Maximum number of CSI2 virtual channels */
#define RZG2L_CRU_CSI2_VCHANNEL 4
#define RZG2L_CRU_MIN_INPUT_WIDTH 320
#define RZG2L_CRU_MIN_INPUT_HEIGHT 240
enum rzg2l_csi2_pads {
RZG2L_CRU_IP_SINK = 0,
RZG2L_CRU_IP_SOURCE,
};
struct rzg2l_cru_dev;
struct rzg2l_cru_csi {
struct v4l2_async_connection *asd;
struct v4l2_subdev *subdev;
};
struct rzg2l_cru_ip {
struct v4l2_subdev subdev;
struct media_pad pads[2];
struct v4l2_async_notifier notifier;
struct v4l2_subdev *remote;
};
/**
* struct rzg2l_cru_ip_format - CRU IP format
* @codes: Array of up to four media bus codes
* @datatype: MIPI CSI2 data type
* @format: 4CC format identifier (V4L2_PIX_FMT_*)
* @icndmr: ICnDMR register value
* @yuv: Flag to indicate whether the format is YUV-based.
*/
struct rzg2l_cru_ip_format {
/*
* RAW output formats might be produced by RAW media codes with any one
* of the 4 common bayer patterns.
*/
u32 codes[4];
u32 datatype;
u32 format;
u32 icndmr;
bool yuv;
};
struct rzg2l_cru_info {
unsigned int max_width;
unsigned int max_height;
u16 image_conv;
const u16 *regs;
bool has_stride;
irqreturn_t (*irq_handler)(int irq, void *data);
void (*enable_interrupts)(struct rzg2l_cru_dev *cru);
void (*disable_interrupts)(struct rzg2l_cru_dev *cru);
bool (*fifo_empty)(struct rzg2l_cru_dev *cru);
};
/**
* struct rzg2l_cru_dev - Renesas CRU device structure
* @dev: (OF) device
* @base: device I/O register space remapped to virtual memory
* @info: info about CRU instance
*
* @presetn: CRU_PRESETN reset line
* @aresetn: CRU_ARESETN reset line
*
* @vclk: CRU Main clock
*
* @vdev: V4L2 video device associated with CRU
* @v4l2_dev: V4L2 device
* @num_buf: Holds the current number of buffers enabled
* @svc_channel: SVC0/1/2/3 to use for RZ/G3E
* @notifier: V4L2 asynchronous subdevs notifier
*
* @ip: Image processing subdev info
* @csi: CSI info
* @mdev: media device
* @mdev_lock: protects the count, notifier and csi members
* @pad: media pad for the video device entity
*
* @hw_lock: protects the @active_slot counter, hardware programming
* of slot addresses and the @buf_addr[] list
* @buf_addr: Memory addresses where current video data is written
* @active_slot: The slot in use
*
* @lock: protects @queue
* @queue: vb2 buffers queue
* @scratch: cpu address for scratch buffer
* @scratch_phys: physical address of the scratch buffer
*
* @qlock: protects @queue_buf, @buf_list, @sequence
* @state
* @queue_buf: Keeps track of buffers given to HW slot
* @buf_list: list of queued buffers
* @sequence: V4L2 buffers sequence number
* @state: keeps track of operation state
*
* @format: active V4L2 pixel format
*/
struct rzg2l_cru_dev {
struct device *dev;
void __iomem *base;
const struct rzg2l_cru_info *info;
struct reset_control *presetn;
struct reset_control *aresetn;
struct clk *vclk;
struct video_device vdev;
struct v4l2_device v4l2_dev;
u8 num_buf;
u8 svc_channel;
struct v4l2_async_notifier notifier;
struct rzg2l_cru_ip ip;
struct rzg2l_cru_csi csi;
struct media_device mdev;
struct mutex mdev_lock;
struct media_pad pad;
spinlock_t hw_lock;
dma_addr_t buf_addr[RZG2L_CRU_HW_BUFFER_DEFAULT];
unsigned int active_slot;
struct mutex lock;
struct vb2_queue queue;
void *scratch;
dma_addr_t scratch_phys;
spinlock_t qlock;
struct vb2_v4l2_buffer *queue_buf[RZG2L_CRU_HW_BUFFER_MAX];
struct list_head buf_list;
unsigned int sequence;
struct v4l2_pix_format format;
};
int rzg2l_cru_start_image_processing(struct rzg2l_cru_dev *cru);
void rzg2l_cru_stop_image_processing(struct rzg2l_cru_dev *cru);
int rzg2l_cru_dma_register(struct rzg2l_cru_dev *cru);
void rzg2l_cru_dma_unregister(struct rzg2l_cru_dev *cru);
int rzg2l_cru_video_register(struct rzg2l_cru_dev *cru);
void rzg2l_cru_video_unregister(struct rzg2l_cru_dev *cru);
irqreturn_t rzg2l_cru_irq(int irq, void *data);
irqreturn_t rzg3e_cru_irq(int irq, void *data);
const struct v4l2_format_info *rzg2l_cru_format_from_pixel(u32 format);
int rzg2l_cru_ip_subdev_register(struct rzg2l_cru_dev *cru);
void rzg2l_cru_ip_subdev_unregister(struct rzg2l_cru_dev *cru);
struct v4l2_mbus_framefmt *rzg2l_cru_ip_get_src_fmt(struct rzg2l_cru_dev *cru);
const struct rzg2l_cru_ip_format *rzg2l_cru_ip_code_to_fmt(unsigned int code);
const struct rzg2l_cru_ip_format *rzg2l_cru_ip_format_to_fmt(u32 format);
const struct rzg2l_cru_ip_format *rzg2l_cru_ip_index_to_fmt(u32 index);
bool rzg2l_cru_ip_fmt_supports_mbus_code(const struct rzg2l_cru_ip_format *fmt,
unsigned int code);
void rzg2l_cru_enable_interrupts(struct rzg2l_cru_dev *cru);
void rzg2l_cru_disable_interrupts(struct rzg2l_cru_dev *cru);
void rzg3e_cru_enable_interrupts(struct rzg2l_cru_dev *cru);
void rzg3e_cru_disable_interrupts(struct rzg2l_cru_dev *cru);
bool rzg2l_fifo_empty(struct rzg2l_cru_dev *cru);
bool rzg3e_fifo_empty(struct rzg2l_cru_dev *cru);
#endif
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,379 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Driver for Renesas RZ/G2L CRU
*
* Copyright (C) 2022 Renesas Electronics Corp.
*/
#include <linux/delay.h>
#include <media/mipi-csi2.h>
#include "rzg2l-cru.h"
#include "rzg2l-cru-regs.h"
static const struct rzg2l_cru_ip_format rzg2l_cru_ip_formats[] = {
{
.codes = {
MEDIA_BUS_FMT_UYVY8_1X16,
},
.datatype = MIPI_CSI2_DT_YUV422_8B,
.format = V4L2_PIX_FMT_UYVY,
.icndmr = ICnDMR_YCMODE_UYVY,
.yuv = true,
},
{
.codes = {
MEDIA_BUS_FMT_SBGGR8_1X8,
},
.format = V4L2_PIX_FMT_SBGGR8,
.datatype = MIPI_CSI2_DT_RAW8,
.icndmr = 0,
.yuv = false,
},
{
.codes = {
MEDIA_BUS_FMT_SGBRG8_1X8,
},
.format = V4L2_PIX_FMT_SGBRG8,
.datatype = MIPI_CSI2_DT_RAW8,
.icndmr = 0,
.yuv = false,
},
{
.codes = {
MEDIA_BUS_FMT_SGRBG8_1X8,
},
.format = V4L2_PIX_FMT_SGRBG8,
.datatype = MIPI_CSI2_DT_RAW8,
.icndmr = 0,
.yuv = false,
},
{
.codes = {
MEDIA_BUS_FMT_SRGGB8_1X8,
},
.format = V4L2_PIX_FMT_SRGGB8,
.datatype = MIPI_CSI2_DT_RAW8,
.icndmr = 0,
.yuv = false,
},
{
.codes = {
MEDIA_BUS_FMT_SBGGR10_1X10,
MEDIA_BUS_FMT_SGBRG10_1X10,
MEDIA_BUS_FMT_SGRBG10_1X10,
MEDIA_BUS_FMT_SRGGB10_1X10
},
.format = V4L2_PIX_FMT_RAW_CRU10,
.datatype = MIPI_CSI2_DT_RAW10,
.icndmr = 0,
.yuv = false,
},
{
.codes = {
MEDIA_BUS_FMT_SBGGR12_1X12,
MEDIA_BUS_FMT_SGBRG12_1X12,
MEDIA_BUS_FMT_SGRBG12_1X12,
MEDIA_BUS_FMT_SRGGB12_1X12
},
.format = V4L2_PIX_FMT_RAW_CRU12,
.datatype = MIPI_CSI2_DT_RAW12,
.icndmr = 0,
.yuv = false,
},
{
.codes = {
MEDIA_BUS_FMT_SBGGR14_1X14,
MEDIA_BUS_FMT_SGBRG14_1X14,
MEDIA_BUS_FMT_SGRBG14_1X14,
MEDIA_BUS_FMT_SRGGB14_1X14
},
.format = V4L2_PIX_FMT_RAW_CRU14,
.datatype = MIPI_CSI2_DT_RAW14,
.icndmr = 0,
.yuv = false,
},
};
const struct rzg2l_cru_ip_format *rzg2l_cru_ip_code_to_fmt(unsigned int code)
{
unsigned int i, j;
for (i = 0; i < ARRAY_SIZE(rzg2l_cru_ip_formats); i++) {
for (j = 0; j < ARRAY_SIZE(rzg2l_cru_ip_formats[i].codes); j++) {
if (rzg2l_cru_ip_formats[i].codes[j] == code)
return &rzg2l_cru_ip_formats[i];
}
}
return NULL;
}
const struct rzg2l_cru_ip_format *rzg2l_cru_ip_format_to_fmt(u32 format)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(rzg2l_cru_ip_formats); i++) {
if (rzg2l_cru_ip_formats[i].format == format)
return &rzg2l_cru_ip_formats[i];
}
return NULL;
}
const struct rzg2l_cru_ip_format *rzg2l_cru_ip_index_to_fmt(u32 index)
{
if (index >= ARRAY_SIZE(rzg2l_cru_ip_formats))
return NULL;
return &rzg2l_cru_ip_formats[index];
}
bool rzg2l_cru_ip_fmt_supports_mbus_code(const struct rzg2l_cru_ip_format *fmt,
unsigned int code)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(fmt->codes); i++)
if (fmt->codes[i] == code)
return true;
return false;
}
struct v4l2_mbus_framefmt *rzg2l_cru_ip_get_src_fmt(struct rzg2l_cru_dev *cru)
{
struct v4l2_subdev_state *state;
struct v4l2_mbus_framefmt *fmt;
state = v4l2_subdev_lock_and_get_active_state(&cru->ip.subdev);
fmt = v4l2_subdev_state_get_format(state, 1);
v4l2_subdev_unlock_state(state);
return fmt;
}
static int rzg2l_cru_ip_s_stream(struct v4l2_subdev *sd, int enable)
{
struct rzg2l_cru_dev *cru;
int s_stream_ret = 0;
int ret;
cru = v4l2_get_subdevdata(sd);
if (!enable) {
ret = v4l2_subdev_call(cru->ip.remote, video, s_stream, enable);
if (ret)
s_stream_ret = ret;
ret = v4l2_subdev_call(cru->ip.remote, video, post_streamoff);
if (ret == -ENOIOCTLCMD)
ret = 0;
if (ret && !s_stream_ret)
s_stream_ret = ret;
rzg2l_cru_stop_image_processing(cru);
} else {
ret = v4l2_subdev_call(cru->ip.remote, video, pre_streamon, 0);
if (ret == -ENOIOCTLCMD)
ret = 0;
if (ret)
return ret;
fsleep(1000);
ret = rzg2l_cru_start_image_processing(cru);
if (ret) {
v4l2_subdev_call(cru->ip.remote, video, post_streamoff);
return ret;
}
ret = v4l2_subdev_call(cru->ip.remote, video, s_stream, enable);
if (!ret || ret == -ENOIOCTLCMD)
return 0;
s_stream_ret = ret;
v4l2_subdev_call(cru->ip.remote, video, post_streamoff);
rzg2l_cru_stop_image_processing(cru);
}
return s_stream_ret;
}
static int rzg2l_cru_ip_set_format(struct v4l2_subdev *sd,
struct v4l2_subdev_state *state,
struct v4l2_subdev_format *fmt)
{
struct rzg2l_cru_dev *cru = v4l2_get_subdevdata(sd);
const struct rzg2l_cru_info *info = cru->info;
struct v4l2_mbus_framefmt *src_format;
struct v4l2_mbus_framefmt *sink_format;
src_format = v4l2_subdev_state_get_format(state, RZG2L_CRU_IP_SOURCE);
if (fmt->pad == RZG2L_CRU_IP_SOURCE) {
fmt->format = *src_format;
return 0;
}
sink_format = v4l2_subdev_state_get_format(state, fmt->pad);
if (!rzg2l_cru_ip_code_to_fmt(fmt->format.code))
sink_format->code = rzg2l_cru_ip_formats[0].codes[0];
else
sink_format->code = fmt->format.code;
sink_format->field = V4L2_FIELD_NONE;
sink_format->colorspace = fmt->format.colorspace;
sink_format->xfer_func = fmt->format.xfer_func;
sink_format->ycbcr_enc = fmt->format.ycbcr_enc;
sink_format->quantization = fmt->format.quantization;
sink_format->width = clamp_t(u32, fmt->format.width,
RZG2L_CRU_MIN_INPUT_WIDTH, info->max_width);
sink_format->height = clamp_t(u32, fmt->format.height,
RZG2L_CRU_MIN_INPUT_HEIGHT, info->max_height);
fmt->format = *sink_format;
/* propagate format to source pad */
*src_format = *sink_format;
return 0;
}
static int rzg2l_cru_ip_enum_mbus_code(struct v4l2_subdev *sd,
struct v4l2_subdev_state *state,
struct v4l2_subdev_mbus_code_enum *code)
{
unsigned int index = code->index;
unsigned int i, j;
for (i = 0; i < ARRAY_SIZE(rzg2l_cru_ip_formats); i++) {
const struct rzg2l_cru_ip_format *fmt = &rzg2l_cru_ip_formats[i];
for (j = 0; j < ARRAY_SIZE(fmt->codes); j++) {
if (!fmt->codes[j])
continue;
if (!index) {
code->code = fmt->codes[j];
return 0;
}
index--;
}
}
return -EINVAL;
}
static int rzg2l_cru_ip_enum_frame_size(struct v4l2_subdev *sd,
struct v4l2_subdev_state *state,
struct v4l2_subdev_frame_size_enum *fse)
{
struct rzg2l_cru_dev *cru = v4l2_get_subdevdata(sd);
const struct rzg2l_cru_info *info = cru->info;
if (fse->index != 0)
return -EINVAL;
if (!rzg2l_cru_ip_code_to_fmt(fse->code))
return -EINVAL;
fse->min_width = RZG2L_CRU_MIN_INPUT_WIDTH;
fse->min_height = RZG2L_CRU_MIN_INPUT_HEIGHT;
fse->max_width = info->max_width;
fse->max_height = info->max_height;
return 0;
}
static int rzg2l_cru_ip_init_state(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state)
{
struct v4l2_subdev_format fmt = { .pad = RZG2L_CRU_IP_SINK, };
fmt.format.width = RZG2L_CRU_MIN_INPUT_WIDTH;
fmt.format.height = RZG2L_CRU_MIN_INPUT_HEIGHT;
fmt.format.field = V4L2_FIELD_NONE;
fmt.format.code = MEDIA_BUS_FMT_UYVY8_1X16;
fmt.format.colorspace = V4L2_COLORSPACE_SRGB;
fmt.format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
fmt.format.quantization = V4L2_QUANTIZATION_DEFAULT;
fmt.format.xfer_func = V4L2_XFER_FUNC_DEFAULT;
return rzg2l_cru_ip_set_format(sd, sd_state, &fmt);
}
static const struct v4l2_subdev_video_ops rzg2l_cru_ip_video_ops = {
.s_stream = rzg2l_cru_ip_s_stream,
};
static const struct v4l2_subdev_pad_ops rzg2l_cru_ip_pad_ops = {
.enum_mbus_code = rzg2l_cru_ip_enum_mbus_code,
.enum_frame_size = rzg2l_cru_ip_enum_frame_size,
.get_fmt = v4l2_subdev_get_fmt,
.set_fmt = rzg2l_cru_ip_set_format,
};
static const struct v4l2_subdev_ops rzg2l_cru_ip_subdev_ops = {
.video = &rzg2l_cru_ip_video_ops,
.pad = &rzg2l_cru_ip_pad_ops,
};
static const struct v4l2_subdev_internal_ops rzg2l_cru_ip_internal_ops = {
.init_state = rzg2l_cru_ip_init_state,
};
static const struct media_entity_operations rzg2l_cru_ip_entity_ops = {
.link_validate = v4l2_subdev_link_validate,
};
int rzg2l_cru_ip_subdev_register(struct rzg2l_cru_dev *cru)
{
struct rzg2l_cru_ip *ip = &cru->ip;
int ret;
ip->subdev.dev = cru->dev;
v4l2_subdev_init(&ip->subdev, &rzg2l_cru_ip_subdev_ops);
ip->subdev.internal_ops = &rzg2l_cru_ip_internal_ops;
v4l2_set_subdevdata(&ip->subdev, cru);
snprintf(ip->subdev.name, sizeof(ip->subdev.name),
"cru-ip-%s", dev_name(cru->dev));
ip->subdev.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
ip->subdev.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
ip->subdev.entity.ops = &rzg2l_cru_ip_entity_ops;
ip->pads[RZG2L_CRU_IP_SINK].flags = MEDIA_PAD_FL_SINK |
MEDIA_PAD_FL_MUST_CONNECT;
ip->pads[RZG2L_CRU_IP_SOURCE].flags = MEDIA_PAD_FL_SOURCE |
MEDIA_PAD_FL_MUST_CONNECT;
ret = media_entity_pads_init(&ip->subdev.entity, 2, ip->pads);
if (ret)
return ret;
ret = v4l2_subdev_init_finalize(&ip->subdev);
if (ret < 0)
goto entity_cleanup;
ret = v4l2_device_register_subdev(&cru->v4l2_dev, &ip->subdev);
if (ret < 0)
goto error_subdev;
return 0;
error_subdev:
v4l2_subdev_cleanup(&ip->subdev);
entity_cleanup:
media_entity_cleanup(&ip->subdev.entity);
return ret;
}
void rzg2l_cru_ip_subdev_unregister(struct rzg2l_cru_dev *cru)
{
struct rzg2l_cru_ip *ip = &cru->ip;
media_entity_cleanup(&ip->subdev.entity);
v4l2_subdev_cleanup(&ip->subdev);
v4l2_device_unregister_subdev(&ip->subdev);
}
File diff suppressed because it is too large Load Diff