1 From 0dc93c9321ba947ac429baeb58202496e4b0f219 Mon Sep 17 00:00:00 2001
2 From: Jack Zhu <jack.zhu@starfivetech.com>
3 Date: Fri, 12 May 2023 18:28:41 +0800
4 Subject: [PATCH 083/122] media: starfive: Add basic driver
6 Add basic platform driver for StarFive Camera Subsystem.
8 Signed-off-by: Jack Zhu <jack.zhu@starfivetech.com>
9 Reviewed-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>
11 drivers/media/platform/Kconfig | 1 +
12 drivers/media/platform/Makefile | 1 +
13 drivers/media/platform/starfive/Kconfig | 19 +
14 drivers/media/platform/starfive/Makefile | 9 +
15 drivers/media/platform/starfive/stf_camss.c | 372 +++++++++++++++++++
16 drivers/media/platform/starfive/stf_camss.h | 153 ++++++++
17 drivers/media/platform/starfive/stf_common.h | 18 +
18 7 files changed, 573 insertions(+)
19 create mode 100644 drivers/media/platform/starfive/Kconfig
20 create mode 100644 drivers/media/platform/starfive/Makefile
21 create mode 100644 drivers/media/platform/starfive/stf_camss.c
22 create mode 100644 drivers/media/platform/starfive/stf_camss.h
23 create mode 100644 drivers/media/platform/starfive/stf_common.h
25 --- a/drivers/media/platform/Kconfig
26 +++ b/drivers/media/platform/Kconfig
27 @@ -79,6 +79,7 @@ source "drivers/media/platform/renesas/K
28 source "drivers/media/platform/rockchip/Kconfig"
29 source "drivers/media/platform/samsung/Kconfig"
30 source "drivers/media/platform/st/Kconfig"
31 +source "drivers/media/platform/starfive/Kconfig"
32 source "drivers/media/platform/sunxi/Kconfig"
33 source "drivers/media/platform/ti/Kconfig"
34 source "drivers/media/platform/verisilicon/Kconfig"
35 --- a/drivers/media/platform/Makefile
36 +++ b/drivers/media/platform/Makefile
37 @@ -22,6 +22,7 @@ obj-y += renesas/
46 +++ b/drivers/media/platform/starfive/Kconfig
48 +# SPDX-License-Identifier: GPL-2.0-only
50 +comment "Starfive media platform drivers"
52 +config VIDEO_STARFIVE_CAMSS
53 + tristate "Starfive Camera Subsystem driver"
54 + depends on V4L_PLATFORM_DRIVERS
55 + depends on VIDEO_DEV && OF
57 + select MEDIA_CONTROLLER
58 + select VIDEO_V4L2_SUBDEV_API
59 + select VIDEOBUF2_DMA_CONTIG
62 + Enable this to support for the Starfive Camera subsystem
63 + found on Starfive JH7110 SoC.
65 + To compile this driver as a module, choose M here: the
66 + module will be called stf-camss.
68 +++ b/drivers/media/platform/starfive/Makefile
70 +# SPDX-License-Identifier: GPL-2.0
72 +# Makefile for StarFive camera subsystem driver.
75 +starfive-camss-objs += \
78 +obj-$(CONFIG_VIDEO_STARFIVE_CAMSS) += starfive-camss.o \
80 +++ b/drivers/media/platform/starfive/stf_camss.c
82 +// SPDX-License-Identifier: GPL-2.0
86 + * Starfive Camera Subsystem driver
88 + * Copyright (C) 2021-2023 StarFive Technology Co., Ltd.
90 +#include <linux/module.h>
91 +#include <linux/of.h>
92 +#include <linux/of_graph.h>
93 +#include <linux/platform_device.h>
94 +#include <linux/pm_runtime.h>
95 +#include <linux/videodev2.h>
96 +#include <media/media-device.h>
97 +#include <media/v4l2-async.h>
98 +#include <media/v4l2-fwnode.h>
99 +#include <media/v4l2-mc.h>
101 +#include "stf_camss.h"
103 +static const char * const stfcamss_clocks[] = {
105 + "clk_wrapper_clk_c",
108 + "clk_mipi_rx0_pxl",
113 +static const char * const stfcamss_resets[] = {
122 +static int stfcamss_get_mem_res(struct platform_device *pdev,
123 + struct stfcamss *stfcamss)
125 + stfcamss->syscon_base =
126 + devm_platform_ioremap_resource_byname(pdev, "syscon");
127 + if (IS_ERR(stfcamss->syscon_base))
128 + return PTR_ERR(stfcamss->syscon_base);
130 + stfcamss->isp_base =
131 + devm_platform_ioremap_resource_byname(pdev, "isp");
132 + if (IS_ERR(stfcamss->isp_base))
133 + return PTR_ERR(stfcamss->isp_base);
139 + * stfcamss_of_parse_endpoint_node - Parse port endpoint node
141 + * @node: Device node to be parsed
142 + * @csd: Parsed data from port endpoint node
144 + * Return 0 on success or a negative error code on failure
146 +static int stfcamss_of_parse_endpoint_node(struct device *dev,
147 + struct device_node *node,
148 + struct stfcamss_async_subdev *csd)
150 + struct v4l2_fwnode_endpoint vep = { { 0 } };
152 + v4l2_fwnode_endpoint_parse(of_fwnode_handle(node), &vep);
153 + dev_dbg(dev, "vep.base.port = 0x%x, id = 0x%x\n",
154 + vep.base.port, vep.base.id);
156 + csd->port = vep.base.port;
162 + * stfcamss_of_parse_ports - Parse ports node
163 + * @stfcamss: STFCAMSS device
165 + * Return number of "port" nodes found in "ports" node
167 +static int stfcamss_of_parse_ports(struct stfcamss *stfcamss)
169 + struct device *dev = stfcamss->dev;
170 + struct device_node *node = NULL;
171 + struct device_node *remote = NULL;
172 + int ret, num_subdevs = 0;
174 + for_each_endpoint_of_node(dev->of_node, node) {
175 + struct stfcamss_async_subdev *csd;
177 + if (!of_device_is_available(node))
180 + remote = of_graph_get_remote_port_parent(node);
182 + dev_err(dev, "Cannot get remote parent\n");
187 + csd = v4l2_async_nf_add_fwnode(&stfcamss->notifier,
188 + of_fwnode_handle(remote),
189 + struct stfcamss_async_subdev);
190 + of_node_put(remote);
192 + ret = PTR_ERR(csd);
196 + ret = stfcamss_of_parse_endpoint_node(dev, node, csd);
203 + return num_subdevs;
210 +static int stfcamss_subdev_notifier_bound(struct v4l2_async_notifier *async,
211 + struct v4l2_subdev *subdev,
212 + struct v4l2_async_subdev *asd)
214 + struct stfcamss *stfcamss =
215 + container_of(async, struct stfcamss, notifier);
216 + struct host_data *host_data = &stfcamss->host_data;
217 + struct media_entity *source;
220 + source = &subdev->entity;
222 + for (i = 0; i < source->num_pads; i++) {
223 + if (source->pads[i].flags & MEDIA_PAD_FL_SOURCE)
227 + if (i == source->num_pads) {
228 + dev_err(stfcamss->dev, "No source pad in external entity\n");
232 + for (j = 0; host_data->host_entity[j] && (j < HOST_ENTITY_MAX); j++) {
233 + struct media_entity *input;
236 + input = host_data->host_entity[j];
238 + ret = media_create_pad_link(
243 + source->function == MEDIA_ENT_F_CAM_SENSOR ?
244 + MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED :
247 + dev_err(stfcamss->dev,
248 + "Failed to link %s->%s entities: %d\n",
249 + source->name, input->name, ret);
257 +static int stfcamss_subdev_notifier_complete(struct v4l2_async_notifier *ntf)
259 + struct stfcamss *stfcamss =
260 + container_of(ntf, struct stfcamss, notifier);
262 + return v4l2_device_register_subdev_nodes(&stfcamss->v4l2_dev);
265 +static const struct v4l2_async_notifier_operations
266 +stfcamss_subdev_notifier_ops = {
267 + .bound = stfcamss_subdev_notifier_bound,
268 + .complete = stfcamss_subdev_notifier_complete,
271 +static const struct media_device_ops stfcamss_media_ops = {
272 + .link_notify = v4l2_pipeline_link_notify,
275 +static void stfcamss_mc_init(struct platform_device *pdev,
276 + struct stfcamss *stfcamss)
278 + stfcamss->media_dev.dev = stfcamss->dev;
279 + strscpy(stfcamss->media_dev.model, "Starfive Camera Subsystem",
280 + sizeof(stfcamss->media_dev.model));
281 + snprintf(stfcamss->media_dev.bus_info,
282 + sizeof(stfcamss->media_dev.bus_info),
283 + "%s:%s", dev_bus_name(&pdev->dev), pdev->name);
284 + stfcamss->media_dev.hw_revision = 0x01;
285 + stfcamss->media_dev.ops = &stfcamss_media_ops;
286 + media_device_init(&stfcamss->media_dev);
288 + stfcamss->v4l2_dev.mdev = &stfcamss->media_dev;
292 + * stfcamss_probe - Probe STFCAMSS platform device
293 + * @pdev: Pointer to STFCAMSS platform device
295 + * Return 0 on success or a negative error code on failure
297 +static int stfcamss_probe(struct platform_device *pdev)
299 + struct stfcamss *stfcamss;
300 + struct device *dev = &pdev->dev;
301 + int ret = 0, i, num_subdevs;
303 + stfcamss = devm_kzalloc(dev, sizeof(*stfcamss), GFP_KERNEL);
307 + for (i = 0; i < ARRAY_SIZE(stfcamss->irq); ++i) {
308 + stfcamss->irq[i] = platform_get_irq(pdev, i);
309 + if (stfcamss->irq[i] < 0)
310 + return dev_err_probe(&pdev->dev, stfcamss->irq[i],
311 + "Failed to get clock%d", i);
314 + stfcamss->nclks = ARRAY_SIZE(stfcamss->sys_clk);
315 + for (i = 0; i < ARRAY_SIZE(stfcamss->sys_clk); ++i)
316 + stfcamss->sys_clk[i].id = stfcamss_clocks[i];
317 + ret = devm_clk_bulk_get(dev, stfcamss->nclks, stfcamss->sys_clk);
319 + dev_err(dev, "Failed to get clk controls\n");
323 + stfcamss->nrsts = ARRAY_SIZE(stfcamss->sys_rst);
324 + for (i = 0; i < ARRAY_SIZE(stfcamss->sys_rst); ++i)
325 + stfcamss->sys_rst[i].id = stfcamss_resets[i];
326 + ret = devm_reset_control_bulk_get_shared(dev, stfcamss->nrsts,
327 + stfcamss->sys_rst);
329 + dev_err(dev, "Failed to get reset controls\n");
333 + ret = stfcamss_get_mem_res(pdev, stfcamss);
335 + dev_err(dev, "Could not map registers\n");
339 + stfcamss->dev = dev;
340 + platform_set_drvdata(pdev, stfcamss);
342 + v4l2_async_nf_init(&stfcamss->notifier);
344 + num_subdevs = stfcamss_of_parse_ports(stfcamss);
345 + if (num_subdevs < 0) {
346 + dev_err(dev, "Failed to find subdevices\n");
350 + stfcamss_mc_init(pdev, stfcamss);
352 + ret = v4l2_device_register(stfcamss->dev, &stfcamss->v4l2_dev);
354 + dev_err(dev, "Failed to register V4L2 device: %d\n", ret);
355 + goto err_cleanup_notifier;
358 + ret = media_device_register(&stfcamss->media_dev);
360 + dev_err(dev, "Failed to register media device: %d\n", ret);
361 + goto err_unregister_device;
364 + stfcamss->notifier.ops = &stfcamss_subdev_notifier_ops;
365 + ret = v4l2_async_nf_register(&stfcamss->v4l2_dev, &stfcamss->notifier);
367 + dev_err(dev, "Failed to register async subdev nodes: %d\n",
369 + goto err_unregister_media_dev;
372 + pm_runtime_enable(dev);
376 +err_unregister_media_dev:
377 + media_device_unregister(&stfcamss->media_dev);
378 +err_unregister_device:
379 + v4l2_device_unregister(&stfcamss->v4l2_dev);
380 +err_cleanup_notifier:
381 + v4l2_async_nf_cleanup(&stfcamss->notifier);
386 + * stfcamss_remove - Remove STFCAMSS platform device
387 + * @pdev: Pointer to STFCAMSS platform device
389 + * Always returns 0.
391 +static int stfcamss_remove(struct platform_device *pdev)
393 + struct stfcamss *stfcamss = platform_get_drvdata(pdev);
395 + v4l2_device_unregister(&stfcamss->v4l2_dev);
396 + media_device_cleanup(&stfcamss->media_dev);
397 + pm_runtime_disable(&pdev->dev);
402 +static const struct of_device_id stfcamss_of_match[] = {
403 + { .compatible = "starfive,jh7110-camss" },
404 + { /* sentinel */ },
407 +MODULE_DEVICE_TABLE(of, stfcamss_of_match);
409 +static int __maybe_unused stfcamss_runtime_suspend(struct device *dev)
411 + struct stfcamss *stfcamss = dev_get_drvdata(dev);
413 + reset_control_assert(stfcamss->sys_rst[STF_RST_ISP_TOP_AXI].rstc);
414 + reset_control_assert(stfcamss->sys_rst[STF_RST_ISP_TOP_N].rstc);
415 + clk_disable_unprepare(stfcamss->sys_clk[STF_CLK_ISP_AXI].clk);
416 + clk_disable_unprepare(stfcamss->sys_clk[STF_CLK_ISPCORE_2X].clk);
421 +static int __maybe_unused stfcamss_runtime_resume(struct device *dev)
423 + struct stfcamss *stfcamss = dev_get_drvdata(dev);
425 + clk_prepare_enable(stfcamss->sys_clk[STF_CLK_ISPCORE_2X].clk);
426 + clk_prepare_enable(stfcamss->sys_clk[STF_CLK_ISP_AXI].clk);
427 + reset_control_deassert(stfcamss->sys_rst[STF_RST_ISP_TOP_N].rstc);
428 + reset_control_deassert(stfcamss->sys_rst[STF_RST_ISP_TOP_AXI].rstc);
433 +static const struct dev_pm_ops stfcamss_pm_ops = {
434 + SET_RUNTIME_PM_OPS(stfcamss_runtime_suspend,
435 + stfcamss_runtime_resume,
439 +static struct platform_driver stfcamss_driver = {
440 + .probe = stfcamss_probe,
441 + .remove = stfcamss_remove,
444 + .pm = &stfcamss_pm_ops,
445 + .of_match_table = of_match_ptr(stfcamss_of_match),
449 +module_platform_driver(stfcamss_driver);
451 +MODULE_AUTHOR("StarFive Corporation");
452 +MODULE_DESCRIPTION("StarFive Camera Subsystem driver");
453 +MODULE_LICENSE("GPL");
455 +++ b/drivers/media/platform/starfive/stf_camss.h
457 +/* SPDX-License-Identifier: GPL-2.0 */
461 + * Starfive Camera Subsystem driver
463 + * Copyright (C) 2021-2023 StarFive Technology Co., Ltd.
469 +#include <linux/clk.h>
470 +#include <linux/delay.h>
471 +#include <linux/reset.h>
472 +#include <media/v4l2-device.h>
474 +#include "stf_common.h"
476 +#define DRV_NAME "starfive-camss"
477 +#define STF_DVP_NAME "stf_dvp"
478 +#define STF_CSI_NAME "cdns_csi2rx"
479 +#define STF_ISP_NAME "stf_isp"
480 +#define STF_VIN_NAME "stf_vin"
482 +#define STF_PAD_SINK 0
483 +#define STF_PAD_SRC 1
484 +#define STF_PADS_NUM 2
487 + PORT_NUMBER_DVP_SENSOR = 0,
492 + STF_CLK_APB_FUNC = 0,
493 + STF_CLK_WRAPPER_CLK_C,
496 + STF_CLK_MIPI_RX0_PXL,
497 + STF_CLK_ISPCORE_2X,
503 + STF_RST_WRAPPER_P = 0,
508 + STF_RST_ISP_TOP_AXI,
519 +#define HOST_ENTITY_MAX 2
522 + struct media_entity *host_entity[HOST_ENTITY_MAX];
526 + struct v4l2_device v4l2_dev;
527 + struct media_device media_dev;
528 + struct media_pipeline pipe;
529 + struct device *dev;
530 + struct v4l2_async_notifier notifier;
531 + struct host_data host_data;
532 + void __iomem *syscon_base;
533 + void __iomem *isp_base;
534 + int irq[STF_IRQ_NUM];
535 + struct clk_bulk_data sys_clk[STF_CLK_NUM];
537 + struct reset_control_bulk_data sys_rst[STF_RST_NUM];
541 +struct stfcamss_async_subdev {
542 + struct v4l2_async_subdev asd; /* must be first */
543 + enum port_num port;
546 +static inline u32 stf_isp_reg_read(struct stfcamss *stfcamss, u32 reg)
548 + return ioread32(stfcamss->isp_base + reg);
551 +static inline void stf_isp_reg_write(struct stfcamss *stfcamss,
554 + iowrite32(val, stfcamss->isp_base + reg);
557 +static inline void stf_isp_reg_write_delay(struct stfcamss *stfcamss,
558 + u32 reg, u32 val, u32 delay)
560 + iowrite32(val, stfcamss->isp_base + reg);
561 + usleep_range(1000 * delay, 1000 * delay + 100);
564 +static inline void stf_isp_reg_set_bit(struct stfcamss *stfcamss,
565 + u32 reg, u32 mask, u32 val)
569 + value = ioread32(stfcamss->isp_base + reg) & ~mask;
572 + iowrite32(val, stfcamss->isp_base + reg);
575 +static inline void stf_isp_reg_set(struct stfcamss *stfcamss, u32 reg, u32 mask)
577 + iowrite32(ioread32(stfcamss->isp_base + reg) | mask,
578 + stfcamss->isp_base + reg);
581 +static inline u32 stf_syscon_reg_read(struct stfcamss *stfcamss, u32 reg)
583 + return ioread32(stfcamss->syscon_base + reg);
586 +static inline void stf_syscon_reg_write(struct stfcamss *stfcamss,
589 + iowrite32(val, stfcamss->syscon_base + reg);
592 +static inline void stf_syscon_reg_set_bit(struct stfcamss *stfcamss,
593 + u32 reg, u32 bit_mask)
597 + value = ioread32(stfcamss->syscon_base + reg);
598 + iowrite32(value | bit_mask, stfcamss->syscon_base + reg);
601 +static inline void stf_syscon_reg_clear_bit(struct stfcamss *stfcamss,
602 + u32 reg, u32 bit_mask)
606 + value = ioread32(stfcamss->syscon_base + reg);
607 + iowrite32(value & ~bit_mask, stfcamss->syscon_base + reg);
609 +#endif /* STF_CAMSS_H */
611 +++ b/drivers/media/platform/starfive/stf_common.h
613 +/* SPDX-License-Identifier: GPL-2.0 */
617 + * StarFive Camera Subsystem - Common definitions
619 + * Copyright (C) 2021-2023 StarFive Technology Co., Ltd.
622 +#ifndef STF_COMMON_H
623 +#define STF_COMMON_H
625 +enum stf_subdev_type {
626 + STF_SUBDEV_TYPE_VIN,
627 + STF_SUBDEV_TYPE_ISP,
630 +#endif /* STF_COMMON_H */