1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for Renesas RZ/G2L CRU
4  *
5  * Copyright (C) 2022 Renesas Electronics Corp.
6  *
7  * Based on Renesas R-Car VIN
8  * Copyright (C) 2011-2013 Renesas Solutions Corp.
9  * Copyright (C) 2013 Cogent Embedded, Inc., <[email protected]>
10  * Copyright (C) 2008 Magnus Damm
11  */
12 
13 #include <linux/clk.h>
14 #include <linux/module.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/of.h>
17 #include <linux/of_graph.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 
21 #include <media/v4l2-fwnode.h>
22 #include <media/v4l2-mc.h>
23 
24 #include "rzg2l-cru.h"
25 
notifier_to_cru(struct v4l2_async_notifier * n)26 static inline struct rzg2l_cru_dev *notifier_to_cru(struct v4l2_async_notifier *n)
27 {
28 	return container_of(n, struct rzg2l_cru_dev, notifier);
29 }
30 
31 static const struct media_device_ops rzg2l_cru_media_ops = {
32 	.link_notify = v4l2_pipeline_link_notify,
33 };
34 
35 /* -----------------------------------------------------------------------------
36  * Group async notifier
37  */
38 
rzg2l_cru_group_notify_complete(struct v4l2_async_notifier * notifier)39 static int rzg2l_cru_group_notify_complete(struct v4l2_async_notifier *notifier)
40 {
41 	struct rzg2l_cru_dev *cru = notifier_to_cru(notifier);
42 	struct media_entity *source, *sink;
43 	int ret;
44 
45 	ret = rzg2l_cru_ip_subdev_register(cru);
46 	if (ret)
47 		return ret;
48 
49 	ret = v4l2_device_register_subdev_nodes(&cru->v4l2_dev);
50 	if (ret) {
51 		dev_err(cru->dev, "Failed to register subdev nodes\n");
52 		return ret;
53 	}
54 
55 	ret = rzg2l_cru_video_register(cru);
56 	if (ret)
57 		return ret;
58 
59 	/*
60 	 * CRU can be connected either to CSI2 or PARALLEL device
61 	 * For now we are only supporting CSI2
62 	 *
63 	 * Create media device link between CSI-2 <-> CRU IP
64 	 */
65 	source = &cru->csi.subdev->entity;
66 	sink = &cru->ip.subdev.entity;
67 	ret = media_create_pad_link(source, 1, sink, 0,
68 				    MEDIA_LNK_FL_ENABLED |
69 				    MEDIA_LNK_FL_IMMUTABLE);
70 	if (ret) {
71 		dev_err(cru->dev, "Error creating link from %s to %s\n",
72 			source->name, sink->name);
73 		return ret;
74 	}
75 	cru->ip.remote = cru->csi.subdev;
76 
77 	/* Create media device link between CRU IP <-> CRU OUTPUT */
78 	source = &cru->ip.subdev.entity;
79 	sink = &cru->vdev.entity;
80 	ret = media_create_pad_link(source, 1, sink, 0,
81 				    MEDIA_LNK_FL_ENABLED |
82 				    MEDIA_LNK_FL_IMMUTABLE);
83 	if (ret) {
84 		dev_err(cru->dev, "Error creating link from %s to %s\n",
85 			source->name, sink->name);
86 		return ret;
87 	}
88 
89 	return 0;
90 }
91 
rzg2l_cru_group_notify_unbind(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_connection * asd)92 static void rzg2l_cru_group_notify_unbind(struct v4l2_async_notifier *notifier,
93 					  struct v4l2_subdev *subdev,
94 					  struct v4l2_async_connection *asd)
95 {
96 	struct rzg2l_cru_dev *cru = notifier_to_cru(notifier);
97 
98 	rzg2l_cru_ip_subdev_unregister(cru);
99 
100 	mutex_lock(&cru->mdev_lock);
101 
102 	if (cru->csi.asd == asd) {
103 		cru->csi.subdev = NULL;
104 		dev_dbg(cru->dev, "Unbind CSI-2 %s\n", subdev->name);
105 	}
106 
107 	mutex_unlock(&cru->mdev_lock);
108 }
109 
rzg2l_cru_group_notify_bound(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_connection * asd)110 static int rzg2l_cru_group_notify_bound(struct v4l2_async_notifier *notifier,
111 					struct v4l2_subdev *subdev,
112 					struct v4l2_async_connection *asd)
113 {
114 	struct rzg2l_cru_dev *cru = notifier_to_cru(notifier);
115 
116 	mutex_lock(&cru->mdev_lock);
117 
118 	if (cru->csi.asd == asd) {
119 		cru->csi.subdev = subdev;
120 		dev_dbg(cru->dev, "Bound CSI-2 %s\n", subdev->name);
121 	}
122 
123 	mutex_unlock(&cru->mdev_lock);
124 
125 	return 0;
126 }
127 
128 static const struct v4l2_async_notifier_operations rzg2l_cru_async_ops = {
129 	.bound = rzg2l_cru_group_notify_bound,
130 	.unbind = rzg2l_cru_group_notify_unbind,
131 	.complete = rzg2l_cru_group_notify_complete,
132 };
133 
rzg2l_cru_mc_parse_of(struct rzg2l_cru_dev * cru)134 static int rzg2l_cru_mc_parse_of(struct rzg2l_cru_dev *cru)
135 {
136 	struct v4l2_fwnode_endpoint vep = {
137 		.bus_type = V4L2_MBUS_CSI2_DPHY,
138 	};
139 	struct fwnode_handle *ep, *fwnode;
140 	struct v4l2_async_connection *asd;
141 	int ret;
142 
143 	ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(cru->dev), 1, 0, 0);
144 	if (!ep)
145 		return 0;
146 
147 	fwnode = fwnode_graph_get_remote_endpoint(ep);
148 	ret = v4l2_fwnode_endpoint_parse(ep, &vep);
149 	fwnode_handle_put(ep);
150 	if (ret) {
151 		dev_err(cru->dev, "Failed to parse %pOF\n", to_of_node(fwnode));
152 		ret = -EINVAL;
153 		goto out;
154 	}
155 
156 	if (!of_device_is_available(to_of_node(fwnode))) {
157 		dev_dbg(cru->dev, "OF device %pOF disabled, ignoring\n",
158 			to_of_node(fwnode));
159 		ret = -ENOTCONN;
160 		goto out;
161 	}
162 
163 	asd = v4l2_async_nf_add_fwnode(&cru->notifier, fwnode,
164 				       struct v4l2_async_connection);
165 	if (IS_ERR(asd)) {
166 		ret = PTR_ERR(asd);
167 		goto out;
168 	}
169 
170 	cru->csi.asd = asd;
171 
172 	dev_dbg(cru->dev, "Added OF device %pOF to slot %u\n",
173 		to_of_node(fwnode), vep.base.id);
174 out:
175 	fwnode_handle_put(fwnode);
176 
177 	return ret;
178 }
179 
rzg2l_cru_mc_parse_of_graph(struct rzg2l_cru_dev * cru)180 static int rzg2l_cru_mc_parse_of_graph(struct rzg2l_cru_dev *cru)
181 {
182 	int ret;
183 
184 	v4l2_async_nf_init(&cru->notifier, &cru->v4l2_dev);
185 
186 	ret = rzg2l_cru_mc_parse_of(cru);
187 	if (ret)
188 		return ret;
189 
190 	cru->notifier.ops = &rzg2l_cru_async_ops;
191 
192 	if (list_empty(&cru->notifier.waiting_list))
193 		return 0;
194 
195 	ret = v4l2_async_nf_register(&cru->notifier);
196 	if (ret < 0) {
197 		dev_err(cru->dev, "Notifier registration failed\n");
198 		v4l2_async_nf_cleanup(&cru->notifier);
199 		return ret;
200 	}
201 
202 	return 0;
203 }
204 
rzg2l_cru_media_init(struct rzg2l_cru_dev * cru)205 static int rzg2l_cru_media_init(struct rzg2l_cru_dev *cru)
206 {
207 	struct media_device *mdev = NULL;
208 	const struct of_device_id *match;
209 	int ret;
210 
211 	cru->pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
212 	ret = media_entity_pads_init(&cru->vdev.entity, 1, &cru->pad);
213 	if (ret)
214 		return ret;
215 
216 	mutex_init(&cru->mdev_lock);
217 	mdev = &cru->mdev;
218 	mdev->dev = cru->dev;
219 	mdev->ops = &rzg2l_cru_media_ops;
220 
221 	match = of_match_node(cru->dev->driver->of_match_table,
222 			      cru->dev->of_node);
223 
224 	strscpy(mdev->driver_name, KBUILD_MODNAME, sizeof(mdev->driver_name));
225 	strscpy(mdev->model, match->compatible, sizeof(mdev->model));
226 
227 	cru->v4l2_dev.mdev = &cru->mdev;
228 
229 	media_device_init(mdev);
230 
231 	ret = rzg2l_cru_mc_parse_of_graph(cru);
232 	if (ret) {
233 		mutex_lock(&cru->mdev_lock);
234 		cru->v4l2_dev.mdev = NULL;
235 		mutex_unlock(&cru->mdev_lock);
236 	}
237 
238 	return 0;
239 }
240 
rzg2l_cru_probe(struct platform_device * pdev)241 static int rzg2l_cru_probe(struct platform_device *pdev)
242 {
243 	struct rzg2l_cru_dev *cru;
244 	int irq, ret;
245 
246 	cru = devm_kzalloc(&pdev->dev, sizeof(*cru), GFP_KERNEL);
247 	if (!cru)
248 		return -ENOMEM;
249 
250 	cru->base = devm_platform_ioremap_resource(pdev, 0);
251 	if (IS_ERR(cru->base))
252 		return PTR_ERR(cru->base);
253 
254 	cru->presetn = devm_reset_control_get_shared(&pdev->dev, "presetn");
255 	if (IS_ERR(cru->presetn))
256 		return dev_err_probe(&pdev->dev, PTR_ERR(cru->presetn),
257 				     "Failed to get cpg presetn\n");
258 
259 	cru->aresetn = devm_reset_control_get_exclusive(&pdev->dev, "aresetn");
260 	if (IS_ERR(cru->aresetn))
261 		return dev_err_probe(&pdev->dev, PTR_ERR(cru->aresetn),
262 				     "Failed to get cpg aresetn\n");
263 
264 	cru->vclk = devm_clk_get(&pdev->dev, "video");
265 	if (IS_ERR(cru->vclk))
266 		return dev_err_probe(&pdev->dev, PTR_ERR(cru->vclk),
267 				     "Failed to get video clock\n");
268 
269 	cru->dev = &pdev->dev;
270 	cru->info = of_device_get_match_data(&pdev->dev);
271 
272 	irq = platform_get_irq(pdev, 0);
273 	if (irq < 0)
274 		return irq;
275 
276 	ret = devm_request_irq(&pdev->dev, irq, rzg2l_cru_irq, 0,
277 			       KBUILD_MODNAME, cru);
278 	if (ret)
279 		return dev_err_probe(&pdev->dev, ret, "failed to request irq\n");
280 
281 	platform_set_drvdata(pdev, cru);
282 
283 	ret = rzg2l_cru_dma_register(cru);
284 	if (ret)
285 		return ret;
286 
287 	cru->num_buf = RZG2L_CRU_HW_BUFFER_DEFAULT;
288 	pm_suspend_ignore_children(&pdev->dev, true);
289 	pm_runtime_enable(&pdev->dev);
290 
291 	ret = rzg2l_cru_media_init(cru);
292 	if (ret)
293 		goto error_dma_unregister;
294 
295 	return 0;
296 
297 error_dma_unregister:
298 	rzg2l_cru_dma_unregister(cru);
299 	pm_runtime_disable(&pdev->dev);
300 
301 	return ret;
302 }
303 
rzg2l_cru_remove(struct platform_device * pdev)304 static void rzg2l_cru_remove(struct platform_device *pdev)
305 {
306 	struct rzg2l_cru_dev *cru = platform_get_drvdata(pdev);
307 
308 	pm_runtime_disable(&pdev->dev);
309 
310 	v4l2_async_nf_unregister(&cru->notifier);
311 	v4l2_async_nf_cleanup(&cru->notifier);
312 
313 	rzg2l_cru_video_unregister(cru);
314 	media_device_cleanup(&cru->mdev);
315 	mutex_destroy(&cru->mdev_lock);
316 
317 	rzg2l_cru_dma_unregister(cru);
318 }
319 
320 static const struct of_device_id rzg2l_cru_of_id_table[] = {
321 	{ .compatible = "renesas,rzg2l-cru", },
322 	{ /* sentinel */ }
323 };
324 MODULE_DEVICE_TABLE(of, rzg2l_cru_of_id_table);
325 
326 static struct platform_driver rzg2l_cru_driver = {
327 	.driver = {
328 		.name = "rzg2l-cru",
329 		.of_match_table = rzg2l_cru_of_id_table,
330 	},
331 	.probe = rzg2l_cru_probe,
332 	.remove = rzg2l_cru_remove,
333 };
334 
335 module_platform_driver(rzg2l_cru_driver);
336 
337 MODULE_AUTHOR("Lad Prabhakar <[email protected]>");
338 MODULE_DESCRIPTION("Renesas RZ/G2L CRU driver");
339 MODULE_LICENSE("GPL");
340