media-dev.c 38.6 KB
Newer Older
1 2 3
/*
 * S5P/EXYNOS4 SoC series camera host interface media device driver
 *
4 5
 * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd.
 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 2 of the License,
 * or (at your option) any later version.
 */

#include <linux/bug.h>
14 15
#include <linux/clk.h>
#include <linux/clk-provider.h>
16 17 18 19 20 21
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/module.h>
22 23 24
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/of_device.h>
25
#include <linux/of_graph.h>
26 27 28 29
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/types.h>
#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 41
#include "mipi-csis.h"

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/* Set up image sensor subdev -> FIMC capture node notifications. */
static void __setup_sensor_notification(struct fimc_md *fmd,
					struct v4l2_subdev *sensor,
					struct v4l2_subdev *fimc_sd)
{
	struct fimc_source_info *src_inf;
	struct fimc_sensor_info *md_si;
	unsigned long flags;

	src_inf = v4l2_get_subdev_hostdata(sensor);
	if (!src_inf || WARN_ON(fmd == NULL))
		return;

	md_si = source_to_sensor_info(src_inf);
	spin_lock_irqsave(&fmd->slock, flags);
	md_si->host = v4l2_get_subdevdata(fimc_sd);
	spin_unlock_irqrestore(&fmd->slock, flags);
}

61 62
/**
 * fimc_pipeline_prepare - update pipeline information with subdevice pointers
63
 * @me: media entity terminating the pipeline
64 65 66
 *
 * Caller holds the graph mutex.
 */
67
static void fimc_pipeline_prepare(struct fimc_pipeline *p,
68
					struct media_entity *me)
69
{
70
	struct fimc_md *fmd = entity_to_fimc_mdev(me);
71
	struct v4l2_subdev *sd;
72
	struct v4l2_subdev *sensor = NULL;
73
	int i;
74

75 76
	for (i = 0; i < IDX_MAX; i++)
		p->subdevs[i] = NULL;
77

78
	while (1) {
79 80 81 82 83 84 85
		struct media_pad *pad = NULL;

		/* Find remote source pad */
		for (i = 0; i < me->num_pads; i++) {
			struct media_pad *spad = &me->pads[i];
			if (!(spad->flags & MEDIA_PAD_FL_SINK))
				continue;
86
			pad = media_entity_remote_pad(spad);
87 88 89
			if (pad)
				break;
		}
90

91
		if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
92 93 94 95
			break;
		sd = media_entity_to_v4l2_subdev(pad->entity);

		switch (sd->grp_id) {
96
		case GRP_ID_SENSOR:
97 98 99
			sensor = sd;
			/* fall through */
		case GRP_ID_FIMC_IS_SENSOR:
100 101
			p->subdevs[IDX_SENSOR] = sd;
			break;
102
		case GRP_ID_CSIS:
103 104
			p->subdevs[IDX_CSIS] = sd;
			break;
105
		case GRP_ID_FLITE:
106 107
			p->subdevs[IDX_FLITE] = sd;
			break;
108
		case GRP_ID_FIMC:
109
			p->subdevs[IDX_FIMC] = sd;
110
			break;
111 112 113
		case GRP_ID_FIMC_IS:
			p->subdevs[IDX_IS_ISP] = sd;
			break;
114
		default:
115
			break;
116
		}
117 118 119
		me = &sd->entity;
		if (me->num_pads == 1)
			break;
120
	}
121 122 123

	if (sensor && p->subdevs[IDX_FIMC])
		__setup_sensor_notification(fmd, sensor, p->subdevs[IDX_FIMC]);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
}

/**
 * __subdev_set_power - change power state of a single subdev
 * @sd: subdevice to change power state for
 * @on: 1 to enable power or 0 to disable
 *
 * Return result of s_power subdev operation or -ENXIO if sd argument
 * is NULL. Return 0 if the subdevice does not implement s_power.
 */
static int __subdev_set_power(struct v4l2_subdev *sd, int on)
{
	int *use_count;
	int ret;

	if (sd == NULL)
		return -ENXIO;

	use_count = &sd->entity.use_count;
	if (on && (*use_count)++ > 0)
		return 0;
	else if (!on && (*use_count == 0 || --(*use_count) > 0))
		return 0;
	ret = v4l2_subdev_call(sd, core, s_power, on);

	return ret != -ENOIOCTLCMD ? ret : 0;
}

/**
 * fimc_pipeline_s_power - change power state of all pipeline subdevs
 * @fimc: fimc device terminating the pipeline
155
 * @state: true to power on, false to power off
156
 *
157
 * Needs to be called with the graph mutex held.
158
 */
159
static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool on)
160
{
161 162 163 164 165
	static const u8 seq[2][IDX_MAX - 1] = {
		{ IDX_IS_ISP, IDX_SENSOR, IDX_CSIS, IDX_FLITE },
		{ IDX_CSIS, IDX_FLITE, IDX_SENSOR, IDX_IS_ISP },
	};
	int i, ret = 0;
166

167
	if (p->subdevs[IDX_SENSOR] == NULL)
168 169
		return -ENXIO;

170 171 172 173 174
	for (i = 0; i < IDX_MAX - 1; i++) {
		unsigned int idx = seq[on][i];

		ret = __subdev_set_power(p->subdevs[idx], on);

175 176

		if (ret < 0 && ret != -ENXIO)
177
			goto error;
178
	}
179
	return 0;
180 181 182 183 184 185
error:
	for (; i >= 0; i--) {
		unsigned int idx = seq[on][i];
		__subdev_set_power(p->subdevs[idx], !on);
	}
	return ret;
186 187
}

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
/**
 * __fimc_pipeline_enable - enable power of all pipeline subdevs
 *			    and the sensor clock
 * @ep: video pipeline structure
 * @fmd: fimc media device
 *
 * Called with the graph mutex held.
 */
static int __fimc_pipeline_enable(struct exynos_media_pipeline *ep,
				  struct fimc_md *fmd)
{
	struct fimc_pipeline *p = to_fimc_pipeline(ep);
	int ret;

	/* Enable PXLASYNC clock if this pipeline includes FIMC-IS */
	if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
		ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
		if (ret < 0)
			return ret;
	}

	ret = fimc_pipeline_s_power(p, 1);
	if (!ret)
		return 0;

	if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
		clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);

	return ret;
}

219
/**
220 221
 * __fimc_pipeline_open - update the pipeline information, enable power
 *                        of all pipeline subdevs and the sensor clock
222
 * @me: media entity to start graph walk with
223
 * @prepare: true to walk the current pipeline and acquire all subdevs
224
 *
225
 * Called with the graph mutex held.
226
 */
227
static int __fimc_pipeline_open(struct exynos_media_pipeline *ep,
228
				struct media_entity *me, bool prepare)
229
{
230
	struct fimc_md *fmd = entity_to_fimc_mdev(me);
231
	struct fimc_pipeline *p = to_fimc_pipeline(ep);
232
	struct v4l2_subdev *sd;
233

234 235 236 237
	if (WARN_ON(p == NULL || me == NULL))
		return -EINVAL;

	if (prepare)
238 239
		fimc_pipeline_prepare(p, me);

240
	sd = p->subdevs[IDX_SENSOR];
241 242 243 244 245 246
	if (sd == NULL) {
		pr_warn("%s(): No sensor subdev\n", __func__);
		/*
		 * Pipeline open cannot fail so as to make it possible
		 * for the user space to configure the pipeline.
		 */
247
		return 0;
248
	}
249

250
	return __fimc_pipeline_enable(ep, fmd);
251 252 253
}

/**
254
 * __fimc_pipeline_close - disable the sensor clock and pipeline power
255 256
 * @fimc: fimc device terminating the pipeline
 *
257
 * Disable power of all subdevs and turn the external sensor clock off.
258
 */
259
static int __fimc_pipeline_close(struct exynos_media_pipeline *ep)
260
{
261
	struct fimc_pipeline *p = to_fimc_pipeline(ep);
262 263
	struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
	struct fimc_md *fmd;
264
	int ret;
265

266 267 268
	if (sd == NULL) {
		pr_warn("%s(): No sensor subdev\n", __func__);
		return 0;
269
	}
270

271 272
	ret = fimc_pipeline_s_power(p, 0);

273 274 275 276 277 278
	fmd = entity_to_fimc_mdev(&sd->entity);

	/* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
	if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
		clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);

279 280 281 282
	return ret == -ENXIO ? 0 : ret;
}

/**
283
 * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
284
 * @pipeline: video pipeline structure
285
 * @on: passed as the s_stream() callback argument
286
 */
287
static int __fimc_pipeline_s_stream(struct exynos_media_pipeline *ep, bool on)
288
{
289 290 291 292
	static const u8 seq[2][IDX_MAX] = {
		{ IDX_FIMC, IDX_SENSOR, IDX_IS_ISP, IDX_CSIS, IDX_FLITE },
		{ IDX_CSIS, IDX_FLITE, IDX_FIMC, IDX_SENSOR, IDX_IS_ISP },
	};
293
	struct fimc_pipeline *p = to_fimc_pipeline(ep);
294 295
	struct fimc_md *fmd = entity_to_fimc_mdev(&p->subdevs[IDX_CSIS]->entity);
	enum fimc_subdev_index sd_id;
296
	int i, ret = 0;
297

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
	if (p->subdevs[IDX_SENSOR] == NULL) {
		if (!fmd->user_subdev_api) {
			/*
			 * Sensor must be already discovered if we
			 * aren't in the user_subdev_api mode
			 */
			return -ENODEV;
		}

		/* Get pipeline sink entity */
		if (p->subdevs[IDX_FIMC])
			sd_id = IDX_FIMC;
		else if (p->subdevs[IDX_IS_ISP])
			sd_id = IDX_IS_ISP;
		else if (p->subdevs[IDX_FLITE])
			sd_id = IDX_FLITE;
		else
			return -ENODEV;

		/*
		 * Sensor could have been linked between open and STREAMON -
		 * check if this is the case.
		 */
		fimc_pipeline_prepare(p, &p->subdevs[sd_id]->entity);

		if (p->subdevs[IDX_SENSOR] == NULL)
			return -ENODEV;

		ret = __fimc_pipeline_enable(ep, fmd);
		if (ret < 0)
			return ret;

	}
331

332
	for (i = 0; i < IDX_MAX; i++) {
333
		unsigned int idx = seq[on][i];
334 335 336 337

		ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);

		if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
338
			goto error;
339
	}
340

341
	return 0;
342
error:
343
	fimc_pipeline_s_power(p, !on);
344 345 346 347 348
	for (; i >= 0; i--) {
		unsigned int idx = seq[on][i];
		v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
	}
	return ret;
349
}
350 351

/* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
352
static const struct exynos_media_pipeline_ops fimc_pipeline_ops = {
353 354 355
	.open		= __fimc_pipeline_open,
	.close		= __fimc_pipeline_close,
	.set_stream	= __fimc_pipeline_s_stream,
356
};
357

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
static struct exynos_media_pipeline *fimc_md_pipeline_create(
						struct fimc_md *fmd)
{
	struct fimc_pipeline *p;

	p = kzalloc(sizeof(*p), GFP_KERNEL);
	if (!p)
		return NULL;

	list_add_tail(&p->list, &fmd->pipelines);

	p->ep.ops = &fimc_pipeline_ops;
	return &p->ep;
}

static void fimc_md_pipelines_free(struct fimc_md *fmd)
{
	while (!list_empty(&fmd->pipelines)) {
		struct fimc_pipeline *p;

		p = list_entry(fmd->pipelines.next, typeof(*p), list);
		list_del(&p->list);
		kfree(p);
	}
}

384 385 386 387 388
/* Parse port node and register as a sub-device any sensor specified there. */
static int fimc_md_parse_port_node(struct fimc_md *fmd,
				   struct device_node *port,
				   unsigned int index)
{
389
	struct fimc_source_info *pd = &fmd->sensor[index].pdata;
390
	struct device_node *rem, *ep, *np;
391
	struct v4l2_fwnode_endpoint endpoint;
392
	int ret;
393 394 395 396 397 398

	/* Assume here a port node can have only one endpoint node. */
	ep = of_get_next_child(port, NULL);
	if (!ep)
		return 0;

399
	ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &endpoint);
400 401 402 403 404
	if (ret) {
		of_node_put(ep);
		return ret;
	}

405 406
	if (WARN_ON(endpoint.base.port == 0) || index >= FIMC_MAX_SENSORS) {
		of_node_put(ep);
407
		return -EINVAL;
408
	}
409

410
	pd->mux_id = (endpoint.base.port - 1) & 0x1;
411

412
	rem = of_graph_get_remote_port_parent(ep);
413 414
	of_node_put(ep);
	if (rem == NULL) {
415 416
		v4l2_info(&fmd->v4l2_dev, "Remote device at %pOF not found\n",
							ep);
417 418 419
		return 0;
	}

420
	if (fimc_input_is_parallel(endpoint.base.port)) {
421 422 423 424 425
		if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
			pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
		else
			pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
		pd->flags = endpoint.bus.parallel.flags;
426
	} else if (fimc_input_is_mipi_csi(endpoint.base.port)) {
427 428 429 430 431 432
		/*
		 * MIPI CSI-2: only input mux selection and
		 * the sensor's clock frequency is needed.
		 */
		pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
	} else {
433 434
		v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %pOF\n",
			 endpoint.base.port, rem);
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
	}
	/*
	 * For FIMC-IS handled sensors, that are placed under i2c-isp device
	 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
	 * input. Sensors are attached to the FIMC-LITE hostdata interface
	 * directly or through MIPI-CSIS, depending on the external media bus
	 * used. This needs to be handled in a more reliable way, not by just
	 * checking parent's node name.
	 */
	np = of_get_parent(rem);

	if (np && !of_node_cmp(np->name, "i2c-isp"))
		pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
	else
		pd->fimc_bus_type = pd->sensor_bus_type;

451 452
	if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor))) {
		of_node_put(rem);
453
		return -EINVAL;
454
	}
455

456 457
	fmd->sensor[index].asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
	fmd->sensor[index].asd.match.fwnode.fwnode = of_fwnode_handle(rem);
458 459 460 461 462 463
	fmd->async_subdevs[index] = &fmd->sensor[index].asd;

	fmd->num_sensors++;

	of_node_put(rem);
	return 0;
464 465 466
}

/* Register all SoC external sub-devices */
467
static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
468 469 470 471 472 473
{
	struct device_node *parent = fmd->pdev->dev.of_node;
	struct device_node *node, *ports;
	int index = 0;
	int ret;

474 475 476 477 478 479 480 481 482 483 484 485 486
	/*
	 * Runtime resume one of the FIMC entities to make sure
	 * the sclk_cam clocks are not globally disabled.
	 */
	if (!fmd->pmf)
		return -ENXIO;

	ret = pm_runtime_get_sync(fmd->pmf);
	if (ret < 0)
		return ret;

	fmd->num_sensors = 0;

487 488 489 490 491 492 493 494 495 496 497 498
	/* Attach sensors linked to MIPI CSI-2 receivers */
	for_each_available_child_of_node(parent, node) {
		struct device_node *port;

		if (of_node_cmp(node->name, "csis"))
			continue;
		/* The csis node can have only port subnode. */
		port = of_get_next_child(node, NULL);
		if (!port)
			continue;

		ret = fimc_md_parse_port_node(fmd, port, index);
499 500
		if (ret < 0) {
			of_node_put(node);
501
			goto rpm_put;
502
		}
503 504 505 506 507 508
		index++;
	}

	/* Attach sensors listed in the parallel-ports node */
	ports = of_get_child_by_name(parent, "parallel-ports");
	if (!ports)
509
		goto rpm_put;
510 511 512

	for_each_child_of_node(ports, node) {
		ret = fimc_md_parse_port_node(fmd, node, index);
513 514
		if (ret < 0) {
			of_node_put(node);
515
			break;
516
		}
517 518
		index++;
	}
519 520 521
rpm_put:
	pm_runtime_put(fmd->pmf);
	return ret;
522 523
}

524 525 526 527 528 529 530 531 532 533
static int __of_get_csis_id(struct device_node *np)
{
	u32 reg = 0;

	np = of_get_child_by_name(np, "port");
	if (!np)
		return -EINVAL;
	of_property_read_u32(np, "reg", &reg);
	return reg - FIMC_INPUT_MIPI_CSI2_0;
}
534 535

/*
536
 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
537
 */
538 539
static int register_fimc_lite_entity(struct fimc_md *fmd,
				     struct fimc_lite *fimc_lite)
540
{
541
	struct v4l2_subdev *sd;
542
	struct exynos_media_pipeline *ep;
543
	int ret;
544

545 546 547
	if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
		    fmd->fimc_lite[fimc_lite->index]))
		return -EBUSY;
548

549 550
	sd = &fimc_lite->subdev;
	sd->grp_id = GRP_ID_FLITE;
551 552 553 554 555 556

	ep = fimc_md_pipeline_create(fmd);
	if (!ep)
		return -ENOMEM;

	v4l2_set_subdev_hostdata(sd, ep);
557 558

	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
559 560 561 562 563 564
	if (!ret)
		fmd->fimc_lite[fimc_lite->index] = fimc_lite;
	else
		v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
			 fimc_lite->index);
	return ret;
565 566
}

567
static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
568
{
569
	struct v4l2_subdev *sd;
570
	struct exynos_media_pipeline *ep;
571 572
	int ret;

573 574
	if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
		return -EBUSY;
575

576 577
	sd = &fimc->vid_cap.subdev;
	sd->grp_id = GRP_ID_FIMC;
578 579 580 581 582 583

	ep = fimc_md_pipeline_create(fmd);
	if (!ep)
		return -ENOMEM;

	v4l2_set_subdev_hostdata(sd, ep);
584

585 586
	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
	if (!ret) {
587 588
		if (!fmd->pmf && fimc->pdev)
			fmd->pmf = &fimc->pdev->dev;
589 590 591 592 593
		fmd->fimc[fimc->id] = fimc;
		fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
	} else {
		v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
			 fimc->id, ret);
594
	}
595
	return ret;
596 597
}

598 599 600
static int register_csis_entity(struct fimc_md *fmd,
				struct platform_device *pdev,
				struct v4l2_subdev *sd)
601
{
602
	struct device_node *node = pdev->dev.of_node;
603 604
	int id, ret;

605
	id = node ? __of_get_csis_id(node) : max(0, pdev->id);
606

607 608
	if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
		return -ENOENT;
609

610 611
	if (WARN_ON(fmd->csis[id].sd))
		return -EBUSY;
612

613
	sd->grp_id = GRP_ID_CSIS;
614
	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
615 616 617
	if (!ret)
		fmd->csis[id].sd = sd;
	else
618
		v4l2_err(&fmd->v4l2_dev,
619
			 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
620 621 622
	return ret;
}

623 624 625
static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
{
	struct v4l2_subdev *sd = &is->isp.subdev;
626
	struct exynos_media_pipeline *ep;
627 628
	int ret;

629 630 631 632 633 634 635
	/* Allocate pipeline object for the ISP capture video node. */
	ep = fimc_md_pipeline_create(fmd);
	if (!ep)
		return -ENOMEM;

	v4l2_set_subdev_hostdata(sd, ep);

636 637 638 639 640 641 642 643 644 645 646
	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
	if (ret) {
		v4l2_err(&fmd->v4l2_dev,
			 "Failed to register FIMC-ISP (%d)\n", ret);
		return ret;
	}

	fmd->fimc_is = is;
	return 0;
}

647 648 649
static int fimc_md_register_platform_entity(struct fimc_md *fmd,
					    struct platform_device *pdev,
					    int plat_entity)
650
{
651 652 653 654 655 656 657 658 659 660 661
	struct device *dev = &pdev->dev;
	int ret = -EPROBE_DEFER;
	void *drvdata;

	/* Lock to ensure dev->driver won't change. */
	device_lock(dev);

	if (!dev->driver || !try_module_get(dev->driver->owner))
		goto dev_unlock;

	drvdata = dev_get_drvdata(dev);
662
	/* Some subdev didn't probe successfully id drvdata is NULL */
663 664 665 666 667 668 669
	if (drvdata) {
		switch (plat_entity) {
		case IDX_FIMC:
			ret = register_fimc_entity(fmd, drvdata);
			break;
		case IDX_FLITE:
			ret = register_fimc_lite_entity(fmd, drvdata);
670
			break;
671 672 673
		case IDX_CSIS:
			ret = register_csis_entity(fmd, pdev, drvdata);
			break;
674 675 676
		case IDX_IS_ISP:
			ret = register_fimc_is_entity(fmd, drvdata);
			break;
677 678
		default:
			ret = -ENODEV;
679 680
		}
	}
681

682 683 684 685 686 687 688 689 690 691 692 693
	module_put(dev->driver->owner);
dev_unlock:
	device_unlock(dev);
	if (ret == -EPROBE_DEFER)
		dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
			dev_name(dev));
	else if (ret < 0)
		dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
			dev_name(dev), ret);
	return ret;
}

694
/* Register FIMC, FIMC-LITE and CSIS media entities */
695 696
static int fimc_md_register_platform_entities(struct fimc_md *fmd,
					      struct device_node *parent)
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
{
	struct device_node *node;
	int ret = 0;

	for_each_available_child_of_node(parent, node) {
		struct platform_device *pdev;
		int plat_entity = -1;

		pdev = of_find_device_by_node(node);
		if (!pdev)
			continue;

		/* If driver of any entity isn't ready try all again later. */
		if (!strcmp(node->name, CSIS_OF_NODE_NAME))
			plat_entity = IDX_CSIS;
712 713
		else if	(!strcmp(node->name, FIMC_IS_OF_NODE_NAME))
			plat_entity = IDX_IS_ISP;
714 715 716 717 718 719 720 721 722 723
		else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
			plat_entity = IDX_FLITE;
		else if	(!strcmp(node->name, FIMC_OF_NODE_NAME) &&
			 !of_property_read_bool(node, "samsung,lcd-wb"))
			plat_entity = IDX_FIMC;

		if (plat_entity >= 0)
			ret = fimc_md_register_platform_entity(fmd, pdev,
							plat_entity);
		put_device(&pdev->dev);
724 725
		if (ret < 0) {
			of_node_put(node);
726
			break;
727
		}
728 729 730 731 732
	}

	return ret;
}

733 734 735 736 737
static void fimc_md_unregister_entities(struct fimc_md *fmd)
{
	int i;

	for (i = 0; i < FIMC_MAX_DEVS; i++) {
738 739
		struct fimc_dev *dev = fmd->fimc[i];
		if (dev == NULL)
740
			continue;
741 742
		v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
		dev->vid_cap.ve.pipe = NULL;
743 744
		fmd->fimc[i] = NULL;
	}
745
	for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
746 747
		struct fimc_lite *dev = fmd->fimc_lite[i];
		if (dev == NULL)
748
			continue;
749 750
		v4l2_device_unregister_subdev(&dev->subdev);
		dev->ve.pipe = NULL;
751 752
		fmd->fimc_lite[i] = NULL;
	}
753 754 755 756 757 758
	for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
		if (fmd->csis[i].sd == NULL)
			continue;
		v4l2_device_unregister_subdev(fmd->csis[i].sd);
		fmd->csis[i].sd = NULL;
	}
759 760 761 762

	if (fmd->fimc_is)
		v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);

763
	v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
764 765 766 767 768 769 770 771
}

/**
 * __fimc_md_create_fimc_links - create links to all FIMC entities
 * @fmd: fimc media device
 * @source: the source entity to create links to all fimc entities from
 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
 * @pad: the source entity pad index
772
 * @link_mask: bitmask of the fimc devices for which link should be enabled
773
 */
774 775 776
static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
					    struct media_entity *source,
					    struct v4l2_subdev *sensor,
777
					    int pad, int link_mask)
778
{
779
	struct fimc_source_info *si = NULL;
780
	struct media_entity *sink;
781
	unsigned int flags = 0;
782
	int i, ret = 0;
783

784 785 786
	if (sensor) {
		si = v4l2_get_subdev_hostdata(sensor);
		/* Skip direct FIMC links in the logical FIMC-IS sensor path */
787
		if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
788 789 790 791
			ret = 1;
	}

	for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
792
		if (!fmd->fimc[i])
793
			continue;
794 795 796 797
		/*
		 * Some FIMC variants are not fitted with camera capture
		 * interface. Skip creating a link from sensor for those.
		 */
798
		if (!fmd->fimc[i]->variant->has_cam_if)
799 800
			continue;

801
		flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
802

803
		sink = &fmd->fimc[i]->vid_cap.subdev.entity;
804
		ret = media_create_pad_link(source, pad, sink,
805
					      FIMC_SD_PAD_SINK_CAM, flags);
806 807 808
		if (ret)
			return ret;

809 810 811 812 813 814
		/* Notify FIMC capture subdev entity */
		ret = media_entity_call(sink, link_setup, &sink->pads[0],
					&source->pads[pad], flags);
		if (ret)
			break;

815
		v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
816 817
			  source->name, flags ? '=' : '-', sink->name);
	}
818 819 820 821 822 823

	for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
		if (!fmd->fimc_lite[i])
			continue;

		sink = &fmd->fimc_lite[i]->subdev.entity;
824
		ret = media_create_pad_link(source, pad, sink,
825
					       FLITE_SD_PAD_SINK, 0);
826 827 828 829 830
		if (ret)
			return ret;

		/* Notify FIMC-LITE subdev entity */
		ret = media_entity_call(sink, link_setup, &sink->pads[0],
831
					&source->pads[pad], 0);
832 833 834
		if (ret)
			break;

835 836
		v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
			  source->name, sink->name);
837
	}
838 839 840
	return 0;
}

841 842 843 844
/* Create links from FIMC-LITE source pads to other entities */
static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
{
	struct media_entity *source, *sink;
845
	int i, ret = 0;
846 847 848

	for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
		struct fimc_lite *fimc = fmd->fimc_lite[i];
849

850 851
		if (fimc == NULL)
			continue;
852

853
		source = &fimc->subdev.entity;
854
		sink = &fimc->ve.vdev.entity;
855
		/* FIMC-LITE's subdev and video node */
856
		ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_DMA,
857 858 859 860 861
					       sink, 0, 0);
		if (ret)
			break;
		/* Link from FIMC-LITE to IS-ISP subdev */
		sink = &fmd->fimc_is->isp.subdev.entity;
862
		ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_ISP,
863
					       sink, 0, 0);
864 865
		if (ret)
			break;
866 867 868 869 870 871 872 873
	}

	return ret;
}

/* Create FIMC-IS links */
static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
{
874
	struct fimc_isp *isp = &fmd->fimc_is->isp;
875 876 877
	struct media_entity *source, *sink;
	int i, ret;

878
	source = &isp->subdev.entity;
879 880 881 882 883

	for (i = 0; i < FIMC_MAX_DEVS; i++) {
		if (fmd->fimc[i] == NULL)
			continue;

884
		/* Link from FIMC-IS-ISP subdev to FIMC */
885
		sink = &fmd->fimc[i]->vid_cap.subdev.entity;
886
		ret = media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
887 888 889
					       sink, FIMC_SD_PAD_SINK_FIFO, 0);
		if (ret)
			return ret;
890 891
	}

892 893 894 895 896 897 898
	/* Link from FIMC-IS-ISP subdev to fimc-is-isp.capture video node */
	sink = &isp->video_capture.ve.vdev.entity;

	/* Skip this link if the fimc-is-isp video node driver isn't built-in */
	if (sink->num_pads == 0)
		return 0;

899
	return media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_DMA,
900
					sink, 0, 0);
901 902
}

903 904 905 906 907 908 909 910 911 912 913 914 915 916
/**
 * fimc_md_create_links - create default links between registered entities
 *
 * Parallel interface sensor entities are connected directly to FIMC capture
 * entities. The sensors using MIPI CSIS bus are connected through immutable
 * link with CSI receiver entity specified by mux_id. Any registered CSIS
 * entity has a link to each registered FIMC capture entity. Enabled links
 * are created by default between each subsequent registered sensor and
 * subsequent FIMC capture entity. The number of default active links is
 * determined by the number of available sensors or FIMC entities,
 * whichever is less.
 */
static int fimc_md_create_links(struct fimc_md *fmd)
{
917
	struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
918
	struct v4l2_subdev *sensor, *csis;
919
	struct fimc_source_info *pdata;
920
	struct media_entity *source, *sink;
921 922
	int i, pad, fimc_id = 0, ret = 0;
	u32 flags, link_mask = 0;
923 924 925 926 927 928

	for (i = 0; i < fmd->num_sensors; i++) {
		if (fmd->sensor[i].subdev == NULL)
			continue;

		sensor = fmd->sensor[i].subdev;
929 930
		pdata = v4l2_get_subdev_hostdata(sensor);
		if (!pdata)
931 932 933 934
			continue;

		source = NULL;

935 936
		switch (pdata->sensor_bus_type) {
		case FIMC_BUS_TYPE_MIPI_CSI2:
937 938 939 940 941 942
			if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
				"Wrong CSI channel id: %d\n", pdata->mux_id))
				return -EINVAL;

			csis = fmd->csis[pdata->mux_id].sd;
			if (WARN(csis == NULL,
943
				 "MIPI-CSI interface specified but s5p-csis module is not loaded!\n"))
944
				return -EINVAL;
945

946
			pad = sensor->entity.num_pads - 1;
947
			ret = media_create_pad_link(&sensor->entity, pad,
948 949 950 951 952 953
					      &csis->entity, CSIS_PAD_SINK,
					      MEDIA_LNK_FL_IMMUTABLE |
					      MEDIA_LNK_FL_ENABLED);
			if (ret)
				return ret;

954
			v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
955 956
				  sensor->entity.name, csis->entity.name);

957
			source = NULL;
958
			csi_sensors[pdata->mux_id] = sensor;
959 960
			break;

961
		case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
962 963 964 965 966 967
			source = &sensor->entity;
			pad = 0;
			break;

		default:
			v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
968
				 pdata->sensor_bus_type);
969 970 971 972 973
			return -EINVAL;
		}
		if (source == NULL)
			continue;

974
		link_mask = 1 << fimc_id++;
975
		ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
976
						       pad, link_mask);
977 978
	}

979
	for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
980 981
		if (fmd->csis[i].sd == NULL)
			continue;
982

983 984
		source = &fmd->csis[i].sd->entity;
		pad = CSIS_PAD_SOURCE;
985
		sensor = csi_sensors[i];
986

987
		link_mask = 1 << fimc_id++;
988
		ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
989
						       pad, link_mask);
990
	}
991

992 993 994 995 996
	/* Create immutable links between each FIMC's subdev and video node */
	flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
	for (i = 0; i < FIMC_MAX_DEVS; i++) {
		if (!fmd->fimc[i])
			continue;
997

998
		source = &fmd->fimc[i]->vid_cap.subdev.entity;
999
		sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
1000

1001
		ret = media_create_pad_link(source, FIMC_SD_PAD_SOURCE,
1002 1003 1004 1005 1006
					      sink, 0, flags);
		if (ret)
			break;
	}

1007 1008 1009 1010 1011 1012 1013 1014
	ret = __fimc_md_create_flite_source_links(fmd);
	if (ret < 0)
		return ret;

	if (fmd->use_isp)
		ret = __fimc_md_create_fimc_is_links(fmd);

	return ret;
1015 1016 1017
}

/*
1018
 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
1019
 */
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
static void fimc_md_put_clocks(struct fimc_md *fmd)
{
	int i = FIMC_MAX_CAMCLKS;

	while (--i >= 0) {
		if (IS_ERR(fmd->camclk[i].clock))
			continue;
		clk_put(fmd->camclk[i].clock);
		fmd->camclk[i].clock = ERR_PTR(-EINVAL);
	}
1030 1031 1032 1033 1034 1035 1036 1037

	/* Writeback (PIXELASYNCMx) clocks */
	for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
		if (IS_ERR(fmd->wbclk[i]))
			continue;
		clk_put(fmd->wbclk[i]);
		fmd->wbclk[i] = ERR_PTR(-EINVAL);
	}
1038 1039
}

1040 1041
static int fimc_md_get_clocks(struct fimc_md *fmd)
{
1042
	struct device *dev = &fmd->pdev->dev;
1043 1044
	char clk_name[32];
	struct clk *clock;
1045
	int i, ret = 0;
1046 1047 1048 1049

	for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
		fmd->camclk[i].clock = ERR_PTR(-EINVAL);

1050 1051
	for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
		snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1052 1053
		clock = clk_get(dev, clk_name);

1054
		if (IS_ERR(clock)) {
1055
			dev_err(dev, "Failed to get clock: %s\n", clk_name);
1056 1057 1058
			ret = PTR_ERR(clock);
			break;
		}
1059 1060
		fmd->camclk[i].clock = clock;
	}
1061 1062
	if (ret)
		fimc_md_put_clocks(fmd);
1063

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
	if (!fmd->use_isp)
		return 0;
	/*
	 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
	 * leave PIXELASYNCM0 out for the LCD Writeback driver.
	 */
	fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);

	for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
		snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
		clock = clk_get(dev, clk_name);
		if (IS_ERR(clock)) {
			v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
				  clk_name);
			ret = PTR_ERR(clock);
			break;
		}
		fmd->wbclk[i] = clock;
	}
	if (ret)
		fimc_md_put_clocks(fmd);

1086
	return ret;
1087 1088
}

1089
static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
1090
{
1091
	struct exynos_video_entity *ve;
1092
	struct fimc_pipeline *p;
1093
	struct video_device *vdev;
1094
	int ret;
1095

1096 1097
	vdev = media_entity_to_video_device(entity);
	if (vdev->entity.use_count == 0)
1098 1099
		return 0;

1100
	ve = vdev_to_exynos_video_entity(vdev);
1101 1102 1103 1104 1105 1106 1107
	p = to_fimc_pipeline(ve->pipe);
	/*
	 * Nothing to do if we are disabling the pipeline, some link
	 * has been disconnected and p->subdevs array is cleared now.
	 */
	if (!enable && p->subdevs[IDX_SENSOR] == NULL)
		return 0;
1108

1109 1110 1111 1112
	if (enable)
		ret = __fimc_pipeline_open(ve->pipe, entity, true);
	else
		ret = __fimc_pipeline_close(ve->pipe);
1113

1114 1115 1116 1117 1118 1119
	if (ret == 0 && !enable)
		memset(p->subdevs, 0, sizeof(p->subdevs));

	return ret;
}

1120
/* Locking: called with entity->graph_obj.mdev->graph_mutex mutex held. */
1121
static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable,
1122
				      struct media_graph *graph)
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
{
	struct media_entity *entity_err = entity;
	int ret;

	/*
	 * Walk current graph and call the pipeline open/close routine for each
	 * opened video node that belongs to the graph of entities connected
	 * through active links. This is needed as we cannot power on/off the
	 * subdevs in random order.
	 */
1133
	media_graph_walk_start(graph, entity);
1134

1135
	while ((entity = media_graph_walk_next(graph))) {
1136
		if (!is_media_entity_v4l2_video_device(entity))
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
			continue;

		ret  = __fimc_md_modify_pipeline(entity, enable);

		if (ret < 0)
			goto err;
	}

	return 0;

1147
err:
1148
	media_graph_walk_start(graph, entity_err);
1149

1150
	while ((entity_err = media_graph_walk_next(graph))) {
1151
		if (!is_media_entity_v4l2_video_device(entity_err))
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
			continue;

		__fimc_md_modify_pipeline(entity_err, !enable);

		if (entity_err == entity)
			break;
	}

	return ret;
}

static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
				unsigned int notification)
{
1166
	struct media_graph *graph =
1167 1168
		&container_of(link->graph_obj.mdev, struct fimc_md,
			      media_dev)->link_setup_graph;
1169 1170 1171 1172 1173
	struct media_entity *sink = link->sink->entity;
	int ret = 0;

	/* Before link disconnection */
	if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
1174
		ret = media_graph_walk_init(graph,
1175 1176 1177
						   link->graph_obj.mdev);
		if (ret)
			return ret;
1178
		if (!(flags & MEDIA_LNK_FL_ENABLED))
1179
			ret = __fimc_md_modify_pipelines(sink, false, graph);
1180
#if 0
1181
		else
1182 1183
			/* TODO: Link state change validation */
#endif
1184
	/* After link activation */
1185 1186 1187
	} else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH) {
		if (link->flags & MEDIA_LNK_FL_ENABLED)
			ret = __fimc_md_modify_pipelines(sink, true, graph);
1188
		media_graph_walk_cleanup(graph);
1189
	}
1190

1191
	return ret ? -EPIPE : 0;
1192 1193
}

1194 1195 1196 1197
static const struct media_device_ops fimc_md_ops = {
	.link_notify = fimc_md_link_notify,
};

1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
static ssize_t fimc_md_sysfs_show(struct device *dev,
				  struct device_attribute *attr, char *buf)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct fimc_md *fmd = platform_get_drvdata(pdev);

	if (fmd->user_subdev_api)
		return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);

	return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
}

static ssize_t fimc_md_sysfs_store(struct device *dev,
				   struct device_attribute *attr,
				   const char *buf, size_t count)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct fimc_md *fmd = platform_get_drvdata(pdev);
	bool subdev_api;
	int i;

	if (!strcmp(buf, "vid-dev\n"))
		subdev_api = false;
	else if (!strcmp(buf, "sub-dev\n"))
		subdev_api = true;
	else
		return count;

	fmd->user_subdev_api = subdev_api;
	for (i = 0; i < FIMC_MAX_DEVS; i++)
		if (fmd->fimc[i])
			fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
	return count;
}
/*
 * This device attribute is to select video pipeline configuration method.
 * There are following valid values:
 *  vid-dev - for V4L2 video node API only, subdevice will be configured
 *  by the host driver.
 *  sub-dev - for media controller API, subdevs must be configured in user
 *  space before starting streaming.
 */
static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
		   fimc_md_sysfs_show, fimc_md_sysfs_store);

1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
static int fimc_md_get_pinctrl(struct fimc_md *fmd)
{
	struct device *dev = &fmd->pdev->dev;
	struct fimc_pinctrl *pctl = &fmd->pinctl;

	pctl->pinctrl = devm_pinctrl_get(dev);
	if (IS_ERR(pctl->pinctrl))
		return PTR_ERR(pctl->pinctrl);

	pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
					PINCTRL_STATE_DEFAULT);
	if (IS_ERR(pctl->state_default))
		return PTR_ERR(pctl->state_default);

	pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
					PINCTRL_STATE_IDLE);
	return 0;
}

1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
static int cam_clk_prepare(struct clk_hw *hw)
{
	struct cam_clk *camclk = to_cam_clk(hw);
	int ret;

	if (camclk->fmd->pmf == NULL)
		return -ENODEV;

	ret = pm_runtime_get_sync(camclk->fmd->pmf);
	return ret < 0 ? ret : 0;
}

static void cam_clk_unprepare(struct clk_hw *hw)
{
	struct cam_clk *camclk = to_cam_clk(hw);

	if (camclk->fmd->pmf == NULL)
		return;

	pm_runtime_put_sync(camclk->fmd->pmf);
}

static const struct clk_ops cam_clk_ops = {
	.prepare = cam_clk_prepare,
	.unprepare = cam_clk_unprepare,
};

static void fimc_md_unregister_clk_provider(struct fimc_md *fmd)
{
	struct cam_clk_provider *cp = &fmd->clk_provider;
	unsigned int i;

	if (cp->of_node)
		of_clk_del_provider(cp->of_node);

	for (i = 0; i < cp->num_clocks; i++)
		clk_unregister(cp->clks[i]);
}

static int fimc_md_register_clk_provider(struct fimc_md *fmd)
{
	struct cam_clk_provider *cp = &fmd->clk_provider;
	struct device *dev = &fmd->pdev->dev;
	int i, ret;

	for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
		struct cam_clk *camclk = &cp->camclk[i];
		struct clk_init_data init;
		const char *p_name;

		ret = of_property_read_string_index(dev->of_node,
					"clock-output-names", i, &init.name);
		if (ret < 0)
			break;

		p_name = __clk_get_name(fmd->camclk[i].clock);

		/* It's safe since clk_register() will duplicate the string. */
		init.parent_names = &p_name;
		init.num_parents = 1;
		init.ops = &cam_clk_ops;
		init.flags = CLK_SET_RATE_PARENT;
		camclk->hw.init = &init;
		camclk->fmd = fmd;

		cp->clks[i] = clk_register(NULL, &camclk->hw);
		if (IS_ERR(cp->clks[i])) {
			dev_err(dev, "failed to register clock: %s (%ld)\n",
					init.name, PTR_ERR(cp->clks[i]));
			ret = PTR_ERR(cp->clks[i]);
			goto err;
		}
		cp->num_clocks++;
	}

	if (cp->num_clocks == 0) {
		dev_warn(dev, "clk provider not registered\n");
		return 0;
	}

	cp->clk_data.clks = cp->clks;
	cp->clk_data.clk_num = cp->num_clocks;
	cp->of_node = dev->of_node;
	ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
				  &cp->clk_data);
	if (ret == 0)
		return 0;
err:
	fimc_md_unregister_clk_provider(fmd);
	return ret;
}

1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
				 struct v4l2_subdev *subdev,
				 struct v4l2_async_subdev *asd)
{
	struct fimc_md *fmd = notifier_to_fimc_md(notifier);
	struct fimc_sensor_info *si = NULL;
	int i;

	/* Find platform data for this sensor subdev */
	for (i = 0; i < ARRAY_SIZE(fmd->sensor); i++)
1364 1365
		if (fmd->sensor[i].asd.match.fwnode.fwnode ==
		    of_fwnode_handle(subdev->dev->of_node))
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
			si = &fmd->sensor[i];

	if (si == NULL)
		return -EINVAL;

	v4l2_set_subdev_hostdata(subdev, &si->pdata);

	if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
		subdev->grp_id = GRP_ID_FIMC_IS_SENSOR;
	else
		subdev->grp_id = GRP_ID_SENSOR;

	si->subdev = subdev;

	v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
		  subdev->name, fmd->num_sensors);

	fmd->num_sensors++;

	return 0;
}

static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
{
	struct fimc_md *fmd = notifier_to_fimc_md(notifier);
	int ret;

	mutex_lock(&fmd->media_dev.graph_mutex);

	ret = fimc_md_create_links(fmd);
	if (ret < 0)
		goto unlock;

	ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
unlock:
	mutex_unlock(&fmd->media_dev.graph_mutex);
1402 1403 1404 1405
	if (ret < 0)
		return ret;

	return media_device_register(&fmd->media_dev);
1406 1407
}

1408 1409 1410 1411 1412
static const struct v4l2_async_notifier_operations subdev_notifier_ops = {
	.bound = subdev_notifier_bound,
	.complete = subdev_notifier_complete,
};

1413
static int fimc_md_probe(struct platform_device *pdev)
1414
{
1415
	struct device *dev = &pdev->dev;
1416 1417 1418 1419
	struct v4l2_device *v4l2_dev;
	struct fimc_md *fmd;
	int ret;

1420
	fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
1421 1422 1423 1424
	if (!fmd)
		return -ENOMEM;

	spin_lock_init(&fmd->slock);
1425
	INIT_LIST_HEAD(&fmd->pipelines);
1426
	fmd->pdev = pdev;
1427 1428 1429

	strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
		sizeof(fmd->media_dev.model));
1430
	fmd->media_dev.ops = &fimc_md_ops;
1431
	fmd->media_dev.dev = dev;
1432 1433 1434

	v4l2_dev = &fmd->v4l2_dev;
	v4l2_dev->mdev = &fmd->media_dev;
1435
	v4l2_dev->notify = fimc_sensor_notify;
1436
	strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
1437

1438
	fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
1439
	fmd->user_subdev_api = true;
1440

1441 1442
	media_device_init(&fmd->media_dev);

1443
	ret = v4l2_device_register(dev, &fmd->v4l2_dev);
1444 1445
	if (ret < 0) {
		v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
1446
		return ret;
1447
	}
1448

1449 1450
	ret = fimc_md_get_clocks(fmd);
	if (ret)
1451
		goto err_md;
1452

1453 1454 1455 1456
	ret = fimc_md_get_pinctrl(fmd);
	if (ret < 0) {
		if (ret != EPROBE_DEFER)
			dev_err(dev, "Failed to get pinctrl: %d\n", ret);
1457
		goto err_clk;
1458 1459
	}

1460 1461
	platform_set_drvdata(pdev, fmd);

1462
	ret = fimc_md_register_platform_entities(fmd, dev->of_node);
1463
	if (ret)
1464
		goto err_clk;
1465

1466
	ret = fimc_md_register_sensor_entities(fmd);
1467
	if (ret)
1468
		goto err_m_ent;
1469 1470

	ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1471
	if (ret)
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
		goto err_m_ent;
	/*
	 * FIMC platform devices need to be registered before the sclk_cam
	 * clocks provider, as one of these devices needs to be activated
	 * to enable the clock.
	 */
	ret = fimc_md_register_clk_provider(fmd);
	if (ret < 0) {
		v4l2_err(v4l2_dev, "clock provider registration failed\n");
		goto err_attr;
	}

	if (fmd->num_sensors > 0) {
		fmd->subdev_notifier.subdevs = fmd->async_subdevs;
		fmd->subdev_notifier.num_subdevs = fmd->num_sensors;
1487
		fmd->subdev_notifier.ops = &subdev_notifier_ops;
1488 1489 1490 1491 1492 1493 1494
		fmd->num_sensors = 0;

		ret = v4l2_async_notifier_register(&fmd->v4l2_dev,
						&fmd->subdev_notifier);
		if (ret)
			goto err_clk_p;
	}
1495 1496 1497

	return 0;

1498 1499 1500 1501
err_clk_p:
	fimc_md_unregister_clk_provider(fmd);
err_attr:
	device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1502
err_clk:
1503
	fimc_md_put_clocks(fmd);
1504
err_m_ent:
1505
	fimc_md_unregister_entities(fmd);
1506
err_md:
1507
	media_device_cleanup(&fmd->media_dev);
1508 1509 1510 1511
	v4l2_device_unregister(&fmd->v4l2_dev);
	return ret;
}

1512
static int fimc_md_remove(struct platform_device *pdev)
1513 1514 1515 1516 1517
{
	struct fimc_md *fmd = platform_get_drvdata(pdev);

	if (!fmd)
		return 0;
1518

1519
	fimc_md_unregister_clk_provider(fmd);
1520 1521
	v4l2_async_notifier_unregister(&fmd->subdev_notifier);

1522
	v4l2_device_unregister(&fmd->v4l2_dev);
1523 1524
	device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
	fimc_md_unregister_entities(fmd);
1525
	fimc_md_pipelines_free(fmd);
1526
	media_device_unregister(&fmd->media_dev);
1527
	media_device_cleanup(&fmd->media_dev);
1528
	fimc_md_put_clocks(fmd);
1529

1530 1531 1532
	return 0;
}

1533
static const struct platform_device_id fimc_driver_ids[] __always_unused = {
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
	{ .name = "s5p-fimc-md" },
	{ },
};
MODULE_DEVICE_TABLE(platform, fimc_driver_ids);

static const struct of_device_id fimc_md_of_match[] = {
	{ .compatible = "samsung,fimc" },
	{ },
};
MODULE_DEVICE_TABLE(of, fimc_md_of_match);

1545 1546
static struct platform_driver fimc_md_driver = {
	.probe		= fimc_md_probe,
1547
	.remove		= fimc_md_remove,
1548
	.driver = {
1549 1550
		.of_match_table = of_match_ptr(fimc_md_of_match),
		.name		= "s5p-fimc-md",
1551 1552 1553
	}
};

1554
static int __init fimc_md_init(void)
1555 1556
{
	int ret;
1557

1558 1559 1560 1561
	request_module("s5p-csis");
	ret = fimc_register_driver();
	if (ret)
		return ret;
1562

1563 1564
	return platform_driver_register(&fimc_md_driver);
}
1565 1566

static void __exit fimc_md_exit(void)
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
{
	platform_driver_unregister(&fimc_md_driver);
	fimc_unregister_driver();
}

module_init(fimc_md_init);
module_exit(fimc_md_exit);

MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
MODULE_LICENSE("GPL");
MODULE_VERSION("2.0.1");