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. */
__setup_sensor_notification(struct fimc_md * fmd,struct v4l2_subdev * sensor,struct v4l2_subdev * fimc_sd)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  */
fimc_pipeline_prepare(struct fimc_pipeline * p,struct media_entity * me)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  */
__subdev_set_power(struct v4l2_subdev * sd,int on)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  */
fimc_pipeline_s_power(struct fimc_pipeline * p,bool on)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  */
__fimc_pipeline_enable(struct exynos_media_pipeline * ep,struct fimc_md * fmd)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  */
__fimc_pipeline_open(struct exynos_media_pipeline * ep,struct media_entity * me,bool prepare)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  */
__fimc_pipeline_close(struct exynos_media_pipeline * ep)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  */
__fimc_pipeline_s_stream(struct exynos_media_pipeline * ep,bool on)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 
fimc_md_pipeline_create(struct fimc_md * fmd)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 
fimc_md_pipelines_free(struct fimc_md * fmd)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 
fimc_md_parse_one_endpoint(struct fimc_md * fmd,struct device_node * ep)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_fwnode_endpoint endpoint = { .bus_type = 0 };
405 	int ret;
406 
407 	ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &endpoint);
408 	if (ret) {
409 		of_node_put(ep);
410 		return ret;
411 	}
412 
413 	if (WARN_ON(endpoint.base.port == 0) || index >= FIMC_MAX_SENSORS) {
414 		of_node_put(ep);
415 		return -EINVAL;
416 	}
417 
418 	pd->mux_id = (endpoint.base.port - 1) & 0x1;
419 
420 	rem = of_graph_get_remote_port_parent(ep);
421 	of_node_put(ep);
422 	if (rem == NULL) {
423 		v4l2_info(&fmd->v4l2_dev, "Remote device at %pOF not found\n",
424 							ep);
425 		return 0;
426 	}
427 
428 	if (fimc_input_is_parallel(endpoint.base.port)) {
429 		if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
430 			pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
431 		else
432 			pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
433 		pd->flags = endpoint.bus.parallel.flags;
434 	} else if (fimc_input_is_mipi_csi(endpoint.base.port)) {
435 		/*
436 		 * MIPI CSI-2: only input mux selection and
437 		 * the sensor's clock frequency is needed.
438 		 */
439 		pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
440 	} else {
441 		v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %pOF\n",
442 			 endpoint.base.port, rem);
443 	}
444 	/*
445 	 * For FIMC-IS handled sensors, that are placed under i2c-isp device
446 	 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
447 	 * input. Sensors are attached to the FIMC-LITE hostdata interface
448 	 * directly or through MIPI-CSIS, depending on the external media bus
449 	 * used. This needs to be handled in a more reliable way, not by just
450 	 * checking parent's node name.
451 	 */
452 	np = of_get_parent(rem);
453 
454 	if (of_node_name_eq(np, "i2c-isp"))
455 		pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
456 	else
457 		pd->fimc_bus_type = pd->sensor_bus_type;
458 	of_node_put(np);
459 
460 	if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor))) {
461 		of_node_put(rem);
462 		return -EINVAL;
463 	}
464 
465 	fmd->sensor[index].asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
466 	fmd->sensor[index].asd.match.fwnode = of_fwnode_handle(rem);
467 
468 	ret = v4l2_async_notifier_add_subdev(&fmd->subdev_notifier,
469 					     &fmd->sensor[index].asd);
470 	if (ret) {
471 		of_node_put(rem);
472 		return ret;
473 	}
474 
475 	fmd->num_sensors++;
476 
477 	return 0;
478 }
479 
480 /* Parse port node and register as a sub-device any sensor specified there. */
fimc_md_parse_port_node(struct fimc_md * fmd,struct device_node * port)481 static int fimc_md_parse_port_node(struct fimc_md *fmd,
482 				   struct device_node *port)
483 {
484 	struct device_node *ep;
485 	int ret;
486 
487 	for_each_child_of_node(port, ep) {
488 		ret = fimc_md_parse_one_endpoint(fmd, ep);
489 		if (ret < 0)
490 			return ret;
491 	}
492 
493 	return 0;
494 }
495 
496 /* Register all SoC external sub-devices */
fimc_md_register_sensor_entities(struct fimc_md * fmd)497 static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
498 {
499 	struct device_node *parent = fmd->pdev->dev.of_node;
500 	struct device_node *ports = NULL;
501 	struct device_node *node;
502 	int ret;
503 
504 	/*
505 	 * Runtime resume one of the FIMC entities to make sure
506 	 * the sclk_cam clocks are not globally disabled.
507 	 */
508 	if (!fmd->pmf)
509 		return -ENXIO;
510 
511 	ret = pm_runtime_get_sync(fmd->pmf);
512 	if (ret < 0) {
513 		pm_runtime_put(fmd->pmf);
514 		return ret;
515 	}
516 
517 	fmd->num_sensors = 0;
518 
519 	/* Attach sensors linked to MIPI CSI-2 receivers */
520 	for_each_available_child_of_node(parent, node) {
521 		struct device_node *port;
522 
523 		if (!of_node_name_eq(node, "csis"))
524 			continue;
525 		/* The csis node can have only port subnode. */
526 		port = of_get_next_child(node, NULL);
527 		if (!port)
528 			continue;
529 
530 		ret = fimc_md_parse_port_node(fmd, port);
531 		of_node_put(port);
532 		if (ret < 0) {
533 			of_node_put(node);
534 			goto cleanup;
535 		}
536 	}
537 
538 	/* Attach sensors listed in the parallel-ports node */
539 	ports = of_get_child_by_name(parent, "parallel-ports");
540 	if (!ports)
541 		goto rpm_put;
542 
543 	for_each_child_of_node(ports, node) {
544 		ret = fimc_md_parse_port_node(fmd, node);
545 		if (ret < 0) {
546 			of_node_put(node);
547 			goto cleanup;
548 		}
549 	}
550 	of_node_put(ports);
551 
552 rpm_put:
553 	pm_runtime_put(fmd->pmf);
554 	return 0;
555 
556 cleanup:
557 	of_node_put(ports);
558 	v4l2_async_notifier_cleanup(&fmd->subdev_notifier);
559 	pm_runtime_put(fmd->pmf);
560 	return ret;
561 }
562 
__of_get_csis_id(struct device_node * np)563 static int __of_get_csis_id(struct device_node *np)
564 {
565 	u32 reg = 0;
566 
567 	np = of_get_child_by_name(np, "port");
568 	if (!np)
569 		return -EINVAL;
570 	of_property_read_u32(np, "reg", &reg);
571 	of_node_put(np);
572 	return reg - FIMC_INPUT_MIPI_CSI2_0;
573 }
574 
575 /*
576  * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
577  */
register_fimc_lite_entity(struct fimc_md * fmd,struct fimc_lite * fimc_lite)578 static int register_fimc_lite_entity(struct fimc_md *fmd,
579 				     struct fimc_lite *fimc_lite)
580 {
581 	struct v4l2_subdev *sd;
582 	struct exynos_media_pipeline *ep;
583 	int ret;
584 
585 	if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
586 		    fmd->fimc_lite[fimc_lite->index]))
587 		return -EBUSY;
588 
589 	sd = &fimc_lite->subdev;
590 	sd->grp_id = GRP_ID_FLITE;
591 
592 	ep = fimc_md_pipeline_create(fmd);
593 	if (!ep)
594 		return -ENOMEM;
595 
596 	v4l2_set_subdev_hostdata(sd, ep);
597 
598 	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
599 	if (!ret)
600 		fmd->fimc_lite[fimc_lite->index] = fimc_lite;
601 	else
602 		v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
603 			 fimc_lite->index);
604 	return ret;
605 }
606 
register_fimc_entity(struct fimc_md * fmd,struct fimc_dev * fimc)607 static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
608 {
609 	struct v4l2_subdev *sd;
610 	struct exynos_media_pipeline *ep;
611 	int ret;
612 
613 	if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
614 		return -EBUSY;
615 
616 	sd = &fimc->vid_cap.subdev;
617 	sd->grp_id = GRP_ID_FIMC;
618 
619 	ep = fimc_md_pipeline_create(fmd);
620 	if (!ep)
621 		return -ENOMEM;
622 
623 	v4l2_set_subdev_hostdata(sd, ep);
624 
625 	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
626 	if (!ret) {
627 		if (!fmd->pmf && fimc->pdev)
628 			fmd->pmf = &fimc->pdev->dev;
629 		fmd->fimc[fimc->id] = fimc;
630 		fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
631 	} else {
632 		v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
633 			 fimc->id, ret);
634 	}
635 	return ret;
636 }
637 
register_csis_entity(struct fimc_md * fmd,struct platform_device * pdev,struct v4l2_subdev * sd)638 static int register_csis_entity(struct fimc_md *fmd,
639 				struct platform_device *pdev,
640 				struct v4l2_subdev *sd)
641 {
642 	struct device_node *node = pdev->dev.of_node;
643 	int id, ret;
644 
645 	id = node ? __of_get_csis_id(node) : max(0, pdev->id);
646 
647 	if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
648 		return -ENOENT;
649 
650 	if (WARN_ON(fmd->csis[id].sd))
651 		return -EBUSY;
652 
653 	sd->grp_id = GRP_ID_CSIS;
654 	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
655 	if (!ret)
656 		fmd->csis[id].sd = sd;
657 	else
658 		v4l2_err(&fmd->v4l2_dev,
659 			 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
660 	return ret;
661 }
662 
register_fimc_is_entity(struct fimc_md * fmd,struct fimc_is * is)663 static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
664 {
665 	struct v4l2_subdev *sd = &is->isp.subdev;
666 	struct exynos_media_pipeline *ep;
667 	int ret;
668 
669 	/* Allocate pipeline object for the ISP capture video node. */
670 	ep = fimc_md_pipeline_create(fmd);
671 	if (!ep)
672 		return -ENOMEM;
673 
674 	v4l2_set_subdev_hostdata(sd, ep);
675 
676 	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
677 	if (ret) {
678 		v4l2_err(&fmd->v4l2_dev,
679 			 "Failed to register FIMC-ISP (%d)\n", ret);
680 		return ret;
681 	}
682 
683 	fmd->fimc_is = is;
684 	return 0;
685 }
686 
fimc_md_register_platform_entity(struct fimc_md * fmd,struct platform_device * pdev,int plat_entity)687 static int fimc_md_register_platform_entity(struct fimc_md *fmd,
688 					    struct platform_device *pdev,
689 					    int plat_entity)
690 {
691 	struct device *dev = &pdev->dev;
692 	int ret = -EPROBE_DEFER;
693 	void *drvdata;
694 
695 	/* Lock to ensure dev->driver won't change. */
696 	device_lock(dev);
697 
698 	if (!dev->driver || !try_module_get(dev->driver->owner))
699 		goto dev_unlock;
700 
701 	drvdata = dev_get_drvdata(dev);
702 	/* Some subdev didn't probe successfully id drvdata is NULL */
703 	if (drvdata) {
704 		switch (plat_entity) {
705 		case IDX_FIMC:
706 			ret = register_fimc_entity(fmd, drvdata);
707 			break;
708 		case IDX_FLITE:
709 			ret = register_fimc_lite_entity(fmd, drvdata);
710 			break;
711 		case IDX_CSIS:
712 			ret = register_csis_entity(fmd, pdev, drvdata);
713 			break;
714 		case IDX_IS_ISP:
715 			ret = register_fimc_is_entity(fmd, drvdata);
716 			break;
717 		default:
718 			ret = -ENODEV;
719 		}
720 	}
721 
722 	module_put(dev->driver->owner);
723 dev_unlock:
724 	device_unlock(dev);
725 	if (ret == -EPROBE_DEFER)
726 		dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
727 			dev_name(dev));
728 	else if (ret < 0)
729 		dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
730 			dev_name(dev), ret);
731 	return ret;
732 }
733 
734 /* Register FIMC, FIMC-LITE and CSIS media entities */
fimc_md_register_platform_entities(struct fimc_md * fmd,struct device_node * parent)735 static int fimc_md_register_platform_entities(struct fimc_md *fmd,
736 					      struct device_node *parent)
737 {
738 	struct device_node *node;
739 	int ret = 0;
740 
741 	for_each_available_child_of_node(parent, node) {
742 		struct platform_device *pdev;
743 		int plat_entity = -1;
744 
745 		pdev = of_find_device_by_node(node);
746 		if (!pdev)
747 			continue;
748 
749 		/* If driver of any entity isn't ready try all again later. */
750 		if (of_node_name_eq(node, CSIS_OF_NODE_NAME))
751 			plat_entity = IDX_CSIS;
752 		else if (of_node_name_eq(node, FIMC_IS_OF_NODE_NAME))
753 			plat_entity = IDX_IS_ISP;
754 		else if (of_node_name_eq(node, FIMC_LITE_OF_NODE_NAME))
755 			plat_entity = IDX_FLITE;
756 		else if (of_node_name_eq(node, FIMC_OF_NODE_NAME) &&
757 			 !of_property_read_bool(node, "samsung,lcd-wb"))
758 			plat_entity = IDX_FIMC;
759 
760 		if (plat_entity >= 0)
761 			ret = fimc_md_register_platform_entity(fmd, pdev,
762 							plat_entity);
763 		put_device(&pdev->dev);
764 		if (ret < 0) {
765 			of_node_put(node);
766 			break;
767 		}
768 	}
769 
770 	return ret;
771 }
772 
fimc_md_unregister_entities(struct fimc_md * fmd)773 static void fimc_md_unregister_entities(struct fimc_md *fmd)
774 {
775 	int i;
776 
777 	for (i = 0; i < FIMC_MAX_DEVS; i++) {
778 		struct fimc_dev *dev = fmd->fimc[i];
779 		if (dev == NULL)
780 			continue;
781 		v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
782 		dev->vid_cap.ve.pipe = NULL;
783 		fmd->fimc[i] = NULL;
784 	}
785 	for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
786 		struct fimc_lite *dev = fmd->fimc_lite[i];
787 		if (dev == NULL)
788 			continue;
789 		v4l2_device_unregister_subdev(&dev->subdev);
790 		dev->ve.pipe = NULL;
791 		fmd->fimc_lite[i] = NULL;
792 	}
793 	for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
794 		if (fmd->csis[i].sd == NULL)
795 			continue;
796 		v4l2_device_unregister_subdev(fmd->csis[i].sd);
797 		fmd->csis[i].sd = NULL;
798 	}
799 
800 	if (fmd->fimc_is)
801 		v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
802 
803 	v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
804 }
805 
806 /**
807  * __fimc_md_create_fimc_links - create links to all FIMC entities
808  * @fmd: fimc media device
809  * @source: the source entity to create links to all fimc entities from
810  * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
811  * @pad: the source entity pad index
812  * @link_mask: bitmask of the fimc devices for which link should be enabled
813  */
__fimc_md_create_fimc_sink_links(struct fimc_md * fmd,struct media_entity * source,struct v4l2_subdev * sensor,int pad,int link_mask)814 static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
815 					    struct media_entity *source,
816 					    struct v4l2_subdev *sensor,
817 					    int pad, int link_mask)
818 {
819 	struct fimc_source_info *si = NULL;
820 	struct media_entity *sink;
821 	unsigned int flags = 0;
822 	int i, ret = 0;
823 
824 	if (sensor) {
825 		si = v4l2_get_subdev_hostdata(sensor);
826 		/* Skip direct FIMC links in the logical FIMC-IS sensor path */
827 		if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
828 			ret = 1;
829 	}
830 
831 	for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
832 		if (!fmd->fimc[i])
833 			continue;
834 		/*
835 		 * Some FIMC variants are not fitted with camera capture
836 		 * interface. Skip creating a link from sensor for those.
837 		 */
838 		if (!fmd->fimc[i]->variant->has_cam_if)
839 			continue;
840 
841 		flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
842 
843 		sink = &fmd->fimc[i]->vid_cap.subdev.entity;
844 		ret = media_create_pad_link(source, pad, sink,
845 					      FIMC_SD_PAD_SINK_CAM, flags);
846 		if (ret)
847 			return ret;
848 
849 		/* Notify FIMC capture subdev entity */
850 		ret = media_entity_call(sink, link_setup, &sink->pads[0],
851 					&source->pads[pad], flags);
852 		if (ret)
853 			break;
854 
855 		v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
856 			  source->name, flags ? '=' : '-', sink->name);
857 	}
858 
859 	for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
860 		if (!fmd->fimc_lite[i])
861 			continue;
862 
863 		sink = &fmd->fimc_lite[i]->subdev.entity;
864 		ret = media_create_pad_link(source, pad, sink,
865 					       FLITE_SD_PAD_SINK, 0);
866 		if (ret)
867 			return ret;
868 
869 		/* Notify FIMC-LITE subdev entity */
870 		ret = media_entity_call(sink, link_setup, &sink->pads[0],
871 					&source->pads[pad], 0);
872 		if (ret)
873 			break;
874 
875 		v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
876 			  source->name, sink->name);
877 	}
878 	return 0;
879 }
880 
881 /* Create links from FIMC-LITE source pads to other entities */
__fimc_md_create_flite_source_links(struct fimc_md * fmd)882 static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
883 {
884 	struct media_entity *source, *sink;
885 	int i, ret = 0;
886 
887 	for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
888 		struct fimc_lite *fimc = fmd->fimc_lite[i];
889 
890 		if (fimc == NULL)
891 			continue;
892 
893 		source = &fimc->subdev.entity;
894 		sink = &fimc->ve.vdev.entity;
895 		/* FIMC-LITE's subdev and video node */
896 		ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_DMA,
897 					       sink, 0, 0);
898 		if (ret)
899 			break;
900 		/* Link from FIMC-LITE to IS-ISP subdev */
901 		sink = &fmd->fimc_is->isp.subdev.entity;
902 		ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_ISP,
903 					       sink, 0, 0);
904 		if (ret)
905 			break;
906 	}
907 
908 	return ret;
909 }
910 
911 /* Create FIMC-IS links */
__fimc_md_create_fimc_is_links(struct fimc_md * fmd)912 static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
913 {
914 	struct fimc_isp *isp = &fmd->fimc_is->isp;
915 	struct media_entity *source, *sink;
916 	int i, ret;
917 
918 	source = &isp->subdev.entity;
919 
920 	for (i = 0; i < FIMC_MAX_DEVS; i++) {
921 		if (fmd->fimc[i] == NULL)
922 			continue;
923 
924 		/* Link from FIMC-IS-ISP subdev to FIMC */
925 		sink = &fmd->fimc[i]->vid_cap.subdev.entity;
926 		ret = media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
927 					       sink, FIMC_SD_PAD_SINK_FIFO, 0);
928 		if (ret)
929 			return ret;
930 	}
931 
932 	/* Link from FIMC-IS-ISP subdev to fimc-is-isp.capture video node */
933 	sink = &isp->video_capture.ve.vdev.entity;
934 
935 	/* Skip this link if the fimc-is-isp video node driver isn't built-in */
936 	if (sink->num_pads == 0)
937 		return 0;
938 
939 	return media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_DMA,
940 					sink, 0, 0);
941 }
942 
943 /**
944  * fimc_md_create_links - create default links between registered entities
945  * @fmd: fimc media device
946  *
947  * Parallel interface sensor entities are connected directly to FIMC capture
948  * entities. The sensors using MIPI CSIS bus are connected through immutable
949  * link with CSI receiver entity specified by mux_id. Any registered CSIS
950  * entity has a link to each registered FIMC capture entity. Enabled links
951  * are created by default between each subsequent registered sensor and
952  * subsequent FIMC capture entity. The number of default active links is
953  * determined by the number of available sensors or FIMC entities,
954  * whichever is less.
955  */
fimc_md_create_links(struct fimc_md * fmd)956 static int fimc_md_create_links(struct fimc_md *fmd)
957 {
958 	struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
959 	struct v4l2_subdev *sensor, *csis;
960 	struct fimc_source_info *pdata;
961 	struct media_entity *source, *sink;
962 	int i, pad, fimc_id = 0, ret = 0;
963 	u32 flags, link_mask = 0;
964 
965 	for (i = 0; i < fmd->num_sensors; i++) {
966 		if (fmd->sensor[i].subdev == NULL)
967 			continue;
968 
969 		sensor = fmd->sensor[i].subdev;
970 		pdata = v4l2_get_subdev_hostdata(sensor);
971 		if (!pdata)
972 			continue;
973 
974 		source = NULL;
975 
976 		switch (pdata->sensor_bus_type) {
977 		case FIMC_BUS_TYPE_MIPI_CSI2:
978 			if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
979 				"Wrong CSI channel id: %d\n", pdata->mux_id))
980 				return -EINVAL;
981 
982 			csis = fmd->csis[pdata->mux_id].sd;
983 			if (WARN(csis == NULL,
984 				 "MIPI-CSI interface specified but s5p-csis module is not loaded!\n"))
985 				return -EINVAL;
986 
987 			pad = sensor->entity.num_pads - 1;
988 			ret = media_create_pad_link(&sensor->entity, pad,
989 					      &csis->entity, CSIS_PAD_SINK,
990 					      MEDIA_LNK_FL_IMMUTABLE |
991 					      MEDIA_LNK_FL_ENABLED);
992 			if (ret)
993 				return ret;
994 
995 			v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
996 				  sensor->entity.name, csis->entity.name);
997 
998 			source = NULL;
999 			csi_sensors[pdata->mux_id] = sensor;
1000 			break;
1001 
1002 		case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
1003 			source = &sensor->entity;
1004 			pad = 0;
1005 			break;
1006 
1007 		default:
1008 			v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
1009 				 pdata->sensor_bus_type);
1010 			return -EINVAL;
1011 		}
1012 		if (source == NULL)
1013 			continue;
1014 
1015 		link_mask = 1 << fimc_id++;
1016 		ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1017 						       pad, link_mask);
1018 	}
1019 
1020 	for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
1021 		if (fmd->csis[i].sd == NULL)
1022 			continue;
1023 
1024 		source = &fmd->csis[i].sd->entity;
1025 		pad = CSIS_PAD_SOURCE;
1026 		sensor = csi_sensors[i];
1027 
1028 		link_mask = 1 << fimc_id++;
1029 		ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1030 						       pad, link_mask);
1031 	}
1032 
1033 	/* Create immutable links between each FIMC's subdev and video node */
1034 	flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
1035 	for (i = 0; i < FIMC_MAX_DEVS; i++) {
1036 		if (!fmd->fimc[i])
1037 			continue;
1038 
1039 		source = &fmd->fimc[i]->vid_cap.subdev.entity;
1040 		sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
1041 
1042 		ret = media_create_pad_link(source, FIMC_SD_PAD_SOURCE,
1043 					      sink, 0, flags);
1044 		if (ret)
1045 			break;
1046 	}
1047 
1048 	ret = __fimc_md_create_flite_source_links(fmd);
1049 	if (ret < 0)
1050 		return ret;
1051 
1052 	if (fmd->use_isp)
1053 		ret = __fimc_md_create_fimc_is_links(fmd);
1054 
1055 	return ret;
1056 }
1057 
1058 /*
1059  * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
1060  */
fimc_md_put_clocks(struct fimc_md * fmd)1061 static void fimc_md_put_clocks(struct fimc_md *fmd)
1062 {
1063 	int i = FIMC_MAX_CAMCLKS;
1064 
1065 	while (--i >= 0) {
1066 		if (IS_ERR(fmd->camclk[i].clock))
1067 			continue;
1068 		clk_put(fmd->camclk[i].clock);
1069 		fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1070 	}
1071 
1072 	/* Writeback (PIXELASYNCMx) clocks */
1073 	for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1074 		if (IS_ERR(fmd->wbclk[i]))
1075 			continue;
1076 		clk_put(fmd->wbclk[i]);
1077 		fmd->wbclk[i] = ERR_PTR(-EINVAL);
1078 	}
1079 }
1080 
fimc_md_get_clocks(struct fimc_md * fmd)1081 static int fimc_md_get_clocks(struct fimc_md *fmd)
1082 {
1083 	struct device *dev = &fmd->pdev->dev;
1084 	char clk_name[32];
1085 	struct clk *clock;
1086 	int i, ret = 0;
1087 
1088 	for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1089 		fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1090 
1091 	for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1092 		snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1093 		clock = clk_get(dev, clk_name);
1094 
1095 		if (IS_ERR(clock)) {
1096 			dev_err(dev, "Failed to get clock: %s\n", clk_name);
1097 			ret = PTR_ERR(clock);
1098 			break;
1099 		}
1100 		fmd->camclk[i].clock = clock;
1101 	}
1102 	if (ret)
1103 		fimc_md_put_clocks(fmd);
1104 
1105 	if (!fmd->use_isp)
1106 		return 0;
1107 	/*
1108 	 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1109 	 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1110 	 */
1111 	fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1112 
1113 	for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1114 		snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1115 		clock = clk_get(dev, clk_name);
1116 		if (IS_ERR(clock)) {
1117 			v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1118 				  clk_name);
1119 			ret = PTR_ERR(clock);
1120 			break;
1121 		}
1122 		fmd->wbclk[i] = clock;
1123 	}
1124 	if (ret)
1125 		fimc_md_put_clocks(fmd);
1126 
1127 	return ret;
1128 }
1129 
__fimc_md_modify_pipeline(struct media_entity * entity,bool enable)1130 static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
1131 {
1132 	struct exynos_video_entity *ve;
1133 	struct fimc_pipeline *p;
1134 	struct video_device *vdev;
1135 	int ret;
1136 
1137 	vdev = media_entity_to_video_device(entity);
1138 	if (vdev->entity.use_count == 0)
1139 		return 0;
1140 
1141 	ve = vdev_to_exynos_video_entity(vdev);
1142 	p = to_fimc_pipeline(ve->pipe);
1143 	/*
1144 	 * Nothing to do if we are disabling the pipeline, some link
1145 	 * has been disconnected and p->subdevs array is cleared now.
1146 	 */
1147 	if (!enable && p->subdevs[IDX_SENSOR] == NULL)
1148 		return 0;
1149 
1150 	if (enable)
1151 		ret = __fimc_pipeline_open(ve->pipe, entity, true);
1152 	else
1153 		ret = __fimc_pipeline_close(ve->pipe);
1154 
1155 	if (ret == 0 && !enable)
1156 		memset(p->subdevs, 0, sizeof(p->subdevs));
1157 
1158 	return ret;
1159 }
1160 
1161 /* Locking: called with entity->graph_obj.mdev->graph_mutex mutex held. */
__fimc_md_modify_pipelines(struct media_entity * entity,bool enable,struct media_graph * graph)1162 static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable,
1163 				      struct media_graph *graph)
1164 {
1165 	struct media_entity *entity_err = entity;
1166 	int ret;
1167 
1168 	/*
1169 	 * Walk current graph and call the pipeline open/close routine for each
1170 	 * opened video node that belongs to the graph of entities connected
1171 	 * through active links. This is needed as we cannot power on/off the
1172 	 * subdevs in random order.
1173 	 */
1174 	media_graph_walk_start(graph, entity);
1175 
1176 	while ((entity = media_graph_walk_next(graph))) {
1177 		if (!is_media_entity_v4l2_video_device(entity))
1178 			continue;
1179 
1180 		ret  = __fimc_md_modify_pipeline(entity, enable);
1181 
1182 		if (ret < 0)
1183 			goto err;
1184 	}
1185 
1186 	return 0;
1187 
1188 err:
1189 	media_graph_walk_start(graph, entity_err);
1190 
1191 	while ((entity_err = media_graph_walk_next(graph))) {
1192 		if (!is_media_entity_v4l2_video_device(entity_err))
1193 			continue;
1194 
1195 		__fimc_md_modify_pipeline(entity_err, !enable);
1196 
1197 		if (entity_err == entity)
1198 			break;
1199 	}
1200 
1201 	return ret;
1202 }
1203 
fimc_md_link_notify(struct media_link * link,unsigned int flags,unsigned int notification)1204 static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
1205 				unsigned int notification)
1206 {
1207 	struct media_graph *graph =
1208 		&container_of(link->graph_obj.mdev, struct fimc_md,
1209 			      media_dev)->link_setup_graph;
1210 	struct media_entity *sink = link->sink->entity;
1211 	int ret = 0;
1212 
1213 	/* Before link disconnection */
1214 	if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
1215 		ret = media_graph_walk_init(graph,
1216 						   link->graph_obj.mdev);
1217 		if (ret)
1218 			return ret;
1219 		if (!(flags & MEDIA_LNK_FL_ENABLED))
1220 			ret = __fimc_md_modify_pipelines(sink, false, graph);
1221 #if 0
1222 		else
1223 			/* TODO: Link state change validation */
1224 #endif
1225 	/* After link activation */
1226 	} else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH) {
1227 		if (link->flags & MEDIA_LNK_FL_ENABLED)
1228 			ret = __fimc_md_modify_pipelines(sink, true, graph);
1229 		media_graph_walk_cleanup(graph);
1230 	}
1231 
1232 	return ret ? -EPIPE : 0;
1233 }
1234 
1235 static const struct media_device_ops fimc_md_ops = {
1236 	.link_notify = fimc_md_link_notify,
1237 };
1238 
fimc_md_sysfs_show(struct device * dev,struct device_attribute * attr,char * buf)1239 static ssize_t fimc_md_sysfs_show(struct device *dev,
1240 				  struct device_attribute *attr, char *buf)
1241 {
1242 	struct fimc_md *fmd = dev_get_drvdata(dev);
1243 
1244 	if (fmd->user_subdev_api)
1245 		return strscpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1246 
1247 	return strscpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1248 }
1249 
fimc_md_sysfs_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1250 static ssize_t fimc_md_sysfs_store(struct device *dev,
1251 				   struct device_attribute *attr,
1252 				   const char *buf, size_t count)
1253 {
1254 	struct fimc_md *fmd = dev_get_drvdata(dev);
1255 	bool subdev_api;
1256 	int i;
1257 
1258 	if (!strcmp(buf, "vid-dev\n"))
1259 		subdev_api = false;
1260 	else if (!strcmp(buf, "sub-dev\n"))
1261 		subdev_api = true;
1262 	else
1263 		return count;
1264 
1265 	fmd->user_subdev_api = subdev_api;
1266 	for (i = 0; i < FIMC_MAX_DEVS; i++)
1267 		if (fmd->fimc[i])
1268 			fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1269 	return count;
1270 }
1271 /*
1272  * This device attribute is to select video pipeline configuration method.
1273  * There are following valid values:
1274  *  vid-dev - for V4L2 video node API only, subdevice will be configured
1275  *  by the host driver.
1276  *  sub-dev - for media controller API, subdevs must be configured in user
1277  *  space before starting streaming.
1278  */
1279 static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1280 		   fimc_md_sysfs_show, fimc_md_sysfs_store);
1281 
cam_clk_prepare(struct clk_hw * hw)1282 static int cam_clk_prepare(struct clk_hw *hw)
1283 {
1284 	struct cam_clk *camclk = to_cam_clk(hw);
1285 	int ret;
1286 
1287 	if (camclk->fmd->pmf == NULL)
1288 		return -ENODEV;
1289 
1290 	ret = pm_runtime_get_sync(camclk->fmd->pmf);
1291 	return ret < 0 ? ret : 0;
1292 }
1293 
cam_clk_unprepare(struct clk_hw * hw)1294 static void cam_clk_unprepare(struct clk_hw *hw)
1295 {
1296 	struct cam_clk *camclk = to_cam_clk(hw);
1297 
1298 	if (camclk->fmd->pmf == NULL)
1299 		return;
1300 
1301 	pm_runtime_put_sync(camclk->fmd->pmf);
1302 }
1303 
1304 static const struct clk_ops cam_clk_ops = {
1305 	.prepare = cam_clk_prepare,
1306 	.unprepare = cam_clk_unprepare,
1307 };
1308 
fimc_md_unregister_clk_provider(struct fimc_md * fmd)1309 static void fimc_md_unregister_clk_provider(struct fimc_md *fmd)
1310 {
1311 	struct cam_clk_provider *cp = &fmd->clk_provider;
1312 	unsigned int i;
1313 
1314 	if (cp->of_node)
1315 		of_clk_del_provider(cp->of_node);
1316 
1317 	for (i = 0; i < cp->num_clocks; i++)
1318 		clk_unregister(cp->clks[i]);
1319 }
1320 
fimc_md_register_clk_provider(struct fimc_md * fmd)1321 static int fimc_md_register_clk_provider(struct fimc_md *fmd)
1322 {
1323 	struct cam_clk_provider *cp = &fmd->clk_provider;
1324 	struct device *dev = &fmd->pdev->dev;
1325 	int i, ret;
1326 
1327 	for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1328 		struct cam_clk *camclk = &cp->camclk[i];
1329 		struct clk_init_data init;
1330 		const char *p_name;
1331 
1332 		ret = of_property_read_string_index(dev->of_node,
1333 					"clock-output-names", i, &init.name);
1334 		if (ret < 0)
1335 			break;
1336 
1337 		p_name = __clk_get_name(fmd->camclk[i].clock);
1338 
1339 		/* It's safe since clk_register() will duplicate the string. */
1340 		init.parent_names = &p_name;
1341 		init.num_parents = 1;
1342 		init.ops = &cam_clk_ops;
1343 		init.flags = CLK_SET_RATE_PARENT;
1344 		camclk->hw.init = &init;
1345 		camclk->fmd = fmd;
1346 
1347 		cp->clks[i] = clk_register(NULL, &camclk->hw);
1348 		if (IS_ERR(cp->clks[i])) {
1349 			dev_err(dev, "failed to register clock: %s (%ld)\n",
1350 					init.name, PTR_ERR(cp->clks[i]));
1351 			ret = PTR_ERR(cp->clks[i]);
1352 			goto err;
1353 		}
1354 		cp->num_clocks++;
1355 	}
1356 
1357 	if (cp->num_clocks == 0) {
1358 		dev_warn(dev, "clk provider not registered\n");
1359 		return 0;
1360 	}
1361 
1362 	cp->clk_data.clks = cp->clks;
1363 	cp->clk_data.clk_num = cp->num_clocks;
1364 	cp->of_node = dev->of_node;
1365 	ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1366 				  &cp->clk_data);
1367 	if (ret == 0)
1368 		return 0;
1369 err:
1370 	fimc_md_unregister_clk_provider(fmd);
1371 	return ret;
1372 }
1373 
subdev_notifier_bound(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_subdev * asd)1374 static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
1375 				 struct v4l2_subdev *subdev,
1376 				 struct v4l2_async_subdev *asd)
1377 {
1378 	struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1379 	struct fimc_sensor_info *si = NULL;
1380 	int i;
1381 
1382 	/* Find platform data for this sensor subdev */
1383 	for (i = 0; i < ARRAY_SIZE(fmd->sensor); i++)
1384 		if (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 
subdev_notifier_complete(struct v4l2_async_notifier * notifier)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 
fimc_md_probe(struct platform_device * pdev)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_notifier_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_notifier_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_notifier_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 
fimc_md_remove(struct platform_device * pdev)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_notifier_unregister(&fmd->subdev_notifier);
1546 	v4l2_async_notifier_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 
fimc_md_init(void)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 
fimc_md_exit(void)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