xref: /linux/drivers/media/platform/rockchip/rkisp1/rkisp1-dev.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
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/build_bug.h>
12 #include <linux/clk.h>
13 #include <linux/interrupt.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/of_graph.h>
18 #include <linux/platform_device.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/pm_domain.h>
21 #include <linux/pm_runtime.h>
22 #include <media/v4l2-fwnode.h>
23 #include <media/v4l2-mc.h>
24 
25 #include "rkisp1-common.h"
26 #include "rkisp1-csi.h"
27 
28 /*
29  * ISP Details
30  * -----------
31  *
32  * ISP Comprises with:
33  *	MIPI serial camera interface
34  *	Image Signal Processing
35  *	Many Image Enhancement Blocks
36  *	Crop
37  *	Resizer
38  *	RBG display ready image
39  *	Image Rotation
40  *
41  * ISP Block Diagram
42  * -----------------
43  *                                                             rkisp1-resizer.c          rkisp1-capture.c
44  *                                                          |====================|  |=======================|
45  *                                rkisp1-isp.c                              Main Picture Path
46  *                        |==========================|      |===============================================|
47  *                        +-----------+  +--+--+--+--+      +--------+  +--------+              +-----------+
48  *                        |           |  |  |  |  |  |      |        |  |        |              |           |
49  * +--------+    |\       |           |  |  |  |  |  |   -->|  Crop  |->|  RSZ   |------------->|           |
50  * |  MIPI  |--->|  \     |           |  |  |  |  |  |   |  |        |  |        |              |           |
51  * +--------+    |   |    |           |  |IE|IE|IE|IE|   |  +--------+  +--------+              |  Memory   |
52  *               |MUX|--->|    ISP    |->|0 |1 |2 |3 |---+                                      | Interface |
53  * +--------+    |   |    |           |  |  |  |  |  |   |  +--------+  +--------+  +--------+  |           |
54  * |Parallel|--->|  /     |           |  |  |  |  |  |   |  |        |  |        |  |        |  |           |
55  * +--------+    |/       |           |  |  |  |  |  |   -->|  Crop  |->|  RSZ   |->|  RGB   |->|           |
56  *                        |           |  |  |  |  |  |      |        |  |        |  | Rotate |  |           |
57  *                        +-----------+  +--+--+--+--+      +--------+  +--------+  +--------+  +-----------+
58  *                                               ^
59  * +--------+                                    |          |===============================================|
60  * |  DMA   |------------------------------------+                          Self Picture Path
61  * +--------+
62  *
63  *         rkisp1-stats.c        rkisp1-params.c
64  *       |===============|      |===============|
65  *       +---------------+      +---------------+
66  *       |               |      |               |
67  *       |      ISP      |      |      ISP      |
68  *       |               |      |               |
69  *       +---------------+      +---------------+
70  *
71  *
72  * Media Topology
73  * --------------
74  *
75  *          +----------+       +----------+
76  *          | Sensor 1 |       | Sensor X |
77  *          ------------  ...  ------------
78  *          |    0     |       |    0     |
79  *          +----------+       +----------+
80  *               |                  |
81  *                \----\       /----/
82  *                     |       |
83  *                     v       v
84  *                  +-------------+
85  *                  |      0      |
86  *                  ---------------
87  *                  |  CSI-2 RX   |
88  *                  ---------------         +-----------+
89  *                  |      1      |         |  params   |
90  *                  +-------------+         | (output)  |
91  *                         |               +-----------+
92  *                         v                     |
93  *                      +------+------+          |
94  *                      |  0   |  1   |<---------+
95  *                      |------+------|
96  *                      |     ISP     |
97  *                      |------+------|
98  *        +-------------|  2   |  3   |----------+
99  *        |             +------+------+          |
100  *        |                |                     |
101  *        v                v                     v
102  *  +- ---------+    +-----------+         +-----------+
103  *  |     0     |    |     0     |         |   stats   |
104  *  -------------    -------------         | (capture) |
105  *  |  Resizer  |    |  Resizer  |         +-----------+
106  *  ------------|    ------------|
107  *  |     1     |    |     1     |
108  *  +-----------+    +-----------+
109  *        |                |
110  *        v                v
111  *  +-----------+    +-----------+
112  *  | selfpath  |    | mainpath  |
113  *  | (capture) |    | (capture) |
114  *  +-----------+    +-----------+
115  */
116 
117 struct rkisp1_isr_data {
118 	const char *name;
119 	irqreturn_t (*isr)(int irq, void *ctx);
120 	u32 line_mask;
121 };
122 
123 /* ----------------------------------------------------------------------------
124  * Sensor DT bindings
125  */
126 
127 static int rkisp1_subdev_notifier_bound(struct v4l2_async_notifier *notifier,
128 					struct v4l2_subdev *sd,
129 					struct v4l2_async_connection *asc)
130 {
131 	struct rkisp1_device *rkisp1 =
132 		container_of(notifier, struct rkisp1_device, notifier);
133 	struct rkisp1_sensor_async *s_asd =
134 		container_of(asc, struct rkisp1_sensor_async, asd);
135 	int source_pad;
136 	int ret;
137 
138 	s_asd->sd = sd;
139 
140 	source_pad = media_entity_get_fwnode_pad(&sd->entity, s_asd->source_ep,
141 						 MEDIA_PAD_FL_SOURCE);
142 	if (source_pad < 0) {
143 		dev_err(rkisp1->dev, "failed to find source pad for %s\n",
144 			sd->name);
145 		return source_pad;
146 	}
147 
148 	if (s_asd->port == 0)
149 		return rkisp1_csi_link_sensor(rkisp1, sd, s_asd, source_pad);
150 
151 	ret = media_create_pad_link(&sd->entity, source_pad,
152 				    &rkisp1->isp.sd.entity,
153 				    RKISP1_ISP_PAD_SINK_VIDEO,
154 				    !s_asd->index ? MEDIA_LNK_FL_ENABLED : 0);
155 	if (ret) {
156 		dev_err(rkisp1->dev, "failed to link source pad of %s\n",
157 			sd->name);
158 		return ret;
159 	}
160 
161 	return 0;
162 }
163 
164 static int rkisp1_subdev_notifier_complete(struct v4l2_async_notifier *notifier)
165 {
166 	struct rkisp1_device *rkisp1 =
167 		container_of(notifier, struct rkisp1_device, notifier);
168 
169 	return v4l2_device_register_subdev_nodes(&rkisp1->v4l2_dev);
170 }
171 
172 static void rkisp1_subdev_notifier_destroy(struct v4l2_async_connection *asc)
173 {
174 	struct rkisp1_sensor_async *rk_asd =
175 		container_of(asc, struct rkisp1_sensor_async, asd);
176 
177 	fwnode_handle_put(rk_asd->source_ep);
178 }
179 
180 static const struct v4l2_async_notifier_operations rkisp1_subdev_notifier_ops = {
181 	.bound = rkisp1_subdev_notifier_bound,
182 	.complete = rkisp1_subdev_notifier_complete,
183 	.destroy = rkisp1_subdev_notifier_destroy,
184 };
185 
186 static int rkisp1_subdev_notifier_register(struct rkisp1_device *rkisp1)
187 {
188 	struct v4l2_async_notifier *ntf = &rkisp1->notifier;
189 	struct fwnode_handle *fwnode = dev_fwnode(rkisp1->dev);
190 	struct fwnode_handle *ep;
191 	unsigned int index = 0;
192 	int ret = 0;
193 
194 	v4l2_async_nf_init(ntf, &rkisp1->v4l2_dev);
195 
196 	ntf->ops = &rkisp1_subdev_notifier_ops;
197 
198 	fwnode_graph_for_each_endpoint(fwnode, ep) {
199 		struct fwnode_handle *port;
200 		struct v4l2_fwnode_endpoint vep = { };
201 		struct rkisp1_sensor_async *rk_asd;
202 		struct fwnode_handle *source;
203 		u32 reg = 0;
204 
205 		/* Select the bus type based on the port. */
206 		port = fwnode_get_parent(ep);
207 		fwnode_property_read_u32(port, "reg", &reg);
208 		fwnode_handle_put(port);
209 
210 		switch (reg) {
211 		case 0:
212 			/* MIPI CSI-2 port */
213 			if (!rkisp1_has_feature(rkisp1, MIPI_CSI2)) {
214 				dev_err(rkisp1->dev,
215 					"internal CSI must be available for port 0\n");
216 				ret = -EINVAL;
217 				break;
218 			}
219 
220 			vep.bus_type = V4L2_MBUS_CSI2_DPHY;
221 			break;
222 
223 		case 1:
224 			/*
225 			 * Parallel port. The bus-type property in DT is
226 			 * mandatory for port 1, it will be used to determine if
227 			 * it's PARALLEL or BT656.
228 			 */
229 			vep.bus_type = V4L2_MBUS_UNKNOWN;
230 			break;
231 		}
232 
233 		if (ret)
234 			break;
235 
236 		/* Parse the endpoint and validate the bus type. */
237 		ret = v4l2_fwnode_endpoint_parse(ep, &vep);
238 		if (ret) {
239 			dev_err(rkisp1->dev, "failed to parse endpoint %pfw\n",
240 				ep);
241 			break;
242 		}
243 
244 		if (vep.base.port == 1) {
245 			if (vep.bus_type != V4L2_MBUS_PARALLEL &&
246 			    vep.bus_type != V4L2_MBUS_BT656) {
247 				dev_err(rkisp1->dev,
248 					"port 1 must be parallel or BT656\n");
249 				ret = -EINVAL;
250 				break;
251 			}
252 		}
253 
254 		/* Add the async subdev to the notifier. */
255 		source = fwnode_graph_get_remote_endpoint(ep);
256 		if (!source) {
257 			dev_err(rkisp1->dev,
258 				"endpoint %pfw has no remote endpoint\n",
259 				ep);
260 			ret = -ENODEV;
261 			break;
262 		}
263 
264 		rk_asd = v4l2_async_nf_add_fwnode(ntf, source,
265 						  struct rkisp1_sensor_async);
266 		if (IS_ERR(rk_asd)) {
267 			fwnode_handle_put(source);
268 			ret = PTR_ERR(rk_asd);
269 			break;
270 		}
271 
272 		rk_asd->index = index++;
273 		rk_asd->source_ep = source;
274 		rk_asd->mbus_type = vep.bus_type;
275 		rk_asd->port = vep.base.port;
276 
277 		if (vep.bus_type == V4L2_MBUS_CSI2_DPHY) {
278 			rk_asd->mbus_flags = vep.bus.mipi_csi2.flags;
279 			rk_asd->lanes = vep.bus.mipi_csi2.num_data_lanes;
280 		} else {
281 			rk_asd->mbus_flags = vep.bus.parallel.flags;
282 		}
283 
284 		dev_dbg(rkisp1->dev, "registered ep id %d, bus type %u, %u lanes\n",
285 			vep.base.id, rk_asd->mbus_type, rk_asd->lanes);
286 	}
287 
288 	if (ret) {
289 		fwnode_handle_put(ep);
290 		v4l2_async_nf_cleanup(ntf);
291 		return ret;
292 	}
293 
294 	if (!index)
295 		dev_dbg(rkisp1->dev, "no remote subdevice found\n");
296 
297 	ret = v4l2_async_nf_register(ntf);
298 	if (ret) {
299 		v4l2_async_nf_cleanup(ntf);
300 		return ret;
301 	}
302 
303 	return 0;
304 }
305 
306 /* ----------------------------------------------------------------------------
307  * Power
308  */
309 
310 static int __maybe_unused rkisp1_runtime_suspend(struct device *dev)
311 {
312 	struct rkisp1_device *rkisp1 = dev_get_drvdata(dev);
313 
314 	rkisp1->irqs_enabled = false;
315 	/* Make sure the IRQ handler will see the above */
316 	mb();
317 
318 	/*
319 	 * Wait until any running IRQ handler has returned. The IRQ handler
320 	 * may get called even after this (as it's a shared interrupt line)
321 	 * but the 'irqs_enabled' flag will make the handler return immediately.
322 	 */
323 	for (unsigned int il = 0; il < ARRAY_SIZE(rkisp1->irqs); ++il) {
324 		if (rkisp1->irqs[il] == -1)
325 			continue;
326 
327 		/* Skip if the irq line is the same as previous */
328 		if (il == 0 || rkisp1->irqs[il - 1] != rkisp1->irqs[il])
329 			synchronize_irq(rkisp1->irqs[il]);
330 	}
331 
332 	clk_bulk_disable_unprepare(rkisp1->clk_size, rkisp1->clks);
333 	return pinctrl_pm_select_sleep_state(dev);
334 }
335 
336 static int __maybe_unused rkisp1_runtime_resume(struct device *dev)
337 {
338 	struct rkisp1_device *rkisp1 = dev_get_drvdata(dev);
339 	int ret;
340 
341 	ret = pinctrl_pm_select_default_state(dev);
342 	if (ret)
343 		return ret;
344 	ret = clk_bulk_prepare_enable(rkisp1->clk_size, rkisp1->clks);
345 	if (ret)
346 		return ret;
347 
348 	rkisp1->irqs_enabled = true;
349 	/* Make sure the IRQ handler will see the above */
350 	mb();
351 
352 	return 0;
353 }
354 
355 static const struct dev_pm_ops rkisp1_pm_ops = {
356 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
357 				pm_runtime_force_resume)
358 	SET_RUNTIME_PM_OPS(rkisp1_runtime_suspend, rkisp1_runtime_resume, NULL)
359 };
360 
361 /* ----------------------------------------------------------------------------
362  * Core
363  */
364 
365 static int rkisp1_create_links(struct rkisp1_device *rkisp1)
366 {
367 	unsigned int dev_count = rkisp1_path_count(rkisp1);
368 	unsigned int i;
369 	int ret;
370 
371 	if (rkisp1_has_feature(rkisp1, MIPI_CSI2)) {
372 		/* Link the CSI receiver to the ISP. */
373 		ret = media_create_pad_link(&rkisp1->csi.sd.entity,
374 					    RKISP1_CSI_PAD_SRC,
375 					    &rkisp1->isp.sd.entity,
376 					    RKISP1_ISP_PAD_SINK_VIDEO,
377 					    MEDIA_LNK_FL_ENABLED);
378 		if (ret)
379 			return ret;
380 	}
381 
382 	/* create ISP->RSZ->CAP links */
383 	for (i = 0; i < dev_count; i++) {
384 		struct media_entity *resizer =
385 			&rkisp1->resizer_devs[i].sd.entity;
386 		struct media_entity *capture =
387 			&rkisp1->capture_devs[i].vnode.vdev.entity;
388 
389 		ret = media_create_pad_link(&rkisp1->isp.sd.entity,
390 					    RKISP1_ISP_PAD_SOURCE_VIDEO,
391 					    resizer, RKISP1_RSZ_PAD_SINK,
392 					    MEDIA_LNK_FL_ENABLED);
393 		if (ret)
394 			return ret;
395 
396 		ret = media_create_pad_link(resizer, RKISP1_RSZ_PAD_SRC,
397 					    capture, 0,
398 					    MEDIA_LNK_FL_ENABLED |
399 					    MEDIA_LNK_FL_IMMUTABLE);
400 		if (ret)
401 			return ret;
402 	}
403 
404 	/* params links */
405 	ret = media_create_pad_link(&rkisp1->params.vnode.vdev.entity, 0,
406 				    &rkisp1->isp.sd.entity,
407 				    RKISP1_ISP_PAD_SINK_PARAMS,
408 				    MEDIA_LNK_FL_ENABLED |
409 				    MEDIA_LNK_FL_IMMUTABLE);
410 	if (ret)
411 		return ret;
412 
413 	/* 3A stats links */
414 	return media_create_pad_link(&rkisp1->isp.sd.entity,
415 				     RKISP1_ISP_PAD_SOURCE_STATS,
416 				     &rkisp1->stats.vnode.vdev.entity, 0,
417 				     MEDIA_LNK_FL_ENABLED |
418 				     MEDIA_LNK_FL_IMMUTABLE);
419 }
420 
421 static void rkisp1_entities_unregister(struct rkisp1_device *rkisp1)
422 {
423 	if (rkisp1_has_feature(rkisp1, MIPI_CSI2))
424 		rkisp1_csi_unregister(rkisp1);
425 	rkisp1_params_unregister(rkisp1);
426 	rkisp1_stats_unregister(rkisp1);
427 	rkisp1_capture_devs_unregister(rkisp1);
428 	rkisp1_resizer_devs_unregister(rkisp1);
429 	rkisp1_isp_unregister(rkisp1);
430 }
431 
432 static int rkisp1_entities_register(struct rkisp1_device *rkisp1)
433 {
434 	int ret;
435 
436 	ret = rkisp1_isp_register(rkisp1);
437 	if (ret)
438 		goto error;
439 
440 	ret = rkisp1_resizer_devs_register(rkisp1);
441 	if (ret)
442 		goto error;
443 
444 	ret = rkisp1_capture_devs_register(rkisp1);
445 	if (ret)
446 		goto error;
447 
448 	ret = rkisp1_stats_register(rkisp1);
449 	if (ret)
450 		goto error;
451 
452 	ret = rkisp1_params_register(rkisp1);
453 	if (ret)
454 		goto error;
455 
456 	if (rkisp1_has_feature(rkisp1, MIPI_CSI2)) {
457 		ret = rkisp1_csi_register(rkisp1);
458 		if (ret)
459 			goto error;
460 	}
461 
462 	ret = rkisp1_create_links(rkisp1);
463 	if (ret)
464 		goto error;
465 
466 	return 0;
467 
468 error:
469 	rkisp1_entities_unregister(rkisp1);
470 	return ret;
471 }
472 
473 static irqreturn_t rkisp1_isr(int irq, void *ctx)
474 {
475 	irqreturn_t ret = IRQ_NONE;
476 
477 	/*
478 	 * Call rkisp1_capture_isr() first to handle the frame that
479 	 * potentially completed using the current frame_sequence number before
480 	 * it is potentially incremented by rkisp1_isp_isr() in the vertical
481 	 * sync.
482 	 */
483 
484 	if (rkisp1_capture_isr(irq, ctx) == IRQ_HANDLED)
485 		ret = IRQ_HANDLED;
486 
487 	if (rkisp1_isp_isr(irq, ctx) == IRQ_HANDLED)
488 		ret = IRQ_HANDLED;
489 
490 	if (rkisp1_csi_isr(irq, ctx) == IRQ_HANDLED)
491 		ret = IRQ_HANDLED;
492 
493 	return ret;
494 }
495 
496 static const struct rkisp1_isr_data px30_isp_isrs[] = {
497 	{ "isp", rkisp1_isp_isr, BIT(RKISP1_IRQ_ISP) },
498 	{ "mi", rkisp1_capture_isr, BIT(RKISP1_IRQ_MI) },
499 	{ "mipi", rkisp1_csi_isr, BIT(RKISP1_IRQ_MIPI) },
500 };
501 
502 static const struct rkisp1_info px30_isp_info = {
503 	.num_clocks = 4,
504 	.isrs = px30_isp_isrs,
505 	.isr_size = ARRAY_SIZE(px30_isp_isrs),
506 	.isp_ver = RKISP1_V12,
507 	.features = RKISP1_FEATURE_MIPI_CSI2
508 		  | RKISP1_FEATURE_SELF_PATH
509 		  | RKISP1_FEATURE_DUAL_CROP
510 		  | RKISP1_FEATURE_BLS,
511 	.max_width = 3264,
512 	.max_height = 2448,
513 };
514 
515 static const struct rkisp1_isr_data rk3399_isp_isrs[] = {
516 	{ NULL, rkisp1_isr, BIT(RKISP1_IRQ_ISP) | BIT(RKISP1_IRQ_MI) | BIT(RKISP1_IRQ_MIPI) },
517 };
518 
519 static const struct rkisp1_info rk3399_isp_info = {
520 	.num_clocks = 3,
521 	.isrs = rk3399_isp_isrs,
522 	.isr_size = ARRAY_SIZE(rk3399_isp_isrs),
523 	.isp_ver = RKISP1_V10,
524 	.features = RKISP1_FEATURE_MIPI_CSI2
525 		  | RKISP1_FEATURE_SELF_PATH
526 		  | RKISP1_FEATURE_DUAL_CROP
527 		  | RKISP1_FEATURE_BLS,
528 	.max_width = 4416,
529 	.max_height = 3312,
530 };
531 
532 static const struct rkisp1_isr_data imx8mp_isp_isrs[] = {
533 	{ NULL, rkisp1_isr, BIT(RKISP1_IRQ_ISP) | BIT(RKISP1_IRQ_MI) },
534 };
535 
536 static const char * const imx8mp_isp_pm_domains[] = {
537 	"isp",
538 	"csi2",
539 };
540 
541 static const struct rkisp1_info imx8mp_isp_info = {
542 	.num_clocks = 3,
543 	.isrs = imx8mp_isp_isrs,
544 	.isr_size = ARRAY_SIZE(imx8mp_isp_isrs),
545 	.isp_ver = RKISP1_V_IMX8MP,
546 	.features = RKISP1_FEATURE_MAIN_STRIDE
547 		  | RKISP1_FEATURE_DMA_34BIT
548 		  | RKISP1_FEATURE_COMPAND,
549 	.max_width = 4096,
550 	.max_height = 3072,
551 	.pm_domains = {
552 		.names = imx8mp_isp_pm_domains,
553 		.count = ARRAY_SIZE(imx8mp_isp_pm_domains),
554 	},
555 };
556 
557 static const struct of_device_id rkisp1_of_match[] = {
558 	{
559 		.compatible = "rockchip,px30-cif-isp",
560 		.data = &px30_isp_info,
561 	},
562 	{
563 		.compatible = "rockchip,rk3399-cif-isp",
564 		.data = &rk3399_isp_info,
565 	},
566 	{
567 		.compatible = "fsl,imx8mp-isp",
568 		.data = &imx8mp_isp_info,
569 	},
570 	{},
571 };
572 MODULE_DEVICE_TABLE(of, rkisp1_of_match);
573 
574 static const char * const rkisp1_clk_names[] = {
575 	"isp",
576 	"aclk",
577 	"hclk",
578 	"pclk",
579 };
580 
581 static int rkisp1_init_clocks(struct rkisp1_device *rkisp1)
582 {
583 	const struct rkisp1_info *info = rkisp1->info;
584 	unsigned int i;
585 	int ret;
586 
587 	static_assert(ARRAY_SIZE(rkisp1_clk_names) == ARRAY_SIZE(rkisp1->clks));
588 
589 	for (i = 0; i < info->num_clocks; i++)
590 		rkisp1->clks[i].id = rkisp1_clk_names[i];
591 
592 	ret = devm_clk_bulk_get(rkisp1->dev, info->num_clocks, rkisp1->clks);
593 	if (ret)
594 		return ret;
595 
596 	rkisp1->clk_size = info->num_clocks;
597 
598 	/*
599 	 * On i.MX8MP the pclk clock is needed to access the HDR stitching
600 	 * registers, but wasn't required by DT bindings. Try to acquire it as
601 	 * an optional clock to avoid breaking backward compatibility.
602 	 */
603 	if (info->isp_ver == RKISP1_V_IMX8MP) {
604 		struct clk *clk;
605 
606 		clk = devm_clk_get_optional(rkisp1->dev, "pclk");
607 		if (IS_ERR(clk))
608 			return dev_err_probe(rkisp1->dev, PTR_ERR(clk),
609 					     "Failed to acquire pclk clock\n");
610 
611 		if (clk)
612 			rkisp1->clks[rkisp1->clk_size++].clk = clk;
613 	}
614 
615 	return 0;
616 }
617 
618 static int rkisp1_init_pm_domains(struct rkisp1_device *rkisp1)
619 {
620 	const struct rkisp1_info *info = rkisp1->info;
621 	struct dev_pm_domain_attach_data pm_domain_data = {
622 		.pd_names = info->pm_domains.names,
623 		.num_pd_names = info->pm_domains.count,
624 	};
625 	int ret;
626 
627 	/*
628 	 * Most platforms have a single power domain, which the PM domain core
629 	 * automatically attaches at probe time. When that's the case there's
630 	 * nothing to do here.
631 	 */
632 	if (rkisp1->dev->pm_domain)
633 		return 0;
634 
635 	if (!pm_domain_data.num_pd_names)
636 		return 0;
637 
638 	ret = devm_pm_domain_attach_list(rkisp1->dev, &pm_domain_data,
639 					 &rkisp1->pm_domains);
640 	if (ret < 0) {
641 		dev_err_probe(rkisp1->dev, ret,
642 			      "Failed to attach power domains\n");
643 		return ret;
644 	}
645 
646 	return 0;
647 }
648 
649 static int rkisp1_probe(struct platform_device *pdev)
650 {
651 	const struct rkisp1_info *info;
652 	struct device *dev = &pdev->dev;
653 	struct rkisp1_device *rkisp1;
654 	struct v4l2_device *v4l2_dev;
655 	unsigned int i;
656 	u64 dma_mask;
657 	int ret, irq;
658 	u32 cif_id;
659 
660 	rkisp1 = devm_kzalloc(dev, sizeof(*rkisp1), GFP_KERNEL);
661 	if (!rkisp1)
662 		return -ENOMEM;
663 
664 	info = of_device_get_match_data(dev);
665 	rkisp1->info = info;
666 
667 	dev_set_drvdata(dev, rkisp1);
668 	rkisp1->dev = dev;
669 
670 	dma_mask = rkisp1_has_feature(rkisp1, DMA_34BIT) ? DMA_BIT_MASK(34) :
671 							   DMA_BIT_MASK(32);
672 
673 	ret = dma_set_mask_and_coherent(dev, dma_mask);
674 	if (ret)
675 		return ret;
676 
677 	mutex_init(&rkisp1->stream_lock);
678 
679 	rkisp1->base_addr = devm_platform_ioremap_resource(pdev, 0);
680 	if (IS_ERR(rkisp1->base_addr))
681 		return PTR_ERR(rkisp1->base_addr);
682 
683 	for (unsigned int il = 0; il < ARRAY_SIZE(rkisp1->irqs); ++il)
684 		rkisp1->irqs[il] = -1;
685 
686 	for (i = 0; i < info->isr_size; i++) {
687 		irq = info->isrs[i].name
688 		    ? platform_get_irq_byname(pdev, info->isrs[i].name)
689 		    : platform_get_irq(pdev, i);
690 		if (irq < 0)
691 			return irq;
692 
693 		for (unsigned int il = 0; il < ARRAY_SIZE(rkisp1->irqs); ++il) {
694 			if (info->isrs[i].line_mask & BIT(il))
695 				rkisp1->irqs[il] = irq;
696 		}
697 
698 		ret = devm_request_irq(dev, irq, info->isrs[i].isr, IRQF_SHARED,
699 				       dev_driver_string(dev), dev);
700 		if (ret) {
701 			dev_err(dev, "request irq failed: %d\n", ret);
702 			return ret;
703 		}
704 	}
705 
706 	ret = rkisp1_init_clocks(rkisp1);
707 	if (ret)
708 		return ret;
709 
710 	ret = rkisp1_init_pm_domains(rkisp1);
711 	if (ret)
712 		return ret;
713 
714 	if (info->isp_ver == RKISP1_V_IMX8MP) {
715 		unsigned int id;
716 
717 		rkisp1->gasket = syscon_regmap_lookup_by_phandle_args(dev->of_node,
718 								      "fsl,blk-ctrl",
719 								      1, &id);
720 		if (IS_ERR(rkisp1->gasket)) {
721 			ret = PTR_ERR(rkisp1->gasket);
722 			dev_err(dev, "failed to get gasket: %d\n", ret);
723 			return ret;
724 		}
725 
726 		rkisp1->gasket_id = id;
727 	}
728 
729 	pm_runtime_enable(&pdev->dev);
730 
731 	ret = pm_runtime_resume_and_get(&pdev->dev);
732 	if (ret)
733 		goto err_pm_runtime_disable;
734 
735 	cif_id = rkisp1_read(rkisp1, RKISP1_CIF_VI_ID);
736 	dev_dbg(rkisp1->dev, "CIF_ID 0x%08x\n", cif_id);
737 
738 	pm_runtime_put(&pdev->dev);
739 
740 	rkisp1->media_dev.hw_revision = info->isp_ver;
741 	strscpy(rkisp1->media_dev.model, RKISP1_DRIVER_NAME,
742 		sizeof(rkisp1->media_dev.model));
743 	rkisp1->media_dev.dev = &pdev->dev;
744 	strscpy(rkisp1->media_dev.bus_info, RKISP1_BUS_INFO,
745 		sizeof(rkisp1->media_dev.bus_info));
746 	media_device_init(&rkisp1->media_dev);
747 
748 	v4l2_dev = &rkisp1->v4l2_dev;
749 	v4l2_dev->mdev = &rkisp1->media_dev;
750 	strscpy(v4l2_dev->name, RKISP1_DRIVER_NAME, sizeof(v4l2_dev->name));
751 
752 	ret = v4l2_device_register(rkisp1->dev, &rkisp1->v4l2_dev);
753 	if (ret)
754 		goto err_media_dev_cleanup;
755 
756 	ret = media_device_register(&rkisp1->media_dev);
757 	if (ret) {
758 		dev_err(dev, "Failed to register media device: %d\n", ret);
759 		goto err_unreg_v4l2_dev;
760 	}
761 
762 	if (rkisp1->info->features & RKISP1_FEATURE_MIPI_CSI2) {
763 		ret = rkisp1_csi_init(rkisp1);
764 		if (ret)
765 			goto err_unreg_media_dev;
766 	}
767 
768 	ret = rkisp1_entities_register(rkisp1);
769 	if (ret)
770 		goto err_cleanup_csi;
771 
772 	ret = rkisp1_subdev_notifier_register(rkisp1);
773 	if (ret)
774 		goto err_unreg_entities;
775 
776 	rkisp1_debug_init(rkisp1);
777 
778 	return 0;
779 
780 err_unreg_entities:
781 	rkisp1_entities_unregister(rkisp1);
782 err_cleanup_csi:
783 	if (rkisp1_has_feature(rkisp1, MIPI_CSI2))
784 		rkisp1_csi_cleanup(rkisp1);
785 err_unreg_media_dev:
786 	media_device_unregister(&rkisp1->media_dev);
787 err_unreg_v4l2_dev:
788 	v4l2_device_unregister(&rkisp1->v4l2_dev);
789 err_media_dev_cleanup:
790 	media_device_cleanup(&rkisp1->media_dev);
791 err_pm_runtime_disable:
792 	pm_runtime_disable(&pdev->dev);
793 	return ret;
794 }
795 
796 static void rkisp1_remove(struct platform_device *pdev)
797 {
798 	struct rkisp1_device *rkisp1 = platform_get_drvdata(pdev);
799 
800 	v4l2_async_nf_unregister(&rkisp1->notifier);
801 	v4l2_async_nf_cleanup(&rkisp1->notifier);
802 
803 	rkisp1_entities_unregister(rkisp1);
804 	if (rkisp1_has_feature(rkisp1, MIPI_CSI2))
805 		rkisp1_csi_cleanup(rkisp1);
806 	rkisp1_debug_cleanup(rkisp1);
807 
808 	media_device_unregister(&rkisp1->media_dev);
809 	v4l2_device_unregister(&rkisp1->v4l2_dev);
810 
811 	media_device_cleanup(&rkisp1->media_dev);
812 
813 	pm_runtime_disable(&pdev->dev);
814 }
815 
816 static struct platform_driver rkisp1_drv = {
817 	.driver = {
818 		.name = RKISP1_DRIVER_NAME,
819 		.of_match_table = of_match_ptr(rkisp1_of_match),
820 		.pm = &rkisp1_pm_ops,
821 	},
822 	.probe = rkisp1_probe,
823 	.remove = rkisp1_remove,
824 };
825 
826 module_platform_driver(rkisp1_drv);
827 MODULE_DESCRIPTION("Rockchip ISP1 platform driver");
828 MODULE_LICENSE("Dual MIT/GPL");
829