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_graph.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/reset.h>
19 #include <linux/videodev2.h>
20 
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>
26 
27 #include <media/videobuf2-core.h>
28 #include <media/videobuf2-dma-contig.h>
29 
30 #include "sun4i_csi.h"
31 
32 struct sun4i_csi_traits {
33 	unsigned int channels;
34 	unsigned int max_width;
35 	bool has_isp;
36 };
37 
38 static const struct media_entity_operations sun4i_csi_video_entity_ops = {
39 	.link_validate = v4l2_subdev_link_validate,
40 };
41 
sun4i_csi_notify_bound(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_connection * asd)42 static int sun4i_csi_notify_bound(struct v4l2_async_notifier *notifier,
43 				  struct v4l2_subdev *subdev,
44 				  struct v4l2_async_connection *asd)
45 {
46 	struct sun4i_csi *csi = container_of(notifier, struct sun4i_csi,
47 					     notifier);
48 
49 	csi->src_subdev = subdev;
50 	csi->src_pad = media_entity_get_fwnode_pad(&subdev->entity,
51 						   subdev->fwnode,
52 						   MEDIA_PAD_FL_SOURCE);
53 	if (csi->src_pad < 0) {
54 		dev_err(csi->dev, "Couldn't find output pad for subdev %s\n",
55 			subdev->name);
56 		return csi->src_pad;
57 	}
58 
59 	dev_dbg(csi->dev, "Bound %s pad: %d\n", subdev->name, csi->src_pad);
60 	return 0;
61 }
62 
sun4i_csi_notify_complete(struct v4l2_async_notifier * notifier)63 static int sun4i_csi_notify_complete(struct v4l2_async_notifier *notifier)
64 {
65 	struct sun4i_csi *csi = container_of(notifier, struct sun4i_csi,
66 					     notifier);
67 	struct v4l2_subdev *subdev = &csi->subdev;
68 	struct video_device *vdev = &csi->vdev;
69 	int ret;
70 
71 	ret = v4l2_device_register_subdev(&csi->v4l, subdev);
72 	if (ret < 0)
73 		return ret;
74 
75 	ret = sun4i_csi_v4l2_register(csi);
76 	if (ret < 0)
77 		return ret;
78 
79 	ret = media_device_register(&csi->mdev);
80 	if (ret)
81 		return ret;
82 
83 	/* Create link from subdev to main device */
84 	ret = media_create_pad_link(&subdev->entity, CSI_SUBDEV_SOURCE,
85 				    &vdev->entity, 0,
86 				    MEDIA_LNK_FL_ENABLED |
87 				    MEDIA_LNK_FL_IMMUTABLE);
88 	if (ret)
89 		goto err_clean_media;
90 
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);
95 	if (ret)
96 		goto err_clean_media;
97 
98 	ret = v4l2_device_register_subdev_nodes(&csi->v4l);
99 	if (ret < 0)
100 		goto err_clean_media;
101 
102 	return 0;
103 
104 err_clean_media:
105 	media_device_unregister(&csi->mdev);
106 
107 	return ret;
108 }
109 
110 static const struct v4l2_async_notifier_operations sun4i_csi_notify_ops = {
111 	.bound		= sun4i_csi_notify_bound,
112 	.complete	= sun4i_csi_notify_complete,
113 };
114 
sun4i_csi_notifier_init(struct sun4i_csi * csi)115 static int sun4i_csi_notifier_init(struct sun4i_csi *csi)
116 {
117 	struct v4l2_fwnode_endpoint vep = {
118 		.bus_type = V4L2_MBUS_PARALLEL,
119 	};
120 	struct v4l2_async_connection *asd;
121 	struct fwnode_handle *ep;
122 	int ret;
123 
124 	v4l2_async_nf_init(&csi->notifier, &csi->v4l);
125 
126 	ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(csi->dev), 0, 0,
127 					     FWNODE_GRAPH_ENDPOINT_NEXT);
128 	if (!ep)
129 		return -EINVAL;
130 
131 	ret = v4l2_fwnode_endpoint_parse(ep, &vep);
132 	if (ret)
133 		goto out;
134 
135 	csi->bus = vep.bus.parallel;
136 
137 	asd = v4l2_async_nf_add_fwnode_remote(&csi->notifier, ep,
138 					      struct v4l2_async_connection);
139 	if (IS_ERR(asd)) {
140 		ret = PTR_ERR(asd);
141 		goto out;
142 	}
143 
144 	csi->notifier.ops = &sun4i_csi_notify_ops;
145 
146 out:
147 	fwnode_handle_put(ep);
148 	return ret;
149 }
150 
sun4i_csi_probe(struct platform_device * pdev)151 static int sun4i_csi_probe(struct platform_device *pdev)
152 {
153 	struct v4l2_subdev *subdev;
154 	struct video_device *vdev;
155 	struct sun4i_csi *csi;
156 	int ret;
157 	int irq;
158 
159 	csi = devm_kzalloc(&pdev->dev, sizeof(*csi), GFP_KERNEL);
160 	if (!csi)
161 		return -ENOMEM;
162 	platform_set_drvdata(pdev, csi);
163 	csi->dev = &pdev->dev;
164 	subdev = &csi->subdev;
165 	vdev = &csi->vdev;
166 
167 	csi->traits = of_device_get_match_data(&pdev->dev);
168 	if (!csi->traits)
169 		return -EINVAL;
170 
171 	csi->mdev.dev = csi->dev;
172 	strscpy(csi->mdev.model, "Allwinner Video Capture Device",
173 		sizeof(csi->mdev.model));
174 	csi->mdev.hw_revision = 0;
175 	media_device_init(&csi->mdev);
176 	csi->v4l.mdev = &csi->mdev;
177 
178 	csi->regs = devm_platform_ioremap_resource(pdev, 0);
179 	if (IS_ERR(csi->regs))
180 		return PTR_ERR(csi->regs);
181 
182 	irq = platform_get_irq(pdev, 0);
183 	if (irq < 0)
184 		return irq;
185 
186 	csi->bus_clk = devm_clk_get(&pdev->dev, "bus");
187 	if (IS_ERR(csi->bus_clk)) {
188 		dev_err(&pdev->dev, "Couldn't get our bus clock\n");
189 		return PTR_ERR(csi->bus_clk);
190 	}
191 
192 	if (csi->traits->has_isp) {
193 		csi->isp_clk = devm_clk_get(&pdev->dev, "isp");
194 		if (IS_ERR(csi->isp_clk)) {
195 			dev_err(&pdev->dev, "Couldn't get our ISP clock\n");
196 			return PTR_ERR(csi->isp_clk);
197 		}
198 	}
199 
200 	csi->ram_clk = devm_clk_get(&pdev->dev, "ram");
201 	if (IS_ERR(csi->ram_clk)) {
202 		dev_err(&pdev->dev, "Couldn't get our ram clock\n");
203 		return PTR_ERR(csi->ram_clk);
204 	}
205 
206 	csi->rst = devm_reset_control_get(&pdev->dev, NULL);
207 	if (IS_ERR(csi->rst)) {
208 		dev_err(&pdev->dev, "Couldn't get our reset line\n");
209 		return PTR_ERR(csi->rst);
210 	}
211 
212 	/* Initialize subdev */
213 	v4l2_subdev_init(subdev, &sun4i_csi_subdev_ops);
214 	subdev->flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
215 	subdev->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
216 	subdev->owner = THIS_MODULE;
217 	snprintf(subdev->name, sizeof(subdev->name), "sun4i-csi-0");
218 	v4l2_set_subdevdata(subdev, csi);
219 
220 	csi->subdev_pads[CSI_SUBDEV_SINK].flags =
221 		MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
222 	csi->subdev_pads[CSI_SUBDEV_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
223 	ret = media_entity_pads_init(&subdev->entity, CSI_SUBDEV_PADS,
224 				     csi->subdev_pads);
225 	if (ret < 0)
226 		return ret;
227 
228 	csi->vdev_pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
229 	vdev->entity.ops = &sun4i_csi_video_entity_ops;
230 	ret = media_entity_pads_init(&vdev->entity, 1, &csi->vdev_pad);
231 	if (ret < 0)
232 		return ret;
233 
234 	ret = sun4i_csi_dma_register(csi, irq);
235 	if (ret)
236 		goto err_clean_pad;
237 
238 	ret = sun4i_csi_notifier_init(csi);
239 	if (ret)
240 		goto err_unregister_media;
241 
242 	ret = v4l2_async_nf_register(&csi->notifier);
243 	if (ret) {
244 		dev_err(csi->dev, "Couldn't register our notifier.\n");
245 		goto err_unregister_media;
246 	}
247 
248 	pm_runtime_enable(&pdev->dev);
249 
250 	return 0;
251 
252 err_unregister_media:
253 	media_device_unregister(&csi->mdev);
254 	sun4i_csi_dma_unregister(csi);
255 
256 err_clean_pad:
257 	media_device_cleanup(&csi->mdev);
258 
259 	return ret;
260 }
261 
sun4i_csi_remove(struct platform_device * pdev)262 static void sun4i_csi_remove(struct platform_device *pdev)
263 {
264 	struct sun4i_csi *csi = platform_get_drvdata(pdev);
265 
266 	pm_runtime_disable(&pdev->dev);
267 	v4l2_async_nf_unregister(&csi->notifier);
268 	v4l2_async_nf_cleanup(&csi->notifier);
269 	vb2_video_unregister_device(&csi->vdev);
270 	media_device_unregister(&csi->mdev);
271 	sun4i_csi_dma_unregister(csi);
272 	media_device_cleanup(&csi->mdev);
273 }
274 
275 static const struct sun4i_csi_traits sun4i_a10_csi1_traits = {
276 	.channels = 1,
277 	.max_width = 24,
278 	.has_isp = false,
279 };
280 
281 static const struct sun4i_csi_traits sun7i_a20_csi0_traits = {
282 	.channels = 4,
283 	.max_width = 16,
284 	.has_isp = true,
285 };
286 
287 static const struct of_device_id sun4i_csi_of_match[] = {
288 	{ .compatible = "allwinner,sun4i-a10-csi1", .data = &sun4i_a10_csi1_traits },
289 	{ .compatible = "allwinner,sun7i-a20-csi0", .data = &sun7i_a20_csi0_traits },
290 	{ /* Sentinel */ }
291 };
292 MODULE_DEVICE_TABLE(of, sun4i_csi_of_match);
293 
sun4i_csi_runtime_resume(struct device * dev)294 static int __maybe_unused sun4i_csi_runtime_resume(struct device *dev)
295 {
296 	struct sun4i_csi *csi = dev_get_drvdata(dev);
297 
298 	reset_control_deassert(csi->rst);
299 	clk_prepare_enable(csi->bus_clk);
300 	clk_prepare_enable(csi->ram_clk);
301 	clk_set_rate(csi->isp_clk, 80000000);
302 	clk_prepare_enable(csi->isp_clk);
303 
304 	writel(1, csi->regs + CSI_EN_REG);
305 
306 	return 0;
307 }
308 
sun4i_csi_runtime_suspend(struct device * dev)309 static int __maybe_unused sun4i_csi_runtime_suspend(struct device *dev)
310 {
311 	struct sun4i_csi *csi = dev_get_drvdata(dev);
312 
313 	clk_disable_unprepare(csi->isp_clk);
314 	clk_disable_unprepare(csi->ram_clk);
315 	clk_disable_unprepare(csi->bus_clk);
316 
317 	reset_control_assert(csi->rst);
318 
319 	return 0;
320 }
321 
322 static const struct dev_pm_ops sun4i_csi_pm_ops = {
323 	SET_RUNTIME_PM_OPS(sun4i_csi_runtime_suspend,
324 			   sun4i_csi_runtime_resume,
325 			   NULL)
326 };
327 
328 static struct platform_driver sun4i_csi_driver = {
329 	.probe	= sun4i_csi_probe,
330 	.remove_new = sun4i_csi_remove,
331 	.driver	= {
332 		.name		= "sun4i-csi",
333 		.of_match_table	= sun4i_csi_of_match,
334 		.pm		= &sun4i_csi_pm_ops,
335 	},
336 };
337 module_platform_driver(sun4i_csi_driver);
338 
339 MODULE_DESCRIPTION("Allwinner A10 Camera Sensor Interface driver");
340 MODULE_AUTHOR("Maxime Ripard <mripard@kernel.org>");
341 MODULE_LICENSE("GPL");
342