media-dev.c 36.0 KB
Newer Older
1 2 3
/*
 * S5P/EXYNOS4 SoC series camera host interface media device driver
 *
4 5
 * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd.
 * Sylwester Nawrocki <s.nawrocki@samsung.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/module.h>
20 21 22 23
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/of_device.h>
#include <linux/of_i2c.h>
24 25 26 27
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/types.h>
#include <linux/slab.h>
28
#include <media/v4l2-ctrls.h>
29
#include <media/v4l2-of.h>
30
#include <media/media-device.h>
31
#include <media/s5p_fimc.h>
32

33
#include "media-dev.h"
34
#include "fimc-core.h"
35
#include "fimc-lite.h"
36 37 38 39 40 41 42
#include "mipi-csis.h"

static int __fimc_md_set_camclk(struct fimc_md *fmd,
				struct fimc_sensor_info *s_info,
				bool on);
/**
 * fimc_pipeline_prepare - update pipeline information with subdevice pointers
43
 * @me: media entity terminating the pipeline
44 45 46
 *
 * Caller holds the graph mutex.
 */
47 48
static void fimc_pipeline_prepare(struct fimc_pipeline *p,
				  struct media_entity *me)
49 50
{
	struct v4l2_subdev *sd;
51
	int i;
52

53 54
	for (i = 0; i < IDX_MAX; i++)
		p->subdevs[i] = NULL;
55

56
	while (1) {
57 58 59 60 61 62 63 64 65 66 67
		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;
			pad = media_entity_remote_source(spad);
			if (pad)
				break;
		}
68 69 70 71 72 73 74

		if (pad == NULL ||
		    media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
			break;
		sd = media_entity_to_v4l2_subdev(pad->entity);

		switch (sd->grp_id) {
75 76
		case GRP_ID_FIMC_IS_SENSOR:
		case GRP_ID_SENSOR:
77 78
			p->subdevs[IDX_SENSOR] = sd;
			break;
79
		case GRP_ID_CSIS:
80 81
			p->subdevs[IDX_CSIS] = sd;
			break;
82
		case GRP_ID_FLITE:
83 84
			p->subdevs[IDX_FLITE] = sd;
			break;
85
		case GRP_ID_FIMC:
86 87 88 89 90 91
			/* No need to control FIMC subdev through subdev ops */
			break;
		default:
			pr_warn("%s: Unknown subdev grp_id: %#x\n",
				__func__, sd->grp_id);
		}
92 93 94
		me = &sd->entity;
		if (me->num_pads == 1)
			break;
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	}
}

/**
 * __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
127
 * @state: true to power on, false to power off
128
 *
129
 * Needs to be called with the graph mutex held.
130
 */
131
static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool on)
132
{
133 134 135 136 137
	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;
138

139
	if (p->subdevs[IDX_SENSOR] == NULL)
140 141
		return -ENXIO;

142 143 144 145 146
	for (i = 0; i < IDX_MAX - 1; i++) {
		unsigned int idx = seq[on][i];

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

147 148

		if (ret < 0 && ret != -ENXIO)
149
			goto error;
150
	}
151
	return 0;
152 153 154 155 156 157
error:
	for (; i >= 0; i--) {
		unsigned int idx = seq[on][i];
		__subdev_set_power(p->subdevs[idx], !on);
	}
	return ret;
158 159 160
}

/**
161 162
 * __fimc_pipeline_open - update the pipeline information, enable power
 *                        of all pipeline subdevs and the sensor clock
163
 * @me: media entity to start graph walk with
164
 * @prepare: true to walk the current pipeline and acquire all subdevs
165
 *
166
 * Called with the graph mutex held.
167
 */
168
static int __fimc_pipeline_open(struct fimc_pipeline *p,
169
				struct media_entity *me, bool prepare)
170
{
171 172
	struct fimc_md *fmd = entity_to_fimc_mdev(me);
	struct v4l2_subdev *sd;
173 174
	int ret;

175 176 177 178
	if (WARN_ON(p == NULL || me == NULL))
		return -EINVAL;

	if (prepare)
179 180
		fimc_pipeline_prepare(p, me);

181 182
	sd = p->subdevs[IDX_SENSOR];
	if (sd == NULL)
183
		return -EINVAL;
184

185 186 187 188 189 190 191 192 193 194 195 196 197
	/* Disable 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_md_set_camclk(sd, true);
	if (ret < 0)
		goto err_wbclk;

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

199 200 201 202 203 204 205
	fimc_md_set_camclk(sd, false);

err_wbclk:
	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;
206 207 208
}

/**
209
 * __fimc_pipeline_close - disable the sensor clock and pipeline power
210 211
 * @fimc: fimc device terminating the pipeline
 *
212
 * Disable power of all subdevs and turn the external sensor clock off.
213
 */
214
static int __fimc_pipeline_close(struct fimc_pipeline *p)
215
{
216 217
	struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
	struct fimc_md *fmd;
218 219
	int ret = 0;

220
	if (WARN_ON(sd == NULL))
221 222
		return -EINVAL;

223 224
	if (p->subdevs[IDX_SENSOR]) {
		ret = fimc_pipeline_s_power(p, 0);
225
		fimc_md_set_camclk(sd, false);
226
	}
227 228 229 230 231 232 233

	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]);

234 235 236 237
	return ret == -ENXIO ? 0 : ret;
}

/**
238
 * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
239
 * @pipeline: video pipeline structure
240
 * @on: passed as the s_stream() callback argument
241
 */
242
static int __fimc_pipeline_s_stream(struct fimc_pipeline *p, bool on)
243
{
244 245 246 247 248
	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 },
	};
	int i, ret = 0;
249

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

253
	for (i = 0; i < IDX_MAX; i++) {
254
		unsigned int idx = seq[on][i];
255 256 257 258

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

		if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
259
			goto error;
260 261
	}
	return 0;
262 263 264 265 266 267
error:
	for (; i >= 0; i--) {
		unsigned int idx = seq[on][i];
		v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
	}
	return ret;
268
}
269 270 271

/* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
static const struct fimc_pipeline_ops fimc_pipeline_ops = {
272 273 274
	.open		= __fimc_pipeline_open,
	.close		= __fimc_pipeline_close,
	.set_stream	= __fimc_pipeline_s_stream,
275
};
276 277 278 279 280 281 282 283 284 285 286 287

/*
 * Sensor subdevice helper functions
 */
static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd,
				   struct fimc_sensor_info *s_info)
{
	struct i2c_adapter *adapter;
	struct v4l2_subdev *sd = NULL;

	if (!s_info || !fmd)
		return NULL;
288 289 290 291 292
	/*
	 * If FIMC bus type is not Writeback FIFO assume it is same
	 * as sensor_bus_type.
	 */
	s_info->pdata.fimc_bus_type = s_info->pdata.sensor_bus_type;
293

294
	adapter = i2c_get_adapter(s_info->pdata.i2c_bus_num);
295 296 297
	if (!adapter) {
		v4l2_warn(&fmd->v4l2_dev,
			  "Failed to get I2C adapter %d, deferring probe\n",
298
			  s_info->pdata.i2c_bus_num);
299 300
		return ERR_PTR(-EPROBE_DEFER);
	}
301
	sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
302
				       s_info->pdata.board_info, NULL);
303
	if (IS_ERR_OR_NULL(sd)) {
304
		i2c_put_adapter(adapter);
305 306
		v4l2_warn(&fmd->v4l2_dev,
			  "Failed to acquire subdev %s, deferring probe\n",
307
			  s_info->pdata.board_info->type);
308
		return ERR_PTR(-EPROBE_DEFER);
309 310
	}
	v4l2_set_subdev_hostdata(sd, s_info);
311
	sd->grp_id = GRP_ID_SENSOR;
312 313

	v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
314
		  sd->name);
315 316 317 318 319 320
	return sd;
}

static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
321
	struct i2c_adapter *adapter;
322 323 324 325

	if (!client)
		return;
	v4l2_device_unregister_subdev(sd);
326 327 328 329 330 331 332

	if (!client->dev.of_node) {
		adapter = client->adapter;
		i2c_unregister_device(client);
		if (adapter)
			i2c_put_adapter(adapter);
	}
333 334
}

335
#ifdef CONFIG_OF
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
/* Register I2C client subdev associated with @node. */
static int fimc_md_of_add_sensor(struct fimc_md *fmd,
				 struct device_node *node, int index)
{
	struct fimc_sensor_info *si;
	struct i2c_client *client;
	struct v4l2_subdev *sd;
	int ret;

	if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor)))
		return -EINVAL;
	si = &fmd->sensor[index];

	client = of_find_i2c_device_by_node(node);
	if (!client)
		return -EPROBE_DEFER;

	device_lock(&client->dev);

	if (!client->driver ||
	    !try_module_get(client->driver->driver.owner)) {
		ret = -EPROBE_DEFER;
		v4l2_info(&fmd->v4l2_dev, "No driver found for %s\n",
						node->full_name);
		goto dev_put;
	}

	/* Enable sensor's master clock */
	ret = __fimc_md_set_camclk(fmd, si, true);
	if (ret < 0)
		goto mod_put;
	sd = i2c_get_clientdata(client);

	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
	__fimc_md_set_camclk(fmd, si, false);
	if (ret < 0)
		goto mod_put;

	v4l2_set_subdev_hostdata(sd, si);
	sd->grp_id = GRP_ID_SENSOR;
	si->subdev = sd;
	v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
		  sd->name, fmd->num_sensors);
	fmd->num_sensors++;

mod_put:
	module_put(client->driver->driver.owner);
dev_put:
	device_unlock(&client->dev);
	put_device(&client->dev);
	return ret;
}

/* 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)
{
	struct device_node *rem, *ep, *np;
	struct fimc_source_info *pd;
	struct v4l2_of_endpoint endpoint;
	int ret;
	u32 val;

	pd = &fmd->sensor[index].pdata;

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

	v4l2_of_parse_endpoint(ep, &endpoint);
	if (WARN_ON(endpoint.port == 0) || index >= FIMC_MAX_SENSORS)
		return -EINVAL;

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

	rem = v4l2_of_get_remote_port_parent(ep);
	of_node_put(ep);
	if (rem == NULL) {
		v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n",
							ep->full_name);
		return 0;
	}
	if (!of_property_read_u32(rem, "samsung,camclk-out", &val))
		pd->clk_id = val;

	if (!of_property_read_u32(rem, "clock-frequency", &val))
		pd->clk_frequency = val;

	if (pd->clk_frequency == 0) {
		v4l2_err(&fmd->v4l2_dev, "Wrong clock frequency at node %s\n",
			 rem->full_name);
		of_node_put(rem);
		return -EINVAL;
	}

	if (fimc_input_is_parallel(endpoint.port)) {
		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;
	} else if (fimc_input_is_mipi_csi(endpoint.port)) {
		/*
		 * 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 {
		v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n",
			 endpoint.port, rem->full_name);
	}
	/*
	 * 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;

	ret = fimc_md_of_add_sensor(fmd, rem, index);
	of_node_put(rem);

	return ret;
}

/* Register all SoC external sub-devices */
static int fimc_md_of_sensors_register(struct fimc_md *fmd,
				       struct device_node *np)
{
	struct device_node *parent = fmd->pdev->dev.of_node;
	struct device_node *node, *ports;
	int index = 0;
	int ret;

	/* 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);
		if (ret < 0)
			return ret;
		index++;
	}

	/* Attach sensors listed in the parallel-ports node */
	ports = of_get_child_by_name(parent, "parallel-ports");
	if (!ports)
		return 0;

	for_each_child_of_node(ports, node) {
		ret = fimc_md_parse_port_node(fmd, node, index);
		if (ret < 0)
			break;
		index++;
	}

	return 0;
}

511 512 513 514 515 516 517 518 519 520 521
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;
}
#else
522
#define fimc_md_of_sensors_register(fmd, np) (-ENOSYS)
523 524 525
#define __of_get_csis_id(np) (-ENOSYS)
#endif

526 527 528
static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
{
	struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
529 530 531
	struct device_node *of_node = fmd->pdev->dev.of_node;
	int num_clients = 0;
	int ret, i;
532 533 534 535 536

	/*
	 * Runtime resume one of the FIMC entities to make sure
	 * the sclk_cam clocks are not globally disabled.
	 */
537
	if (!fmd->pmf)
538
		return -ENXIO;
539

540
	ret = pm_runtime_get_sync(fmd->pmf);
541 542 543
	if (ret < 0)
		return ret;

544 545 546 547 548 549 550 551
	if (of_node) {
		fmd->num_sensors = 0;
		ret = fimc_md_of_sensors_register(fmd, of_node);
	} else if (pdata) {
		WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor));
		num_clients = min_t(u32, pdata->num_clients,
				    ARRAY_SIZE(fmd->sensor));
		fmd->num_sensors = num_clients;
552

553 554
		for (i = 0; i < num_clients; i++) {
			struct v4l2_subdev *sd;
555

556 557 558 559 560 561 562 563 564 565 566 567
			fmd->sensor[i].pdata = pdata->source_info[i];
			ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true);
			if (ret)
				break;
			sd = fimc_md_register_sensor(fmd, &fmd->sensor[i]);
			ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false);

			if (IS_ERR(sd)) {
				fmd->sensor[i].subdev = NULL;
				ret = PTR_ERR(sd);
				break;
			}
568
			fmd->sensor[i].subdev = sd;
569 570
			if (ret)
				break;
571
		}
572
	}
573

574
	pm_runtime_put(fmd->pmf);
575 576 577 578
	return ret;
}

/*
579
 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
580
 */
581 582 583

static int register_fimc_lite_entity(struct fimc_md *fmd,
				     struct fimc_lite *fimc_lite)
584
{
585
	struct v4l2_subdev *sd;
586
	int ret;
587

588 589 590
	if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
		    fmd->fimc_lite[fimc_lite->index]))
		return -EBUSY;
591

592 593
	sd = &fimc_lite->subdev;
	sd->grp_id = GRP_ID_FLITE;
594
	v4l2_set_subdev_hostdata(sd, (void *)&fimc_pipeline_ops);
595 596

	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
597 598 599 600 601 602
	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;
603 604
}

605
static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
606
{
607
	struct v4l2_subdev *sd;
608 609
	int ret;

610 611
	if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
		return -EBUSY;
612

613 614 615
	sd = &fimc->vid_cap.subdev;
	sd->grp_id = GRP_ID_FIMC;
	v4l2_set_subdev_hostdata(sd, (void *)&fimc_pipeline_ops);
616

617 618
	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
	if (!ret) {
619 620
		if (!fmd->pmf && fimc->pdev)
			fmd->pmf = &fimc->pdev->dev;
621 622 623 624 625
		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);
626
	}
627
	return ret;
628 629
}

630 631 632
static int register_csis_entity(struct fimc_md *fmd,
				struct platform_device *pdev,
				struct v4l2_subdev *sd)
633
{
634
	struct device_node *node = pdev->dev.of_node;
635 636
	int id, ret;

637
	id = node ? __of_get_csis_id(node) : max(0, pdev->id);
638

639 640
	if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
		return -ENOENT;
641

642 643
	if (WARN_ON(fmd->csis[id].sd))
		return -EBUSY;
644

645
	sd->grp_id = GRP_ID_CSIS;
646
	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
647 648 649
	if (!ret)
		fmd->csis[id].sd = sd;
	else
650
		v4l2_err(&fmd->v4l2_dev,
651
			 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
652 653 654
	return ret;
}

655 656 657
static int fimc_md_register_platform_entity(struct fimc_md *fmd,
					    struct platform_device *pdev,
					    int plat_entity)
658
{
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
	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);
	/* Some subdev didn't probe succesfully id drvdata is NULL */
	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);
678
			break;
679 680 681 682 683
		case IDX_CSIS:
			ret = register_csis_entity(fmd, pdev, drvdata);
			break;
		default:
			ret = -ENODEV;
684 685
		}
	}
686

687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
	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;
}

static int fimc_md_pdev_match(struct device *dev, void *data)
{
	struct platform_device *pdev = to_platform_device(dev);
	int plat_entity = -1;
	int ret;
	char *p;

	if (!get_device(dev))
		return -ENODEV;

	if (!strcmp(pdev->name, CSIS_DRIVER_NAME)) {
		plat_entity = IDX_CSIS;
	} else if (!strcmp(pdev->name, FIMC_LITE_DRV_NAME)) {
		plat_entity = IDX_FLITE;
	} else {
		p = strstr(pdev->name, "fimc");
		if (p && *(p + 4) == 0)
			plat_entity = IDX_FIMC;
717 718
	}

719 720 721 722 723
	if (plat_entity >= 0)
		ret = fimc_md_register_platform_entity(data, pdev,
						       plat_entity);
	put_device(dev);
	return 0;
724 725
}

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
/* Register FIMC, FIMC-LITE and CSIS media entities */
#ifdef CONFIG_OF
static int fimc_md_register_of_platform_entities(struct fimc_md *fmd,
						 struct device_node *parent)
{
	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;
		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);
		if (ret < 0)
			break;
	}

	return ret;
}
#else
#define fimc_md_register_of_platform_entities(fmd, node) (-ENOSYS)
#endif

765 766 767 768 769 770 771
static void fimc_md_unregister_entities(struct fimc_md *fmd)
{
	int i;

	for (i = 0; i < FIMC_MAX_DEVS; i++) {
		if (fmd->fimc[i] == NULL)
			continue;
772
		v4l2_device_unregister_subdev(&fmd->fimc[i]->vid_cap.subdev);
773
		fmd->fimc[i]->pipeline_ops = NULL;
774 775
		fmd->fimc[i] = NULL;
	}
776 777 778 779
	for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
		if (fmd->fimc_lite[i] == NULL)
			continue;
		v4l2_device_unregister_subdev(&fmd->fimc_lite[i]->subdev);
780
		fmd->fimc_lite[i]->pipeline_ops = NULL;
781 782
		fmd->fimc_lite[i] = NULL;
	}
783 784 785 786
	for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
		if (fmd->csis[i].sd == NULL)
			continue;
		v4l2_device_unregister_subdev(fmd->csis[i].sd);
787
		module_put(fmd->csis[i].sd->owner);
788 789 790 791 792 793 794 795
		fmd->csis[i].sd = NULL;
	}
	for (i = 0; i < fmd->num_sensors; i++) {
		if (fmd->sensor[i].subdev == NULL)
			continue;
		fimc_md_unregister_sensor(fmd->sensor[i].subdev);
		fmd->sensor[i].subdev = NULL;
	}
796
	v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
797 798 799 800 801 802 803 804
}

/**
 * __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
805
 * @link_mask: bitmask of the fimc devices for which link should be enabled
806
 */
807 808 809
static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
					    struct media_entity *source,
					    struct v4l2_subdev *sensor,
810
					    int pad, int link_mask)
811
{
812
	struct fimc_sensor_info *s_info = NULL;
813
	struct media_entity *sink;
814
	unsigned int flags = 0;
815
	int ret, i;
816 817 818

	for (i = 0; i < FIMC_MAX_DEVS; i++) {
		if (!fmd->fimc[i])
819
			continue;
820 821 822 823
		/*
		 * Some FIMC variants are not fitted with camera capture
		 * interface. Skip creating a link from sensor for those.
		 */
824
		if (!fmd->fimc[i]->variant->has_cam_if)
825 826
			continue;

827
		flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
828

829
		sink = &fmd->fimc[i]->vid_cap.subdev.entity;
830
		ret = media_entity_create_link(source, pad, sink,
831
					      FIMC_SD_PAD_SINK_CAM, flags);
832 833 834
		if (ret)
			return ret;

835 836 837 838 839 840
		/* Notify FIMC capture subdev entity */
		ret = media_entity_call(sink, link_setup, &sink->pads[0],
					&source->pads[pad], flags);
		if (ret)
			break;

841
		v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
842 843
			  source->name, flags ? '=' : '-', sink->name);

844
		if (flags == 0 || sensor == NULL)
845 846 847 848 849 850 851 852 853
			continue;
		s_info = v4l2_get_subdev_hostdata(sensor);
		if (!WARN_ON(s_info == NULL)) {
			unsigned long irq_flags;
			spin_lock_irqsave(&fmd->slock, irq_flags);
			s_info->host = fmd->fimc[i];
			spin_unlock_irqrestore(&fmd->slock, irq_flags);
		}
	}
854 855 856 857 858

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

859 860 861 862
		if (link_mask & (1 << (i + FIMC_MAX_DEVS)))
			flags = MEDIA_LNK_FL_ENABLED;
		else
			flags = 0;
863 864 865 866 867 868 869 870 871 872 873 874 875

		sink = &fmd->fimc_lite[i]->subdev.entity;
		ret = media_entity_create_link(source, pad, sink,
					       FLITE_SD_PAD_SINK, flags);
		if (ret)
			return ret;

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

876
		v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
877 878
			  source->name, flags ? '=' : '-', sink->name);
	}
879 880 881
	return 0;
}

882 883 884 885 886
/* 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;
	unsigned int flags = MEDIA_LNK_FL_ENABLED;
887
	int i, ret = 0;
888 889 890 891 892 893

	for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
		struct fimc_lite *fimc = fmd->fimc_lite[i];
		if (fimc == NULL)
			continue;
		source = &fimc->subdev.entity;
894
		sink = &fimc->vfd.entity;
895
		/* FIMC-LITE's subdev and video node */
896
		ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_DMA,
897 898 899 900 901 902 903 904 905
					       sink, 0, flags);
		if (ret)
			break;
		/* TODO: create links to other entities */
	}

	return ret;
}

906 907 908 909 910 911 912 913 914 915 916 917 918 919
/**
 * 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)
{
920
	struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
921
	struct v4l2_subdev *sensor, *csis;
922
	struct fimc_source_info *pdata;
923
	struct fimc_sensor_info *s_info;
924
	struct media_entity *source, *sink;
925 926
	int i, pad, fimc_id = 0, ret = 0;
	u32 flags, link_mask = 0;
927 928 929 930 931 932 933

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

		sensor = fmd->sensor[i].subdev;
		s_info = v4l2_get_subdev_hostdata(sensor);
934
		if (!s_info)
935 936 937
			continue;

		source = NULL;
938
		pdata = &s_info->pdata;
939

940 941
		switch (pdata->sensor_bus_type) {
		case FIMC_BUS_TYPE_MIPI_CSI2:
942 943 944 945 946 947 948 949
			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,
				 "MIPI-CSI interface specified "
				 "but s5p-csis module is not loaded!\n"))
950
				return -EINVAL;
951

952 953
			pad = sensor->entity.num_pads - 1;
			ret = media_entity_create_link(&sensor->entity, pad,
954 955 956 957 958 959
					      &csis->entity, CSIS_PAD_SINK,
					      MEDIA_LNK_FL_IMMUTABLE |
					      MEDIA_LNK_FL_ENABLED);
			if (ret)
				return ret;

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

963
			source = NULL;
964
			csi_sensors[pdata->mux_id] = sensor;
965 966
			break;

967
		case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
968 969 970 971 972 973
			source = &sensor->entity;
			pad = 0;
			break;

		default:
			v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
974
				 pdata->sensor_bus_type);
975 976 977 978 979
			return -EINVAL;
		}
		if (source == NULL)
			continue;

980
		link_mask = 1 << fimc_id++;
981
		ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
982
						       pad, link_mask);
983 984
	}

985
	for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
986 987 988 989
		if (fmd->csis[i].sd == NULL)
			continue;
		source = &fmd->csis[i].sd->entity;
		pad = CSIS_PAD_SOURCE;
990
		sensor = csi_sensors[i];
991

992
		link_mask = 1 << fimc_id++;
993
		ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
994
						       pad, link_mask);
995
	}
996

997 998 999 1000 1001
	/* 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;
1002
		source = &fmd->fimc[i]->vid_cap.subdev.entity;
1003
		sink = &fmd->fimc[i]->vid_cap.vfd.entity;
1004 1005 1006 1007 1008 1009
		ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
					      sink, 0, flags);
		if (ret)
			break;
	}

1010
	return __fimc_md_create_flite_source_links(fmd);
1011 1012 1013
}

/*
1014
 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
1015
 */
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
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_unprepare(fmd->camclk[i].clock);
		clk_put(fmd->camclk[i].clock);
		fmd->camclk[i].clock = ERR_PTR(-EINVAL);
	}
1027 1028 1029 1030 1031 1032 1033 1034

	/* 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);
	}
1035 1036
}

1037 1038
static int fimc_md_get_clocks(struct fimc_md *fmd)
{
1039
	struct device *dev = NULL;
1040 1041
	char clk_name[32];
	struct clk *clock;
1042 1043 1044 1045 1046 1047 1048
	int ret, i;

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

	if (fmd->pdev->dev.of_node)
		dev = &fmd->pdev->dev;
1049 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 1056 1057 1058 1059 1060 1061 1062 1063 1064
			dev_err(&fmd->pdev->dev, "Failed to get clock: %s\n",
								clk_name);
			ret = PTR_ERR(clock);
			break;
		}
		ret = clk_prepare(clock);
		if (ret < 0) {
			clk_put(clock);
			fmd->camclk[i].clock = ERR_PTR(-EINVAL);
			break;
1065 1066 1067
		}
		fmd->camclk[i].clock = clock;
	}
1068 1069
	if (ret)
		fimc_md_put_clocks(fmd);
1070

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
	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);

1093
	return ret;
1094 1095 1096
}

static int __fimc_md_set_camclk(struct fimc_md *fmd,
1097 1098
				struct fimc_sensor_info *s_info,
				bool on)
1099
{
1100
	struct fimc_source_info *pdata = &s_info->pdata;
1101 1102 1103
	struct fimc_camclk_info *camclk;
	int ret = 0;

1104
	if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || !fmd || !fmd->pmf)
1105 1106 1107 1108
		return -EINVAL;

	camclk = &fmd->camclk[pdata->clk_id];

1109 1110
	dbg("camclk %d, f: %lu, use_count: %d, on: %d",
	    pdata->clk_id, pdata->clk_frequency, camclk->use_count, on);
1111 1112 1113 1114 1115 1116 1117 1118 1119

	if (on) {
		if (camclk->use_count > 0 &&
		    camclk->frequency != pdata->clk_frequency)
			return -EINVAL;

		if (camclk->use_count++ == 0) {
			clk_set_rate(camclk->clock, pdata->clk_frequency);
			camclk->frequency = pdata->clk_frequency;
1120 1121 1122
			ret = pm_runtime_get_sync(fmd->pmf);
			if (ret < 0)
				return ret;
1123
			ret = clk_enable(camclk->clock);
1124 1125
			dbg("Enabled camclk %d: f: %lu", pdata->clk_id,
			    clk_get_rate(camclk->clock));
1126 1127 1128 1129 1130 1131 1132 1133 1134
		}
		return ret;
	}

	if (WARN_ON(camclk->use_count == 0))
		return 0;

	if (--camclk->use_count == 0) {
		clk_disable(camclk->clock);
1135
		pm_runtime_put(fmd->pmf);
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
		dbg("Disabled camclk %d", pdata->clk_id);
	}
	return ret;
}

/**
 * fimc_md_set_camclk - peripheral sensor clock setup
 * @sd: sensor subdev to configure sclk_cam clock for
 * @on: 1 to enable or 0 to disable the clock
 *
 * There are 2 separate clock outputs available in the SoC for external
 * image processors. These clocks are shared between all registered FIMC
 * devices to which sensors can be attached, either directly or through
 * the MIPI CSI receiver. The clock is allowed here to be used by
 * multiple sensors concurrently if they use same frequency.
 * This function should only be called when the graph mutex is held.
 */
int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on)
{
	struct fimc_sensor_info *s_info = v4l2_get_subdev_hostdata(sd);
	struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity);

	return __fimc_md_set_camclk(fmd, s_info, on);
}

static int fimc_md_link_notify(struct media_pad *source,
			       struct media_pad *sink, u32 flags)
{
1164 1165
	struct fimc_lite *fimc_lite = NULL;
	struct fimc_dev *fimc = NULL;
1166
	struct fimc_pipeline *pipeline;
1167
	struct v4l2_subdev *sd;
1168
	struct mutex *lock;
1169
	int i, ret = 0;
1170
	int ref_count;
1171

1172
	if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1173 1174
		return 0;

1175
	sd = media_entity_to_v4l2_subdev(sink->entity);
1176

1177
	switch (sd->grp_id) {
1178
	case GRP_ID_FLITE:
1179
		fimc_lite = v4l2_get_subdevdata(sd);
1180 1181
		if (WARN_ON(fimc_lite == NULL))
			return 0;
1182
		pipeline = &fimc_lite->pipeline;
1183
		lock = &fimc_lite->lock;
1184
		break;
1185
	case GRP_ID_FIMC:
1186
		fimc = v4l2_get_subdevdata(sd);
1187 1188
		if (WARN_ON(fimc == NULL))
			return 0;
1189
		pipeline = &fimc->pipeline;
1190
		lock = &fimc->lock;
1191 1192 1193 1194
		break;
	default:
		return 0;
	}
1195

1196 1197 1198
	mutex_lock(lock);
	ref_count = fimc ? fimc->vid_cap.refcnt : fimc_lite->ref_count;

1199
	if (!(flags & MEDIA_LNK_FL_ENABLED)) {
1200 1201 1202 1203 1204
		if (ref_count > 0) {
			ret = __fimc_pipeline_close(pipeline);
			if (!ret && fimc)
				fimc_ctrls_delete(fimc->vid_cap.ctx);
		}
1205 1206
		for (i = 0; i < IDX_MAX; i++)
			pipeline->subdevs[i] = NULL;
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
	} else if (ref_count > 0) {
		/*
		 * Link activation. Enable power of pipeline elements only if
		 * the pipeline is already in use, i.e. its video node is open.
		 * Recreate the controls destroyed during the link deactivation.
		 */
		ret = __fimc_pipeline_open(pipeline,
					   source->entity, true);
		if (!ret && fimc)
			ret = fimc_capture_ctrls_create(fimc);
1217
	}
1218 1219

	mutex_unlock(lock);
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
	return ret ? -EPIPE : ret;
}

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);

1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
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;
}

1287
static int fimc_md_probe(struct platform_device *pdev)
1288
{
1289
	struct device *dev = &pdev->dev;
1290 1291 1292 1293
	struct v4l2_device *v4l2_dev;
	struct fimc_md *fmd;
	int ret;

1294
	fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
1295 1296 1297 1298 1299 1300 1301 1302 1303
	if (!fmd)
		return -ENOMEM;

	spin_lock_init(&fmd->slock);
	fmd->pdev = pdev;

	strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
		sizeof(fmd->media_dev.model));
	fmd->media_dev.link_notify = fimc_md_link_notify;
1304
	fmd->media_dev.dev = dev;
1305 1306 1307

	v4l2_dev = &fmd->v4l2_dev;
	v4l2_dev->mdev = &fmd->media_dev;
1308
	v4l2_dev->notify = fimc_sensor_notify;
1309
	strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
1310

1311
	ret = v4l2_device_register(dev, &fmd->v4l2_dev);
1312 1313
	if (ret < 0) {
		v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
1314
		return ret;
1315 1316 1317 1318
	}
	ret = media_device_register(&fmd->media_dev);
	if (ret < 0) {
		v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
1319
		goto err_md;
1320 1321 1322
	}
	ret = fimc_md_get_clocks(fmd);
	if (ret)
1323
		goto err_clk;
1324

1325
	fmd->user_subdev_api = (dev->of_node != NULL);
1326 1327 1328 1329

	/* Protect the media graph while we're registering entities */
	mutex_lock(&fmd->media_dev.graph_mutex);

1330 1331 1332 1333 1334 1335 1336
	ret = fimc_md_get_pinctrl(fmd);
	if (ret < 0) {
		if (ret != EPROBE_DEFER)
			dev_err(dev, "Failed to get pinctrl: %d\n", ret);
		goto err_unlock;
	}

1337 1338 1339 1340 1341
	if (dev->of_node)
		ret = fimc_md_register_of_platform_entities(fmd, dev->of_node);
	else
		ret = bus_for_each_dev(&platform_bus_type, NULL, fmd,
						fimc_md_pdev_match);
1342
	if (ret)
1343
		goto err_unlock;
1344

1345
	if (dev->platform_data || dev->of_node) {
1346 1347
		ret = fimc_md_register_sensor_entities(fmd);
		if (ret)
1348
			goto err_unlock;
1349
	}
1350

1351 1352
	ret = fimc_md_create_links(fmd);
	if (ret)
1353
		goto err_unlock;
1354 1355
	ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
	if (ret)
1356
		goto err_unlock;
1357 1358

	ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
	if (ret)
		goto err_unlock;

	platform_set_drvdata(pdev, fmd);
	mutex_unlock(&fmd->media_dev.graph_mutex);
	return 0;

err_unlock:
	mutex_unlock(&fmd->media_dev.graph_mutex);
err_clk:
1369 1370 1371
	media_device_unregister(&fmd->media_dev);
	fimc_md_put_clocks(fmd);
	fimc_md_unregister_entities(fmd);
1372
err_md:
1373 1374 1375 1376
	v4l2_device_unregister(&fmd->v4l2_dev);
	return ret;
}

1377
static int fimc_md_remove(struct platform_device *pdev)
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
{
	struct fimc_md *fmd = platform_get_drvdata(pdev);

	if (!fmd)
		return 0;
	device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
	fimc_md_unregister_entities(fmd);
	media_device_unregister(&fmd->media_dev);
	fimc_md_put_clocks(fmd);
	return 0;
}

1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
static struct platform_device_id fimc_driver_ids[] __always_unused = {
	{ .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);

1402 1403
static struct platform_driver fimc_md_driver = {
	.probe		= fimc_md_probe,
1404
	.remove		= fimc_md_remove,
1405
	.driver = {
1406 1407 1408
		.of_match_table = of_match_ptr(fimc_md_of_match),
		.name		= "s5p-fimc-md",
		.owner		= THIS_MODULE,
1409 1410 1411
	}
};

1412
static int __init fimc_md_init(void)
1413 1414
{
	int ret;
1415

1416 1417 1418 1419
	request_module("s5p-csis");
	ret = fimc_register_driver();
	if (ret)
		return ret;
1420

1421 1422
	return platform_driver_register(&fimc_md_driver);
}
1423 1424

static void __exit fimc_md_exit(void)
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
{
	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");