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