1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * S5P/EXYNOS4 SoC series camera host interface media device driver
4  *
5  * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd.
6  * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
7  */
8 
9 #include <linux/bug.h>
10 #include <linux/clk.h>
11 #include <linux/clk-provider.h>
12 #include <linux/device.h>
13 #include <linux/errno.h>
14 #include <linux/i2c.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/of_platform.h>
20 #include <linux/of_device.h>
21 #include <linux/of_graph.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/types.h>
26 #include <linux/slab.h>
27 #include <media/v4l2-async.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-fwnode.h>
30 #include <media/media-device.h>
31 #include <media/drv-intf/exynos-fimc.h>
32 
33 #include "media-dev.h"
34 #include "fimc-core.h"
35 #include "fimc-is.h"
36 #include "fimc-lite.h"
37 #include "mipi-csis.h"
38 
39 /* Set up image sensor subdev -> FIMC capture node notifications. */
40 static void __setup_sensor_notification(struct fimc_md *fmd,
41 					struct v4l2_subdev *sensor,
42 					struct v4l2_subdev *fimc_sd)
43 {
44 	struct fimc_source_info *src_inf;
45 	struct fimc_sensor_info *md_si;
46 	unsigned long flags;
47 
48 	src_inf = v4l2_get_subdev_hostdata(sensor);
49 	if (!src_inf || WARN_ON(fmd == NULL))
50 		return;
51 
52 	md_si = source_to_sensor_info(src_inf);
53 	spin_lock_irqsave(&fmd->slock, flags);
54 	md_si->host = v4l2_get_subdevdata(fimc_sd);
55 	spin_unlock_irqrestore(&fmd->slock, flags);
56 }
57 
58 /**
59  * fimc_pipeline_prepare - update pipeline information with subdevice pointers
60  * @p: fimc pipeline
61  * @me: media entity terminating the pipeline
62  *
63  * Caller holds the graph mutex.
64  */
65 static void fimc_pipeline_prepare(struct fimc_pipeline *p,
66 					struct media_entity *me)
67 {
68 	struct fimc_md *fmd = entity_to_fimc_mdev(me);
69 	struct v4l2_subdev *sd;
70 	struct v4l2_subdev *sensor = NULL;
71 	int i;
72 
73 	for (i = 0; i < IDX_MAX; i++)
74 		p->subdevs[i] = NULL;
75 
76 	while (1) {
77 		struct media_pad *pad = NULL;
78 
79 		/* Find remote source pad */
80 		for (i = 0; i < me->num_pads; i++) {
81 			struct media_pad *spad = &me->pads[i];
82 			if (!(spad->flags & MEDIA_PAD_FL_SINK))
83 				continue;
84 			pad = media_entity_remote_pad(spad);
85 			if (pad)
86 				break;
87 		}
88 
89 		if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
90 			break;
91 		sd = media_entity_to_v4l2_subdev(pad->entity);
92 
93 		switch (sd->grp_id) {
94 		case GRP_ID_SENSOR:
95 			sensor = sd;
96 			fallthrough;
97 		case GRP_ID_FIMC_IS_SENSOR:
98 			p->subdevs[IDX_SENSOR] = sd;
99 			break;
100 		case GRP_ID_CSIS:
101 			p->subdevs[IDX_CSIS] = sd;
102 			break;
103 		case GRP_ID_FLITE:
104 			p->subdevs[IDX_FLITE] = sd;
105 			break;
106 		case GRP_ID_FIMC:
107 			p->subdevs[IDX_FIMC] = sd;
108 			break;
109 		case GRP_ID_FIMC_IS:
110 			p->subdevs[IDX_IS_ISP] = sd;
111 			break;
112 		default:
113 			break;
114 		}
115 		me = &sd->entity;
116 		if (me->num_pads == 1)
117 			break;
118 	}
119 
120 	if (sensor && p->subdevs[IDX_FIMC])
121 		__setup_sensor_notification(fmd, sensor, p->subdevs[IDX_FIMC]);
122 }
123 
124 /**
125  * __subdev_set_power - change power state of a single subdev
126  * @sd: subdevice to change power state for
127  * @on: 1 to enable power or 0 to disable
128  *
129  * Return result of s_power subdev operation or -ENXIO if sd argument
130  * is NULL. Return 0 if the subdevice does not implement s_power.
131  */
132 static int __subdev_set_power(struct v4l2_subdev *sd, int on)
133 {
134 	int *use_count;
135 	int ret;
136 
137 	if (sd == NULL)
138 		return -ENXIO;
139 
140 	use_count = &sd->entity.use_count;
141 	if (on && (*use_count)++ > 0)
142 		return 0;
143 	else if (!on && (*use_count == 0 || --(*use_count) > 0))
144 		return 0;
145 	ret = v4l2_subdev_call(sd, core, s_power, on);
146 
147 	return ret != -ENOIOCTLCMD ? ret : 0;
148 }
149 
150 /**
151  * fimc_pipeline_s_power - change power state of all pipeline subdevs
152  * @p: fimc device terminating the pipeline
153  * @on: true to power on, false to power off
154  *
155  * Needs to be called with the graph mutex held.
156  */
157 static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool on)
158 {
159 	static const u8 seq[2][IDX_MAX - 1] = {
160 		{ IDX_IS_ISP, IDX_SENSOR, IDX_CSIS, IDX_FLITE },
161 		{ IDX_CSIS, IDX_FLITE, IDX_SENSOR, IDX_IS_ISP },
162 	};
163 	int i, ret = 0;
164 
165 	if (p->subdevs[IDX_SENSOR] == NULL)
166 		return -ENXIO;
167 
168 	for (i = 0; i < IDX_MAX - 1; i++) {
169 		unsigned int idx = seq[on][i];
170 
171 		ret = __subdev_set_power(p->subdevs[idx], on);
172 
173 
174 		if (ret < 0 && ret != -ENXIO)
175 			goto error;
176 	}
177 	return 0;
178 error:
179 	for (; i >= 0; i--) {
180 		unsigned int idx = seq[on][i];
181 		__subdev_set_power(p->subdevs[idx], !on);
182 	}
183 	return ret;
184 }
185 
186 /**
187  * __fimc_pipeline_enable - enable power of all pipeline subdevs
188  *			    and the sensor clock
189  * @ep: video pipeline structure
190  * @fmd: fimc media device
191  *
192  * Called with the graph mutex held.
193  */
194 static int __fimc_pipeline_enable(struct exynos_media_pipeline *ep,
195 				  struct fimc_md *fmd)
196 {
197 	struct fimc_pipeline *p = to_fimc_pipeline(ep);
198 	int ret;
199 
200 	/* Enable PXLASYNC clock if this pipeline includes FIMC-IS */
201 	if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
202 		ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
203 		if (ret < 0)
204 			return ret;
205 	}
206 
207 	ret = fimc_pipeline_s_power(p, 1);
208 	if (!ret)
209 		return 0;
210 
211 	if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
212 		clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
213 
214 	return ret;
215 }
216 
217 /**
218  * __fimc_pipeline_open - update the pipeline information, enable power
219  *                        of all pipeline subdevs and the sensor clock
220  * @ep: fimc device terminating the pipeline
221  * @me: media entity to start graph walk with
222  * @prepare: true to walk the current pipeline and acquire all subdevs
223  *
224  * Called with the graph mutex held.
225  */
226 static int __fimc_pipeline_open(struct exynos_media_pipeline *ep,
227 				struct media_entity *me, bool prepare)
228 {
229 	struct fimc_md *fmd = entity_to_fimc_mdev(me);
230 	struct fimc_pipeline *p = to_fimc_pipeline(ep);
231 	struct v4l2_subdev *sd;
232 
233 	if (WARN_ON(p == NULL || me == NULL))
234 		return -EINVAL;
235 
236 	if (prepare)
237 		fimc_pipeline_prepare(p, me);
238 
239 	sd = p->subdevs[IDX_SENSOR];
240 	if (sd == NULL) {
241 		pr_warn("%s(): No sensor subdev\n", __func__);
242 		/*
243 		 * Pipeline open cannot fail so as to make it possible
244 		 * for the user space to configure the pipeline.
245 		 */
246 		return 0;
247 	}
248 
249 	return __fimc_pipeline_enable(ep, fmd);
250 }
251 
252 /**
253  * __fimc_pipeline_close - disable the sensor clock and pipeline power
254  * @ep: fimc device terminating the pipeline
255  *
256  * Disable power of all subdevs and turn the external sensor clock off.
257  */
258 static int __fimc_pipeline_close(struct exynos_media_pipeline *ep)
259 {
260 	struct fimc_pipeline *p = to_fimc_pipeline(ep);
261 	struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
262 	struct fimc_md *fmd;
263 	int ret;
264 
265 	if (sd == NULL) {
266 		pr_warn("%s(): No sensor subdev\n", __func__);
267 		return 0;
268 	}
269 
270 	ret = fimc_pipeline_s_power(p, 0);
271 
272 	fmd = entity_to_fimc_mdev(&sd->entity);
273 
274 	/* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
275 	if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
276 		clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
277 
278 	return ret == -ENXIO ? 0 : ret;
279 }
280 
281 /**
282  * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
283  * @ep: video pipeline structure
284  * @on: passed as the s_stream() callback argument
285  */
286 static int __fimc_pipeline_s_stream(struct exynos_media_pipeline *ep, bool on)
287 {
288 	static const u8 seq[2][IDX_MAX] = {
289 		{ IDX_FIMC, IDX_SENSOR, IDX_IS_ISP, IDX_CSIS, IDX_FLITE },
290 		{ IDX_CSIS, IDX_FLITE, IDX_FIMC, IDX_SENSOR, IDX_IS_ISP },
291 	};
292 	struct fimc_pipeline *p = to_fimc_pipeline(ep);
293 	enum fimc_subdev_index sd_id;
294 	int i, ret = 0;
295 
296 	if (p->subdevs[IDX_SENSOR] == NULL) {
297 		struct fimc_md *fmd;
298 		struct v4l2_subdev *sd = p->subdevs[IDX_CSIS];
299 
300 		if (!sd)
301 			sd = p->subdevs[IDX_FIMC];
302 
303 		if (!sd) {
304 			/*
305 			 * If neither CSIS nor FIMC was set up,
306 			 * it's impossible to have any sensors
307 			 */
308 			return -ENODEV;
309 		}
310 
311 		fmd = entity_to_fimc_mdev(&sd->entity);
312 
313 		if (!fmd->user_subdev_api) {
314 			/*
315 			 * Sensor must be already discovered if we
316 			 * aren't in the user_subdev_api mode
317 			 */
318 			return -ENODEV;
319 		}
320 
321 		/* Get pipeline sink entity */
322 		if (p->subdevs[IDX_FIMC])
323 			sd_id = IDX_FIMC;
324 		else if (p->subdevs[IDX_IS_ISP])
325 			sd_id = IDX_IS_ISP;
326 		else if (p->subdevs[IDX_FLITE])
327 			sd_id = IDX_FLITE;
328 		else
329 			return -ENODEV;
330 
331 		/*
332 		 * Sensor could have been linked between open and STREAMON -
333 		 * check if this is the case.
334 		 */
335 		fimc_pipeline_prepare(p, &p->subdevs[sd_id]->entity);
336 
337 		if (p->subdevs[IDX_SENSOR] == NULL)
338 			return -ENODEV;
339 
340 		ret = __fimc_pipeline_enable(ep, fmd);
341 		if (ret < 0)
342 			return ret;
343 
344 	}
345 
346 	for (i = 0; i < IDX_MAX; i++) {
347 		unsigned int idx = seq[on][i];
348 
349 		ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
350 
351 		if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
352 			goto error;
353 	}
354 
355 	return 0;
356 error:
357 	fimc_pipeline_s_power(p, !on);
358 	for (; i >= 0; i--) {
359 		unsigned int idx = seq[on][i];
360 		v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
361 	}
362 	return ret;
363 }
364 
365 /* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
366 static const struct exynos_media_pipeline_ops fimc_pipeline_ops = {
367 	.open		= __fimc_pipeline_open,
368 	.close		= __fimc_pipeline_close,
369 	.set_stream	= __fimc_pipeline_s_stream,
370 };
371 
372 static struct exynos_media_pipeline *fimc_md_pipeline_create(
373 						struct fimc_md *fmd)
374 {
375 	struct fimc_pipeline *p;
376 
377 	p = kzalloc(sizeof(*p), GFP_KERNEL);
378 	if (!p)
379 		return NULL;
380 
381 	list_add_tail(&p->list, &fmd->pipelines);
382 
383 	p->ep.ops = &fimc_pipeline_ops;
384 	return &p->ep;
385 }
386 
387 static void fimc_md_pipelines_free(struct fimc_md *fmd)
388 {
389 	while (!list_empty(&fmd->pipelines)) {
390 		struct fimc_pipeline *p;
391 
392 		p = list_entry(fmd->pipelines.next, typeof(*p), list);
393 		list_del(&p->list);
394 		kfree(p);
395 	}
396 }
397 
398 static int fimc_md_parse_one_endpoint(struct fimc_md *fmd,
399 				   struct device_node *ep)
400 {
401 	int index = fmd->num_sensors;
402 	struct fimc_source_info *pd = &fmd->sensor[index].pdata;
403 	struct device_node *rem, *np;
404 	struct v4l2_async_subdev *asd;
405 	struct v4l2_fwnode_endpoint endpoint = { .bus_type = 0 };
406 	int ret;
407 
408 	ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &endpoint);
409 	if (ret) {
410 		of_node_put(ep);
411 		return ret;
412 	}
413 
414 	if (WARN_ON(endpoint.base.port == 0) || index >= FIMC_MAX_SENSORS) {
415 		of_node_put(ep);
416 		return -EINVAL;
417 	}
418 
419 	pd->mux_id = (endpoint.base.port - 1) & 0x1;
420 
421 	rem = of_graph_get_remote_port_parent(ep);
422 	if (rem == NULL) {
423 		v4l2_info(&fmd->v4l2_dev, "Remote device at %pOF not found\n",
424 							ep);
425 		of_node_put(ep);
426 		return 0;
427 	}
428 
429 	if (fimc_input_is_parallel(endpoint.base.port)) {
430 		if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
431 			pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
432 		else
433 			pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
434 		pd->flags = endpoint.bus.parallel.flags;
435 	} else if (fimc_input_is_mipi_csi(endpoint.base.port)) {
436 		/*
437 		 * MIPI CSI-2: only input mux selection and
438 		 * the sensor's clock frequency is needed.
439 		 */
440 		pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
441 	} else {
442 		v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %pOF\n",
443 			 endpoint.base.port, rem);
444 	}
445 	/*
446 	 * For FIMC-IS handled sensors, that are placed under i2c-isp device
447 	 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
448 	 * input. Sensors are attached to the FIMC-LITE hostdata interface
449 	 * directly or through MIPI-CSIS, depending on the external media bus
450 	 * used. This needs to be handled in a more reliable way, not by just
451 	 * checking parent's node name.
452 	 */
453 	np = of_get_parent(rem);
454 	of_node_put(rem);
455 
456 	if (of_node_name_eq(np, "i2c-isp"))
457 		pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
458 	else
459 		pd->fimc_bus_type = pd->sensor_bus_type;
460 	of_node_put(np);
461 
462 	if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor))) {
463 		of_node_put(ep);
464 		return -EINVAL;
465 	}
466 
467 	asd = v4l2_async_nf_add_fwnode_remote(&fmd->subdev_notifier,
468 					      of_fwnode_handle(ep),
469 					      struct v4l2_async_subdev);
470 
471 	of_node_put(ep);
472 
473 	if (IS_ERR(asd))
474 		return PTR_ERR(asd);
475 
476 	fmd->sensor[index].asd = asd;
477 	fmd->num_sensors++;
478 
479 	return 0;
480 }
481 
482 /* Parse port node and register as a sub-device any sensor specified there. */
483 static int fimc_md_parse_port_node(struct fimc_md *fmd,
484 				   struct device_node *port)
485 {
486 	struct device_node *ep;
487 	int ret;
488 
489 	for_each_child_of_node(port, ep) {
490 		ret = fimc_md_parse_one_endpoint(fmd, ep);
491 		if (ret < 0) {
492 			of_node_put(ep);
493 			return ret;
494 		}
495 	}
496 
497 	return 0;
498 }
499 
500 /* Register all SoC external sub-devices */
501 static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
502 {
503 	struct device_node *parent = fmd->pdev->dev.of_node;
504 	struct device_node *ports = NULL;
505 	struct device_node *node;
506 	int ret;
507 
508 	/*
509 	 * Runtime resume one of the FIMC entities to make sure
510 	 * the sclk_cam clocks are not globally disabled.
511 	 */
512 	if (!fmd->pmf)
513 		return -ENXIO;
514 
515 	ret = pm_runtime_resume_and_get(fmd->pmf);
516 	if (ret < 0)
517 		return ret;
518 
519 	fmd->num_sensors = 0;
520 
521 	/* Attach sensors linked to MIPI CSI-2 receivers */
522 	for_each_available_child_of_node(parent, node) {
523 		struct device_node *port;
524 
525 		if (!of_node_name_eq(node, "csis"))
526 			continue;
527 		/* The csis node can have only port subnode. */
528 		port = of_get_next_child(node, NULL);
529 		if (!port)
530 			continue;
531 
532 		ret = fimc_md_parse_port_node(fmd, port);
533 		of_node_put(port);
534 		if (ret < 0) {
535 			of_node_put(node);
536 			goto cleanup;
537 		}
538 	}
539 
540 	/* Attach sensors listed in the parallel-ports node */
541 	ports = of_get_child_by_name(parent, "parallel-ports");
542 	if (!ports)
543 		goto rpm_put;
544 
545 	for_each_child_of_node(ports, node) {
546 		ret = fimc_md_parse_port_node(fmd, node);
547 		if (ret < 0) {
548 			of_node_put(node);
549 			goto cleanup;
550 		}
551 	}
552 	of_node_put(ports);
553 
554 rpm_put:
555 	pm_runtime_put(fmd->pmf);
556 	return 0;
557 
558 cleanup:
559 	of_node_put(ports);
560 	v4l2_async_nf_cleanup(&fmd->subdev_notifier);
561 	pm_runtime_put(fmd->pmf);
562 	return ret;
563 }
564 
565 static int __of_get_csis_id(struct device_node *np)
566 {
567 	u32 reg = 0;
568 
569 	np = of_get_child_by_name(np, "port");
570 	if (!np)
571 		return -EINVAL;
572 	of_property_read_u32(np, "reg", &reg);
573 	of_node_put(np);
574 	return reg - FIMC_INPUT_MIPI_CSI2_0;
575 }
576 
577 /*
578  * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
579  */
580 static int register_fimc_lite_entity(struct fimc_md *fmd,
581 				     struct fimc_lite *fimc_lite)
582 {
583 	struct v4l2_subdev *sd;
584 	struct exynos_media_pipeline *ep;
585 	int ret;
586 
587 	if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
588 		    fmd->fimc_lite[fimc_lite->index]))
589 		return -EBUSY;
590 
591 	sd = &fimc_lite->subdev;
592 	sd->grp_id = GRP_ID_FLITE;
593 
594 	ep = fimc_md_pipeline_create(fmd);
595 	if (!ep)
596 		return -ENOMEM;
597 
598 	v4l2_set_subdev_hostdata(sd, ep);
599 
600 	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
601 	if (!ret)
602 		fmd->fimc_lite[fimc_lite->index] = fimc_lite;
603 	else
604 		v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
605 			 fimc_lite->index);
606 	return ret;
607 }
608 
609 static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
610 {
611 	struct v4l2_subdev *sd;
612 	struct exynos_media_pipeline *ep;
613 	int ret;
614 
615 	if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
616 		return -EBUSY;
617 
618 	sd = &fimc->vid_cap.subdev;
619 	sd->grp_id = GRP_ID_FIMC;
620 
621 	ep = fimc_md_pipeline_create(fmd);
622 	if (!ep)
623 		return -ENOMEM;
624 
625 	v4l2_set_subdev_hostdata(sd, ep);
626 
627 	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
628 	if (!ret) {
629 		if (!fmd->pmf && fimc->pdev)
630 			fmd->pmf = &fimc->pdev->dev;
631 		fmd->fimc[fimc->id] = fimc;
632 		fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
633 	} else {
634 		v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
635 			 fimc->id, ret);
636 	}
637 	return ret;
638 }
639 
640 static int register_csis_entity(struct fimc_md *fmd,
641 				struct platform_device *pdev,
642 				struct v4l2_subdev *sd)
643 {
644 	struct device_node *node = pdev->dev.of_node;
645 	int id, ret;
646 
647 	id = node ? __of_get_csis_id(node) : max(0, pdev->id);
648 
649 	if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
650 		return -ENOENT;
651 
652 	if (WARN_ON(fmd->csis[id].sd))
653 		return -EBUSY;
654 
655 	sd->grp_id = GRP_ID_CSIS;
656 	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
657 	if (!ret)
658 		fmd->csis[id].sd = sd;
659 	else
660 		v4l2_err(&fmd->v4l2_dev,
661 			 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
662 	return ret;
663 }
664 
665 static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
666 {
667 	struct v4l2_subdev *sd = &is->isp.subdev;
668 	struct exynos_media_pipeline *ep;
669 	int ret;
670 
671 	/* Allocate pipeline object for the ISP capture video node. */
672 	ep = fimc_md_pipeline_create(fmd);
673 	if (!ep)
674 		return -ENOMEM;
675 
676 	v4l2_set_subdev_hostdata(sd, ep);
677 
678 	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
679 	if (ret) {
680 		v4l2_err(&fmd->v4l2_dev,
681 			 "Failed to register FIMC-ISP (%d)\n", ret);
682 		return ret;
683 	}
684 
685 	fmd->fimc_is = is;
686 	return 0;
687 }
688 
689 static int fimc_md_register_platform_entity(struct fimc_md *fmd,
690 					    struct platform_device *pdev,
691 					    int plat_entity)
692 {
693 	struct device *dev = &pdev->dev;
694 	int ret = -EPROBE_DEFER;
695 	void *drvdata;
696 
697 	/* Lock to ensure dev->driver won't change. */
698 	device_lock(dev);
699 
700 	if (!dev->driver || !try_module_get(dev->driver->owner))
701 		goto dev_unlock;
702 
703 	drvdata = dev_get_drvdata(dev);
704 	/* Some subdev didn't probe successfully id drvdata is NULL */
705 	if (drvdata) {
706 		switch (plat_entity) {
707 		case IDX_FIMC:
708 			ret = register_fimc_entity(fmd, drvdata);
709 			break;
710 		case IDX_FLITE:
711 			ret = register_fimc_lite_entity(fmd, drvdata);
712 			break;
713 		case IDX_CSIS:
714 			ret = register_csis_entity(fmd, pdev, drvdata);
715 			break;
716 		case IDX_IS_ISP:
717 			ret = register_fimc_is_entity(fmd, drvdata);
718 			break;
719 		default:
720 			ret = -ENODEV;
721 		}
722 	}
723 
724 	module_put(dev->driver->owner);
725 dev_unlock:
726 	device_unlock(dev);
727 	if (ret == -EPROBE_DEFER)
728 		dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
729 			dev_name(dev));
730 	else if (ret < 0)
731 		dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
732 			dev_name(dev), ret);
733 	return ret;
734 }
735 
736 /* Register FIMC, FIMC-LITE and CSIS media entities */
737 static int fimc_md_register_platform_entities(struct fimc_md *fmd,
738 					      struct device_node *parent)
739 {
740 	struct device_node *node;
741 	int ret = 0;
742 
743 	for_each_available_child_of_node(parent, node) {
744 		struct platform_device *pdev;
745 		int plat_entity = -1;
746 
747 		pdev = of_find_device_by_node(node);
748 		if (!pdev)
749 			continue;
750 
751 		/* If driver of any entity isn't ready try all again later. */
752 		if (of_node_name_eq(node, CSIS_OF_NODE_NAME))
753 			plat_entity = IDX_CSIS;
754 		else if (of_node_name_eq(node, FIMC_IS_OF_NODE_NAME))
755 			plat_entity = IDX_IS_ISP;
756 		else if (of_node_name_eq(node, FIMC_LITE_OF_NODE_NAME))
757 			plat_entity = IDX_FLITE;
758 		else if (of_node_name_eq(node, FIMC_OF_NODE_NAME) &&
759 			 !of_property_read_bool(node, "samsung,lcd-wb"))
760 			plat_entity = IDX_FIMC;
761 
762 		if (plat_entity >= 0)
763 			ret = fimc_md_register_platform_entity(fmd, pdev,
764 							plat_entity);
765 		put_device(&pdev->dev);
766 		if (ret < 0) {
767 			of_node_put(node);
768 			break;
769 		}
770 	}
771 
772 	return ret;
773 }
774 
775 static void fimc_md_unregister_entities(struct fimc_md *fmd)
776 {
777 	int i;
778 
779 	for (i = 0; i < FIMC_MAX_DEVS; i++) {
780 		struct fimc_dev *dev = fmd->fimc[i];
781 		if (dev == NULL)
782 			continue;
783 		v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
784 		dev->vid_cap.ve.pipe = NULL;
785 		fmd->fimc[i] = NULL;
786 	}
787 	for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
788 		struct fimc_lite *dev = fmd->fimc_lite[i];
789 		if (dev == NULL)
790 			continue;
791 		v4l2_device_unregister_subdev(&dev->subdev);
792 		dev->ve.pipe = NULL;
793 		fmd->fimc_lite[i] = NULL;
794 	}
795 	for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
796 		if (fmd->csis[i].sd == NULL)
797 			continue;
798 		v4l2_device_unregister_subdev(fmd->csis[i].sd);
799 		fmd->csis[i].sd = NULL;
800 	}
801 
802 	if (fmd->fimc_is)
803 		v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
804 
805 	v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
806 }
807 
808 /**
809  * __fimc_md_create_fimc_sink_links - create links to all FIMC entities
810  * @fmd: fimc media device
811  * @source: the source entity to create links to all fimc entities from
812  * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
813  * @pad: the source entity pad index
814  * @link_mask: bitmask of the fimc devices for which link should be enabled
815  */
816 static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
817 					    struct media_entity *source,
818 					    struct v4l2_subdev *sensor,
819 					    int pad, int link_mask)
820 {
821 	struct fimc_source_info *si = NULL;
822 	struct media_entity *sink;
823 	unsigned int flags = 0;
824 	int i, ret = 0;
825 
826 	if (sensor) {
827 		si = v4l2_get_subdev_hostdata(sensor);
828 		/* Skip direct FIMC links in the logical FIMC-IS sensor path */
829 		if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
830 			ret = 1;
831 	}
832 
833 	for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
834 		if (!fmd->fimc[i])
835 			continue;
836 		/*
837 		 * Some FIMC variants are not fitted with camera capture
838 		 * interface. Skip creating a link from sensor for those.
839 		 */
840 		if (!fmd->fimc[i]->variant->has_cam_if)
841 			continue;
842 
843 		flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
844 
845 		sink = &fmd->fimc[i]->vid_cap.subdev.entity;
846 		ret = media_create_pad_link(source, pad, sink,
847 					      FIMC_SD_PAD_SINK_CAM, flags);
848 		if (ret)
849 			return ret;
850 
851 		/* Notify FIMC capture subdev entity */
852 		ret = media_entity_call(sink, link_setup, &sink->pads[0],
853 					&source->pads[pad], flags);
854 		if (ret)
855 			break;
856 
857 		v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
858 			  source->name, flags ? '=' : '-', sink->name);
859 	}
860 
861 	for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
862 		if (!fmd->fimc_lite[i])
863 			continue;
864 
865 		sink = &fmd->fimc_lite[i]->subdev.entity;
866 		ret = media_create_pad_link(source, pad, sink,
867 					       FLITE_SD_PAD_SINK, 0);
868 		if (ret)
869 			return ret;
870 
871 		/* Notify FIMC-LITE subdev entity */
872 		ret = media_entity_call(sink, link_setup, &sink->pads[0],
873 					&source->pads[pad], 0);
874 		if (ret)
875 			break;
876 
877 		v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
878 			  source->name, sink->name);
879 	}
880 	return 0;
881 }
882 
883 /* Create links from FIMC-LITE source pads to other entities */
884 static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
885 {
886 	struct media_entity *source, *sink;
887 	int i, ret = 0;
888 
889 	for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
890 		struct fimc_lite *fimc = fmd->fimc_lite[i];
891 
892 		if (fimc == NULL)
893 			continue;
894 
895 		source = &fimc->subdev.entity;
896 		sink = &fimc->ve.vdev.entity;
897 		/* FIMC-LITE's subdev and video node */
898 		ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_DMA,
899 					       sink, 0, 0);
900 		if (ret)
901 			break;
902 		/* Link from FIMC-LITE to IS-ISP subdev */
903 		sink = &fmd->fimc_is->isp.subdev.entity;
904 		ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_ISP,
905 					       sink, 0, 0);
906 		if (ret)
907 			break;
908 	}
909 
910 	return ret;
911 }
912 
913 /* Create FIMC-IS links */
914 static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
915 {
916 	struct fimc_isp *isp = &fmd->fimc_is->isp;
917 	struct media_entity *source, *sink;
918 	int i, ret;
919 
920 	source = &isp->subdev.entity;
921 
922 	for (i = 0; i < FIMC_MAX_DEVS; i++) {
923 		if (fmd->fimc[i] == NULL)
924 			continue;
925 
926 		/* Link from FIMC-IS-ISP subdev to FIMC */
927 		sink = &fmd->fimc[i]->vid_cap.subdev.entity;
928 		ret = media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
929 					       sink, FIMC_SD_PAD_SINK_FIFO, 0);
930 		if (ret)
931 			return ret;
932 	}
933 
934 	/* Link from FIMC-IS-ISP subdev to fimc-is-isp.capture video node */
935 	sink = &isp->video_capture.ve.vdev.entity;
936 
937 	/* Skip this link if the fimc-is-isp video node driver isn't built-in */
938 	if (sink->num_pads == 0)
939 		return 0;
940 
941 	return media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_DMA,
942 					sink, 0, 0);
943 }
944 
945 /**
946  * fimc_md_create_links - create default links between registered entities
947  * @fmd: fimc media device
948  *
949  * Parallel interface sensor entities are connected directly to FIMC capture
950  * entities. The sensors using MIPI CSIS bus are connected through immutable
951  * link with CSI receiver entity specified by mux_id. Any registered CSIS
952  * entity has a link to each registered FIMC capture entity. Enabled links
953  * are created by default between each subsequent registered sensor and
954  * subsequent FIMC capture entity. The number of default active links is
955  * determined by the number of available sensors or FIMC entities,
956  * whichever is less.
957  */
958 static int fimc_md_create_links(struct fimc_md *fmd)
959 {
960 	struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
961 	struct v4l2_subdev *sensor, *csis;
962 	struct fimc_source_info *pdata;
963 	struct media_entity *source, *sink;
964 	int i, pad, fimc_id = 0, ret = 0;
965 	u32 flags, link_mask = 0;
966 
967 	for (i = 0; i < fmd->num_sensors; i++) {
968 		if (fmd->sensor[i].subdev == NULL)
969 			continue;
970 
971 		sensor = fmd->sensor[i].subdev;
972 		pdata = v4l2_get_subdev_hostdata(sensor);
973 		if (!pdata)
974 			continue;
975 
976 		source = NULL;
977 
978 		switch (pdata->sensor_bus_type) {
979 		case FIMC_BUS_TYPE_MIPI_CSI2:
980 			if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
981 				"Wrong CSI channel id: %d\n", pdata->mux_id))
982 				return -EINVAL;
983 
984 			csis = fmd->csis[pdata->mux_id].sd;
985 			if (WARN(csis == NULL,
986 				 "MIPI-CSI interface specified but s5p-csis module is not loaded!\n"))
987 				return -EINVAL;
988 
989 			pad = sensor->entity.num_pads - 1;
990 			ret = media_create_pad_link(&sensor->entity, pad,
991 					      &csis->entity, CSIS_PAD_SINK,
992 					      MEDIA_LNK_FL_IMMUTABLE |
993 					      MEDIA_LNK_FL_ENABLED);
994 			if (ret)
995 				return ret;
996 
997 			v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
998 				  sensor->entity.name, csis->entity.name);
999 
1000 			source = NULL;
1001 			csi_sensors[pdata->mux_id] = sensor;
1002 			break;
1003 
1004 		case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
1005 			source = &sensor->entity;
1006 			pad = 0;
1007 			break;
1008 
1009 		default:
1010 			v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
1011 				 pdata->sensor_bus_type);
1012 			return -EINVAL;
1013 		}
1014 		if (source == NULL)
1015 			continue;
1016 
1017 		link_mask = 1 << fimc_id++;
1018 		ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1019 						       pad, link_mask);
1020 	}
1021 
1022 	for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
1023 		if (fmd->csis[i].sd == NULL)
1024 			continue;
1025 
1026 		source = &fmd->csis[i].sd->entity;
1027 		pad = CSIS_PAD_SOURCE;
1028 		sensor = csi_sensors[i];
1029 
1030 		link_mask = 1 << fimc_id++;
1031 		ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1032 						       pad, link_mask);
1033 	}
1034 
1035 	/* Create immutable links between each FIMC's subdev and video node */
1036 	flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
1037 	for (i = 0; i < FIMC_MAX_DEVS; i++) {
1038 		if (!fmd->fimc[i])
1039 			continue;
1040 
1041 		source = &fmd->fimc[i]->vid_cap.subdev.entity;
1042 		sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
1043 
1044 		ret = media_create_pad_link(source, FIMC_SD_PAD_SOURCE,
1045 					      sink, 0, flags);
1046 		if (ret)
1047 			break;
1048 	}
1049 
1050 	ret = __fimc_md_create_flite_source_links(fmd);
1051 	if (ret < 0)
1052 		return ret;
1053 
1054 	if (fmd->use_isp)
1055 		ret = __fimc_md_create_fimc_is_links(fmd);
1056 
1057 	return ret;
1058 }
1059 
1060 /*
1061  * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
1062  */
1063 static void fimc_md_put_clocks(struct fimc_md *fmd)
1064 {
1065 	int i = FIMC_MAX_CAMCLKS;
1066 
1067 	while (--i >= 0) {
1068 		if (IS_ERR(fmd->camclk[i].clock))
1069 			continue;
1070 		clk_put(fmd->camclk[i].clock);
1071 		fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1072 	}
1073 
1074 	/* Writeback (PIXELASYNCMx) clocks */
1075 	for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1076 		if (IS_ERR(fmd->wbclk[i]))
1077 			continue;
1078 		clk_put(fmd->wbclk[i]);
1079 		fmd->wbclk[i] = ERR_PTR(-EINVAL);
1080 	}
1081 }
1082 
1083 static int fimc_md_get_clocks(struct fimc_md *fmd)
1084 {
1085 	struct device *dev = &fmd->pdev->dev;
1086 	char clk_name[32];
1087 	struct clk *clock;
1088 	int i, ret = 0;
1089 
1090 	for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1091 		fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1092 
1093 	for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1094 		snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1095 		clock = clk_get(dev, clk_name);
1096 
1097 		if (IS_ERR(clock)) {
1098 			dev_err(dev, "Failed to get clock: %s\n", clk_name);
1099 			ret = PTR_ERR(clock);
1100 			break;
1101 		}
1102 		fmd->camclk[i].clock = clock;
1103 	}
1104 	if (ret)
1105 		fimc_md_put_clocks(fmd);
1106 
1107 	if (!fmd->use_isp)
1108 		return 0;
1109 	/*
1110 	 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1111 	 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1112 	 */
1113 	fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1114 
1115 	for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1116 		snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1117 		clock = clk_get(dev, clk_name);
1118 		if (IS_ERR(clock)) {
1119 			v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1120 				  clk_name);
1121 			ret = PTR_ERR(clock);
1122 			break;
1123 		}
1124 		fmd->wbclk[i] = clock;
1125 	}
1126 	if (ret)
1127 		fimc_md_put_clocks(fmd);
1128 
1129 	return ret;
1130 }
1131 
1132 static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
1133 {
1134 	struct exynos_video_entity *ve;
1135 	struct fimc_pipeline *p;
1136 	struct video_device *vdev;
1137 	int ret;
1138 
1139 	vdev = media_entity_to_video_device(entity);
1140 	if (vdev->entity.use_count == 0)
1141 		return 0;
1142 
1143 	ve = vdev_to_exynos_video_entity(vdev);
1144 	p = to_fimc_pipeline(ve->pipe);
1145 	/*
1146 	 * Nothing to do if we are disabling the pipeline, some link
1147 	 * has been disconnected and p->subdevs array is cleared now.
1148 	 */
1149 	if (!enable && p->subdevs[IDX_SENSOR] == NULL)
1150 		return 0;
1151 
1152 	if (enable)
1153 		ret = __fimc_pipeline_open(ve->pipe, entity, true);
1154 	else
1155 		ret = __fimc_pipeline_close(ve->pipe);
1156 
1157 	if (ret == 0 && !enable)
1158 		memset(p->subdevs, 0, sizeof(p->subdevs));
1159 
1160 	return ret;
1161 }
1162 
1163 /* Locking: called with entity->graph_obj.mdev->graph_mutex mutex held. */
1164 static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable,
1165 				      struct media_graph *graph)
1166 {
1167 	struct media_entity *entity_err = entity;
1168 	int ret;
1169 
1170 	/*
1171 	 * Walk current graph and call the pipeline open/close routine for each
1172 	 * opened video node that belongs to the graph of entities connected
1173 	 * through active links. This is needed as we cannot power on/off the
1174 	 * subdevs in random order.
1175 	 */
1176 	media_graph_walk_start(graph, entity);
1177 
1178 	while ((entity = media_graph_walk_next(graph))) {
1179 		if (!is_media_entity_v4l2_video_device(entity))
1180 			continue;
1181 
1182 		ret  = __fimc_md_modify_pipeline(entity, enable);
1183 
1184 		if (ret < 0)
1185 			goto err;
1186 	}
1187 
1188 	return 0;
1189 
1190 err:
1191 	media_graph_walk_start(graph, entity_err);
1192 
1193 	while ((entity_err = media_graph_walk_next(graph))) {
1194 		if (!is_media_entity_v4l2_video_device(entity_err))
1195 			continue;
1196 
1197 		__fimc_md_modify_pipeline(entity_err, !enable);
1198 
1199 		if (entity_err == entity)
1200 			break;
1201 	}
1202 
1203 	return ret;
1204 }
1205 
1206 static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
1207 				unsigned int notification)
1208 {
1209 	struct media_graph *graph =
1210 		&container_of(link->graph_obj.mdev, struct fimc_md,
1211 			      media_dev)->link_setup_graph;
1212 	struct media_entity *sink = link->sink->entity;
1213 	int ret = 0;
1214 
1215 	/* Before link disconnection */
1216 	if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
1217 		ret = media_graph_walk_init(graph,
1218 						   link->graph_obj.mdev);
1219 		if (ret)
1220 			return ret;
1221 		if (!(flags & MEDIA_LNK_FL_ENABLED))
1222 			ret = __fimc_md_modify_pipelines(sink, false, graph);
1223 #if 0
1224 		else
1225 			/* TODO: Link state change validation */
1226 #endif
1227 	/* After link activation */
1228 	} else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH) {
1229 		if (link->flags & MEDIA_LNK_FL_ENABLED)
1230 			ret = __fimc_md_modify_pipelines(sink, true, graph);
1231 		media_graph_walk_cleanup(graph);
1232 	}
1233 
1234 	return ret ? -EPIPE : 0;
1235 }
1236 
1237 static const struct media_device_ops fimc_md_ops = {
1238 	.link_notify = fimc_md_link_notify,
1239 };
1240 
1241 static ssize_t subdev_conf_mode_show(struct device *dev,
1242 				     struct device_attribute *attr, char *buf)
1243 {
1244 	struct fimc_md *fmd = dev_get_drvdata(dev);
1245 
1246 	if (fmd->user_subdev_api)
1247 		return strscpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1248 
1249 	return strscpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1250 }
1251 
1252 static ssize_t subdev_conf_mode_store(struct device *dev,
1253 				      struct device_attribute *attr,
1254 				      const char *buf, size_t count)
1255 {
1256 	struct fimc_md *fmd = dev_get_drvdata(dev);
1257 	bool subdev_api;
1258 	int i;
1259 
1260 	if (!strcmp(buf, "vid-dev\n"))
1261 		subdev_api = false;
1262 	else if (!strcmp(buf, "sub-dev\n"))
1263 		subdev_api = true;
1264 	else
1265 		return count;
1266 
1267 	fmd->user_subdev_api = subdev_api;
1268 	for (i = 0; i < FIMC_MAX_DEVS; i++)
1269 		if (fmd->fimc[i])
1270 			fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1271 	return count;
1272 }
1273 /*
1274  * This device attribute is to select video pipeline configuration method.
1275  * There are following valid values:
1276  *  vid-dev - for V4L2 video node API only, subdevice will be configured
1277  *  by the host driver.
1278  *  sub-dev - for media controller API, subdevs must be configured in user
1279  *  space before starting streaming.
1280  */
1281 static DEVICE_ATTR_RW(subdev_conf_mode);
1282 
1283 static int cam_clk_prepare(struct clk_hw *hw)
1284 {
1285 	struct cam_clk *camclk = to_cam_clk(hw);
1286 
1287 	if (camclk->fmd->pmf == NULL)
1288 		return -ENODEV;
1289 
1290 	return pm_runtime_resume_and_get(camclk->fmd->pmf);
1291 }
1292 
1293 static void cam_clk_unprepare(struct clk_hw *hw)
1294 {
1295 	struct cam_clk *camclk = to_cam_clk(hw);
1296 
1297 	if (camclk->fmd->pmf == NULL)
1298 		return;
1299 
1300 	pm_runtime_put_sync(camclk->fmd->pmf);
1301 }
1302 
1303 static const struct clk_ops cam_clk_ops = {
1304 	.prepare = cam_clk_prepare,
1305 	.unprepare = cam_clk_unprepare,
1306 };
1307 
1308 static void fimc_md_unregister_clk_provider(struct fimc_md *fmd)
1309 {
1310 	struct cam_clk_provider *cp = &fmd->clk_provider;
1311 	unsigned int i;
1312 
1313 	if (cp->of_node)
1314 		of_clk_del_provider(cp->of_node);
1315 
1316 	for (i = 0; i < cp->num_clocks; i++)
1317 		clk_unregister(cp->clks[i]);
1318 }
1319 
1320 static int fimc_md_register_clk_provider(struct fimc_md *fmd)
1321 {
1322 	struct cam_clk_provider *cp = &fmd->clk_provider;
1323 	struct device *dev = &fmd->pdev->dev;
1324 	int i, ret;
1325 
1326 	for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1327 		struct cam_clk *camclk = &cp->camclk[i];
1328 		struct clk_init_data init;
1329 		const char *p_name;
1330 
1331 		ret = of_property_read_string_index(dev->of_node,
1332 					"clock-output-names", i, &init.name);
1333 		if (ret < 0)
1334 			break;
1335 
1336 		p_name = __clk_get_name(fmd->camclk[i].clock);
1337 
1338 		/* It's safe since clk_register() will duplicate the string. */
1339 		init.parent_names = &p_name;
1340 		init.num_parents = 1;
1341 		init.ops = &cam_clk_ops;
1342 		init.flags = CLK_SET_RATE_PARENT;
1343 		camclk->hw.init = &init;
1344 		camclk->fmd = fmd;
1345 
1346 		cp->clks[i] = clk_register(NULL, &camclk->hw);
1347 		if (IS_ERR(cp->clks[i])) {
1348 			dev_err(dev, "failed to register clock: %s (%ld)\n",
1349 					init.name, PTR_ERR(cp->clks[i]));
1350 			ret = PTR_ERR(cp->clks[i]);
1351 			goto err;
1352 		}
1353 		cp->num_clocks++;
1354 	}
1355 
1356 	if (cp->num_clocks == 0) {
1357 		dev_warn(dev, "clk provider not registered\n");
1358 		return 0;
1359 	}
1360 
1361 	cp->clk_data.clks = cp->clks;
1362 	cp->clk_data.clk_num = cp->num_clocks;
1363 	cp->of_node = dev->of_node;
1364 	ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1365 				  &cp->clk_data);
1366 	if (ret == 0)
1367 		return 0;
1368 err:
1369 	fimc_md_unregister_clk_provider(fmd);
1370 	return ret;
1371 }
1372 
1373 static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
1374 				 struct v4l2_subdev *subdev,
1375 				 struct v4l2_async_subdev *asd)
1376 {
1377 	struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1378 	struct fimc_sensor_info *si = NULL;
1379 	int i;
1380 
1381 	/* Find platform data for this sensor subdev */
1382 	for (i = 0; i < ARRAY_SIZE(fmd->sensor); i++)
1383 		if (fmd->sensor[i].asd &&
1384 		    fmd->sensor[i].asd->match.fwnode ==
1385 		    of_fwnode_handle(subdev->dev->of_node))
1386 			si = &fmd->sensor[i];
1387 
1388 	if (si == NULL)
1389 		return -EINVAL;
1390 
1391 	v4l2_set_subdev_hostdata(subdev, &si->pdata);
1392 
1393 	if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
1394 		subdev->grp_id = GRP_ID_FIMC_IS_SENSOR;
1395 	else
1396 		subdev->grp_id = GRP_ID_SENSOR;
1397 
1398 	si->subdev = subdev;
1399 
1400 	v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
1401 		  subdev->name, fmd->num_sensors);
1402 
1403 	fmd->num_sensors++;
1404 
1405 	return 0;
1406 }
1407 
1408 static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
1409 {
1410 	struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1411 	int ret;
1412 
1413 	mutex_lock(&fmd->media_dev.graph_mutex);
1414 
1415 	ret = fimc_md_create_links(fmd);
1416 	if (ret < 0)
1417 		goto unlock;
1418 
1419 	ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1420 unlock:
1421 	mutex_unlock(&fmd->media_dev.graph_mutex);
1422 	if (ret < 0)
1423 		return ret;
1424 
1425 	return media_device_register(&fmd->media_dev);
1426 }
1427 
1428 static const struct v4l2_async_notifier_operations subdev_notifier_ops = {
1429 	.bound = subdev_notifier_bound,
1430 	.complete = subdev_notifier_complete,
1431 };
1432 
1433 static int fimc_md_probe(struct platform_device *pdev)
1434 {
1435 	struct device *dev = &pdev->dev;
1436 	struct v4l2_device *v4l2_dev;
1437 	struct pinctrl *pinctrl;
1438 	struct fimc_md *fmd;
1439 	int ret;
1440 
1441 	fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
1442 	if (!fmd)
1443 		return -ENOMEM;
1444 
1445 	spin_lock_init(&fmd->slock);
1446 	INIT_LIST_HEAD(&fmd->pipelines);
1447 	fmd->pdev = pdev;
1448 
1449 	strscpy(fmd->media_dev.model, "Samsung S5P FIMC",
1450 		sizeof(fmd->media_dev.model));
1451 	fmd->media_dev.ops = &fimc_md_ops;
1452 	fmd->media_dev.dev = dev;
1453 
1454 	v4l2_dev = &fmd->v4l2_dev;
1455 	v4l2_dev->mdev = &fmd->media_dev;
1456 	v4l2_dev->notify = fimc_sensor_notify;
1457 	strscpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
1458 
1459 	fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
1460 	fmd->user_subdev_api = true;
1461 
1462 	media_device_init(&fmd->media_dev);
1463 
1464 	ret = v4l2_device_register(dev, &fmd->v4l2_dev);
1465 	if (ret < 0) {
1466 		v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
1467 		goto err_md;
1468 	}
1469 
1470 	ret = fimc_md_get_clocks(fmd);
1471 	if (ret)
1472 		goto err_v4l2dev;
1473 
1474 	pinctrl = devm_pinctrl_get(dev);
1475 	if (IS_ERR(pinctrl)) {
1476 		ret = PTR_ERR(pinctrl);
1477 		if (ret != EPROBE_DEFER)
1478 			dev_err(dev, "Failed to get pinctrl: %d\n", ret);
1479 		goto err_clk;
1480 	}
1481 
1482 	platform_set_drvdata(pdev, fmd);
1483 
1484 	v4l2_async_nf_init(&fmd->subdev_notifier);
1485 
1486 	ret = fimc_md_register_platform_entities(fmd, dev->of_node);
1487 	if (ret)
1488 		goto err_clk;
1489 
1490 	ret = fimc_md_register_sensor_entities(fmd);
1491 	if (ret)
1492 		goto err_m_ent;
1493 
1494 	ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1495 	if (ret)
1496 		goto err_cleanup;
1497 	/*
1498 	 * FIMC platform devices need to be registered before the sclk_cam
1499 	 * clocks provider, as one of these devices needs to be activated
1500 	 * to enable the clock.
1501 	 */
1502 	ret = fimc_md_register_clk_provider(fmd);
1503 	if (ret < 0) {
1504 		v4l2_err(v4l2_dev, "clock provider registration failed\n");
1505 		goto err_attr;
1506 	}
1507 
1508 	if (fmd->num_sensors > 0) {
1509 		fmd->subdev_notifier.ops = &subdev_notifier_ops;
1510 		fmd->num_sensors = 0;
1511 
1512 		ret = v4l2_async_nf_register(&fmd->v4l2_dev,
1513 					     &fmd->subdev_notifier);
1514 		if (ret)
1515 			goto err_clk_p;
1516 	}
1517 
1518 	return 0;
1519 
1520 err_clk_p:
1521 	fimc_md_unregister_clk_provider(fmd);
1522 err_attr:
1523 	device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1524 err_cleanup:
1525 	v4l2_async_nf_cleanup(&fmd->subdev_notifier);
1526 err_m_ent:
1527 	fimc_md_unregister_entities(fmd);
1528 err_clk:
1529 	fimc_md_put_clocks(fmd);
1530 err_v4l2dev:
1531 	v4l2_device_unregister(&fmd->v4l2_dev);
1532 err_md:
1533 	media_device_cleanup(&fmd->media_dev);
1534 	return ret;
1535 }
1536 
1537 static int fimc_md_remove(struct platform_device *pdev)
1538 {
1539 	struct fimc_md *fmd = platform_get_drvdata(pdev);
1540 
1541 	if (!fmd)
1542 		return 0;
1543 
1544 	fimc_md_unregister_clk_provider(fmd);
1545 	v4l2_async_nf_unregister(&fmd->subdev_notifier);
1546 	v4l2_async_nf_cleanup(&fmd->subdev_notifier);
1547 
1548 	v4l2_device_unregister(&fmd->v4l2_dev);
1549 	device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1550 	fimc_md_unregister_entities(fmd);
1551 	fimc_md_pipelines_free(fmd);
1552 	media_device_unregister(&fmd->media_dev);
1553 	media_device_cleanup(&fmd->media_dev);
1554 	fimc_md_put_clocks(fmd);
1555 
1556 	return 0;
1557 }
1558 
1559 static const struct platform_device_id fimc_driver_ids[] __always_unused = {
1560 	{ .name = "s5p-fimc-md" },
1561 	{ },
1562 };
1563 MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1564 
1565 static const struct of_device_id fimc_md_of_match[] = {
1566 	{ .compatible = "samsung,fimc" },
1567 	{ },
1568 };
1569 MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1570 
1571 static struct platform_driver fimc_md_driver = {
1572 	.probe		= fimc_md_probe,
1573 	.remove		= fimc_md_remove,
1574 	.driver = {
1575 		.of_match_table = of_match_ptr(fimc_md_of_match),
1576 		.name		= "s5p-fimc-md",
1577 	}
1578 };
1579 
1580 static int __init fimc_md_init(void)
1581 {
1582 	int ret;
1583 
1584 	request_module("s5p-csis");
1585 	ret = fimc_register_driver();
1586 	if (ret)
1587 		return ret;
1588 
1589 	return platform_driver_register(&fimc_md_driver);
1590 }
1591 
1592 static void __exit fimc_md_exit(void)
1593 {
1594 	platform_driver_unregister(&fimc_md_driver);
1595 	fimc_unregister_driver();
1596 }
1597 
1598 module_init(fimc_md_init);
1599 module_exit(fimc_md_exit);
1600 
1601 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1602 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1603 MODULE_LICENSE("GPL");
1604 MODULE_VERSION("2.0.1");
1605