fimc-mdevice.c 26.4 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 20 21 22 23
 *
 * 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
#include <media/media-device.h>
26
#include <media/s5p_fimc.h>
27 28

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

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

52 53 54 55 56 57 58 59 60
	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;
61

62 63 64
		sd = media_entity_to_v4l2_subdev(pad->entity);

		switch (sd->grp_id) {
65 66
		case GRP_ID_FIMC_IS_SENSOR:
		case GRP_ID_SENSOR:
67 68
			p->subdevs[IDX_SENSOR] = sd;
			break;
69
		case GRP_ID_CSIS:
70 71
			p->subdevs[IDX_CSIS] = sd;
			break;
72
		case GRP_ID_FLITE:
73 74
			p->subdevs[IDX_FLITE] = sd;
			break;
75
		case GRP_ID_FIMC:
76 77 78 79 80 81 82 83
			/* 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];
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 113 114 115
	}
}

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

125
	if (p->subdevs[IDX_SENSOR] == NULL)
126 127
		return -ENXIO;

128 129 130 131 132
	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)
133 134 135
			return ret;
	}

136
	return 0;
137 138 139
}

/**
140 141
 * __fimc_pipeline_open - update the pipeline information, enable power
 *                        of all pipeline subdevs and the sensor clock
142 143 144
 * @me: media entity to start graph walk with
 * @prep: true to acquire sensor (and csis) subdevs
 *
145
 * Called with the graph mutex held.
146
 */
147 148
static int __fimc_pipeline_open(struct fimc_pipeline *p,
				struct media_entity *me, bool prep)
149 150 151 152
{
	int ret;

	if (prep)
153 154 155
		fimc_pipeline_prepare(p, me);

	if (p->subdevs[IDX_SENSOR] == NULL)
156
		return -EINVAL;
157 158

	ret = fimc_md_set_camclk(p->subdevs[IDX_SENSOR], true);
159 160
	if (ret)
		return ret;
161 162

	return fimc_pipeline_s_power(p, 1);
163 164 165
}

/**
166
 * __fimc_pipeline_close - disable the sensor clock and pipeline power
167 168
 * @fimc: fimc device terminating the pipeline
 *
169
 * Disable power of all subdevs and turn the external sensor clock off.
170
 */
171
static int __fimc_pipeline_close(struct fimc_pipeline *p)
172 173 174
{
	int ret = 0;

175 176 177
	if (!p || !p->subdevs[IDX_SENSOR])
		return -EINVAL;

178 179 180
	if (p->subdevs[IDX_SENSOR]) {
		ret = fimc_pipeline_s_power(p, 0);
		fimc_md_set_camclk(p->subdevs[IDX_SENSOR], false);
181 182 183 184 185
	}
	return ret == -ENXIO ? 0 : ret;
}

/**
186
 * __fimc_pipeline_s_stream - invoke s_stream on pipeline subdevs
187
 * @pipeline: video pipeline structure
188 189
 * @on: passed as the s_stream call argument
 */
190
static int __fimc_pipeline_s_stream(struct fimc_pipeline *p, bool on)
191
{
192
	int i, ret;
193

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

197 198 199 200 201 202 203 204 205 206 207
	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;

208
}
209 210 211

/* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
static const struct fimc_pipeline_ops fimc_pipeline_ops = {
212 213 214
	.open		= __fimc_pipeline_open,
	.close		= __fimc_pipeline_close,
	.set_stream	= __fimc_pipeline_s_stream,
215
};
216 217 218 219 220 221 222 223 224 225 226 227 228

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

229
	adapter = i2c_get_adapter(s_info->pdata.i2c_bus_num);
230 231 232
	if (!adapter) {
		v4l2_warn(&fmd->v4l2_dev,
			  "Failed to get I2C adapter %d, deferring probe\n",
233
			  s_info->pdata.i2c_bus_num);
234 235
		return ERR_PTR(-EPROBE_DEFER);
	}
236
	sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
237
				       s_info->pdata.board_info, NULL);
238
	if (IS_ERR_OR_NULL(sd)) {
239
		i2c_put_adapter(adapter);
240 241
		v4l2_warn(&fmd->v4l2_dev,
			  "Failed to acquire subdev %s, deferring probe\n",
242
			  s_info->pdata.board_info->type);
243
		return ERR_PTR(-EPROBE_DEFER);
244 245
	}
	v4l2_set_subdev_hostdata(sd, s_info);
246
	sd->grp_id = GRP_ID_SENSOR;
247 248

	v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
249
		  s_info->pdata.board_info->type);
250 251 252 253 254 255
	return sd;
}

static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
256
	struct i2c_adapter *adapter;
257 258 259 260

	if (!client)
		return;
	v4l2_device_unregister_subdev(sd);
261
	adapter = client->adapter;
262
	i2c_unregister_device(client);
263 264
	if (adapter)
		i2c_put_adapter(adapter);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
}

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++) {
291 292
		struct v4l2_subdev *sd;

293
		fmd->sensor[i].pdata = pdata->source_info[i];
294 295 296
		ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], true);
		if (ret)
			break;
297
		sd = fimc_md_register_sensor(fmd, &fmd->sensor[i]);
298
		ret = __fimc_md_set_camclk(fmd, &fmd->sensor[i], false);
299 300 301 302 303 304 305 306

		if (!IS_ERR(sd)) {
			fmd->sensor[i].subdev = sd;
		} else {
			fmd->sensor[i].subdev = NULL;
			ret = PTR_ERR(sd);
			break;
		}
307 308 309 310 311 312 313 314
		if (ret)
			break;
	}
	pm_runtime_put(&fd->pdev->dev);
	return ret;
}

/*
315
 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
316
 */
317 318 319

static int register_fimc_lite_entity(struct fimc_md *fmd,
				     struct fimc_lite *fimc_lite)
320
{
321
	struct v4l2_subdev *sd;
322
	int ret;
323

324 325 326
	if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
		    fmd->fimc_lite[fimc_lite->index]))
		return -EBUSY;
327

328 329
	sd = &fimc_lite->subdev;
	sd->grp_id = GRP_ID_FLITE;
330
	v4l2_set_subdev_hostdata(sd, (void *)&fimc_pipeline_ops);
331 332

	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
333 334 335 336 337 338
	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;
339 340
}

341
static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
342
{
343
	struct v4l2_subdev *sd;
344 345
	int ret;

346 347
	if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
		return -EBUSY;
348

349 350 351
	sd = &fimc->vid_cap.subdev;
	sd->grp_id = GRP_ID_FIMC;
	v4l2_set_subdev_hostdata(sd, (void *)&fimc_pipeline_ops);
352

353 354 355 356 357 358 359
	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
	if (!ret) {
		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);
360
	}
361
	return ret;
362 363
}

364 365 366
static int register_csis_entity(struct fimc_md *fmd,
				struct platform_device *pdev,
				struct v4l2_subdev *sd)
367
{
368
	struct device_node *node = pdev->dev.of_node;
369 370
	int id, ret;

371 372 373 374 375 376
	id = node ? of_alias_get_id(node, "csis") : max(0, pdev->id);

	if (WARN_ON(id >= CSIS_MAX_ENTITIES || fmd->csis[id].sd))
		return -EBUSY;

	if (WARN_ON(id >= CSIS_MAX_ENTITIES))
377 378
		return 0;

379
	sd->grp_id = GRP_ID_CSIS;
380
	ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
381 382 383
	if (!ret)
		fmd->csis[id].sd = sd;
	else
384
		v4l2_err(&fmd->v4l2_dev,
385
			 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
386 387 388
	return ret;
}

389 390 391
static int fimc_md_register_platform_entity(struct fimc_md *fmd,
					    struct platform_device *pdev,
					    int plat_entity)
392
{
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
	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);
412
			break;
413 414 415 416 417
		case IDX_CSIS:
			ret = register_csis_entity(fmd, pdev, drvdata);
			break;
		default:
			ret = -ENODEV;
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
	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;
451 452
	}

453 454 455 456 457
	if (plat_entity >= 0)
		ret = fimc_md_register_platform_entity(data, pdev,
						       plat_entity);
	put_device(dev);
	return 0;
458 459 460 461 462 463 464 465 466
}

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;
467
		v4l2_device_unregister_subdev(&fmd->fimc[i]->vid_cap.subdev);
468
		fmd->fimc[i]->pipeline_ops = NULL;
469 470
		fmd->fimc[i] = NULL;
	}
471 472 473 474
	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);
475
		fmd->fimc[i]->pipeline_ops = NULL;
476 477
		fmd->fimc_lite[i] = NULL;
	}
478 479 480 481
	for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
		if (fmd->csis[i].sd == NULL)
			continue;
		v4l2_device_unregister_subdev(fmd->csis[i].sd);
482
		module_put(fmd->csis[i].sd->owner);
483 484 485 486 487 488 489 490
		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;
	}
491
	v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
492 493 494 495 496 497 498 499
}

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

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

522
		flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
523

524
		sink = &fmd->fimc[i]->vid_cap.subdev.entity;
525 526
		ret = media_entity_create_link(source, pad, sink,
					      FIMC_SD_PAD_SINK, flags);
527 528 529
		if (ret)
			return ret;

530 531 532 533 534 535
		/* Notify FIMC capture subdev entity */
		ret = media_entity_call(sink, link_setup, &sink->pads[0],
					&source->pads[pad], flags);
		if (ret)
			break;

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

539
		if (flags == 0 || sensor == NULL)
540 541 542 543 544 545 546 547 548
			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);
		}
	}
549 550 551 552 553

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

554 555 556 557
		if (link_mask & (1 << (i + FIMC_MAX_DEVS)))
			flags = MEDIA_LNK_FL_ENABLED;
		else
			flags = 0;
558 559 560 561 562 563 564 565 566 567 568 569 570

		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;

571
		v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
572 573
			  source->name, flags ? '=' : '-', sink->name);
	}
574 575 576
	return 0;
}

577 578 579 580 581
/* 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;
582
	int i, ret = 0;
583 584 585 586 587 588

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

	return ret;
}

601 602 603 604 605 606 607 608 609 610 611 612 613 614
/**
 * 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)
{
615
	struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
616
	struct v4l2_subdev *sensor, *csis;
617
	struct fimc_source_info *pdata;
618
	struct fimc_sensor_info *s_info;
619
	struct media_entity *source, *sink;
620 621
	int i, pad, fimc_id = 0, ret = 0;
	u32 flags, link_mask = 0;
622 623 624 625 626 627 628

	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);
629
		if (!s_info)
630 631 632
			continue;

		source = NULL;
633
		pdata = &s_info->pdata;
634

635 636
		switch (pdata->sensor_bus_type) {
		case FIMC_BUS_TYPE_MIPI_CSI2:
637 638 639 640 641 642 643 644
			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"))
645
				return -EINVAL;
646

647 648
			pad = sensor->entity.num_pads - 1;
			ret = media_entity_create_link(&sensor->entity, pad,
649 650 651 652 653 654
					      &csis->entity, CSIS_PAD_SINK,
					      MEDIA_LNK_FL_IMMUTABLE |
					      MEDIA_LNK_FL_ENABLED);
			if (ret)
				return ret;

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

658
			source = NULL;
659
			csi_sensors[pdata->mux_id] = sensor;
660 661
			break;

662
		case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
663 664 665 666 667 668
			source = &sensor->entity;
			pad = 0;
			break;

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

675
		link_mask = 1 << fimc_id++;
676
		ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
677
						       pad, link_mask);
678 679
	}

680
	for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
681 682 683 684
		if (fmd->csis[i].sd == NULL)
			continue;
		source = &fmd->csis[i].sd->entity;
		pad = CSIS_PAD_SOURCE;
685
		sensor = csi_sensors[i];
686

687
		link_mask = 1 << fimc_id++;
688
		ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
689
						       pad, link_mask);
690
	}
691

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

705
	return __fimc_md_create_flite_source_links(fmd);
706 707 708 709 710
}

/*
 * The peripheral sensor clock management.
 */
711 712 713 714 715 716 717 718 719 720 721 722 723
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);
	}
}

724 725
static int fimc_md_get_clocks(struct fimc_md *fmd)
{
726
	struct device *dev = NULL;
727 728
	char clk_name[32];
	struct clk *clock;
729 730 731 732 733 734 735
	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;
736 737 738

	for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
		snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
739 740
		clock = clk_get(dev, clk_name);

741
		if (IS_ERR(clock)) {
742 743 744 745 746 747 748 749 750 751
			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;
752 753 754
		}
		fmd->camclk[i].clock = clock;
	}
755 756
	if (ret)
		fimc_md_put_clocks(fmd);
757

758
	return ret;
759 760 761
}

static int __fimc_md_set_camclk(struct fimc_md *fmd,
762 763
				struct fimc_sensor_info *s_info,
				bool on)
764
{
765
	struct fimc_source_info *pdata = &s_info->pdata;
766 767 768 769 770 771 772 773
	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];

774 775
	dbg("camclk %d, f: %lu, use_count: %d, on: %d",
	    pdata->clk_id, pdata->clk_frequency, camclk->use_count, on);
776 777 778 779 780 781 782 783 784 785

	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);
786 787
			dbg("Enabled camclk %d: f: %lu", pdata->clk_id,
			    clk_get_rate(camclk->clock));
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
		}
		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)
{
825 826
	struct fimc_lite *fimc_lite = NULL;
	struct fimc_dev *fimc = NULL;
827
	struct fimc_pipeline *pipeline;
828
	struct v4l2_subdev *sd;
829
	struct mutex *lock;
830
	int ret = 0;
831
	int ref_count;
832

833
	if (media_entity_type(sink->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
834 835
		return 0;

836
	sd = media_entity_to_v4l2_subdev(sink->entity);
837

838
	switch (sd->grp_id) {
839
	case GRP_ID_FLITE:
840
		fimc_lite = v4l2_get_subdevdata(sd);
841 842
		if (WARN_ON(fimc_lite == NULL))
			return 0;
843
		pipeline = &fimc_lite->pipeline;
844
		lock = &fimc_lite->lock;
845
		break;
846
	case GRP_ID_FIMC:
847
		fimc = v4l2_get_subdevdata(sd);
848 849
		if (WARN_ON(fimc == NULL))
			return 0;
850
		pipeline = &fimc->pipeline;
851
		lock = &fimc->lock;
852 853 854 855
		break;
	default:
		return 0;
	}
856

857
	if (!(flags & MEDIA_LNK_FL_ENABLED)) {
858 859
		int i;
		mutex_lock(lock);
860
		ret = __fimc_pipeline_close(pipeline);
861 862 863
		for (i = 0; i < IDX_MAX; i++)
			pipeline->subdevs[i] = NULL;
		if (fimc)
864
			fimc_ctrls_delete(fimc->vid_cap.ctx);
865
		mutex_unlock(lock);
866 867 868 869 870
		return ret;
	}
	/*
	 * Link activation. Enable power of pipeline elements only if the
	 * pipeline is already in use, i.e. its video node is opened.
871
	 * Recreate the controls destroyed during the link deactivation.
872
	 */
873 874 875 876 877 878 879 880 881
	mutex_lock(lock);

	ref_count = fimc ? fimc->vid_cap.refcnt : fimc_lite->ref_count;
	if (ref_count > 0)
		ret = __fimc_pipeline_open(pipeline, source->entity, true);
	if (!ret && fimc)
		ret = fimc_capture_ctrls_create(fimc);

	mutex_unlock(lock);
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 916 917 918 919 920 921 922 923 924 925 926 927 928 929
	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);

930
static int fimc_md_probe(struct platform_device *pdev)
931 932 933 934 935
{
	struct v4l2_device *v4l2_dev;
	struct fimc_md *fmd;
	int ret;

936
	fmd = devm_kzalloc(&pdev->dev, sizeof(*fmd), GFP_KERNEL);
937 938 939 940 941 942 943 944 945 946 947 948 949
	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;
950
	v4l2_dev->notify = fimc_sensor_notify;
951 952 953 954 955 956
	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);
957
		return ret;
958 959 960 961
	}
	ret = media_device_register(&fmd->media_dev);
	if (ret < 0) {
		v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
962
		goto err_md;
963 964 965
	}
	ret = fimc_md_get_clocks(fmd);
	if (ret)
966
		goto err_clk;
967 968

	fmd->user_subdev_api = false;
969 970 971 972

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

973 974
	ret = bus_for_each_dev(&platform_bus_type, NULL, fmd,
					fimc_md_pdev_match);
975
	if (ret)
976
		goto err_unlock;
977

978 979 980
	if (pdev->dev.platform_data) {
		ret = fimc_md_register_sensor_entities(fmd);
		if (ret)
981
			goto err_unlock;
982
	}
983 984
	ret = fimc_md_create_links(fmd);
	if (ret)
985
		goto err_unlock;
986 987
	ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
	if (ret)
988
		goto err_unlock;
989 990

	ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
991 992 993 994 995 996 997 998 999 1000
	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:
1001 1002 1003
	media_device_unregister(&fmd->media_dev);
	fimc_md_put_clocks(fmd);
	fimc_md_unregister_entities(fmd);
1004
err_md:
1005 1006 1007 1008
	v4l2_device_unregister(&fmd->v4l2_dev);
	return ret;
}

1009
static int fimc_md_remove(struct platform_device *pdev)
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
{
	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,
1024
	.remove		= fimc_md_remove,
1025 1026 1027 1028 1029 1030
	.driver = {
		.name	= "s5p-fimc-md",
		.owner	= THIS_MODULE,
	}
};

1031
static int __init fimc_md_init(void)
1032 1033
{
	int ret;
1034

1035 1036 1037 1038
	request_module("s5p-csis");
	ret = fimc_register_driver();
	if (ret)
		return ret;
1039

1040 1041
	return platform_driver_register(&fimc_md_driver);
}
1042 1043

static void __exit fimc_md_exit(void)
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
{
	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");