fimc-mdevice.c 26.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * S5P/EXYNOS4 SoC series camera host interface media device driver
 *
 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
 * Contact: Sylwester Nawrocki, <s.nawrocki@samsung.com>
 *
 * 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>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/types.h>
#include <linux/slab.h>
24
#include <media/v4l2-ctrls.h>
25 26 27
#include <media/media-device.h>

#include "fimc-core.h"
28
#include "fimc-lite.h"
29 30 31 32 33 34 35 36 37 38 39 40
#include "fimc-mdevice.h"
#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
 * @fimc: fimc device terminating the pipeline
 *
 * Caller holds the graph mutex.
 */
41
void fimc_pipeline_prepare(struct fimc_pipeline *p, struct media_entity *me)
42
{
43
	struct media_pad *pad = &me->pads[0];
44
	struct v4l2_subdev *sd;
45
	int i;
46

47 48
	for (i = 0; i < IDX_MAX; i++)
		p->subdevs[i] = NULL;
49

50 51 52 53 54 55 56 57 58
	while (1) {
		if (!(pad->flags & MEDIA_PAD_FL_SINK))
			break;

		/* source pad */
		pad = media_entity_remote_source(pad);
		if (pad == NULL ||
		    media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
			break;
59

60 61 62 63 64 65 66 67 68
		sd = media_entity_to_v4l2_subdev(pad->entity);

		switch (sd->grp_id) {
		case SENSOR_GROUP_ID:
			p->subdevs[IDX_SENSOR] = sd;
			break;
		case CSIS_GROUP_ID:
			p->subdevs[IDX_CSIS] = sd;
			break;
69 70 71
		case FLITE_GROUP_ID:
			p->subdevs[IDX_FLITE] = sd;
			break;
72 73 74 75 76 77 78 79 80
		case FIMC_GROUP_ID:
			/* No need to control FIMC subdev through subdev ops */
			break;
		default:
			pr_warn("%s: Unknown subdev grp_id: %#x\n",
				__func__, sd->grp_id);
		}
		/* sink pad */
		pad = &sd->entity.pads[0];
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	}
}

/**
 * __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
113
 * @state: true to power on, false to power off
114
 *
115
 * Needs to be called with the graph mutex held.
116
 */
117
int fimc_pipeline_s_power(struct fimc_pipeline *p, bool state)
118
{
119 120
	unsigned int i;
	int ret;
121

122
	if (p->subdevs[IDX_SENSOR] == NULL)
123 124
		return -ENXIO;

125 126 127 128 129
	for (i = 0; i < IDX_MAX; i++) {
		unsigned int idx = state ? (IDX_MAX - 1) - i : i;

		ret = __subdev_set_power(p->subdevs[idx], state);
		if (ret < 0 && ret != -ENXIO)
130 131 132
			return ret;
	}

133
	return 0;
134 135 136 137 138 139 140 141 142 143
}

/**
 * __fimc_pipeline_initialize - update the pipeline information, enable power
 *                              of all pipeline subdevs and the sensor clock
 * @me: media entity to start graph walk with
 * @prep: true to acquire sensor (and csis) subdevs
 *
 * This function must be called with the graph mutex held.
 */
144
static int __fimc_pipeline_initialize(struct fimc_pipeline *p,
145 146 147 148 149
				      struct media_entity *me, bool prep)
{
	int ret;

	if (prep)
150 151 152
		fimc_pipeline_prepare(p, me);

	if (p->subdevs[IDX_SENSOR] == NULL)
153
		return -EINVAL;
154 155

	ret = fimc_md_set_camclk(p->subdevs[IDX_SENSOR], true);
156 157
	if (ret)
		return ret;
158 159

	return fimc_pipeline_s_power(p, 1);
160 161
}

162
int fimc_pipeline_initialize(struct fimc_pipeline *p, struct media_entity *me,
163 164 165 166 167
			     bool prep)
{
	int ret;

	mutex_lock(&me->parent->graph_mutex);
168
	ret =  __fimc_pipeline_initialize(p, me, prep);
169 170 171 172
	mutex_unlock(&me->parent->graph_mutex);

	return ret;
}
173
EXPORT_SYMBOL_GPL(fimc_pipeline_initialize);
174 175 176 177 178 179 180 181 182

/**
 * __fimc_pipeline_shutdown - disable the sensor clock and pipeline power
 * @fimc: fimc device terminating the pipeline
 *
 * Disable power of all subdevs in the pipeline and turn off the external
 * sensor clock.
 * Called with the graph mutex held.
 */
183
static int __fimc_pipeline_shutdown(struct fimc_pipeline *p)
184 185 186
{
	int ret = 0;

187 188 189
	if (p->subdevs[IDX_SENSOR]) {
		ret = fimc_pipeline_s_power(p, 0);
		fimc_md_set_camclk(p->subdevs[IDX_SENSOR], false);
190 191 192 193
	}
	return ret == -ENXIO ? 0 : ret;
}

194
int fimc_pipeline_shutdown(struct fimc_pipeline *p)
195
{
196
	struct media_entity *me;
197 198
	int ret;

199 200 201 202
	if (!p || !p->subdevs[IDX_SENSOR])
		return -EINVAL;

	me = &p->subdevs[IDX_SENSOR]->entity;
203
	mutex_lock(&me->parent->graph_mutex);
204
	ret = __fimc_pipeline_shutdown(p);
205 206 207 208
	mutex_unlock(&me->parent->graph_mutex);

	return ret;
}
209
EXPORT_SYMBOL_GPL(fimc_pipeline_shutdown);
210 211 212

/**
 * fimc_pipeline_s_stream - invoke s_stream on pipeline subdevs
213
 * @pipeline: video pipeline structure
214 215
 * @on: passed as the s_stream call argument
 */
216
int fimc_pipeline_s_stream(struct fimc_pipeline *p, bool on)
217
{
218
	int i, ret;
219

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

223 224 225 226 227 228 229 230 231 232 233
	for (i = 0; i < IDX_MAX; i++) {
		unsigned int idx = on ? (IDX_MAX - 1) - i : i;

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

		if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
			return ret;
	}

	return 0;

234
}
235
EXPORT_SYMBOL_GPL(fimc_pipeline_s_stream);
236 237 238 239 240 241 242 243 244 245 246 247 248 249

/*
 * 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;

	adapter = i2c_get_adapter(s_info->pdata->i2c_bus_num);
250 251 252 253 254 255
	if (!adapter) {
		v4l2_warn(&fmd->v4l2_dev,
			  "Failed to get I2C adapter %d, deferring probe\n",
			  s_info->pdata->i2c_bus_num);
		return ERR_PTR(-EPROBE_DEFER);
	}
256 257 258
	sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
				       s_info->pdata->board_info, NULL);
	if (IS_ERR_OR_NULL(sd)) {
259
		i2c_put_adapter(adapter);
260 261 262 263
		v4l2_warn(&fmd->v4l2_dev,
			  "Failed to acquire subdev %s, deferring probe\n",
			  s_info->pdata->board_info->type);
		return ERR_PTR(-EPROBE_DEFER);
264 265 266 267 268 269 270 271 272 273 274 275
	}
	v4l2_set_subdev_hostdata(sd, s_info);
	sd->grp_id = SENSOR_GROUP_ID;

	v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
		  s_info->pdata->board_info->type);
	return sd;
}

static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
276
	struct i2c_adapter *adapter;
277 278 279 280

	if (!client)
		return;
	v4l2_device_unregister_subdev(sd);
281
	adapter = client->adapter;
282
	i2c_unregister_device(client);
283 284
	if (adapter)
		i2c_put_adapter(adapter);
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
}

static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
{
	struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
	struct fimc_dev *fd = NULL;
	int num_clients, ret, i;

	/*
	 * Runtime resume one of the FIMC entities to make sure
	 * the sclk_cam clocks are not globally disabled.
	 */
	for (i = 0; !fd && i < ARRAY_SIZE(fmd->fimc); i++)
		if (fmd->fimc[i])
			fd = fmd->fimc[i];
	if (!fd)
		return -ENXIO;
	ret = pm_runtime_get_sync(&fd->pdev->dev);
	if (ret < 0)
		return ret;

	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;
	for (i = 0; i < num_clients; i++) {
311 312
		struct v4l2_subdev *sd;

313 314 315 316
		fmd->sensor[i].pdata = &pdata->isp_info[i];
		ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true);
		if (ret)
			break;
317
		sd = fimc_md_register_sensor(fmd, &fmd->sensor[i]);
318
		ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false);
319 320 321 322 323 324 325 326

		if (!IS_ERR(sd)) {
			fmd->sensor[i].subdev = sd;
		} else {
			fmd->sensor[i].subdev = NULL;
			ret = PTR_ERR(sd);
			break;
		}
327 328 329 330 331 332 333 334 335 336 337 338 339
		if (ret)
			break;
	}
	pm_runtime_put(&fd->pdev->dev);
	return ret;
}

/*
 * MIPI CSIS and FIMC platform devices registration.
 */
static int fimc_register_callback(struct device *dev, void *p)
{
	struct fimc_dev *fimc = dev_get_drvdata(dev);
340
	struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
341
	struct fimc_md *fmd = p;
342
	int ret = 0;
343 344 345

	if (!fimc || !fimc->pdev)
		return 0;
346

347 348 349 350
	if (fimc->pdev->id < 0 || fimc->pdev->id >= FIMC_MAX_DEVS)
		return 0;

	fmd->fimc[fimc->pdev->id] = fimc;
351 352 353 354 355 356 357 358
	sd->grp_id = FIMC_GROUP_ID;

	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
	if (ret) {
		v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
			 fimc->id, ret);
	}

359 360 361
	return ret;
}

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
static int fimc_lite_register_callback(struct device *dev, void *p)
{
	struct fimc_lite *fimc = dev_get_drvdata(dev);
	struct v4l2_subdev *sd = &fimc->subdev;
	struct fimc_md *fmd = p;
	int ret;

	if (fimc == NULL)
		return 0;

	if (fimc->index >= FIMC_LITE_MAX_DEVS)
		return 0;

	fmd->fimc_lite[fimc->index] = fimc;
	sd->grp_id = FLITE_GROUP_ID;

	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
	if (ret) {
		v4l2_err(&fmd->v4l2_dev,
			 "Failed to register FIMC-LITE.%d (%d)\n",
			 fimc->index, ret);
	}
	return ret;
}

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
static int csis_register_callback(struct device *dev, void *p)
{
	struct v4l2_subdev *sd = dev_get_drvdata(dev);
	struct platform_device *pdev;
	struct fimc_md *fmd = p;
	int id, ret;

	if (!sd)
		return 0;
	pdev = v4l2_get_subdevdata(sd);
	if (!pdev || pdev->id < 0 || pdev->id >= CSIS_MAX_ENTITIES)
		return 0;
	v4l2_info(sd, "csis%d sd: %s\n", pdev->id, sd->name);

	id = pdev->id < 0 ? 0 : pdev->id;
	fmd->csis[id].sd = sd;
	sd->grp_id = CSIS_GROUP_ID;
	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
	if (ret)
		v4l2_err(&fmd->v4l2_dev,
			 "Failed to register CSIS subdevice: %d\n", ret);
	return ret;
}

/**
 * fimc_md_register_platform_entities - register FIMC and CSIS media entities
 */
static int fimc_md_register_platform_entities(struct fimc_md *fmd)
{
416
	struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
417
	struct device_driver *driver;
418
	int ret, i;
419 420

	driver = driver_find(FIMC_MODULE_NAME, &platform_bus_type);
421 422 423 424 425 426 427
	if (!driver) {
		v4l2_warn(&fmd->v4l2_dev,
			 "%s driver not found, deffering probe\n",
			 FIMC_MODULE_NAME);
		return -EPROBE_DEFER;
	}

428 429 430 431
	ret = driver_for_each_device(driver, NULL, fmd,
				     fimc_register_callback);
	if (ret)
		return ret;
432 433 434 435 436 437 438 439 440

	driver = driver_find(FIMC_LITE_DRV_NAME, &platform_bus_type);
	if (driver && try_module_get(driver->owner)) {
		ret = driver_for_each_device(driver, NULL, fmd,
					     fimc_lite_register_callback);
		if (ret)
			return ret;
		module_put(driver->owner);
	}
441 442 443 444
	/*
	 * Check if there is any sensor on the MIPI-CSI2 bus and
	 * if not skip the s5p-csis module loading.
	 */
445 446
	if (pdata == NULL)
		return 0;
447 448 449 450 451 452 453 454
	for (i = 0; i < pdata->num_clients; i++) {
		if (pdata->isp_info[i].bus_type == FIMC_MIPI_CSI2) {
			ret = 1;
			break;
		}
	}
	if (!ret)
		return 0;
455 456

	driver = driver_find(CSIS_DRIVER_NAME, &platform_bus_type);
457 458 459 460 461 462 463 464 465
	if (!driver || !try_module_get(driver->owner)) {
		v4l2_warn(&fmd->v4l2_dev,
			 "%s driver not found, deffering probe\n",
			 CSIS_DRIVER_NAME);
		return -EPROBE_DEFER;
	}

	return driver_for_each_device(driver, NULL, fmd,
				      csis_register_callback);
466 467 468 469 470 471 472 473 474
}

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;
475
		v4l2_device_unregister_subdev(&fmd->fimc[i]->vid_cap.subdev);
476 477
		fmd->fimc[i] = NULL;
	}
478 479 480 481 482 483
	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);
		fmd->fimc_lite[i] = NULL;
	}
484 485 486 487
	for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
		if (fmd->csis[i].sd == NULL)
			continue;
		v4l2_device_unregister_subdev(fmd->csis[i].sd);
488
		module_put(fmd->csis[i].sd->owner);
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
		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;
	}
}

/**
 * __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
505
 * @link_mask: bitmask of the fimc devices for which link should be enabled
506
 */
507 508 509
static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
					    struct media_entity *source,
					    struct v4l2_subdev *sensor,
510
					    int pad, int link_mask)
511 512 513
{
	struct fimc_sensor_info *s_info;
	struct media_entity *sink;
514
	unsigned int flags = 0;
515
	int ret, i;
516 517 518

	for (i = 0; i < FIMC_MAX_DEVS; i++) {
		if (!fmd->fimc[i])
519
			continue;
520 521 522 523
		/*
		 * Some FIMC variants are not fitted with camera capture
		 * interface. Skip creating a link from sensor for those.
		 */
524
		if (!fmd->fimc[i]->variant->has_cam_if)
525 526
			continue;

527
		flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
528

529
		sink = &fmd->fimc[i]->vid_cap.subdev.entity;
530 531
		ret = media_entity_create_link(source, pad, sink,
					      FIMC_SD_PAD_SINK, flags);
532 533 534
		if (ret)
			return ret;

535 536 537 538 539 540
		/* Notify FIMC capture subdev entity */
		ret = media_entity_call(sink, link_setup, &sink->pads[0],
					&source->pads[pad], flags);
		if (ret)
			break;

541 542 543
		v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]",
			  source->name, flags ? '=' : '-', sink->name);

544
		if (flags == 0 || sensor == NULL)
545 546 547 548 549 550 551 552 553
			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);
		}
	}
554 555 556 557 558

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

559 560 561 562
		if (link_mask & (1 << (i + FIMC_MAX_DEVS)))
			flags = MEDIA_LNK_FL_ENABLED;
		else
			flags = 0;
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578

		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;

		v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]",
			  source->name, flags ? '=' : '-', sink->name);
	}
579 580 581
	return 0;
}

582 583 584 585 586 587 588 589 590 591 592 593
/* 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;
	int i, ret;

	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;
594
		sink = &fimc->vfd.entity;
595 596 597 598 599 600 601 602 603 604 605
		/* FIMC-LITE's subdev and video node */
		ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
					       sink, 0, flags);
		if (ret)
			break;
		/* TODO: create links to other entities */
	}

	return ret;
}

606 607 608 609 610 611 612 613 614 615 616 617 618 619
/**
 * 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)
{
620
	struct v4l2_subdev *csi_sensors[2] = { NULL };
621 622 623
	struct v4l2_subdev *sensor, *csis;
	struct s5p_fimc_isp_info *pdata;
	struct fimc_sensor_info *s_info;
624
	struct media_entity *source, *sink;
625 626
	int i, pad, fimc_id = 0, ret = 0;
	u32 flags, link_mask = 0;
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649

	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);
		if (!s_info || !s_info->pdata)
			continue;

		source = NULL;
		pdata = s_info->pdata;

		switch (pdata->bus_type) {
		case FIMC_MIPI_CSI2:
			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"))
650
				return -EINVAL;
651 652 653 654 655 656 657 658 659 660 661

			ret = media_entity_create_link(&sensor->entity, 0,
					      &csis->entity, CSIS_PAD_SINK,
					      MEDIA_LNK_FL_IMMUTABLE |
					      MEDIA_LNK_FL_ENABLED);
			if (ret)
				return ret;

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

662
			source = NULL;
663
			csi_sensors[pdata->mux_id] = sensor;
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
			break;

		case FIMC_ITU_601...FIMC_ITU_656:
			source = &sensor->entity;
			pad = 0;
			break;

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

679
		link_mask = 1 << fimc_id++;
680
		ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
681
						       pad, link_mask);
682 683 684 685 686 687 688
	}

	for (i = 0; i < ARRAY_SIZE(fmd->csis); i++) {
		if (fmd->csis[i].sd == NULL)
			continue;
		source = &fmd->csis[i].sd->entity;
		pad = CSIS_PAD_SOURCE;
689
		sensor = csi_sensors[i];
690

691
		link_mask = 1 << fimc_id++;
692
		ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
693
						       pad, link_mask);
694
	}
695

696 697 698 699 700
	/* 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;
701
		source = &fmd->fimc[i]->vid_cap.subdev.entity;
702
		sink = &fmd->fimc[i]->vid_cap.vfd.entity;
703 704 705 706 707 708
		ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
					      sink, 0, flags);
		if (ret)
			break;
	}

709
	return __fimc_md_create_flite_source_links(fmd);
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
}

/*
 * The peripheral sensor clock management.
 */
static int fimc_md_get_clocks(struct fimc_md *fmd)
{
	char clk_name[32];
	struct clk *clock;
	int i;

	for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
		snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
		clock = clk_get(NULL, clk_name);
		if (IS_ERR_OR_NULL(clock)) {
			v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s",
				  clk_name);
			return -ENXIO;
		}
		fmd->camclk[i].clock = clock;
	}
	return 0;
}

static void fimc_md_put_clocks(struct fimc_md *fmd)
{
	int i = FIMC_MAX_CAMCLKS;

	while (--i >= 0) {
		if (IS_ERR_OR_NULL(fmd->camclk[i].clock))
			continue;
		clk_put(fmd->camclk[i].clock);
		fmd->camclk[i].clock = NULL;
	}
}

static int __fimc_md_set_camclk(struct fimc_md *fmd,
747 748
				struct fimc_sensor_info *s_info,
				bool on)
749 750 751 752 753 754 755 756 757 758
{
	struct s5p_fimc_isp_info *pdata = s_info->pdata;
	struct fimc_camclk_info *camclk;
	int ret = 0;

	if (WARN_ON(pdata->clk_id >= FIMC_MAX_CAMCLKS) || fmd == NULL)
		return -EINVAL;

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

759 760
	dbg("camclk %d, f: %lu, use_count: %d, on: %d",
	    pdata->clk_id, pdata->clk_frequency, camclk->use_count, on);
761 762 763 764 765 766 767 768 769 770

	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;
			ret = clk_enable(camclk->clock);
771 772
			dbg("Enabled camclk %d: f: %lu", pdata->clk_id,
			    clk_get_rate(camclk->clock));
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
		}
		return ret;
	}

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

	if (--camclk->use_count == 0) {
		clk_disable(camclk->clock);
		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)
{
810 811
	struct fimc_lite *fimc_lite = NULL;
	struct fimc_dev *fimc = NULL;
812
	struct fimc_pipeline *pipeline;
813
	struct v4l2_subdev *sd;
814 815
	int ret = 0;

816
	if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
817 818
		return 0;

819
	sd = media_entity_to_v4l2_subdev(sink->entity);
820

821
	switch (sd->grp_id) {
822 823 824 825
	case FLITE_GROUP_ID:
		fimc_lite = v4l2_get_subdevdata(sd);
		pipeline = &fimc_lite->pipeline;
		break;
826 827 828 829 830 831 832
	case FIMC_GROUP_ID:
		fimc = v4l2_get_subdevdata(sd);
		pipeline = &fimc->pipeline;
		break;
	default:
		return 0;
	}
833

834 835 836 837 838 839 840 841 842 843
	if (!(flags & MEDIA_LNK_FL_ENABLED)) {
		ret = __fimc_pipeline_shutdown(pipeline);
		pipeline->subdevs[IDX_SENSOR] = NULL;
		pipeline->subdevs[IDX_CSIS] = NULL;

		if (fimc) {
			mutex_lock(&fimc->lock);
			fimc_ctrls_delete(fimc->vid_cap.ctx);
			mutex_unlock(&fimc->lock);
		}
844 845 846 847 848
		return ret;
	}
	/*
	 * Link activation. Enable power of pipeline elements only if the
	 * pipeline is already in use, i.e. its video node is opened.
849
	 * Recreate the controls destroyed during the link deactivation.
850
	 */
851 852 853
	if (fimc) {
		mutex_lock(&fimc->lock);
		if (fimc->vid_cap.refcnt > 0) {
854 855
			ret = __fimc_pipeline_initialize(pipeline,
							 source->entity, true);
856 857
		if (!ret)
			ret = fimc_capture_ctrls_create(fimc);
858 859 860 861 862 863 864 865 866
		}
		mutex_unlock(&fimc->lock);
	} else {
		mutex_lock(&fimc_lite->lock);
		if (fimc_lite->ref_count > 0) {
			ret = __fimc_pipeline_initialize(pipeline,
							 source->entity, true);
		}
		mutex_unlock(&fimc_lite->lock);
867
	}
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
	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);

916
static int fimc_md_probe(struct platform_device *pdev)
917 918 919 920 921
{
	struct v4l2_device *v4l2_dev;
	struct fimc_md *fmd;
	int ret;

922
	fmd = devm_kzalloc(&pdev->dev, sizeof(*fmd), GFP_KERNEL);
923 924 925 926 927 928 929 930 931 932 933 934 935
	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;
	fmd->media_dev.dev = &pdev->dev;

	v4l2_dev = &fmd->v4l2_dev;
	v4l2_dev->mdev = &fmd->media_dev;
936
	v4l2_dev->notify = fimc_sensor_notify;
937 938 939 940 941 942
	snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s",
		 dev_name(&pdev->dev));

	ret = v4l2_device_register(&pdev->dev, &fmd->v4l2_dev);
	if (ret < 0) {
		v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
943
		return ret;
944 945 946 947
	}
	ret = media_device_register(&fmd->media_dev);
	if (ret < 0) {
		v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
948
		goto err_md;
949 950 951
	}
	ret = fimc_md_get_clocks(fmd);
	if (ret)
952
		goto err_clk;
953 954

	fmd->user_subdev_api = false;
955 956 957 958

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

959 960
	ret = fimc_md_register_platform_entities(fmd);
	if (ret)
961
		goto err_unlock;
962

963 964 965
	if (pdev->dev.platform_data) {
		ret = fimc_md_register_sensor_entities(fmd);
		if (ret)
966
			goto err_unlock;
967
	}
968 969
	ret = fimc_md_create_links(fmd);
	if (ret)
970
		goto err_unlock;
971 972
	ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
	if (ret)
973
		goto err_unlock;
974 975

	ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
976 977 978 979 980 981 982 983 984 985
	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:
986 987 988
	media_device_unregister(&fmd->media_dev);
	fimc_md_put_clocks(fmd);
	fimc_md_unregister_entities(fmd);
989
err_md:
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	v4l2_device_unregister(&fmd->v4l2_dev);
	return ret;
}

static int __devexit fimc_md_remove(struct platform_device *pdev)
{
	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;
}

static struct platform_driver fimc_md_driver = {
	.probe		= fimc_md_probe,
	.remove		= __devexit_p(fimc_md_remove),
	.driver = {
		.name	= "s5p-fimc-md",
		.owner	= THIS_MODULE,
	}
};

1016
static int __init fimc_md_init(void)
1017 1018
{
	int ret;
1019

1020 1021 1022 1023
	request_module("s5p-csis");
	ret = fimc_register_driver();
	if (ret)
		return ret;
1024

1025 1026
	return platform_driver_register(&fimc_md_driver);
}
1027 1028

static void __exit fimc_md_exit(void)
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
{
	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");