1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2016 NextThing Co
4 * Copyright (C) 2016-2019 Bootlin
5 *
6 * Author: Maxime Ripard <maxime.ripard@bootlin.com>
7 */
8
9 #include <linux/clk.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/of.h>
15 #include <linux/of_device.h>
16 #include <linux/of_graph.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/reset.h>
20 #include <linux/videodev2.h>
21
22 #include <media/v4l2-dev.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-fwnode.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-mediabus.h>
27
28 #include <media/videobuf2-core.h>
29 #include <media/videobuf2-dma-contig.h>
30
31 #include "sun4i_csi.h"
32
33 struct sun4i_csi_traits {
34 unsigned int channels;
35 unsigned int max_width;
36 bool has_isp;
37 };
38
39 static const struct media_entity_operations sun4i_csi_video_entity_ops = {
40 .link_validate = v4l2_subdev_link_validate,
41 };
42
sun4i_csi_notify_bound(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_subdev * asd)43 static int sun4i_csi_notify_bound(struct v4l2_async_notifier *notifier,
44 struct v4l2_subdev *subdev,
45 struct v4l2_async_subdev *asd)
46 {
47 struct sun4i_csi *csi = container_of(notifier, struct sun4i_csi,
48 notifier);
49
50 csi->src_subdev = subdev;
51 csi->src_pad = media_entity_get_fwnode_pad(&subdev->entity,
52 subdev->fwnode,
53 MEDIA_PAD_FL_SOURCE);
54 if (csi->src_pad < 0) {
55 dev_err(csi->dev, "Couldn't find output pad for subdev %s\n",
56 subdev->name);
57 return csi->src_pad;
58 }
59
60 dev_dbg(csi->dev, "Bound %s pad: %d\n", subdev->name, csi->src_pad);
61 return 0;
62 }
63
sun4i_csi_notify_complete(struct v4l2_async_notifier * notifier)64 static int sun4i_csi_notify_complete(struct v4l2_async_notifier *notifier)
65 {
66 struct sun4i_csi *csi = container_of(notifier, struct sun4i_csi,
67 notifier);
68 struct v4l2_subdev *subdev = &csi->subdev;
69 struct video_device *vdev = &csi->vdev;
70 int ret;
71
72 ret = v4l2_device_register_subdev(&csi->v4l, subdev);
73 if (ret < 0)
74 return ret;
75
76 ret = sun4i_csi_v4l2_register(csi);
77 if (ret < 0)
78 return ret;
79
80 ret = media_device_register(&csi->mdev);
81 if (ret)
82 return ret;
83
84 /* Create link from subdev to main device */
85 ret = media_create_pad_link(&subdev->entity, CSI_SUBDEV_SOURCE,
86 &vdev->entity, 0,
87 MEDIA_LNK_FL_ENABLED |
88 MEDIA_LNK_FL_IMMUTABLE);
89 if (ret)
90 goto err_clean_media;
91
92 ret = media_create_pad_link(&csi->src_subdev->entity, csi->src_pad,
93 &subdev->entity, CSI_SUBDEV_SINK,
94 MEDIA_LNK_FL_ENABLED |
95 MEDIA_LNK_FL_IMMUTABLE);
96 if (ret)
97 goto err_clean_media;
98
99 ret = v4l2_device_register_subdev_nodes(&csi->v4l);
100 if (ret < 0)
101 goto err_clean_media;
102
103 return 0;
104
105 err_clean_media:
106 media_device_unregister(&csi->mdev);
107
108 return ret;
109 }
110
111 static const struct v4l2_async_notifier_operations sun4i_csi_notify_ops = {
112 .bound = sun4i_csi_notify_bound,
113 .complete = sun4i_csi_notify_complete,
114 };
115
sun4i_csi_notifier_init(struct sun4i_csi * csi)116 static int sun4i_csi_notifier_init(struct sun4i_csi *csi)
117 {
118 struct v4l2_fwnode_endpoint vep = {
119 .bus_type = V4L2_MBUS_PARALLEL,
120 };
121 struct v4l2_async_subdev *asd;
122 struct fwnode_handle *ep;
123 int ret;
124
125 v4l2_async_notifier_init(&csi->notifier);
126
127 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(csi->dev), 0, 0,
128 FWNODE_GRAPH_ENDPOINT_NEXT);
129 if (!ep)
130 return -EINVAL;
131
132 ret = v4l2_fwnode_endpoint_parse(ep, &vep);
133 if (ret)
134 goto out;
135
136 csi->bus = vep.bus.parallel;
137
138 asd = v4l2_async_notifier_add_fwnode_remote_subdev(&csi->notifier,
139 ep, sizeof(*asd));
140 if (IS_ERR(asd)) {
141 ret = PTR_ERR(asd);
142 goto out;
143 }
144
145 csi->notifier.ops = &sun4i_csi_notify_ops;
146
147 out:
148 fwnode_handle_put(ep);
149 return ret;
150 }
151
sun4i_csi_probe(struct platform_device * pdev)152 static int sun4i_csi_probe(struct platform_device *pdev)
153 {
154 struct v4l2_subdev *subdev;
155 struct video_device *vdev;
156 struct sun4i_csi *csi;
157 struct resource *res;
158 int ret;
159 int irq;
160
161 csi = devm_kzalloc(&pdev->dev, sizeof(*csi), GFP_KERNEL);
162 if (!csi)
163 return -ENOMEM;
164 platform_set_drvdata(pdev, csi);
165 csi->dev = &pdev->dev;
166 subdev = &csi->subdev;
167 vdev = &csi->vdev;
168
169 csi->traits = of_device_get_match_data(&pdev->dev);
170 if (!csi->traits)
171 return -EINVAL;
172
173 /*
174 * On Allwinner SoCs, some high memory bandwidth devices do DMA
175 * directly over the memory bus (called MBUS), instead of the
176 * system bus. The memory bus has a different addressing scheme
177 * without the DRAM starting offset.
178 *
179 * In some cases this can be described by an interconnect in
180 * the device tree. In other cases where the hardware is not
181 * fully understood and the interconnect is left out of the
182 * device tree, fall back to a default offset.
183 */
184 if (of_find_property(csi->dev->of_node, "interconnects", NULL)) {
185 ret = of_dma_configure(csi->dev, csi->dev->of_node, true);
186 if (ret)
187 return ret;
188 } else {
189 /*
190 * XXX(hch): this has no business in a driver and needs to move
191 * to the device tree.
192 */
193 #ifdef PHYS_PFN_OFFSET
194 ret = dma_direct_set_offset(csi->dev, PHYS_OFFSET, 0, SZ_4G);
195 if (ret)
196 return ret;
197 #endif
198 }
199
200 csi->mdev.dev = csi->dev;
201 strscpy(csi->mdev.model, "Allwinner Video Capture Device",
202 sizeof(csi->mdev.model));
203 csi->mdev.hw_revision = 0;
204 snprintf(csi->mdev.bus_info, sizeof(csi->mdev.bus_info), "platform:%s",
205 dev_name(csi->dev));
206 media_device_init(&csi->mdev);
207 csi->v4l.mdev = &csi->mdev;
208
209 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
210 csi->regs = devm_ioremap_resource(&pdev->dev, res);
211 if (IS_ERR(csi->regs))
212 return PTR_ERR(csi->regs);
213
214 irq = platform_get_irq(pdev, 0);
215 if (irq < 0)
216 return irq;
217
218 csi->bus_clk = devm_clk_get(&pdev->dev, "bus");
219 if (IS_ERR(csi->bus_clk)) {
220 dev_err(&pdev->dev, "Couldn't get our bus clock\n");
221 return PTR_ERR(csi->bus_clk);
222 }
223
224 if (csi->traits->has_isp) {
225 csi->isp_clk = devm_clk_get(&pdev->dev, "isp");
226 if (IS_ERR(csi->isp_clk)) {
227 dev_err(&pdev->dev, "Couldn't get our ISP clock\n");
228 return PTR_ERR(csi->isp_clk);
229 }
230 }
231
232 csi->ram_clk = devm_clk_get(&pdev->dev, "ram");
233 if (IS_ERR(csi->ram_clk)) {
234 dev_err(&pdev->dev, "Couldn't get our ram clock\n");
235 return PTR_ERR(csi->ram_clk);
236 }
237
238 csi->rst = devm_reset_control_get(&pdev->dev, NULL);
239 if (IS_ERR(csi->rst)) {
240 dev_err(&pdev->dev, "Couldn't get our reset line\n");
241 return PTR_ERR(csi->rst);
242 }
243
244 /* Initialize subdev */
245 v4l2_subdev_init(subdev, &sun4i_csi_subdev_ops);
246 subdev->flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
247 subdev->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
248 subdev->owner = THIS_MODULE;
249 snprintf(subdev->name, sizeof(subdev->name), "sun4i-csi-0");
250 v4l2_set_subdevdata(subdev, csi);
251
252 csi->subdev_pads[CSI_SUBDEV_SINK].flags =
253 MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
254 csi->subdev_pads[CSI_SUBDEV_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
255 ret = media_entity_pads_init(&subdev->entity, CSI_SUBDEV_PADS,
256 csi->subdev_pads);
257 if (ret < 0)
258 return ret;
259
260 csi->vdev_pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
261 vdev->entity.ops = &sun4i_csi_video_entity_ops;
262 ret = media_entity_pads_init(&vdev->entity, 1, &csi->vdev_pad);
263 if (ret < 0)
264 return ret;
265
266 ret = sun4i_csi_dma_register(csi, irq);
267 if (ret)
268 goto err_clean_pad;
269
270 ret = sun4i_csi_notifier_init(csi);
271 if (ret)
272 goto err_unregister_media;
273
274 ret = v4l2_async_notifier_register(&csi->v4l, &csi->notifier);
275 if (ret) {
276 dev_err(csi->dev, "Couldn't register our notifier.\n");
277 goto err_unregister_media;
278 }
279
280 pm_runtime_enable(&pdev->dev);
281
282 return 0;
283
284 err_unregister_media:
285 media_device_unregister(&csi->mdev);
286 sun4i_csi_dma_unregister(csi);
287
288 err_clean_pad:
289 media_device_cleanup(&csi->mdev);
290
291 return ret;
292 }
293
sun4i_csi_remove(struct platform_device * pdev)294 static int sun4i_csi_remove(struct platform_device *pdev)
295 {
296 struct sun4i_csi *csi = platform_get_drvdata(pdev);
297
298 v4l2_async_notifier_unregister(&csi->notifier);
299 v4l2_async_notifier_cleanup(&csi->notifier);
300 vb2_video_unregister_device(&csi->vdev);
301 media_device_unregister(&csi->mdev);
302 sun4i_csi_dma_unregister(csi);
303 media_device_cleanup(&csi->mdev);
304
305 return 0;
306 }
307
308 static const struct sun4i_csi_traits sun4i_a10_csi1_traits = {
309 .channels = 1,
310 .max_width = 24,
311 .has_isp = false,
312 };
313
314 static const struct sun4i_csi_traits sun7i_a20_csi0_traits = {
315 .channels = 4,
316 .max_width = 16,
317 .has_isp = true,
318 };
319
320 static const struct of_device_id sun4i_csi_of_match[] = {
321 { .compatible = "allwinner,sun4i-a10-csi1", .data = &sun4i_a10_csi1_traits },
322 { .compatible = "allwinner,sun7i-a20-csi0", .data = &sun7i_a20_csi0_traits },
323 { /* Sentinel */ }
324 };
325 MODULE_DEVICE_TABLE(of, sun4i_csi_of_match);
326
sun4i_csi_runtime_resume(struct device * dev)327 static int __maybe_unused sun4i_csi_runtime_resume(struct device *dev)
328 {
329 struct sun4i_csi *csi = dev_get_drvdata(dev);
330
331 reset_control_deassert(csi->rst);
332 clk_prepare_enable(csi->bus_clk);
333 clk_prepare_enable(csi->ram_clk);
334 clk_set_rate(csi->isp_clk, 80000000);
335 clk_prepare_enable(csi->isp_clk);
336
337 writel(1, csi->regs + CSI_EN_REG);
338
339 return 0;
340 }
341
sun4i_csi_runtime_suspend(struct device * dev)342 static int __maybe_unused sun4i_csi_runtime_suspend(struct device *dev)
343 {
344 struct sun4i_csi *csi = dev_get_drvdata(dev);
345
346 clk_disable_unprepare(csi->isp_clk);
347 clk_disable_unprepare(csi->ram_clk);
348 clk_disable_unprepare(csi->bus_clk);
349
350 reset_control_assert(csi->rst);
351
352 return 0;
353 }
354
355 static const struct dev_pm_ops sun4i_csi_pm_ops = {
356 SET_RUNTIME_PM_OPS(sun4i_csi_runtime_suspend,
357 sun4i_csi_runtime_resume,
358 NULL)
359 };
360
361 static struct platform_driver sun4i_csi_driver = {
362 .probe = sun4i_csi_probe,
363 .remove = sun4i_csi_remove,
364 .driver = {
365 .name = "sun4i-csi",
366 .of_match_table = sun4i_csi_of_match,
367 .pm = &sun4i_csi_pm_ops,
368 },
369 };
370 module_platform_driver(sun4i_csi_driver);
371
372 MODULE_DESCRIPTION("Allwinner A10 Camera Sensor Interface driver");
373 MODULE_AUTHOR("Maxime Ripard <mripard@kernel.org>");
374 MODULE_LICENSE("GPL");
375