• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_resume_and_get(fmd->pmf);
512 	if (ret < 0)
513 		return ret;
514 
515 	fmd->num_sensors = 0;
516 
517 	/* Attach sensors linked to MIPI CSI-2 receivers */
518 	for_each_available_child_of_node(parent, node) {
519 		struct device_node *port;
520 
521 		if (!of_node_name_eq(node, "csis"))
522 			continue;
523 		/* The csis node can have only port subnode. */
524 		port = of_get_next_child(node, NULL);
525 		if (!port)
526 			continue;
527 
528 		ret = fimc_md_parse_port_node(fmd, port);
529 		of_node_put(port);
530 		if (ret < 0) {
531 			of_node_put(node);
532 			goto cleanup;
533 		}
534 	}
535 
536 	/* Attach sensors listed in the parallel-ports node */
537 	ports = of_get_child_by_name(parent, "parallel-ports");
538 	if (!ports)
539 		goto rpm_put;
540 
541 	for_each_child_of_node(ports, node) {
542 		ret = fimc_md_parse_port_node(fmd, node);
543 		if (ret < 0) {
544 			of_node_put(node);
545 			goto cleanup;
546 		}
547 	}
548 	of_node_put(ports);
549 
550 rpm_put:
551 	pm_runtime_put(fmd->pmf);
552 	return 0;
553 
554 cleanup:
555 	of_node_put(ports);
556 	v4l2_async_notifier_cleanup(&fmd->subdev_notifier);
557 	pm_runtime_put(fmd->pmf);
558 	return ret;
559 }
560 
__of_get_csis_id(struct device_node * np)561 static int __of_get_csis_id(struct device_node *np)
562 {
563 	u32 reg = 0;
564 
565 	np = of_get_child_by_name(np, "port");
566 	if (!np)
567 		return -EINVAL;
568 	of_property_read_u32(np, "reg", &reg);
569 	of_node_put(np);
570 	return reg - FIMC_INPUT_MIPI_CSI2_0;
571 }
572 
573 /*
574  * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
575  */
register_fimc_lite_entity(struct fimc_md * fmd,struct fimc_lite * fimc_lite)576 static int register_fimc_lite_entity(struct fimc_md *fmd,
577 				     struct fimc_lite *fimc_lite)
578 {
579 	struct v4l2_subdev *sd;
580 	struct exynos_media_pipeline *ep;
581 	int ret;
582 
583 	if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
584 		    fmd->fimc_lite[fimc_lite->index]))
585 		return -EBUSY;
586 
587 	sd = &fimc_lite->subdev;
588 	sd->grp_id = GRP_ID_FLITE;
589 
590 	ep = fimc_md_pipeline_create(fmd);
591 	if (!ep)
592 		return -ENOMEM;
593 
594 	v4l2_set_subdev_hostdata(sd, ep);
595 
596 	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
597 	if (!ret)
598 		fmd->fimc_lite[fimc_lite->index] = fimc_lite;
599 	else
600 		v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
601 			 fimc_lite->index);
602 	return ret;
603 }
604 
register_fimc_entity(struct fimc_md * fmd,struct fimc_dev * fimc)605 static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
606 {
607 	struct v4l2_subdev *sd;
608 	struct exynos_media_pipeline *ep;
609 	int ret;
610 
611 	if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
612 		return -EBUSY;
613 
614 	sd = &fimc->vid_cap.subdev;
615 	sd->grp_id = GRP_ID_FIMC;
616 
617 	ep = fimc_md_pipeline_create(fmd);
618 	if (!ep)
619 		return -ENOMEM;
620 
621 	v4l2_set_subdev_hostdata(sd, ep);
622 
623 	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
624 	if (!ret) {
625 		if (!fmd->pmf && fimc->pdev)
626 			fmd->pmf = &fimc->pdev->dev;
627 		fmd->fimc[fimc->id] = fimc;
628 		fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
629 	} else {
630 		v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
631 			 fimc->id, ret);
632 	}
633 	return ret;
634 }
635 
register_csis_entity(struct fimc_md * fmd,struct platform_device * pdev,struct v4l2_subdev * sd)636 static int register_csis_entity(struct fimc_md *fmd,
637 				struct platform_device *pdev,
638 				struct v4l2_subdev *sd)
639 {
640 	struct device_node *node = pdev->dev.of_node;
641 	int id, ret;
642 
643 	id = node ? __of_get_csis_id(node) : max(0, pdev->id);
644 
645 	if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
646 		return -ENOENT;
647 
648 	if (WARN_ON(fmd->csis[id].sd))
649 		return -EBUSY;
650 
651 	sd->grp_id = GRP_ID_CSIS;
652 	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
653 	if (!ret)
654 		fmd->csis[id].sd = sd;
655 	else
656 		v4l2_err(&fmd->v4l2_dev,
657 			 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
658 	return ret;
659 }
660 
register_fimc_is_entity(struct fimc_md * fmd,struct fimc_is * is)661 static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
662 {
663 	struct v4l2_subdev *sd = &is->isp.subdev;
664 	struct exynos_media_pipeline *ep;
665 	int ret;
666 
667 	/* Allocate pipeline object for the ISP capture video node. */
668 	ep = fimc_md_pipeline_create(fmd);
669 	if (!ep)
670 		return -ENOMEM;
671 
672 	v4l2_set_subdev_hostdata(sd, ep);
673 
674 	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
675 	if (ret) {
676 		v4l2_err(&fmd->v4l2_dev,
677 			 "Failed to register FIMC-ISP (%d)\n", ret);
678 		return ret;
679 	}
680 
681 	fmd->fimc_is = is;
682 	return 0;
683 }
684 
fimc_md_register_platform_entity(struct fimc_md * fmd,struct platform_device * pdev,int plat_entity)685 static int fimc_md_register_platform_entity(struct fimc_md *fmd,
686 					    struct platform_device *pdev,
687 					    int plat_entity)
688 {
689 	struct device *dev = &pdev->dev;
690 	int ret = -EPROBE_DEFER;
691 	void *drvdata;
692 
693 	/* Lock to ensure dev->driver won't change. */
694 	device_lock(dev);
695 
696 	if (!dev->driver || !try_module_get(dev->driver->owner))
697 		goto dev_unlock;
698 
699 	drvdata = dev_get_drvdata(dev);
700 	/* Some subdev didn't probe successfully id drvdata is NULL */
701 	if (drvdata) {
702 		switch (plat_entity) {
703 		case IDX_FIMC:
704 			ret = register_fimc_entity(fmd, drvdata);
705 			break;
706 		case IDX_FLITE:
707 			ret = register_fimc_lite_entity(fmd, drvdata);
708 			break;
709 		case IDX_CSIS:
710 			ret = register_csis_entity(fmd, pdev, drvdata);
711 			break;
712 		case IDX_IS_ISP:
713 			ret = register_fimc_is_entity(fmd, drvdata);
714 			break;
715 		default:
716 			ret = -ENODEV;
717 		}
718 	}
719 
720 	module_put(dev->driver->owner);
721 dev_unlock:
722 	device_unlock(dev);
723 	if (ret == -EPROBE_DEFER)
724 		dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
725 			dev_name(dev));
726 	else if (ret < 0)
727 		dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
728 			dev_name(dev), ret);
729 	return ret;
730 }
731 
732 /* Register FIMC, FIMC-LITE and CSIS media entities */
fimc_md_register_platform_entities(struct fimc_md * fmd,struct device_node * parent)733 static int fimc_md_register_platform_entities(struct fimc_md *fmd,
734 					      struct device_node *parent)
735 {
736 	struct device_node *node;
737 	int ret = 0;
738 
739 	for_each_available_child_of_node(parent, node) {
740 		struct platform_device *pdev;
741 		int plat_entity = -1;
742 
743 		pdev = of_find_device_by_node(node);
744 		if (!pdev)
745 			continue;
746 
747 		/* If driver of any entity isn't ready try all again later. */
748 		if (of_node_name_eq(node, CSIS_OF_NODE_NAME))
749 			plat_entity = IDX_CSIS;
750 		else if (of_node_name_eq(node, FIMC_IS_OF_NODE_NAME))
751 			plat_entity = IDX_IS_ISP;
752 		else if (of_node_name_eq(node, FIMC_LITE_OF_NODE_NAME))
753 			plat_entity = IDX_FLITE;
754 		else if (of_node_name_eq(node, FIMC_OF_NODE_NAME) &&
755 			 !of_property_read_bool(node, "samsung,lcd-wb"))
756 			plat_entity = IDX_FIMC;
757 
758 		if (plat_entity >= 0)
759 			ret = fimc_md_register_platform_entity(fmd, pdev,
760 							plat_entity);
761 		put_device(&pdev->dev);
762 		if (ret < 0) {
763 			of_node_put(node);
764 			break;
765 		}
766 	}
767 
768 	return ret;
769 }
770 
fimc_md_unregister_entities(struct fimc_md * fmd)771 static void fimc_md_unregister_entities(struct fimc_md *fmd)
772 {
773 	int i;
774 
775 	for (i = 0; i < FIMC_MAX_DEVS; i++) {
776 		struct fimc_dev *dev = fmd->fimc[i];
777 		if (dev == NULL)
778 			continue;
779 		v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
780 		dev->vid_cap.ve.pipe = NULL;
781 		fmd->fimc[i] = NULL;
782 	}
783 	for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
784 		struct fimc_lite *dev = fmd->fimc_lite[i];
785 		if (dev == NULL)
786 			continue;
787 		v4l2_device_unregister_subdev(&dev->subdev);
788 		dev->ve.pipe = NULL;
789 		fmd->fimc_lite[i] = NULL;
790 	}
791 	for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
792 		if (fmd->csis[i].sd == NULL)
793 			continue;
794 		v4l2_device_unregister_subdev(fmd->csis[i].sd);
795 		fmd->csis[i].sd = NULL;
796 	}
797 
798 	if (fmd->fimc_is)
799 		v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
800 
801 	v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
802 }
803 
804 /**
805  * __fimc_md_create_fimc_links - create links to all FIMC entities
806  * @fmd: fimc media device
807  * @source: the source entity to create links to all fimc entities from
808  * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
809  * @pad: the source entity pad index
810  * @link_mask: bitmask of the fimc devices for which link should be enabled
811  */
__fimc_md_create_fimc_sink_links(struct fimc_md * fmd,struct media_entity * source,struct v4l2_subdev * sensor,int pad,int link_mask)812 static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
813 					    struct media_entity *source,
814 					    struct v4l2_subdev *sensor,
815 					    int pad, int link_mask)
816 {
817 	struct fimc_source_info *si = NULL;
818 	struct media_entity *sink;
819 	unsigned int flags = 0;
820 	int i, ret = 0;
821 
822 	if (sensor) {
823 		si = v4l2_get_subdev_hostdata(sensor);
824 		/* Skip direct FIMC links in the logical FIMC-IS sensor path */
825 		if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
826 			ret = 1;
827 	}
828 
829 	for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
830 		if (!fmd->fimc[i])
831 			continue;
832 		/*
833 		 * Some FIMC variants are not fitted with camera capture
834 		 * interface. Skip creating a link from sensor for those.
835 		 */
836 		if (!fmd->fimc[i]->variant->has_cam_if)
837 			continue;
838 
839 		flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
840 
841 		sink = &fmd->fimc[i]->vid_cap.subdev.entity;
842 		ret = media_create_pad_link(source, pad, sink,
843 					      FIMC_SD_PAD_SINK_CAM, flags);
844 		if (ret)
845 			return ret;
846 
847 		/* Notify FIMC capture subdev entity */
848 		ret = media_entity_call(sink, link_setup, &sink->pads[0],
849 					&source->pads[pad], flags);
850 		if (ret)
851 			break;
852 
853 		v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
854 			  source->name, flags ? '=' : '-', sink->name);
855 	}
856 
857 	for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
858 		if (!fmd->fimc_lite[i])
859 			continue;
860 
861 		sink = &fmd->fimc_lite[i]->subdev.entity;
862 		ret = media_create_pad_link(source, pad, sink,
863 					       FLITE_SD_PAD_SINK, 0);
864 		if (ret)
865 			return ret;
866 
867 		/* Notify FIMC-LITE subdev entity */
868 		ret = media_entity_call(sink, link_setup, &sink->pads[0],
869 					&source->pads[pad], 0);
870 		if (ret)
871 			break;
872 
873 		v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
874 			  source->name, sink->name);
875 	}
876 	return 0;
877 }
878 
879 /* Create links from FIMC-LITE source pads to other entities */
__fimc_md_create_flite_source_links(struct fimc_md * fmd)880 static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
881 {
882 	struct media_entity *source, *sink;
883 	int i, ret = 0;
884 
885 	for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
886 		struct fimc_lite *fimc = fmd->fimc_lite[i];
887 
888 		if (fimc == NULL)
889 			continue;
890 
891 		source = &fimc->subdev.entity;
892 		sink = &fimc->ve.vdev.entity;
893 		/* FIMC-LITE's subdev and video node */
894 		ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_DMA,
895 					       sink, 0, 0);
896 		if (ret)
897 			break;
898 		/* Link from FIMC-LITE to IS-ISP subdev */
899 		sink = &fmd->fimc_is->isp.subdev.entity;
900 		ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_ISP,
901 					       sink, 0, 0);
902 		if (ret)
903 			break;
904 	}
905 
906 	return ret;
907 }
908 
909 /* Create FIMC-IS links */
__fimc_md_create_fimc_is_links(struct fimc_md * fmd)910 static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
911 {
912 	struct fimc_isp *isp = &fmd->fimc_is->isp;
913 	struct media_entity *source, *sink;
914 	int i, ret;
915 
916 	source = &isp->subdev.entity;
917 
918 	for (i = 0; i < FIMC_MAX_DEVS; i++) {
919 		if (fmd->fimc[i] == NULL)
920 			continue;
921 
922 		/* Link from FIMC-IS-ISP subdev to FIMC */
923 		sink = &fmd->fimc[i]->vid_cap.subdev.entity;
924 		ret = media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
925 					       sink, FIMC_SD_PAD_SINK_FIFO, 0);
926 		if (ret)
927 			return ret;
928 	}
929 
930 	/* Link from FIMC-IS-ISP subdev to fimc-is-isp.capture video node */
931 	sink = &isp->video_capture.ve.vdev.entity;
932 
933 	/* Skip this link if the fimc-is-isp video node driver isn't built-in */
934 	if (sink->num_pads == 0)
935 		return 0;
936 
937 	return media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_DMA,
938 					sink, 0, 0);
939 }
940 
941 /**
942  * fimc_md_create_links - create default links between registered entities
943  * @fmd: fimc media device
944  *
945  * Parallel interface sensor entities are connected directly to FIMC capture
946  * entities. The sensors using MIPI CSIS bus are connected through immutable
947  * link with CSI receiver entity specified by mux_id. Any registered CSIS
948  * entity has a link to each registered FIMC capture entity. Enabled links
949  * are created by default between each subsequent registered sensor and
950  * subsequent FIMC capture entity. The number of default active links is
951  * determined by the number of available sensors or FIMC entities,
952  * whichever is less.
953  */
fimc_md_create_links(struct fimc_md * fmd)954 static int fimc_md_create_links(struct fimc_md *fmd)
955 {
956 	struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
957 	struct v4l2_subdev *sensor, *csis;
958 	struct fimc_source_info *pdata;
959 	struct media_entity *source, *sink;
960 	int i, pad, fimc_id = 0, ret = 0;
961 	u32 flags, link_mask = 0;
962 
963 	for (i = 0; i < fmd->num_sensors; i++) {
964 		if (fmd->sensor[i].subdev == NULL)
965 			continue;
966 
967 		sensor = fmd->sensor[i].subdev;
968 		pdata = v4l2_get_subdev_hostdata(sensor);
969 		if (!pdata)
970 			continue;
971 
972 		source = NULL;
973 
974 		switch (pdata->sensor_bus_type) {
975 		case FIMC_BUS_TYPE_MIPI_CSI2:
976 			if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
977 				"Wrong CSI channel id: %d\n", pdata->mux_id))
978 				return -EINVAL;
979 
980 			csis = fmd->csis[pdata->mux_id].sd;
981 			if (WARN(csis == NULL,
982 				 "MIPI-CSI interface specified but s5p-csis module is not loaded!\n"))
983 				return -EINVAL;
984 
985 			pad = sensor->entity.num_pads - 1;
986 			ret = media_create_pad_link(&sensor->entity, pad,
987 					      &csis->entity, CSIS_PAD_SINK,
988 					      MEDIA_LNK_FL_IMMUTABLE |
989 					      MEDIA_LNK_FL_ENABLED);
990 			if (ret)
991 				return ret;
992 
993 			v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
994 				  sensor->entity.name, csis->entity.name);
995 
996 			source = NULL;
997 			csi_sensors[pdata->mux_id] = sensor;
998 			break;
999 
1000 		case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
1001 			source = &sensor->entity;
1002 			pad = 0;
1003 			break;
1004 
1005 		default:
1006 			v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
1007 				 pdata->sensor_bus_type);
1008 			return -EINVAL;
1009 		}
1010 		if (source == NULL)
1011 			continue;
1012 
1013 		link_mask = 1 << fimc_id++;
1014 		ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1015 						       pad, link_mask);
1016 	}
1017 
1018 	for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
1019 		if (fmd->csis[i].sd == NULL)
1020 			continue;
1021 
1022 		source = &fmd->csis[i].sd->entity;
1023 		pad = CSIS_PAD_SOURCE;
1024 		sensor = csi_sensors[i];
1025 
1026 		link_mask = 1 << fimc_id++;
1027 		ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1028 						       pad, link_mask);
1029 	}
1030 
1031 	/* Create immutable links between each FIMC's subdev and video node */
1032 	flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
1033 	for (i = 0; i < FIMC_MAX_DEVS; i++) {
1034 		if (!fmd->fimc[i])
1035 			continue;
1036 
1037 		source = &fmd->fimc[i]->vid_cap.subdev.entity;
1038 		sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
1039 
1040 		ret = media_create_pad_link(source, FIMC_SD_PAD_SOURCE,
1041 					      sink, 0, flags);
1042 		if (ret)
1043 			break;
1044 	}
1045 
1046 	ret = __fimc_md_create_flite_source_links(fmd);
1047 	if (ret < 0)
1048 		return ret;
1049 
1050 	if (fmd->use_isp)
1051 		ret = __fimc_md_create_fimc_is_links(fmd);
1052 
1053 	return ret;
1054 }
1055 
1056 /*
1057  * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
1058  */
fimc_md_put_clocks(struct fimc_md * fmd)1059 static void fimc_md_put_clocks(struct fimc_md *fmd)
1060 {
1061 	int i = FIMC_MAX_CAMCLKS;
1062 
1063 	while (--i >= 0) {
1064 		if (IS_ERR(fmd->camclk[i].clock))
1065 			continue;
1066 		clk_put(fmd->camclk[i].clock);
1067 		fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1068 	}
1069 
1070 	/* Writeback (PIXELASYNCMx) clocks */
1071 	for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1072 		if (IS_ERR(fmd->wbclk[i]))
1073 			continue;
1074 		clk_put(fmd->wbclk[i]);
1075 		fmd->wbclk[i] = ERR_PTR(-EINVAL);
1076 	}
1077 }
1078 
fimc_md_get_clocks(struct fimc_md * fmd)1079 static int fimc_md_get_clocks(struct fimc_md *fmd)
1080 {
1081 	struct device *dev = &fmd->pdev->dev;
1082 	char clk_name[32];
1083 	struct clk *clock;
1084 	int i, ret = 0;
1085 
1086 	for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1087 		fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1088 
1089 	for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1090 		snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1091 		clock = clk_get(dev, clk_name);
1092 
1093 		if (IS_ERR(clock)) {
1094 			dev_err(dev, "Failed to get clock: %s\n", clk_name);
1095 			ret = PTR_ERR(clock);
1096 			break;
1097 		}
1098 		fmd->camclk[i].clock = clock;
1099 	}
1100 	if (ret)
1101 		fimc_md_put_clocks(fmd);
1102 
1103 	if (!fmd->use_isp)
1104 		return 0;
1105 	/*
1106 	 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1107 	 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1108 	 */
1109 	fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1110 
1111 	for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1112 		snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1113 		clock = clk_get(dev, clk_name);
1114 		if (IS_ERR(clock)) {
1115 			v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1116 				  clk_name);
1117 			ret = PTR_ERR(clock);
1118 			break;
1119 		}
1120 		fmd->wbclk[i] = clock;
1121 	}
1122 	if (ret)
1123 		fimc_md_put_clocks(fmd);
1124 
1125 	return ret;
1126 }
1127 
__fimc_md_modify_pipeline(struct media_entity * entity,bool enable)1128 static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
1129 {
1130 	struct exynos_video_entity *ve;
1131 	struct fimc_pipeline *p;
1132 	struct video_device *vdev;
1133 	int ret;
1134 
1135 	vdev = media_entity_to_video_device(entity);
1136 	if (vdev->entity.use_count == 0)
1137 		return 0;
1138 
1139 	ve = vdev_to_exynos_video_entity(vdev);
1140 	p = to_fimc_pipeline(ve->pipe);
1141 	/*
1142 	 * Nothing to do if we are disabling the pipeline, some link
1143 	 * has been disconnected and p->subdevs array is cleared now.
1144 	 */
1145 	if (!enable && p->subdevs[IDX_SENSOR] == NULL)
1146 		return 0;
1147 
1148 	if (enable)
1149 		ret = __fimc_pipeline_open(ve->pipe, entity, true);
1150 	else
1151 		ret = __fimc_pipeline_close(ve->pipe);
1152 
1153 	if (ret == 0 && !enable)
1154 		memset(p->subdevs, 0, sizeof(p->subdevs));
1155 
1156 	return ret;
1157 }
1158 
1159 /* 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)1160 static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable,
1161 				      struct media_graph *graph)
1162 {
1163 	struct media_entity *entity_err = entity;
1164 	int ret;
1165 
1166 	/*
1167 	 * Walk current graph and call the pipeline open/close routine for each
1168 	 * opened video node that belongs to the graph of entities connected
1169 	 * through active links. This is needed as we cannot power on/off the
1170 	 * subdevs in random order.
1171 	 */
1172 	media_graph_walk_start(graph, entity);
1173 
1174 	while ((entity = media_graph_walk_next(graph))) {
1175 		if (!is_media_entity_v4l2_video_device(entity))
1176 			continue;
1177 
1178 		ret  = __fimc_md_modify_pipeline(entity, enable);
1179 
1180 		if (ret < 0)
1181 			goto err;
1182 	}
1183 
1184 	return 0;
1185 
1186 err:
1187 	media_graph_walk_start(graph, entity_err);
1188 
1189 	while ((entity_err = media_graph_walk_next(graph))) {
1190 		if (!is_media_entity_v4l2_video_device(entity_err))
1191 			continue;
1192 
1193 		__fimc_md_modify_pipeline(entity_err, !enable);
1194 
1195 		if (entity_err == entity)
1196 			break;
1197 	}
1198 
1199 	return ret;
1200 }
1201 
fimc_md_link_notify(struct media_link * link,unsigned int flags,unsigned int notification)1202 static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
1203 				unsigned int notification)
1204 {
1205 	struct media_graph *graph =
1206 		&container_of(link->graph_obj.mdev, struct fimc_md,
1207 			      media_dev)->link_setup_graph;
1208 	struct media_entity *sink = link->sink->entity;
1209 	int ret = 0;
1210 
1211 	/* Before link disconnection */
1212 	if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
1213 		ret = media_graph_walk_init(graph,
1214 						   link->graph_obj.mdev);
1215 		if (ret)
1216 			return ret;
1217 		if (!(flags & MEDIA_LNK_FL_ENABLED))
1218 			ret = __fimc_md_modify_pipelines(sink, false, graph);
1219 #if 0
1220 		else
1221 			/* TODO: Link state change validation */
1222 #endif
1223 	/* After link activation */
1224 	} else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH) {
1225 		if (link->flags & MEDIA_LNK_FL_ENABLED)
1226 			ret = __fimc_md_modify_pipelines(sink, true, graph);
1227 		media_graph_walk_cleanup(graph);
1228 	}
1229 
1230 	return ret ? -EPIPE : 0;
1231 }
1232 
1233 static const struct media_device_ops fimc_md_ops = {
1234 	.link_notify = fimc_md_link_notify,
1235 };
1236 
fimc_md_sysfs_show(struct device * dev,struct device_attribute * attr,char * buf)1237 static ssize_t fimc_md_sysfs_show(struct device *dev,
1238 				  struct device_attribute *attr, char *buf)
1239 {
1240 	struct fimc_md *fmd = dev_get_drvdata(dev);
1241 
1242 	if (fmd->user_subdev_api)
1243 		return strscpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1244 
1245 	return strscpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1246 }
1247 
fimc_md_sysfs_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1248 static ssize_t fimc_md_sysfs_store(struct device *dev,
1249 				   struct device_attribute *attr,
1250 				   const char *buf, size_t count)
1251 {
1252 	struct fimc_md *fmd = dev_get_drvdata(dev);
1253 	bool subdev_api;
1254 	int i;
1255 
1256 	if (!strcmp(buf, "vid-dev\n"))
1257 		subdev_api = false;
1258 	else if (!strcmp(buf, "sub-dev\n"))
1259 		subdev_api = true;
1260 	else
1261 		return count;
1262 
1263 	fmd->user_subdev_api = subdev_api;
1264 	for (i = 0; i < FIMC_MAX_DEVS; i++)
1265 		if (fmd->fimc[i])
1266 			fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1267 	return count;
1268 }
1269 /*
1270  * This device attribute is to select video pipeline configuration method.
1271  * There are following valid values:
1272  *  vid-dev - for V4L2 video node API only, subdevice will be configured
1273  *  by the host driver.
1274  *  sub-dev - for media controller API, subdevs must be configured in user
1275  *  space before starting streaming.
1276  */
1277 static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1278 		   fimc_md_sysfs_show, fimc_md_sysfs_store);
1279 
cam_clk_prepare(struct clk_hw * hw)1280 static int cam_clk_prepare(struct clk_hw *hw)
1281 {
1282 	struct cam_clk *camclk = to_cam_clk(hw);
1283 
1284 	if (camclk->fmd->pmf == NULL)
1285 		return -ENODEV;
1286 
1287 	return pm_runtime_resume_and_get(camclk->fmd->pmf);
1288 }
1289 
cam_clk_unprepare(struct clk_hw * hw)1290 static void cam_clk_unprepare(struct clk_hw *hw)
1291 {
1292 	struct cam_clk *camclk = to_cam_clk(hw);
1293 
1294 	if (camclk->fmd->pmf == NULL)
1295 		return;
1296 
1297 	pm_runtime_put_sync(camclk->fmd->pmf);
1298 }
1299 
1300 static const struct clk_ops cam_clk_ops = {
1301 	.prepare = cam_clk_prepare,
1302 	.unprepare = cam_clk_unprepare,
1303 };
1304 
fimc_md_unregister_clk_provider(struct fimc_md * fmd)1305 static void fimc_md_unregister_clk_provider(struct fimc_md *fmd)
1306 {
1307 	struct cam_clk_provider *cp = &fmd->clk_provider;
1308 	unsigned int i;
1309 
1310 	if (cp->of_node)
1311 		of_clk_del_provider(cp->of_node);
1312 
1313 	for (i = 0; i < cp->num_clocks; i++)
1314 		clk_unregister(cp->clks[i]);
1315 }
1316 
fimc_md_register_clk_provider(struct fimc_md * fmd)1317 static int fimc_md_register_clk_provider(struct fimc_md *fmd)
1318 {
1319 	struct cam_clk_provider *cp = &fmd->clk_provider;
1320 	struct device *dev = &fmd->pdev->dev;
1321 	int i, ret;
1322 
1323 	for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1324 		struct cam_clk *camclk = &cp->camclk[i];
1325 		struct clk_init_data init;
1326 		const char *p_name;
1327 
1328 		ret = of_property_read_string_index(dev->of_node,
1329 					"clock-output-names", i, &init.name);
1330 		if (ret < 0)
1331 			break;
1332 
1333 		p_name = __clk_get_name(fmd->camclk[i].clock);
1334 
1335 		/* It's safe since clk_register() will duplicate the string. */
1336 		init.parent_names = &p_name;
1337 		init.num_parents = 1;
1338 		init.ops = &cam_clk_ops;
1339 		init.flags = CLK_SET_RATE_PARENT;
1340 		camclk->hw.init = &init;
1341 		camclk->fmd = fmd;
1342 
1343 		cp->clks[i] = clk_register(NULL, &camclk->hw);
1344 		if (IS_ERR(cp->clks[i])) {
1345 			dev_err(dev, "failed to register clock: %s (%ld)\n",
1346 					init.name, PTR_ERR(cp->clks[i]));
1347 			ret = PTR_ERR(cp->clks[i]);
1348 			goto err;
1349 		}
1350 		cp->num_clocks++;
1351 	}
1352 
1353 	if (cp->num_clocks == 0) {
1354 		dev_warn(dev, "clk provider not registered\n");
1355 		return 0;
1356 	}
1357 
1358 	cp->clk_data.clks = cp->clks;
1359 	cp->clk_data.clk_num = cp->num_clocks;
1360 	cp->of_node = dev->of_node;
1361 	ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1362 				  &cp->clk_data);
1363 	if (ret == 0)
1364 		return 0;
1365 err:
1366 	fimc_md_unregister_clk_provider(fmd);
1367 	return ret;
1368 }
1369 
subdev_notifier_bound(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_subdev * asd)1370 static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
1371 				 struct v4l2_subdev *subdev,
1372 				 struct v4l2_async_subdev *asd)
1373 {
1374 	struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1375 	struct fimc_sensor_info *si = NULL;
1376 	int i;
1377 
1378 	/* Find platform data for this sensor subdev */
1379 	for (i = 0; i < ARRAY_SIZE(fmd->sensor); i++)
1380 		if (fmd->sensor[i].asd.match.fwnode ==
1381 		    of_fwnode_handle(subdev->dev->of_node))
1382 			si = &fmd->sensor[i];
1383 
1384 	if (si == NULL)
1385 		return -EINVAL;
1386 
1387 	v4l2_set_subdev_hostdata(subdev, &si->pdata);
1388 
1389 	if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
1390 		subdev->grp_id = GRP_ID_FIMC_IS_SENSOR;
1391 	else
1392 		subdev->grp_id = GRP_ID_SENSOR;
1393 
1394 	si->subdev = subdev;
1395 
1396 	v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
1397 		  subdev->name, fmd->num_sensors);
1398 
1399 	fmd->num_sensors++;
1400 
1401 	return 0;
1402 }
1403 
subdev_notifier_complete(struct v4l2_async_notifier * notifier)1404 static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
1405 {
1406 	struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1407 	int ret;
1408 
1409 	mutex_lock(&fmd->media_dev.graph_mutex);
1410 
1411 	ret = fimc_md_create_links(fmd);
1412 	if (ret < 0)
1413 		goto unlock;
1414 
1415 	ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1416 unlock:
1417 	mutex_unlock(&fmd->media_dev.graph_mutex);
1418 	if (ret < 0)
1419 		return ret;
1420 
1421 	return media_device_register(&fmd->media_dev);
1422 }
1423 
1424 static const struct v4l2_async_notifier_operations subdev_notifier_ops = {
1425 	.bound = subdev_notifier_bound,
1426 	.complete = subdev_notifier_complete,
1427 };
1428 
fimc_md_probe(struct platform_device * pdev)1429 static int fimc_md_probe(struct platform_device *pdev)
1430 {
1431 	struct device *dev = &pdev->dev;
1432 	struct v4l2_device *v4l2_dev;
1433 	struct pinctrl *pinctrl;
1434 	struct fimc_md *fmd;
1435 	int ret;
1436 
1437 	fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
1438 	if (!fmd)
1439 		return -ENOMEM;
1440 
1441 	spin_lock_init(&fmd->slock);
1442 	INIT_LIST_HEAD(&fmd->pipelines);
1443 	fmd->pdev = pdev;
1444 
1445 	strscpy(fmd->media_dev.model, "Samsung S5P FIMC",
1446 		sizeof(fmd->media_dev.model));
1447 	fmd->media_dev.ops = &fimc_md_ops;
1448 	fmd->media_dev.dev = dev;
1449 
1450 	v4l2_dev = &fmd->v4l2_dev;
1451 	v4l2_dev->mdev = &fmd->media_dev;
1452 	v4l2_dev->notify = fimc_sensor_notify;
1453 	strscpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
1454 
1455 	fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
1456 	fmd->user_subdev_api = true;
1457 
1458 	media_device_init(&fmd->media_dev);
1459 
1460 	ret = v4l2_device_register(dev, &fmd->v4l2_dev);
1461 	if (ret < 0) {
1462 		v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
1463 		goto err_md;
1464 	}
1465 
1466 	ret = fimc_md_get_clocks(fmd);
1467 	if (ret)
1468 		goto err_v4l2dev;
1469 
1470 	pinctrl = devm_pinctrl_get(dev);
1471 	if (IS_ERR(pinctrl)) {
1472 		ret = PTR_ERR(pinctrl);
1473 		if (ret != EPROBE_DEFER)
1474 			dev_err(dev, "Failed to get pinctrl: %d\n", ret);
1475 		goto err_clk;
1476 	}
1477 
1478 	platform_set_drvdata(pdev, fmd);
1479 
1480 	v4l2_async_notifier_init(&fmd->subdev_notifier);
1481 
1482 	ret = fimc_md_register_platform_entities(fmd, dev->of_node);
1483 	if (ret)
1484 		goto err_clk;
1485 
1486 	ret = fimc_md_register_sensor_entities(fmd);
1487 	if (ret)
1488 		goto err_m_ent;
1489 
1490 	ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1491 	if (ret)
1492 		goto err_cleanup;
1493 	/*
1494 	 * FIMC platform devices need to be registered before the sclk_cam
1495 	 * clocks provider, as one of these devices needs to be activated
1496 	 * to enable the clock.
1497 	 */
1498 	ret = fimc_md_register_clk_provider(fmd);
1499 	if (ret < 0) {
1500 		v4l2_err(v4l2_dev, "clock provider registration failed\n");
1501 		goto err_attr;
1502 	}
1503 
1504 	if (fmd->num_sensors > 0) {
1505 		fmd->subdev_notifier.ops = &subdev_notifier_ops;
1506 		fmd->num_sensors = 0;
1507 
1508 		ret = v4l2_async_notifier_register(&fmd->v4l2_dev,
1509 						&fmd->subdev_notifier);
1510 		if (ret)
1511 			goto err_clk_p;
1512 	}
1513 
1514 	return 0;
1515 
1516 err_clk_p:
1517 	fimc_md_unregister_clk_provider(fmd);
1518 err_attr:
1519 	device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1520 err_cleanup:
1521 	v4l2_async_notifier_cleanup(&fmd->subdev_notifier);
1522 err_m_ent:
1523 	fimc_md_unregister_entities(fmd);
1524 err_clk:
1525 	fimc_md_put_clocks(fmd);
1526 err_v4l2dev:
1527 	v4l2_device_unregister(&fmd->v4l2_dev);
1528 err_md:
1529 	media_device_cleanup(&fmd->media_dev);
1530 	return ret;
1531 }
1532 
fimc_md_remove(struct platform_device * pdev)1533 static int fimc_md_remove(struct platform_device *pdev)
1534 {
1535 	struct fimc_md *fmd = platform_get_drvdata(pdev);
1536 
1537 	if (!fmd)
1538 		return 0;
1539 
1540 	fimc_md_unregister_clk_provider(fmd);
1541 	v4l2_async_notifier_unregister(&fmd->subdev_notifier);
1542 	v4l2_async_notifier_cleanup(&fmd->subdev_notifier);
1543 
1544 	v4l2_device_unregister(&fmd->v4l2_dev);
1545 	device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1546 	fimc_md_unregister_entities(fmd);
1547 	fimc_md_pipelines_free(fmd);
1548 	media_device_unregister(&fmd->media_dev);
1549 	media_device_cleanup(&fmd->media_dev);
1550 	fimc_md_put_clocks(fmd);
1551 
1552 	return 0;
1553 }
1554 
1555 static const struct platform_device_id fimc_driver_ids[] __always_unused = {
1556 	{ .name = "s5p-fimc-md" },
1557 	{ },
1558 };
1559 MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1560 
1561 static const struct of_device_id fimc_md_of_match[] = {
1562 	{ .compatible = "samsung,fimc" },
1563 	{ },
1564 };
1565 MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1566 
1567 static struct platform_driver fimc_md_driver = {
1568 	.probe		= fimc_md_probe,
1569 	.remove		= fimc_md_remove,
1570 	.driver = {
1571 		.of_match_table = of_match_ptr(fimc_md_of_match),
1572 		.name		= "s5p-fimc-md",
1573 	}
1574 };
1575 
fimc_md_init(void)1576 static int __init fimc_md_init(void)
1577 {
1578 	int ret;
1579 
1580 	request_module("s5p-csis");
1581 	ret = fimc_register_driver();
1582 	if (ret)
1583 		return ret;
1584 
1585 	return platform_driver_register(&fimc_md_driver);
1586 }
1587 
fimc_md_exit(void)1588 static void __exit fimc_md_exit(void)
1589 {
1590 	platform_driver_unregister(&fimc_md_driver);
1591 	fimc_unregister_driver();
1592 }
1593 
1594 module_init(fimc_md_init);
1595 module_exit(fimc_md_exit);
1596 
1597 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1598 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1599 MODULE_LICENSE("GPL");
1600 MODULE_VERSION("2.0.1");
1601