1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 NextThing Co
4 * Copyright (C) 2016-2019 Bootlin
6 * Author: Maxime Ripard <maxime.ripard@bootlin.com>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
14 #include <linux/of_device.h>
15 #include <linux/of_graph.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/reset.h>
19 #include <linux/videodev2.h>
21 #include <media/v4l2-dev.h>
22 #include <media/v4l2-device.h>
23 #include <media/v4l2-fwnode.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-mediabus.h>
27 #include <media/videobuf2-core.h>
28 #include <media/videobuf2-dma-contig.h>
30 #include "sun4i_csi.h"
32 struct sun4i_csi_traits
{
33 unsigned int channels
;
34 unsigned int max_width
;
38 static const struct media_entity_operations sun4i_csi_video_entity_ops
= {
39 .link_validate
= v4l2_subdev_link_validate
,
42 static int sun4i_csi_notify_bound(struct v4l2_async_notifier
*notifier
,
43 struct v4l2_subdev
*subdev
,
44 struct v4l2_async_subdev
*asd
)
46 struct sun4i_csi
*csi
= container_of(notifier
, struct sun4i_csi
,
49 csi
->src_subdev
= subdev
;
50 csi
->src_pad
= media_entity_get_fwnode_pad(&subdev
->entity
,
53 if (csi
->src_pad
< 0) {
54 dev_err(csi
->dev
, "Couldn't find output pad for subdev %s\n",
59 dev_dbg(csi
->dev
, "Bound %s pad: %d\n", subdev
->name
, csi
->src_pad
);
63 static int sun4i_csi_notify_complete(struct v4l2_async_notifier
*notifier
)
65 struct sun4i_csi
*csi
= container_of(notifier
, struct sun4i_csi
,
67 struct v4l2_subdev
*subdev
= &csi
->subdev
;
68 struct video_device
*vdev
= &csi
->vdev
;
71 ret
= v4l2_device_register_subdev(&csi
->v4l
, subdev
);
75 ret
= sun4i_csi_v4l2_register(csi
);
79 ret
= media_device_register(&csi
->mdev
);
83 /* Create link from subdev to main device */
84 ret
= media_create_pad_link(&subdev
->entity
, CSI_SUBDEV_SOURCE
,
86 MEDIA_LNK_FL_ENABLED
|
87 MEDIA_LNK_FL_IMMUTABLE
);
91 ret
= media_create_pad_link(&csi
->src_subdev
->entity
, csi
->src_pad
,
92 &subdev
->entity
, CSI_SUBDEV_SINK
,
93 MEDIA_LNK_FL_ENABLED
|
94 MEDIA_LNK_FL_IMMUTABLE
);
98 ret
= v4l2_device_register_subdev_nodes(&csi
->v4l
);
100 goto err_clean_media
;
105 media_device_unregister(&csi
->mdev
);
110 static const struct v4l2_async_notifier_operations sun4i_csi_notify_ops
= {
111 .bound
= sun4i_csi_notify_bound
,
112 .complete
= sun4i_csi_notify_complete
,
115 static int sun4i_csi_notifier_init(struct sun4i_csi
*csi
)
117 struct v4l2_fwnode_endpoint vep
= {
118 .bus_type
= V4L2_MBUS_PARALLEL
,
120 struct fwnode_handle
*ep
;
123 v4l2_async_notifier_init(&csi
->notifier
);
125 ep
= fwnode_graph_get_endpoint_by_id(dev_fwnode(csi
->dev
), 0, 0,
126 FWNODE_GRAPH_ENDPOINT_NEXT
);
130 ret
= v4l2_fwnode_endpoint_parse(ep
, &vep
);
134 csi
->bus
= vep
.bus
.parallel
;
136 ret
= v4l2_async_notifier_add_fwnode_remote_subdev(&csi
->notifier
,
141 csi
->notifier
.ops
= &sun4i_csi_notify_ops
;
144 fwnode_handle_put(ep
);
148 static int sun4i_csi_probe(struct platform_device
*pdev
)
150 struct v4l2_subdev
*subdev
;
151 struct video_device
*vdev
;
152 struct sun4i_csi
*csi
;
153 struct resource
*res
;
157 csi
= devm_kzalloc(&pdev
->dev
, sizeof(*csi
), GFP_KERNEL
);
160 platform_set_drvdata(pdev
, csi
);
161 csi
->dev
= &pdev
->dev
;
162 subdev
= &csi
->subdev
;
165 csi
->traits
= of_device_get_match_data(&pdev
->dev
);
170 * On Allwinner SoCs, some high memory bandwidth devices do DMA
171 * directly over the memory bus (called MBUS), instead of the
172 * system bus. The memory bus has a different addressing scheme
173 * without the DRAM starting offset.
175 * In some cases this can be described by an interconnect in
176 * the device tree. In other cases where the hardware is not
177 * fully understood and the interconnect is left out of the
178 * device tree, fall back to a default offset.
180 if (of_find_property(csi
->dev
->of_node
, "interconnects", NULL
)) {
181 ret
= of_dma_configure(csi
->dev
, csi
->dev
->of_node
, true);
185 #ifdef PHYS_PFN_OFFSET
186 csi
->dev
->dma_pfn_offset
= PHYS_PFN_OFFSET
;
190 csi
->mdev
.dev
= csi
->dev
;
191 strscpy(csi
->mdev
.model
, "Allwinner Video Capture Device",
192 sizeof(csi
->mdev
.model
));
193 csi
->mdev
.hw_revision
= 0;
194 media_device_init(&csi
->mdev
);
195 csi
->v4l
.mdev
= &csi
->mdev
;
197 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
198 csi
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
199 if (IS_ERR(csi
->regs
))
200 return PTR_ERR(csi
->regs
);
202 irq
= platform_get_irq(pdev
, 0);
206 csi
->bus_clk
= devm_clk_get(&pdev
->dev
, "bus");
207 if (IS_ERR(csi
->bus_clk
)) {
208 dev_err(&pdev
->dev
, "Couldn't get our bus clock\n");
209 return PTR_ERR(csi
->bus_clk
);
212 if (csi
->traits
->has_isp
) {
213 csi
->isp_clk
= devm_clk_get(&pdev
->dev
, "isp");
214 if (IS_ERR(csi
->isp_clk
)) {
215 dev_err(&pdev
->dev
, "Couldn't get our ISP clock\n");
216 return PTR_ERR(csi
->isp_clk
);
220 csi
->ram_clk
= devm_clk_get(&pdev
->dev
, "ram");
221 if (IS_ERR(csi
->ram_clk
)) {
222 dev_err(&pdev
->dev
, "Couldn't get our ram clock\n");
223 return PTR_ERR(csi
->ram_clk
);
226 csi
->rst
= devm_reset_control_get(&pdev
->dev
, NULL
);
227 if (IS_ERR(csi
->rst
)) {
228 dev_err(&pdev
->dev
, "Couldn't get our reset line\n");
229 return PTR_ERR(csi
->rst
);
232 /* Initialize subdev */
233 v4l2_subdev_init(subdev
, &sun4i_csi_subdev_ops
);
234 subdev
->flags
= V4L2_SUBDEV_FL_HAS_DEVNODE
| V4L2_SUBDEV_FL_HAS_EVENTS
;
235 subdev
->entity
.function
= MEDIA_ENT_F_VID_IF_BRIDGE
;
236 subdev
->owner
= THIS_MODULE
;
237 snprintf(subdev
->name
, sizeof(subdev
->name
), "sun4i-csi-0");
238 v4l2_set_subdevdata(subdev
, csi
);
240 csi
->subdev_pads
[CSI_SUBDEV_SINK
].flags
=
241 MEDIA_PAD_FL_SINK
| MEDIA_PAD_FL_MUST_CONNECT
;
242 csi
->subdev_pads
[CSI_SUBDEV_SOURCE
].flags
= MEDIA_PAD_FL_SOURCE
;
243 ret
= media_entity_pads_init(&subdev
->entity
, CSI_SUBDEV_PADS
,
248 csi
->vdev_pad
.flags
= MEDIA_PAD_FL_SINK
| MEDIA_PAD_FL_MUST_CONNECT
;
249 vdev
->entity
.ops
= &sun4i_csi_video_entity_ops
;
250 ret
= media_entity_pads_init(&vdev
->entity
, 1, &csi
->vdev_pad
);
254 ret
= sun4i_csi_dma_register(csi
, irq
);
258 ret
= sun4i_csi_notifier_init(csi
);
260 goto err_unregister_media
;
262 ret
= v4l2_async_notifier_register(&csi
->v4l
, &csi
->notifier
);
264 dev_err(csi
->dev
, "Couldn't register our notifier.\n");
265 goto err_unregister_media
;
268 pm_runtime_enable(&pdev
->dev
);
272 err_unregister_media
:
273 media_device_unregister(&csi
->mdev
);
274 sun4i_csi_dma_unregister(csi
);
277 media_device_cleanup(&csi
->mdev
);
282 static int sun4i_csi_remove(struct platform_device
*pdev
)
284 struct sun4i_csi
*csi
= platform_get_drvdata(pdev
);
286 v4l2_async_notifier_unregister(&csi
->notifier
);
287 v4l2_async_notifier_cleanup(&csi
->notifier
);
288 media_device_unregister(&csi
->mdev
);
289 sun4i_csi_dma_unregister(csi
);
290 media_device_cleanup(&csi
->mdev
);
295 static const struct sun4i_csi_traits sun4i_a10_csi1_traits
= {
301 static const struct sun4i_csi_traits sun7i_a20_csi0_traits
= {
307 static const struct of_device_id sun4i_csi_of_match
[] = {
308 { .compatible
= "allwinner,sun4i-a10-csi1", .data
= &sun4i_a10_csi1_traits
},
309 { .compatible
= "allwinner,sun7i-a20-csi0", .data
= &sun7i_a20_csi0_traits
},
312 MODULE_DEVICE_TABLE(of
, sun4i_csi_of_match
);
314 static int __maybe_unused
sun4i_csi_runtime_resume(struct device
*dev
)
316 struct sun4i_csi
*csi
= dev_get_drvdata(dev
);
318 reset_control_deassert(csi
->rst
);
319 clk_prepare_enable(csi
->bus_clk
);
320 clk_prepare_enable(csi
->ram_clk
);
321 clk_set_rate(csi
->isp_clk
, 80000000);
322 clk_prepare_enable(csi
->isp_clk
);
324 writel(1, csi
->regs
+ CSI_EN_REG
);
329 static int __maybe_unused
sun4i_csi_runtime_suspend(struct device
*dev
)
331 struct sun4i_csi
*csi
= dev_get_drvdata(dev
);
333 clk_disable_unprepare(csi
->isp_clk
);
334 clk_disable_unprepare(csi
->ram_clk
);
335 clk_disable_unprepare(csi
->bus_clk
);
337 reset_control_assert(csi
->rst
);
342 static const struct dev_pm_ops sun4i_csi_pm_ops
= {
343 SET_RUNTIME_PM_OPS(sun4i_csi_runtime_suspend
,
344 sun4i_csi_runtime_resume
,
348 static struct platform_driver sun4i_csi_driver
= {
349 .probe
= sun4i_csi_probe
,
350 .remove
= sun4i_csi_remove
,
353 .of_match_table
= sun4i_csi_of_match
,
354 .pm
= &sun4i_csi_pm_ops
,
357 module_platform_driver(sun4i_csi_driver
);
359 MODULE_DESCRIPTION("Allwinner A10 Camera Sensor Interface driver");
360 MODULE_AUTHOR("Maxime Ripard <mripard@kernel.org>");
361 MODULE_LICENSE("GPL");