1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2 /*
3  * Rockchip ISP1 Driver - Base driver
4  *
5  * Copyright (C) 2019 Collabora, Ltd.
6  *
7  * Based on Rockchip ISP1 driver by Rockchip Electronics Co., Ltd.
8  * Copyright (C) 2017 Rockchip Electronics Co., Ltd.
9  */
10 
11 #include <linux/clk.h>
12 #include <linux/debugfs.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_graph.h>
17 #include <linux/of_platform.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/phy/phy.h>
20 #include <linux/phy/phy-mipi-dphy.h>
21 #include <media/v4l2-fwnode.h>
22 
23 #include "rkisp1-common.h"
24 
25 /*
26  * ISP Details
27  * -----------
28  *
29  * ISP Comprises with:
30  *	MIPI serial camera interface
31  *	Image Signal Processing
32  *	Many Image Enhancement Blocks
33  *	Crop
34  *	Resizer
35  *	RBG display ready image
36  *	Image Rotation
37  *
38  * ISP Block Diagram
39  * -----------------
40  *                                                             rkisp1-resizer.c          rkisp1-capture.c
41  *                                                          |====================|  |=======================|
42  *                                rkisp1-isp.c                              Main Picture Path
43  *                        |==========================|      |===============================================|
44  *                        +-----------+  +--+--+--+--+      +--------+  +--------+              +-----------+
45  *                        |           |  |  |  |  |  |      |        |  |        |              |           |
46  * +--------+    |\       |           |  |  |  |  |  |   -->|  Crop  |->|  RSZ   |------------->|           |
47  * |  MIPI  |--->|  \     |           |  |  |  |  |  |   |  |        |  |        |              |           |
48  * +--------+    |   |    |           |  |IE|IE|IE|IE|   |  +--------+  +--------+              |  Memory   |
49  *               |MUX|--->|    ISP    |->|0 |1 |2 |3 |---+                                      | Interface |
50  * +--------+    |   |    |           |  |  |  |  |  |   |  +--------+  +--------+  +--------+  |           |
51  * |Parallel|--->|  /     |           |  |  |  |  |  |   |  |        |  |        |  |        |  |           |
52  * +--------+    |/       |           |  |  |  |  |  |   -->|  Crop  |->|  RSZ   |->|  RGB   |->|           |
53  *                        |           |  |  |  |  |  |      |        |  |        |  | Rotate |  |           |
54  *                        +-----------+  +--+--+--+--+      +--------+  +--------+  +--------+  +-----------+
55  *                                               ^
56  * +--------+                                    |          |===============================================|
57  * |  DMA   |------------------------------------+                          Self Picture Path
58  * +--------+
59  *
60  *         rkisp1-stats.c        rkisp1-params.c
61  *       |===============|      |===============|
62  *       +---------------+      +---------------+
63  *       |               |      |               |
64  *       |      ISP      |      |      ISP      |
65  *       |               |      |               |
66  *       +---------------+      +---------------+
67  *
68  *
69  * Media Topology
70  * --------------
71  *      +----------+     +----------+
72  *      | Sensor 2 |     | Sensor X |
73  *      ------------ ... ------------
74  *      |    0     |     |    0     |
75  *      +----------+     +----------+      +-----------+
76  *                  \      |               |  params   |
77  *                   \     |               | (output)  |
78  *    +----------+    \    |               +-----------+
79  *    | Sensor 1 |     v   v                     |
80  *    ------------      +------+------+          |
81  *    |    0     |----->|  0   |  1   |<---------+
82  *    +----------+      |------+------|
83  *                      |     ISP     |
84  *                      |------+------|
85  *        +-------------|  2   |  3   |----------+
86  *        |             +------+------+          |
87  *        |                |                     |
88  *        v                v                     v
89  *  +- ---------+    +-----------+         +-----------+
90  *  |     0     |    |     0     |         |   stats   |
91  *  -------------    -------------         | (capture) |
92  *  |  Resizer  |    |  Resizer  |         +-----------+
93  *  ------------|    ------------|
94  *  |     1     |    |     1     |
95  *  +-----------+    +-----------+
96  *        |                |
97  *        v                v
98  *  +-----------+    +-----------+
99  *  | selfpath  |    | mainpath  |
100  *  | (capture) |    | (capture) |
101  *  +-----------+    +-----------+
102  */
103 
104 struct rkisp1_match_data {
105 	const char * const *clks;
106 	unsigned int size;
107 };
108 
109 /* ----------------------------------------------------------------------------
110  * Sensor DT bindings
111  */
112 
113 static int rkisp1_create_links(struct rkisp1_device *rkisp1)
114 {
115 	struct media_entity *source, *sink;
116 	unsigned int flags, source_pad;
117 	struct v4l2_subdev *sd;
118 	unsigned int i;
119 	int ret;
120 
121 	/* sensor links */
122 	flags = MEDIA_LNK_FL_ENABLED;
123 	list_for_each_entry(sd, &rkisp1->v4l2_dev.subdevs, list) {
124 		if (sd == &rkisp1->isp.sd ||
125 		    sd == &rkisp1->resizer_devs[RKISP1_MAINPATH].sd ||
126 		    sd == &rkisp1->resizer_devs[RKISP1_SELFPATH].sd)
127 			continue;
128 
129 		ret = media_entity_get_fwnode_pad(&sd->entity, sd->fwnode,
130 						  MEDIA_PAD_FL_SOURCE);
131 		if (ret < 0) {
132 			dev_err(rkisp1->dev, "failed to find src pad for %s\n",
133 				sd->name);
134 			return ret;
135 		}
136 		source_pad = ret;
137 
138 		ret = media_create_pad_link(&sd->entity, source_pad,
139 					    &rkisp1->isp.sd.entity,
140 					    RKISP1_ISP_PAD_SINK_VIDEO,
141 					    flags);
142 		if (ret)
143 			return ret;
144 
145 		flags = 0;
146 	}
147 
148 	flags = MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE;
149 
150 	/* create ISP->RSZ->CAP links */
151 	for (i = 0; i < 2; i++) {
152 		source = &rkisp1->isp.sd.entity;
153 		sink = &rkisp1->resizer_devs[i].sd.entity;
154 		ret = media_create_pad_link(source, RKISP1_ISP_PAD_SOURCE_VIDEO,
155 					    sink, RKISP1_RSZ_PAD_SINK,
156 					    MEDIA_LNK_FL_ENABLED);
157 		if (ret)
158 			return ret;
159 
160 		source = sink;
161 		sink = &rkisp1->capture_devs[i].vnode.vdev.entity;
162 		ret = media_create_pad_link(source, RKISP1_RSZ_PAD_SRC,
163 					    sink, 0, flags);
164 		if (ret)
165 			return ret;
166 	}
167 
168 	/* params links */
169 	source = &rkisp1->params.vnode.vdev.entity;
170 	sink = &rkisp1->isp.sd.entity;
171 	ret = media_create_pad_link(source, 0, sink,
172 				    RKISP1_ISP_PAD_SINK_PARAMS, flags);
173 	if (ret)
174 		return ret;
175 
176 	/* 3A stats links */
177 	source = &rkisp1->isp.sd.entity;
178 	sink = &rkisp1->stats.vnode.vdev.entity;
179 	return media_create_pad_link(source, RKISP1_ISP_PAD_SOURCE_STATS,
180 				     sink, 0, flags);
181 }
182 
183 static int rkisp1_subdev_notifier_bound(struct v4l2_async_notifier *notifier,
184 					struct v4l2_subdev *sd,
185 					struct v4l2_async_subdev *asd)
186 {
187 	struct rkisp1_device *rkisp1 =
188 		container_of(notifier, struct rkisp1_device, notifier);
189 	struct rkisp1_sensor_async *s_asd =
190 		container_of(asd, struct rkisp1_sensor_async, asd);
191 
192 	s_asd->pixel_rate_ctrl = v4l2_ctrl_find(sd->ctrl_handler,
193 						V4L2_CID_PIXEL_RATE);
194 	s_asd->sd = sd;
195 	s_asd->dphy = devm_phy_get(rkisp1->dev, "dphy");
196 	if (IS_ERR(s_asd->dphy)) {
197 		if (PTR_ERR(s_asd->dphy) != -EPROBE_DEFER)
198 			dev_err(rkisp1->dev, "Couldn't get the MIPI D-PHY\n");
199 		return PTR_ERR(s_asd->dphy);
200 	}
201 
202 	phy_init(s_asd->dphy);
203 
204 	return 0;
205 }
206 
207 static void rkisp1_subdev_notifier_unbind(struct v4l2_async_notifier *notifier,
208 					  struct v4l2_subdev *sd,
209 					  struct v4l2_async_subdev *asd)
210 {
211 	struct rkisp1_sensor_async *s_asd =
212 		container_of(asd, struct rkisp1_sensor_async, asd);
213 
214 	phy_exit(s_asd->dphy);
215 }
216 
217 static int rkisp1_subdev_notifier_complete(struct v4l2_async_notifier *notifier)
218 {
219 	struct rkisp1_device *rkisp1 =
220 		container_of(notifier, struct rkisp1_device, notifier);
221 	int ret;
222 
223 	ret = rkisp1_create_links(rkisp1);
224 	if (ret)
225 		return ret;
226 
227 	ret = v4l2_device_register_subdev_nodes(&rkisp1->v4l2_dev);
228 	if (ret)
229 		return ret;
230 
231 	dev_dbg(rkisp1->dev, "Async subdev notifier completed\n");
232 
233 	return 0;
234 }
235 
236 static const struct v4l2_async_notifier_operations rkisp1_subdev_notifier_ops = {
237 	.bound = rkisp1_subdev_notifier_bound,
238 	.unbind = rkisp1_subdev_notifier_unbind,
239 	.complete = rkisp1_subdev_notifier_complete,
240 };
241 
242 static int rkisp1_subdev_notifier(struct rkisp1_device *rkisp1)
243 {
244 	struct v4l2_async_notifier *ntf = &rkisp1->notifier;
245 	unsigned int next_id = 0;
246 	int ret;
247 
248 	v4l2_async_notifier_init(ntf);
249 
250 	while (1) {
251 		struct v4l2_fwnode_endpoint vep = {
252 			.bus_type = V4L2_MBUS_CSI2_DPHY
253 		};
254 		struct rkisp1_sensor_async *rk_asd;
255 		struct fwnode_handle *ep;
256 
257 		ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(rkisp1->dev),
258 						     0, next_id,
259 						     FWNODE_GRAPH_ENDPOINT_NEXT);
260 		if (!ep)
261 			break;
262 
263 		ret = v4l2_fwnode_endpoint_parse(ep, &vep);
264 		if (ret)
265 			goto err_parse;
266 
267 		rk_asd = v4l2_async_notifier_add_fwnode_remote_subdev(ntf, ep,
268 							struct rkisp1_sensor_async);
269 		if (IS_ERR(rk_asd)) {
270 			ret = PTR_ERR(rk_asd);
271 			goto err_parse;
272 		}
273 
274 		rk_asd->mbus_type = vep.bus_type;
275 		rk_asd->mbus_flags = vep.bus.mipi_csi2.flags;
276 		rk_asd->lanes = vep.bus.mipi_csi2.num_data_lanes;
277 
278 		dev_dbg(rkisp1->dev, "registered ep id %d with %d lanes\n",
279 			vep.base.id, rk_asd->lanes);
280 
281 		next_id = vep.base.id + 1;
282 
283 		fwnode_handle_put(ep);
284 
285 		continue;
286 err_parse:
287 		fwnode_handle_put(ep);
288 		v4l2_async_notifier_cleanup(ntf);
289 		return ret;
290 	}
291 
292 	if (next_id == 0)
293 		dev_dbg(rkisp1->dev, "no remote subdevice found\n");
294 	ntf->ops = &rkisp1_subdev_notifier_ops;
295 	ret = v4l2_async_notifier_register(&rkisp1->v4l2_dev, ntf);
296 	if (ret) {
297 		v4l2_async_notifier_cleanup(ntf);
298 		return ret;
299 	}
300 	return 0;
301 }
302 
303 /* ----------------------------------------------------------------------------
304  * Power
305  */
306 
307 static int __maybe_unused rkisp1_runtime_suspend(struct device *dev)
308 {
309 	struct rkisp1_device *rkisp1 = dev_get_drvdata(dev);
310 
311 	clk_bulk_disable_unprepare(rkisp1->clk_size, rkisp1->clks);
312 	return pinctrl_pm_select_sleep_state(dev);
313 }
314 
315 static int __maybe_unused rkisp1_runtime_resume(struct device *dev)
316 {
317 	struct rkisp1_device *rkisp1 = dev_get_drvdata(dev);
318 	int ret;
319 
320 	ret = pinctrl_pm_select_default_state(dev);
321 	if (ret)
322 		return ret;
323 	ret = clk_bulk_prepare_enable(rkisp1->clk_size, rkisp1->clks);
324 	if (ret)
325 		return ret;
326 
327 	return 0;
328 }
329 
330 static const struct dev_pm_ops rkisp1_pm_ops = {
331 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
332 				pm_runtime_force_resume)
333 	SET_RUNTIME_PM_OPS(rkisp1_runtime_suspend, rkisp1_runtime_resume, NULL)
334 };
335 
336 /* ----------------------------------------------------------------------------
337  * Core
338  */
339 
340 static int rkisp1_entities_register(struct rkisp1_device *rkisp1)
341 {
342 	int ret;
343 
344 	ret = rkisp1_isp_register(rkisp1);
345 	if (ret)
346 		return ret;
347 
348 	ret = rkisp1_resizer_devs_register(rkisp1);
349 	if (ret)
350 		goto err_unreg_isp_subdev;
351 
352 	ret = rkisp1_capture_devs_register(rkisp1);
353 	if (ret)
354 		goto err_unreg_resizer_devs;
355 
356 	ret = rkisp1_stats_register(rkisp1);
357 	if (ret)
358 		goto err_unreg_capture_devs;
359 
360 	ret = rkisp1_params_register(rkisp1);
361 	if (ret)
362 		goto err_unreg_stats;
363 
364 	ret = rkisp1_subdev_notifier(rkisp1);
365 	if (ret) {
366 		dev_err(rkisp1->dev,
367 			"Failed to register subdev notifier(%d)\n", ret);
368 		goto err_unreg_params;
369 	}
370 
371 	return 0;
372 err_unreg_params:
373 	rkisp1_params_unregister(rkisp1);
374 err_unreg_stats:
375 	rkisp1_stats_unregister(rkisp1);
376 err_unreg_capture_devs:
377 	rkisp1_capture_devs_unregister(rkisp1);
378 err_unreg_resizer_devs:
379 	rkisp1_resizer_devs_unregister(rkisp1);
380 err_unreg_isp_subdev:
381 	rkisp1_isp_unregister(rkisp1);
382 	return ret;
383 }
384 
385 static irqreturn_t rkisp1_isr(int irq, void *ctx)
386 {
387 	struct device *dev = ctx;
388 	struct rkisp1_device *rkisp1 = dev_get_drvdata(dev);
389 
390 	/*
391 	 * Call rkisp1_capture_isr() first to handle the frame that
392 	 * potentially completed using the current frame_sequence number before
393 	 * it is potentially incremented by rkisp1_isp_isr() in the vertical
394 	 * sync.
395 	 */
396 	rkisp1_capture_isr(rkisp1);
397 	rkisp1_isp_isr(rkisp1);
398 	rkisp1_mipi_isr(rkisp1);
399 
400 	return IRQ_HANDLED;
401 }
402 
403 static const char * const rk3399_isp_clks[] = {
404 	"isp",
405 	"aclk",
406 	"hclk",
407 };
408 
409 static const struct rkisp1_match_data rk3399_isp_clk_data = {
410 	.clks = rk3399_isp_clks,
411 	.size = ARRAY_SIZE(rk3399_isp_clks),
412 };
413 
414 static const struct of_device_id rkisp1_of_match[] = {
415 	{
416 		.compatible = "rockchip,rk3399-cif-isp",
417 		.data = &rk3399_isp_clk_data,
418 	},
419 	{},
420 };
421 MODULE_DEVICE_TABLE(of, rkisp1_of_match);
422 
423 static void rkisp1_debug_init(struct rkisp1_device *rkisp1)
424 {
425 	struct rkisp1_debug *debug = &rkisp1->debug;
426 
427 	debug->debugfs_dir = debugfs_create_dir(RKISP1_DRIVER_NAME, NULL);
428 	debugfs_create_ulong("data_loss", 0444, debug->debugfs_dir,
429 			     &debug->data_loss);
430 	debugfs_create_ulong("outform_size_err", 0444,  debug->debugfs_dir,
431 			     &debug->outform_size_error);
432 	debugfs_create_ulong("img_stabilization_size_error", 0444,
433 			     debug->debugfs_dir,
434 			     &debug->img_stabilization_size_error);
435 	debugfs_create_ulong("inform_size_error", 0444,  debug->debugfs_dir,
436 			     &debug->inform_size_error);
437 	debugfs_create_ulong("irq_delay", 0444,  debug->debugfs_dir,
438 			     &debug->irq_delay);
439 	debugfs_create_ulong("mipi_error", 0444, debug->debugfs_dir,
440 			     &debug->mipi_error);
441 	debugfs_create_ulong("stats_error", 0444, debug->debugfs_dir,
442 			     &debug->stats_error);
443 	debugfs_create_ulong("mp_stop_timeout", 0444, debug->debugfs_dir,
444 			     &debug->stop_timeout[RKISP1_MAINPATH]);
445 	debugfs_create_ulong("sp_stop_timeout", 0444, debug->debugfs_dir,
446 			     &debug->stop_timeout[RKISP1_SELFPATH]);
447 	debugfs_create_ulong("mp_frame_drop", 0444, debug->debugfs_dir,
448 			     &debug->frame_drop[RKISP1_MAINPATH]);
449 	debugfs_create_ulong("sp_frame_drop", 0444, debug->debugfs_dir,
450 			     &debug->frame_drop[RKISP1_SELFPATH]);
451 }
452 
453 static int rkisp1_probe(struct platform_device *pdev)
454 {
455 	const struct rkisp1_match_data *clk_data;
456 	struct device *dev = &pdev->dev;
457 	struct rkisp1_device *rkisp1;
458 	struct v4l2_device *v4l2_dev;
459 	unsigned int i;
460 	int ret, irq;
461 
462 	clk_data = of_device_get_match_data(&pdev->dev);
463 	if (!clk_data)
464 		return -ENODEV;
465 
466 	rkisp1 = devm_kzalloc(dev, sizeof(*rkisp1), GFP_KERNEL);
467 	if (!rkisp1)
468 		return -ENOMEM;
469 
470 	dev_set_drvdata(dev, rkisp1);
471 	rkisp1->dev = dev;
472 
473 	mutex_init(&rkisp1->stream_lock);
474 
475 	rkisp1->base_addr = devm_platform_ioremap_resource(pdev, 0);
476 	if (IS_ERR(rkisp1->base_addr))
477 		return PTR_ERR(rkisp1->base_addr);
478 
479 	irq = platform_get_irq(pdev, 0);
480 	if (irq < 0)
481 		return irq;
482 
483 	ret = devm_request_irq(dev, irq, rkisp1_isr, IRQF_SHARED,
484 			       dev_driver_string(dev), dev);
485 	if (ret) {
486 		dev_err(dev, "request irq failed: %d\n", ret);
487 		return ret;
488 	}
489 
490 	rkisp1->irq = irq;
491 
492 	for (i = 0; i < clk_data->size; i++)
493 		rkisp1->clks[i].id = clk_data->clks[i];
494 	ret = devm_clk_bulk_get(dev, clk_data->size, rkisp1->clks);
495 	if (ret)
496 		return ret;
497 	rkisp1->clk_size = clk_data->size;
498 
499 	pm_runtime_enable(&pdev->dev);
500 
501 	strscpy(rkisp1->media_dev.model, RKISP1_DRIVER_NAME,
502 		sizeof(rkisp1->media_dev.model));
503 	rkisp1->media_dev.dev = &pdev->dev;
504 	strscpy(rkisp1->media_dev.bus_info, RKISP1_BUS_INFO,
505 		sizeof(rkisp1->media_dev.bus_info));
506 	media_device_init(&rkisp1->media_dev);
507 
508 	v4l2_dev = &rkisp1->v4l2_dev;
509 	v4l2_dev->mdev = &rkisp1->media_dev;
510 	strscpy(v4l2_dev->name, RKISP1_DRIVER_NAME, sizeof(v4l2_dev->name));
511 
512 	ret = v4l2_device_register(rkisp1->dev, &rkisp1->v4l2_dev);
513 	if (ret)
514 		return ret;
515 
516 	ret = media_device_register(&rkisp1->media_dev);
517 	if (ret) {
518 		dev_err(dev, "Failed to register media device: %d\n", ret);
519 		goto err_unreg_v4l2_dev;
520 	}
521 
522 	ret = rkisp1_entities_register(rkisp1);
523 	if (ret)
524 		goto err_unreg_media_dev;
525 
526 	rkisp1_debug_init(rkisp1);
527 
528 	return 0;
529 
530 err_unreg_media_dev:
531 	media_device_unregister(&rkisp1->media_dev);
532 err_unreg_v4l2_dev:
533 	v4l2_device_unregister(&rkisp1->v4l2_dev);
534 	pm_runtime_disable(&pdev->dev);
535 	return ret;
536 }
537 
538 static int rkisp1_remove(struct platform_device *pdev)
539 {
540 	struct rkisp1_device *rkisp1 = platform_get_drvdata(pdev);
541 
542 	v4l2_async_notifier_unregister(&rkisp1->notifier);
543 	v4l2_async_notifier_cleanup(&rkisp1->notifier);
544 
545 	rkisp1_params_unregister(rkisp1);
546 	rkisp1_stats_unregister(rkisp1);
547 	rkisp1_capture_devs_unregister(rkisp1);
548 	rkisp1_resizer_devs_unregister(rkisp1);
549 	rkisp1_isp_unregister(rkisp1);
550 
551 	media_device_unregister(&rkisp1->media_dev);
552 	v4l2_device_unregister(&rkisp1->v4l2_dev);
553 
554 	pm_runtime_disable(&pdev->dev);
555 
556 	debugfs_remove_recursive(rkisp1->debug.debugfs_dir);
557 	return 0;
558 }
559 
560 static struct platform_driver rkisp1_drv = {
561 	.driver = {
562 		.name = RKISP1_DRIVER_NAME,
563 		.of_match_table = of_match_ptr(rkisp1_of_match),
564 		.pm = &rkisp1_pm_ops,
565 	},
566 	.probe = rkisp1_probe,
567 	.remove = rkisp1_remove,
568 };
569 
570 module_platform_driver(rkisp1_drv);
571 MODULE_DESCRIPTION("Rockchip ISP1 platform driver");
572 MODULE_LICENSE("Dual MIT/GPL");
573