fimc-lite.c 43.9 KB
Newer Older
1 2 3
/*
 * Samsung EXYNOS FIMC-LITE (camera host interface) driver
*
4 5
 * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__

#include <linux/bug.h>
14
#include <linux/clk.h>
15 16 17 18 19 20
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/module.h>
21
#include <linux/of.h>
22 23 24 25 26 27 28 29 30
#include <linux/types.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/slab.h>
#include <linux/videodev2.h>

#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-mem2mem.h>
31
#include <media/videobuf2-v4l2.h>
32
#include <media/videobuf2-dma-contig.h>
33
#include <media/drv-intf/exynos-fimc.h>
34

35
#include "common.h"
36
#include "fimc-core.h"
37
#include "fimc-lite.h"
38 39 40 41 42 43 44 45 46
#include "fimc-lite-reg.h"

static int debug;
module_param(debug, int, 0644);

static const struct fimc_fmt fimc_lite_formats[] = {
	{
		.name		= "YUV 4:2:2 packed, YCbYCr",
		.fourcc		= V4L2_PIX_FMT_YUYV,
47
		.colorspace	= V4L2_COLORSPACE_JPEG,
48 49 50
		.depth		= { 16 },
		.color		= FIMC_FMT_YCBYCR422,
		.memplanes	= 1,
51
		.mbus_code	= MEDIA_BUS_FMT_YUYV8_2X8,
52
		.flags		= FMT_FLAGS_YUV,
53 54 55
	}, {
		.name		= "YUV 4:2:2 packed, CbYCrY",
		.fourcc		= V4L2_PIX_FMT_UYVY,
56
		.colorspace	= V4L2_COLORSPACE_JPEG,
57 58 59
		.depth		= { 16 },
		.color		= FIMC_FMT_CBYCRY422,
		.memplanes	= 1,
60
		.mbus_code	= MEDIA_BUS_FMT_UYVY8_2X8,
61
		.flags		= FMT_FLAGS_YUV,
62 63 64
	}, {
		.name		= "YUV 4:2:2 packed, CrYCbY",
		.fourcc		= V4L2_PIX_FMT_VYUY,
65
		.colorspace	= V4L2_COLORSPACE_JPEG,
66 67 68
		.depth		= { 16 },
		.color		= FIMC_FMT_CRYCBY422,
		.memplanes	= 1,
69
		.mbus_code	= MEDIA_BUS_FMT_VYUY8_2X8,
70
		.flags		= FMT_FLAGS_YUV,
71 72 73
	}, {
		.name		= "YUV 4:2:2 packed, YCrYCb",
		.fourcc		= V4L2_PIX_FMT_YVYU,
74
		.colorspace	= V4L2_COLORSPACE_JPEG,
75 76 77
		.depth		= { 16 },
		.color		= FIMC_FMT_YCRYCB422,
		.memplanes	= 1,
78
		.mbus_code	= MEDIA_BUS_FMT_YVYU8_2X8,
79
		.flags		= FMT_FLAGS_YUV,
80 81 82
	}, {
		.name		= "RAW8 (GRBG)",
		.fourcc		= V4L2_PIX_FMT_SGRBG8,
83
		.colorspace	= V4L2_COLORSPACE_SRGB,
84 85 86
		.depth		= { 8 },
		.color		= FIMC_FMT_RAW8,
		.memplanes	= 1,
87
		.mbus_code	= MEDIA_BUS_FMT_SGRBG8_1X8,
88
		.flags		= FMT_FLAGS_RAW_BAYER,
89 90 91
	}, {
		.name		= "RAW10 (GRBG)",
		.fourcc		= V4L2_PIX_FMT_SGRBG10,
92
		.colorspace	= V4L2_COLORSPACE_SRGB,
93
		.depth		= { 16 },
94 95
		.color		= FIMC_FMT_RAW10,
		.memplanes	= 1,
96
		.mbus_code	= MEDIA_BUS_FMT_SGRBG10_1X10,
97
		.flags		= FMT_FLAGS_RAW_BAYER,
98 99 100
	}, {
		.name		= "RAW12 (GRBG)",
		.fourcc		= V4L2_PIX_FMT_SGRBG12,
101
		.colorspace	= V4L2_COLORSPACE_SRGB,
102
		.depth		= { 16 },
103 104
		.color		= FIMC_FMT_RAW12,
		.memplanes	= 1,
105
		.mbus_code	= MEDIA_BUS_FMT_SGRBG12_1X12,
106
		.flags		= FMT_FLAGS_RAW_BAYER,
107 108 109 110 111 112 113
	},
};

/**
 * fimc_lite_find_format - lookup fimc color format by fourcc or media bus code
 * @pixelformat: fourcc to match, ignored if null
 * @mbus_code: media bus code to match, ignored if null
114
 * @mask: the color format flags to match
115 116 117
 * @index: index to the fimc_lite_formats array, ignored if negative
 */
static const struct fimc_fmt *fimc_lite_find_format(const u32 *pixelformat,
118
			const u32 *mbus_code, unsigned int mask, int index)
119 120 121 122 123 124 125 126 127 128
{
	const struct fimc_fmt *fmt, *def_fmt = NULL;
	unsigned int i;
	int id = 0;

	if (index >= (int)ARRAY_SIZE(fimc_lite_formats))
		return NULL;

	for (i = 0; i < ARRAY_SIZE(fimc_lite_formats); ++i) {
		fmt = &fimc_lite_formats[i];
129 130
		if (mask && !(fmt->flags & mask))
			continue;
131 132 133 134 135 136 137 138 139 140 141
		if (pixelformat && fmt->fourcc == *pixelformat)
			return fmt;
		if (mbus_code && fmt->mbus_code == *mbus_code)
			return fmt;
		if (index == id)
			def_fmt = fmt;
		id++;
	}
	return def_fmt;
}

142
static int fimc_lite_hw_init(struct fimc_lite *fimc, bool isp_output)
143
{
144
	struct fimc_source_info *si;
145 146
	unsigned long flags;

147
	if (fimc->sensor == NULL)
148 149
		return -ENXIO;

150
	if (fimc->inp_frame.fmt == NULL || fimc->out_frame.fmt == NULL)
151 152
		return -EINVAL;

153
	/* Get sensor configuration data from the sensor subdev */
154
	si = v4l2_get_subdev_hostdata(fimc->sensor);
155 156 157
	if (!si)
		return -EINVAL;

158 159
	spin_lock_irqsave(&fimc->slock, flags);

160
	flite_hw_set_camera_bus(fimc, si);
161 162
	flite_hw_set_source_format(fimc, &fimc->inp_frame);
	flite_hw_set_window_offset(fimc, &fimc->inp_frame);
163
	flite_hw_set_dma_buf_mask(fimc, 0);
164
	flite_hw_set_output_dma(fimc, &fimc->out_frame, !isp_output);
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	flite_hw_set_interrupt_mask(fimc);
	flite_hw_set_test_pattern(fimc, fimc->test_pattern->val);

	if (debug > 0)
		flite_hw_dump_regs(fimc, __func__);

	spin_unlock_irqrestore(&fimc->slock, flags);
	return 0;
}

/*
 * Reinitialize the driver so it is ready to start the streaming again.
 * Set fimc->state to indicate stream off and the hardware shut down state.
 * If not suspending (@suspend is false), return any buffers to videobuf2.
 * Otherwise put any owned buffers onto the pending buffers queue, so they
 * can be re-spun when the device is being resumed. Also perform FIMC
 * software reset and disable streaming on the whole pipeline if required.
 */
static int fimc_lite_reinit(struct fimc_lite *fimc, bool suspend)
{
	struct flite_buffer *buf;
	unsigned long flags;
	bool streaming;

	spin_lock_irqsave(&fimc->slock, flags);
	streaming = fimc->state & (1 << ST_SENSOR_STREAM);

	fimc->state &= ~(1 << ST_FLITE_RUN | 1 << ST_FLITE_OFF |
			 1 << ST_FLITE_STREAM | 1 << ST_SENSOR_STREAM);
	if (suspend)
		fimc->state |= (1 << ST_FLITE_SUSPENDED);
	else
		fimc->state &= ~(1 << ST_FLITE_PENDING |
				 1 << ST_FLITE_SUSPENDED);

	/* Release unused buffers */
	while (!suspend && !list_empty(&fimc->pending_buf_q)) {
		buf = fimc_lite_pending_queue_pop(fimc);
203
		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
204 205 206 207 208 209 210
	}
	/* If suspending put unused buffers onto pending queue */
	while (!list_empty(&fimc->active_buf_q)) {
		buf = fimc_lite_active_queue_pop(fimc);
		if (suspend)
			fimc_lite_pending_queue_add(fimc, buf);
		else
211
			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
212 213 214 215 216 217 218 219 220
	}

	spin_unlock_irqrestore(&fimc->slock, flags);

	flite_hw_reset(fimc);

	if (!streaming)
		return 0;

221
	return fimc_pipeline_call(&fimc->ve, set_stream, 0);
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
}

static int fimc_lite_stop_capture(struct fimc_lite *fimc, bool suspend)
{
	unsigned long flags;

	if (!fimc_lite_active(fimc))
		return 0;

	spin_lock_irqsave(&fimc->slock, flags);
	set_bit(ST_FLITE_OFF, &fimc->state);
	flite_hw_capture_stop(fimc);
	spin_unlock_irqrestore(&fimc->slock, flags);

	wait_event_timeout(fimc->irq_queue,
			   !test_bit(ST_FLITE_OFF, &fimc->state),
			   (2*HZ/10)); /* 200 ms */

	return fimc_lite_reinit(fimc, suspend);
}

/* Must be called  with fimc.slock spinlock held. */
static void fimc_lite_config_update(struct fimc_lite *fimc)
{
	flite_hw_set_window_offset(fimc, &fimc->inp_frame);
	flite_hw_set_dma_window(fimc, &fimc->out_frame);
	flite_hw_set_test_pattern(fimc, fimc->test_pattern->val);
	clear_bit(ST_FLITE_CONFIG, &fimc->state);
}

static irqreturn_t flite_irq_handler(int irq, void *priv)
{
	struct fimc_lite *fimc = priv;
	struct flite_buffer *vbuf;
	unsigned long flags;
	u32 intsrc;

	spin_lock_irqsave(&fimc->slock, flags);

	intsrc = flite_hw_get_interrupt_source(fimc);
	flite_hw_clear_pending_irq(fimc);

	if (test_and_clear_bit(ST_FLITE_OFF, &fimc->state)) {
		wake_up(&fimc->irq_queue);
		goto done;
	}

	if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_OVERFLOW) {
		clear_bit(ST_FLITE_RUN, &fimc->state);
		fimc->events.data_overflow++;
	}

	if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_LASTCAPEND) {
		flite_hw_clear_last_capture_end(fimc);
		clear_bit(ST_FLITE_STREAM, &fimc->state);
		wake_up(&fimc->irq_queue);
	}

280
	if (atomic_read(&fimc->out_path) != FIMC_IO_DMA)
281 282 283 284 285
		goto done;

	if ((intsrc & FLITE_REG_CISTATUS_IRQ_SRC_FRMSTART) &&
	    test_bit(ST_FLITE_RUN, &fimc->state) &&
	    !list_empty(&fimc->pending_buf_q)) {
286 287 288 289 290 291 292 293
		vbuf = fimc_lite_pending_queue_pop(fimc);
		flite_hw_set_dma_buffer(fimc, vbuf);
		fimc_lite_active_queue_add(fimc, vbuf);
	}

	if ((intsrc & FLITE_REG_CISTATUS_IRQ_SRC_FRMEND) &&
	    test_bit(ST_FLITE_RUN, &fimc->state) &&
	    !list_empty(&fimc->active_buf_q)) {
294
		vbuf = fimc_lite_active_queue_pop(fimc);
295
		vbuf->vb.vb2_buf.timestamp = ktime_get_ns();
296
		vbuf->vb.sequence = fimc->frame_count++;
297
		flite_hw_mask_dma_buffer(fimc, vbuf->index);
298
		vb2_buffer_done(&vbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	}

	if (test_bit(ST_FLITE_CONFIG, &fimc->state))
		fimc_lite_config_update(fimc);

	if (list_empty(&fimc->pending_buf_q)) {
		flite_hw_capture_stop(fimc);
		clear_bit(ST_FLITE_STREAM, &fimc->state);
	}
done:
	set_bit(ST_FLITE_RUN, &fimc->state);
	spin_unlock_irqrestore(&fimc->slock, flags);
	return IRQ_HANDLED;
}

static int start_streaming(struct vb2_queue *q, unsigned int count)
{
	struct fimc_lite *fimc = q->drv_priv;
317
	unsigned long flags;
318 319
	int ret;

320 321 322
	spin_lock_irqsave(&fimc->slock, flags);

	fimc->buf_index = 0;
323 324
	fimc->frame_count = 0;

325 326
	spin_unlock_irqrestore(&fimc->slock, flags);

327
	ret = fimc_lite_hw_init(fimc, false);
328 329 330 331 332 333 334 335 336 337 338 339
	if (ret) {
		fimc_lite_reinit(fimc, false);
		return ret;
	}

	set_bit(ST_FLITE_PENDING, &fimc->state);

	if (!list_empty(&fimc->active_buf_q) &&
	    !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) {
		flite_hw_capture_start(fimc);

		if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state))
340
			fimc_pipeline_call(&fimc->ve, set_stream, 1);
341 342 343 344 345 346 347
	}
	if (debug > 0)
		flite_hw_dump_regs(fimc, __func__);

	return 0;
}

348
static void stop_streaming(struct vb2_queue *q)
349 350 351 352
{
	struct fimc_lite *fimc = q->drv_priv;

	if (!fimc_lite_active(fimc))
353
		return;
354

355
	fimc_lite_stop_capture(fimc, false);
356 357
}

358
static int queue_setup(struct vb2_queue *vq,
359
		       unsigned int *num_buffers, unsigned int *num_planes,
360
		       unsigned int sizes[], struct device *alloc_devs[])
361 362 363
{
	struct fimc_lite *fimc = vq->drv_priv;
	struct flite_frame *frame = &fimc->out_frame;
364
	const struct fimc_fmt *fmt = frame->fmt;
365
	unsigned long wh = frame->f_width * frame->f_height;
366 367 368 369 370
	int i;

	if (fmt == NULL)
		return -EINVAL;

371 372 373
	if (*num_planes) {
		if (*num_planes != fmt->memplanes)
			return -EINVAL;
374
		for (i = 0; i < *num_planes; i++)
375 376 377 378 379
			if (sizes[i] < (wh * fmt->depth[i]) / 8)
				return -EINVAL;
		return 0;
	}

380 381
	*num_planes = fmt->memplanes;

382
	for (i = 0; i < fmt->memplanes; i++)
383
		sizes[i] = (wh * fmt->depth[i]) / 8;
384 385 386 387 388 389 390 391 392 393

	return 0;
}

static int buffer_prepare(struct vb2_buffer *vb)
{
	struct vb2_queue *vq = vb->vb2_queue;
	struct fimc_lite *fimc = vq->drv_priv;
	int i;

394
	if (fimc->out_frame.fmt == NULL)
395 396
		return -EINVAL;

397
	for (i = 0; i < fimc->out_frame.fmt->memplanes; i++) {
398 399 400
		unsigned long size = fimc->payload[i];

		if (vb2_plane_size(vb, i) < size) {
401
			v4l2_err(&fimc->ve.vdev,
402 403 404 405 406 407 408 409 410 411 412 413
				 "User buffer too small (%ld < %ld)\n",
				 vb2_plane_size(vb, i), size);
			return -EINVAL;
		}
		vb2_set_plane_payload(vb, i, size);
	}

	return 0;
}

static void buffer_queue(struct vb2_buffer *vb)
{
414
	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
415
	struct flite_buffer *buf
416
		= container_of(vbuf, struct flite_buffer, vb);
417 418 419 420 421 422
	struct fimc_lite *fimc = vb2_get_drv_priv(vb->vb2_queue);
	unsigned long flags;

	spin_lock_irqsave(&fimc->slock, flags);
	buf->paddr = vb2_dma_contig_plane_dma_addr(vb, 0);

423 424 425 426
	buf->index = fimc->buf_index++;
	if (fimc->buf_index >= fimc->reqbufs_count)
		fimc->buf_index = 0;

427 428 429
	if (!test_bit(ST_FLITE_SUSPENDED, &fimc->state) &&
	    !test_bit(ST_FLITE_STREAM, &fimc->state) &&
	    list_empty(&fimc->active_buf_q)) {
430
		flite_hw_set_dma_buffer(fimc, buf);
431 432 433 434 435 436 437 438 439 440 441 442
		fimc_lite_active_queue_add(fimc, buf);
	} else {
		fimc_lite_pending_queue_add(fimc, buf);
	}

	if (vb2_is_streaming(&fimc->vb_queue) &&
	    !list_empty(&fimc->pending_buf_q) &&
	    !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) {
		flite_hw_capture_start(fimc);
		spin_unlock_irqrestore(&fimc->slock, flags);

		if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state))
443
			fimc_pipeline_call(&fimc->ve, set_stream, 1);
444 445 446 447 448 449 450 451 452
		return;
	}
	spin_unlock_irqrestore(&fimc->slock, flags);
}

static const struct vb2_ops fimc_lite_qops = {
	.queue_setup	 = queue_setup,
	.buf_prepare	 = buffer_prepare,
	.buf_queue	 = buffer_queue,
453 454
	.wait_prepare	 = vb2_ops_wait_prepare,
	.wait_finish	 = vb2_ops_wait_finish,
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
	.start_streaming = start_streaming,
	.stop_streaming	 = stop_streaming,
};

static void fimc_lite_clear_event_counters(struct fimc_lite *fimc)
{
	unsigned long flags;

	spin_lock_irqsave(&fimc->slock, flags);
	memset(&fimc->events, 0, sizeof(fimc->events));
	spin_unlock_irqrestore(&fimc->slock, flags);
}

static int fimc_lite_open(struct file *file)
{
	struct fimc_lite *fimc = video_drvdata(file);
471
	struct media_entity *me = &fimc->ve.vdev.entity;
472
	int ret;
473

474
	mutex_lock(&fimc->lock);
475
	if (atomic_read(&fimc->out_path) != FIMC_IO_DMA) {
476
		ret = -EBUSY;
477
		goto unlock;
478 479
	}

480
	set_bit(ST_FLITE_IN_USE, &fimc->state);
481 482
	ret = pm_runtime_get_sync(&fimc->pdev->dev);
	if (ret < 0)
483
		goto unlock;
484

485 486
	ret = v4l2_fh_open(file);
	if (ret < 0)
487
		goto err_pm;
488

489 490 491
	if (!v4l2_fh_is_singular_file(file) ||
	    atomic_read(&fimc->out_path) != FIMC_IO_DMA)
		goto unlock;
492

493
	mutex_lock(&me->graph_obj.mdev->graph_mutex);
494

495
	ret = fimc_pipeline_call(&fimc->ve, open, me, true);
496 497 498 499 500

	/* Mark video pipeline ending at this video node as in use. */
	if (ret == 0)
		me->use_count++;

501
	mutex_unlock(&me->graph_obj.mdev->graph_mutex);
502

503
	if (!ret) {
504
		fimc_lite_clear_event_counters(fimc);
505
		goto unlock;
506
	}
507 508 509 510 511 512

	v4l2_fh_release(file);
err_pm:
	pm_runtime_put_sync(&fimc->pdev->dev);
	clear_bit(ST_FLITE_IN_USE, &fimc->state);
unlock:
513
	mutex_unlock(&fimc->lock);
514 515 516
	return ret;
}

517
static int fimc_lite_release(struct file *file)
518 519
{
	struct fimc_lite *fimc = video_drvdata(file);
520
	struct media_entity *entity = &fimc->ve.vdev.entity;
521

522
	mutex_lock(&fimc->lock);
523

524
	if (v4l2_fh_is_singular_file(file) &&
525
	    atomic_read(&fimc->out_path) == FIMC_IO_DMA) {
526
		if (fimc->streaming) {
527
			media_entity_pipeline_stop(entity);
528 529
			fimc->streaming = false;
		}
530
		fimc_lite_stop_capture(fimc, false);
531 532 533
		fimc_pipeline_call(&fimc->ve, close);
		clear_bit(ST_FLITE_IN_USE, &fimc->state);

534
		mutex_lock(&entity->graph_obj.mdev->graph_mutex);
535
		entity->use_count--;
536
		mutex_unlock(&entity->graph_obj.mdev->graph_mutex);
537 538
	}

539
	_vb2_fop_release(file, NULL);
540
	pm_runtime_put(&fimc->pdev->dev);
541
	clear_bit(ST_FLITE_SUSPENDED, &fimc->state);
542

543
	mutex_unlock(&fimc->lock);
544
	return 0;
545 546 547 548 549
}

static const struct v4l2_file_operations fimc_lite_fops = {
	.owner		= THIS_MODULE,
	.open		= fimc_lite_open,
550 551
	.release	= fimc_lite_release,
	.poll		= vb2_fop_poll,
552
	.unlocked_ioctl	= video_ioctl2,
553
	.mmap		= vb2_fop_mmap,
554 555 556 557 558 559
};

/*
 * Format and crop negotiation helpers
 */

560
static const struct fimc_fmt *fimc_lite_subdev_try_fmt(struct fimc_lite *fimc,
561
					struct v4l2_subdev_pad_config *cfg,
562
					struct v4l2_subdev_format *format)
563
{
564
	struct flite_drvdata *dd = fimc->dd;
565 566 567 568 569 570 571
	struct v4l2_mbus_framefmt *mf = &format->format;
	const struct fimc_fmt *fmt = NULL;

	if (format->pad == FLITE_SD_PAD_SINK) {
		v4l_bound_align_image(&mf->width, 8, dd->max_width,
				ffs(dd->out_width_align) - 1,
				&mf->height, 0, dd->max_height, 0, 0);
572

573 574 575 576
		fmt = fimc_lite_find_format(NULL, &mf->code, 0, 0);
		if (WARN_ON(!fmt))
			return NULL;

577
		mf->colorspace = fmt->colorspace;
578
		mf->code = fmt->mbus_code;
579
	} else {
580 581 582
		struct flite_frame *sink = &fimc->inp_frame;
		struct v4l2_mbus_framefmt *sink_fmt;
		struct v4l2_rect *rect;
583

584
		if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
585
			sink_fmt = v4l2_subdev_get_try_format(&fimc->subdev, cfg,
586
						FLITE_SD_PAD_SINK);
587

588
			mf->code = sink_fmt->code;
589
			mf->colorspace = sink_fmt->colorspace;
590

591
			rect = v4l2_subdev_get_try_crop(&fimc->subdev, cfg,
592 593 594
						FLITE_SD_PAD_SINK);
		} else {
			mf->code = sink->fmt->mbus_code;
595
			mf->colorspace = sink->fmt->colorspace;
596 597 598 599 600 601 602
			rect = &sink->rect;
		}

		/* Allow changing format only on sink pad */
		mf->width = rect->width;
		mf->height = rect->height;
	}
603

604 605 606 607
	mf->field = V4L2_FIELD_NONE;

	v4l2_dbg(1, debug, &fimc->subdev, "code: %#x (%d), %dx%d\n",
		 mf->code, mf->colorspace, mf->width, mf->height);
608 609 610 611 612 613 614 615 616 617 618 619 620

	return fmt;
}

static void fimc_lite_try_crop(struct fimc_lite *fimc, struct v4l2_rect *r)
{
	struct flite_frame *frame = &fimc->inp_frame;

	v4l_bound_align_image(&r->width, 0, frame->f_width, 0,
			      &r->height, 0, frame->f_height, 0, 0);

	/* Adjust left/top if cropping rectangle got out of bounds */
	r->left = clamp_t(u32, r->left, 0, frame->f_width - r->width);
621
	r->left = round_down(r->left, fimc->dd->win_hor_offs_align);
622 623
	r->top  = clamp_t(u32, r->top, 0, frame->f_height - r->height);

624
	v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, sink fmt: %dx%d\n",
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
		 r->left, r->top, r->width, r->height,
		 frame->f_width, frame->f_height);
}

static void fimc_lite_try_compose(struct fimc_lite *fimc, struct v4l2_rect *r)
{
	struct flite_frame *frame = &fimc->out_frame;
	struct v4l2_rect *crop_rect = &fimc->inp_frame.rect;

	/* Scaling is not supported so we enforce compose rectangle size
	   same as size of the sink crop rectangle. */
	r->width = crop_rect->width;
	r->height = crop_rect->height;

	/* Adjust left/top if the composing rectangle got out of bounds */
	r->left = clamp_t(u32, r->left, 0, frame->f_width - r->width);
641
	r->left = round_down(r->left, fimc->dd->out_hor_offs_align);
642 643
	r->top  = clamp_t(u32, r->top, 0, fimc->out_frame.f_height - r->height);

644
	v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, source fmt: %dx%d\n",
645 646 647 648 649 650 651
		 r->left, r->top, r->width, r->height,
		 frame->f_width, frame->f_height);
}

/*
 * Video node ioctl operations
 */
652
static int fimc_lite_querycap(struct file *file, void *priv,
653 654
					struct v4l2_capability *cap)
{
655 656
	struct fimc_lite *fimc = video_drvdata(file);

657
	strlcpy(cap->driver, FIMC_LITE_DRV_NAME, sizeof(cap->driver));
658 659 660 661 662 663
	strlcpy(cap->card, FIMC_LITE_DRV_NAME, sizeof(cap->card));
	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
					dev_name(&fimc->pdev->dev));

	cap->device_caps = V4L2_CAP_STREAMING;
	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
	return 0;
}

static int fimc_lite_enum_fmt_mplane(struct file *file, void *priv,
				     struct v4l2_fmtdesc *f)
{
	const struct fimc_fmt *fmt;

	if (f->index >= ARRAY_SIZE(fimc_lite_formats))
		return -EINVAL;

	fmt = &fimc_lite_formats[f->index];
	strlcpy(f->description, fmt->name, sizeof(f->description));
	f->pixelformat = fmt->fourcc;

	return 0;
}

static int fimc_lite_g_fmt_mplane(struct file *file, void *fh,
				  struct v4l2_format *f)
{
	struct fimc_lite *fimc = video_drvdata(file);
	struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
	struct v4l2_plane_pix_format *plane_fmt = &pixm->plane_fmt[0];
	struct flite_frame *frame = &fimc->out_frame;
689
	const struct fimc_fmt *fmt = frame->fmt;
690 691 692 693 694 695 696 697 698

	plane_fmt->bytesperline = (frame->f_width * fmt->depth[0]) / 8;
	plane_fmt->sizeimage = plane_fmt->bytesperline * frame->f_height;

	pixm->num_planes = fmt->memplanes;
	pixm->pixelformat = fmt->fourcc;
	pixm->width = frame->f_width;
	pixm->height = frame->f_height;
	pixm->field = V4L2_FIELD_NONE;
699
	pixm->colorspace = fmt->colorspace;
700 701 702 703 704 705 706 707
	return 0;
}

static int fimc_lite_try_fmt(struct fimc_lite *fimc,
			     struct v4l2_pix_format_mplane *pixm,
			     const struct fimc_fmt **ffmt)
{
	u32 bpl = pixm->plane_fmt[0].bytesperline;
708
	struct flite_drvdata *dd = fimc->dd;
709
	const struct fimc_fmt *inp_fmt = fimc->inp_frame.fmt;
710 711
	const struct fimc_fmt *fmt;

712 713 714 715 716 717 718 719 720 721 722 723 724
	if (WARN_ON(inp_fmt == NULL))
		return -EINVAL;
	/*
	 * We allow some flexibility only for YUV formats. In case of raw
	 * raw Bayer the FIMC-LITE's output format must match its camera
	 * interface input format.
	 */
	if (inp_fmt->flags & FMT_FLAGS_YUV)
		fmt = fimc_lite_find_format(&pixm->pixelformat, NULL,
						inp_fmt->flags, 0);
	else
		fmt = inp_fmt;

725 726 727 728
	if (WARN_ON(fmt == NULL))
		return -EINVAL;
	if (ffmt)
		*ffmt = fmt;
729 730 731
	v4l_bound_align_image(&pixm->width, 8, dd->max_width,
			      ffs(dd->out_width_align) - 1,
			      &pixm->height, 0, dd->max_height, 0, 0);
732 733 734 735 736 737 738 739 740 741

	if ((bpl == 0 || ((bpl * 8) / fmt->depth[0]) < pixm->width))
		pixm->plane_fmt[0].bytesperline = (pixm->width *
						   fmt->depth[0]) / 8;

	if (pixm->plane_fmt[0].sizeimage == 0)
		pixm->plane_fmt[0].sizeimage = (pixm->width * pixm->height *
						fmt->depth[0]) / 8;
	pixm->num_planes = fmt->memplanes;
	pixm->pixelformat = fmt->fourcc;
742
	pixm->colorspace = fmt->colorspace;
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
	pixm->field = V4L2_FIELD_NONE;
	return 0;
}

static int fimc_lite_try_fmt_mplane(struct file *file, void *fh,
				    struct v4l2_format *f)
{
	struct fimc_lite *fimc = video_drvdata(file);
	return fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, NULL);
}

static int fimc_lite_s_fmt_mplane(struct file *file, void *priv,
				  struct v4l2_format *f)
{
	struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
	struct fimc_lite *fimc = video_drvdata(file);
	struct flite_frame *frame = &fimc->out_frame;
	const struct fimc_fmt *fmt = NULL;
	int ret;

	if (vb2_is_busy(&fimc->vb_queue))
		return -EBUSY;

	ret = fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, &fmt);
	if (ret < 0)
		return ret;

770
	frame->fmt = fmt;
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
	fimc->payload[0] = max((pixm->width * pixm->height * fmt->depth[0]) / 8,
			       pixm->plane_fmt[0].sizeimage);
	frame->f_width = pixm->width;
	frame->f_height = pixm->height;

	return 0;
}

static int fimc_pipeline_validate(struct fimc_lite *fimc)
{
	struct v4l2_subdev *sd = &fimc->subdev;
	struct v4l2_subdev_format sink_fmt, src_fmt;
	struct media_pad *pad;
	int ret;

	while (1) {
		/* Retrieve format at the sink pad */
		pad = &sd->entity.pads[0];
		if (!(pad->flags & MEDIA_PAD_FL_SINK))
			break;
		/* Don't call FIMC subdev operation to avoid nested locking */
		if (sd == &fimc->subdev) {
			struct flite_frame *ff = &fimc->out_frame;
			sink_fmt.format.width = ff->f_width;
			sink_fmt.format.height = ff->f_height;
796
			sink_fmt.format.code = fimc->inp_frame.fmt->mbus_code;
797 798 799 800 801 802 803 804 805
		} else {
			sink_fmt.pad = pad->index;
			sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
			ret = v4l2_subdev_call(sd, pad, get_fmt, NULL,
					       &sink_fmt);
			if (ret < 0 && ret != -ENOIOCTLCMD)
				return -EPIPE;
		}
		/* Retrieve format at the source pad */
806
		pad = media_entity_remote_pad(pad);
807
		if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
			break;

		sd = media_entity_to_v4l2_subdev(pad->entity);
		src_fmt.pad = pad->index;
		src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
		ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
		if (ret < 0 && ret != -ENOIOCTLCMD)
			return -EPIPE;

		if (src_fmt.format.width != sink_fmt.format.width ||
		    src_fmt.format.height != sink_fmt.format.height ||
		    src_fmt.format.code != sink_fmt.format.code)
			return -EPIPE;
	}
	return 0;
}

static int fimc_lite_streamon(struct file *file, void *priv,
			      enum v4l2_buf_type type)
{
	struct fimc_lite *fimc = video_drvdata(file);
829
	struct media_entity *entity = &fimc->ve.vdev.entity;
830 831 832 833 834
	int ret;

	if (fimc_lite_active(fimc))
		return -EBUSY;

835
	ret = media_entity_pipeline_start(entity, &fimc->ve.pipe->mp);
836 837
	if (ret < 0)
		return ret;
838 839

	ret = fimc_pipeline_validate(fimc);
840 841
	if (ret < 0)
		goto err_p_stop;
842

843
	fimc->sensor = fimc_find_remote_sensor(&fimc->subdev.entity);
844

845
	ret = vb2_ioctl_streamon(file, priv, type);
846 847
	if (!ret) {
		fimc->streaming = true;
848
		return ret;
849 850
	}

851 852 853
err_p_stop:
	media_entity_pipeline_stop(entity);
	return 0;
854 855 856 857 858 859 860 861
}

static int fimc_lite_streamoff(struct file *file, void *priv,
			       enum v4l2_buf_type type)
{
	struct fimc_lite *fimc = video_drvdata(file);
	int ret;

862
	ret = vb2_ioctl_streamoff(file, priv, type);
863 864 865
	if (ret < 0)
		return ret;

866
	media_entity_pipeline_stop(&fimc->ve.vdev.entity);
867 868
	fimc->streaming = false;
	return 0;
869 870 871 872 873 874 875 876 877
}

static int fimc_lite_reqbufs(struct file *file, void *priv,
			     struct v4l2_requestbuffers *reqbufs)
{
	struct fimc_lite *fimc = video_drvdata(file);
	int ret;

	reqbufs->count = max_t(u32, FLITE_REQ_BUFS_MIN, reqbufs->count);
878
	ret = vb2_ioctl_reqbufs(file, priv, reqbufs);
879
	if (!ret)
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
		fimc->reqbufs_count = reqbufs->count;

	return ret;
}

/* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
{
	if (a->left < b->left || a->top < b->top)
		return 0;
	if (a->left + a->width > b->left + b->width)
		return 0;
	if (a->top + a->height > b->top + b->height)
		return 0;

	return 1;
}

static int fimc_lite_g_selection(struct file *file, void *fh,
				 struct v4l2_selection *sel)
{
	struct fimc_lite *fimc = video_drvdata(file);
	struct flite_frame *f = &fimc->out_frame;

	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
		return -EINVAL;

	switch (sel->target) {
	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
		sel->r.left = 0;
		sel->r.top = 0;
		sel->r.width = f->f_width;
		sel->r.height = f->f_height;
		return 0;

916
	case V4L2_SEL_TGT_COMPOSE:
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
		sel->r = f->rect;
		return 0;
	}

	return -EINVAL;
}

static int fimc_lite_s_selection(struct file *file, void *fh,
				 struct v4l2_selection *sel)
{
	struct fimc_lite *fimc = video_drvdata(file);
	struct flite_frame *f = &fimc->out_frame;
	struct v4l2_rect rect = sel->r;
	unsigned long flags;

	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
933
	    sel->target != V4L2_SEL_TGT_COMPOSE)
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
		return -EINVAL;

	fimc_lite_try_compose(fimc, &rect);

	if ((sel->flags & V4L2_SEL_FLAG_LE) &&
	    !enclosed_rectangle(&rect, &sel->r))
		return -ERANGE;

	if ((sel->flags & V4L2_SEL_FLAG_GE) &&
	    !enclosed_rectangle(&sel->r, &rect))
		return -ERANGE;

	sel->r = rect;
	spin_lock_irqsave(&fimc->slock, flags);
	f->rect = rect;
	set_bit(ST_FLITE_CONFIG, &fimc->state);
	spin_unlock_irqrestore(&fimc->slock, flags);

	return 0;
}

static const struct v4l2_ioctl_ops fimc_lite_ioctl_ops = {
956
	.vidioc_querycap		= fimc_lite_querycap,
957 958 959 960 961 962 963
	.vidioc_enum_fmt_vid_cap_mplane	= fimc_lite_enum_fmt_mplane,
	.vidioc_try_fmt_vid_cap_mplane	= fimc_lite_try_fmt_mplane,
	.vidioc_s_fmt_vid_cap_mplane	= fimc_lite_s_fmt_mplane,
	.vidioc_g_fmt_vid_cap_mplane	= fimc_lite_g_fmt_mplane,
	.vidioc_g_selection		= fimc_lite_g_selection,
	.vidioc_s_selection		= fimc_lite_s_selection,
	.vidioc_reqbufs			= fimc_lite_reqbufs,
964 965 966 967 968
	.vidioc_querybuf		= vb2_ioctl_querybuf,
	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
	.vidioc_qbuf			= vb2_ioctl_qbuf,
	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
969 970 971 972 973 974 975 976 977 978 979
	.vidioc_streamon		= fimc_lite_streamon,
	.vidioc_streamoff		= fimc_lite_streamoff,
};

/* Capture subdev media entity operations */
static int fimc_lite_link_setup(struct media_entity *entity,
				const struct media_pad *local,
				const struct media_pad *remote, u32 flags)
{
	struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
	struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
980
	int ret = 0;
981 982 983 984

	if (WARN_ON(fimc == NULL))
		return 0;

985
	v4l2_dbg(1, debug, sd, "%s: %s --> %s, flags: 0x%x. source_id: 0x%x\n",
986
		 __func__, remote->entity->name, local->entity->name,
987 988
		 flags, fimc->source_subdev_grp_id);

989 990
	switch (local->index) {
	case FLITE_SD_PAD_SINK:
991
		if (flags & MEDIA_LNK_FL_ENABLED) {
992 993 994 995 996 997 998
			if (fimc->source_subdev_grp_id == 0)
				fimc->source_subdev_grp_id = sd->grp_id;
			else
				ret = -EBUSY;
		} else {
			fimc->source_subdev_grp_id = 0;
			fimc->sensor = NULL;
999
		}
1000
		break;
1001

1002 1003
	case FLITE_SD_PAD_SOURCE_DMA:
		if (!(flags & MEDIA_LNK_FL_ENABLED))
1004
			atomic_set(&fimc->out_path, FIMC_IO_NONE);
1005
		else
1006
			atomic_set(&fimc->out_path, FIMC_IO_DMA);
1007 1008
		break;

1009 1010
	case FLITE_SD_PAD_SOURCE_ISP:
		if (!(flags & MEDIA_LNK_FL_ENABLED))
1011
			atomic_set(&fimc->out_path, FIMC_IO_NONE);
1012
		else
1013
			atomic_set(&fimc->out_path, FIMC_IO_ISP);
1014 1015 1016 1017
		break;

	default:
		v4l2_err(sd, "Invalid pad index\n");
1018
		ret = -EINVAL;
1019
	}
1020
	mb();
1021

1022
	return ret;
1023 1024 1025 1026 1027 1028 1029
}

static const struct media_entity_operations fimc_lite_subdev_media_ops = {
	.link_setup = fimc_lite_link_setup,
};

static int fimc_lite_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1030
					   struct v4l2_subdev_pad_config *cfg,
1031 1032 1033 1034
					   struct v4l2_subdev_mbus_code_enum *code)
{
	const struct fimc_fmt *fmt;

1035
	fmt = fimc_lite_find_format(NULL, NULL, 0, code->index);
1036 1037 1038 1039 1040 1041
	if (!fmt)
		return -EINVAL;
	code->code = fmt->mbus_code;
	return 0;
}

1042
static struct v4l2_mbus_framefmt *__fimc_lite_subdev_get_try_fmt(
1043 1044
		struct v4l2_subdev *sd,
		struct v4l2_subdev_pad_config *cfg, unsigned int pad)
1045 1046 1047 1048
{
	if (pad != FLITE_SD_PAD_SINK)
		pad = FLITE_SD_PAD_SOURCE_DMA;

1049
	return v4l2_subdev_get_try_format(sd, cfg, pad);
1050 1051
}

1052
static int fimc_lite_subdev_get_fmt(struct v4l2_subdev *sd,
1053
				    struct v4l2_subdev_pad_config *cfg,
1054 1055 1056 1057
				    struct v4l2_subdev_format *fmt)
{
	struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
	struct v4l2_mbus_framefmt *mf = &fmt->format;
1058
	struct flite_frame *f = &fimc->inp_frame;
1059 1060

	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1061
		mf = __fimc_lite_subdev_get_try_fmt(sd, cfg, fmt->pad);
1062 1063 1064 1065 1066
		fmt->format = *mf;
		return 0;
	}

	mutex_lock(&fimc->lock);
1067
	mf->colorspace = f->fmt->colorspace;
1068
	mf->code = f->fmt->mbus_code;
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083

	if (fmt->pad == FLITE_SD_PAD_SINK) {
		/* full camera input frame size */
		mf->width = f->f_width;
		mf->height = f->f_height;
	} else {
		/* crop size */
		mf->width = f->rect.width;
		mf->height = f->rect.height;
	}
	mutex_unlock(&fimc->lock);
	return 0;
}

static int fimc_lite_subdev_set_fmt(struct v4l2_subdev *sd,
1084
				    struct v4l2_subdev_pad_config *cfg,
1085 1086 1087 1088 1089
				    struct v4l2_subdev_format *fmt)
{
	struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
	struct v4l2_mbus_framefmt *mf = &fmt->format;
	struct flite_frame *sink = &fimc->inp_frame;
1090
	struct flite_frame *source = &fimc->out_frame;
1091 1092
	const struct fimc_fmt *ffmt;

1093
	v4l2_dbg(1, debug, sd, "pad%d: code: 0x%x, %dx%d\n",
1094 1095 1096 1097
		 fmt->pad, mf->code, mf->width, mf->height);

	mutex_lock(&fimc->lock);

1098 1099 1100 1101
	if ((atomic_read(&fimc->out_path) == FIMC_IO_ISP &&
	    sd->entity.stream_count > 0) ||
	    (atomic_read(&fimc->out_path) == FIMC_IO_DMA &&
	    vb2_is_busy(&fimc->vb_queue))) {
1102 1103 1104 1105
		mutex_unlock(&fimc->lock);
		return -EBUSY;
	}

1106
	ffmt = fimc_lite_subdev_try_fmt(fimc, cfg, fmt);
1107 1108

	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1109 1110
		struct v4l2_mbus_framefmt *src_fmt;

1111
		mf = __fimc_lite_subdev_get_try_fmt(sd, cfg, fmt->pad);
1112
		*mf = fmt->format;
1113 1114 1115

		if (fmt->pad == FLITE_SD_PAD_SINK) {
			unsigned int pad = FLITE_SD_PAD_SOURCE_DMA;
1116
			src_fmt = __fimc_lite_subdev_get_try_fmt(sd, cfg, pad);
1117 1118 1119
			*src_fmt = *mf;
		}

1120 1121 1122 1123 1124 1125 1126
		mutex_unlock(&fimc->lock);
		return 0;
	}

	if (fmt->pad == FLITE_SD_PAD_SINK) {
		sink->f_width = mf->width;
		sink->f_height = mf->height;
1127
		sink->fmt = ffmt;
1128 1129 1130 1131 1132
		/* Set sink crop rectangle */
		sink->rect.width = mf->width;
		sink->rect.height = mf->height;
		sink->rect.left = 0;
		sink->rect.top = 0;
1133 1134 1135 1136
		/* Reset source format and crop rectangle */
		source->rect = sink->rect;
		source->f_width = mf->width;
		source->f_height = mf->height;
1137 1138 1139 1140 1141 1142 1143
	}

	mutex_unlock(&fimc->lock);
	return 0;
}

static int fimc_lite_subdev_get_selection(struct v4l2_subdev *sd,
1144
					  struct v4l2_subdev_pad_config *cfg,
1145 1146 1147 1148 1149
					  struct v4l2_subdev_selection *sel)
{
	struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
	struct flite_frame *f = &fimc->inp_frame;

1150 1151 1152
	if ((sel->target != V4L2_SEL_TGT_CROP &&
	     sel->target != V4L2_SEL_TGT_CROP_BOUNDS) ||
	     sel->pad != FLITE_SD_PAD_SINK)
1153 1154 1155
		return -EINVAL;

	if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1156
		sel->r = *v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
1157 1158 1159 1160
		return 0;
	}

	mutex_lock(&fimc->lock);
1161
	if (sel->target == V4L2_SEL_TGT_CROP) {
1162 1163 1164 1165 1166 1167 1168 1169 1170
		sel->r = f->rect;
	} else {
		sel->r.left = 0;
		sel->r.top = 0;
		sel->r.width = f->f_width;
		sel->r.height = f->f_height;
	}
	mutex_unlock(&fimc->lock);

1171
	v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d\n",
1172 1173 1174 1175 1176 1177 1178
		 __func__, f->rect.left, f->rect.top, f->rect.width,
		 f->rect.height, f->f_width, f->f_height);

	return 0;
}

static int fimc_lite_subdev_set_selection(struct v4l2_subdev *sd,
1179
					  struct v4l2_subdev_pad_config *cfg,
1180 1181 1182 1183 1184 1185
					  struct v4l2_subdev_selection *sel)
{
	struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
	struct flite_frame *f = &fimc->inp_frame;
	int ret = 0;

1186
	if (sel->target != V4L2_SEL_TGT_CROP || sel->pad != FLITE_SD_PAD_SINK)
1187 1188 1189 1190 1191 1192
		return -EINVAL;

	mutex_lock(&fimc->lock);
	fimc_lite_try_crop(fimc, &sel->r);

	if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1193
		*v4l2_subdev_get_try_crop(sd, cfg, sel->pad) = sel->r;
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
	} else {
		unsigned long flags;
		spin_lock_irqsave(&fimc->slock, flags);
		f->rect = sel->r;
		/* Same crop rectangle on the source pad */
		fimc->out_frame.rect = sel->r;
		set_bit(ST_FLITE_CONFIG, &fimc->state);
		spin_unlock_irqrestore(&fimc->slock, flags);
	}
	mutex_unlock(&fimc->lock);

1205
	v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d\n",
1206 1207 1208 1209 1210 1211 1212 1213 1214
		 __func__, f->rect.left, f->rect.top, f->rect.width,
		 f->rect.height, f->f_width, f->f_height);

	return ret;
}

static int fimc_lite_subdev_s_stream(struct v4l2_subdev *sd, int on)
{
	struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1215 1216
	unsigned long flags;
	int ret;
1217

1218 1219 1220 1221 1222 1223 1224
	/*
	 * Find sensor subdev linked to FIMC-LITE directly or through
	 * MIPI-CSIS. This is required for configuration where FIMC-LITE
	 * is used as a subdev only and feeds data internally to FIMC-IS.
	 * The pipeline links are protected through entity.stream_count
	 * so there is no need to take the media graph mutex here.
	 */
1225
	fimc->sensor = fimc_find_remote_sensor(&sd->entity);
1226

1227
	if (atomic_read(&fimc->out_path) != FIMC_IO_ISP)
1228 1229
		return -ENOIOCTLCMD;

1230
	mutex_lock(&fimc->lock);
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
	if (on) {
		flite_hw_reset(fimc);
		ret = fimc_lite_hw_init(fimc, true);
		if (!ret) {
			spin_lock_irqsave(&fimc->slock, flags);
			flite_hw_capture_start(fimc);
			spin_unlock_irqrestore(&fimc->slock, flags);
		}
	} else {
		set_bit(ST_FLITE_OFF, &fimc->state);
1241

1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
		spin_lock_irqsave(&fimc->slock, flags);
		flite_hw_capture_stop(fimc);
		spin_unlock_irqrestore(&fimc->slock, flags);

		ret = wait_event_timeout(fimc->irq_queue,
				!test_bit(ST_FLITE_OFF, &fimc->state),
				msecs_to_jiffies(200));
		if (ret == 0)
			v4l2_err(sd, "s_stream(0) timeout\n");
		clear_bit(ST_FLITE_RUN, &fimc->state);
	}

	mutex_unlock(&fimc->lock);
	return ret;
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
}

static int fimc_lite_log_status(struct v4l2_subdev *sd)
{
	struct fimc_lite *fimc = v4l2_get_subdevdata(sd);

	flite_hw_dump_regs(fimc, __func__);
	return 0;
}

static int fimc_lite_subdev_registered(struct v4l2_subdev *sd)
{
	struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
	struct vb2_queue *q = &fimc->vb_queue;
1270
	struct video_device *vfd = &fimc->ve.vdev;
1271 1272
	int ret;

1273
	memset(vfd, 0, sizeof(*vfd));
1274
	atomic_set(&fimc->out_path, FIMC_IO_DMA);
1275 1276 1277 1278 1279 1280 1281 1282

	snprintf(vfd->name, sizeof(vfd->name), "fimc-lite.%d.capture",
		 fimc->index);

	vfd->fops = &fimc_lite_fops;
	vfd->ioctl_ops = &fimc_lite_ioctl_ops;
	vfd->v4l2_dev = sd->v4l2_dev;
	vfd->minor = -1;
1283
	vfd->release = video_device_release_empty;
1284
	vfd->queue = q;
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
	fimc->reqbufs_count = 0;

	INIT_LIST_HEAD(&fimc->pending_buf_q);
	INIT_LIST_HEAD(&fimc->active_buf_q);

	memset(q, 0, sizeof(*q));
	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
	q->io_modes = VB2_MMAP | VB2_USERPTR;
	q->ops = &fimc_lite_qops;
	q->mem_ops = &vb2_dma_contig_memops;
	q->buf_struct_size = sizeof(struct flite_buffer);
	q->drv_priv = fimc;
1297
	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1298
	q->lock = &fimc->lock;
1299
	q->dev = &fimc->pdev->dev;
1300

1301 1302 1303
	ret = vb2_queue_init(q);
	if (ret < 0)
		return ret;
1304 1305

	fimc->vd_pad.flags = MEDIA_PAD_FL_SINK;
1306
	ret = media_entity_pads_init(&vfd->entity, 1, &fimc->vd_pad);
1307 1308
	if (ret < 0)
		return ret;
1309 1310

	video_set_drvdata(vfd, fimc);
1311
	fimc->ve.pipe = v4l2_get_subdev_hostdata(sd);
1312 1313

	ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1314 1315
	if (ret < 0) {
		media_entity_cleanup(&vfd->entity);
1316
		fimc->ve.pipe = NULL;
1317 1318
		return ret;
	}
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331

	v4l2_info(sd->v4l2_dev, "Registered %s as /dev/%s\n",
		  vfd->name, video_device_node_name(vfd));
	return 0;
}

static void fimc_lite_subdev_unregistered(struct v4l2_subdev *sd)
{
	struct fimc_lite *fimc = v4l2_get_subdevdata(sd);

	if (fimc == NULL)
		return;

1332 1333
	mutex_lock(&fimc->lock);

1334 1335 1336
	if (video_is_registered(&fimc->ve.vdev)) {
		video_unregister_device(&fimc->ve.vdev);
		media_entity_cleanup(&fimc->ve.vdev.entity);
1337
		fimc->ve.pipe = NULL;
1338
	}
1339 1340

	mutex_unlock(&fimc->lock);
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
}

static const struct v4l2_subdev_internal_ops fimc_lite_subdev_internal_ops = {
	.registered = fimc_lite_subdev_registered,
	.unregistered = fimc_lite_subdev_unregistered,
};

static const struct v4l2_subdev_pad_ops fimc_lite_subdev_pad_ops = {
	.enum_mbus_code = fimc_lite_subdev_enum_mbus_code,
	.get_selection = fimc_lite_subdev_get_selection,
	.set_selection = fimc_lite_subdev_set_selection,
	.get_fmt = fimc_lite_subdev_get_fmt,
	.set_fmt = fimc_lite_subdev_set_fmt,
};

static const struct v4l2_subdev_video_ops fimc_lite_subdev_video_ops = {
	.s_stream = fimc_lite_subdev_s_stream,
};

static const struct v4l2_subdev_core_ops fimc_lite_core_ops = {
	.log_status = fimc_lite_log_status,
};

static struct v4l2_subdev_ops fimc_lite_subdev_ops = {
	.core = &fimc_lite_core_ops,
	.video = &fimc_lite_subdev_video_ops,
	.pad = &fimc_lite_subdev_pad_ops,
};

static int fimc_lite_s_ctrl(struct v4l2_ctrl *ctrl)
{
	struct fimc_lite *fimc = container_of(ctrl->handler, struct fimc_lite,
					      ctrl_handler);
	set_bit(ST_FLITE_CONFIG, &fimc->state);
	return 0;
}

static const struct v4l2_ctrl_ops fimc_lite_ctrl_ops = {
	.s_ctrl	= fimc_lite_s_ctrl,
};

static const struct v4l2_ctrl_config fimc_lite_ctrl = {
	.ops	= &fimc_lite_ctrl_ops,
	.id	= V4L2_CTRL_CLASS_USER | 0x1001,
	.type	= V4L2_CTRL_TYPE_BOOLEAN,
	.name	= "Test Pattern 640x480",
1387
	.step	= 1,
1388 1389
};

1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
static void fimc_lite_set_default_config(struct fimc_lite *fimc)
{
	struct flite_frame *sink = &fimc->inp_frame;
	struct flite_frame *source = &fimc->out_frame;

	sink->fmt = &fimc_lite_formats[0];
	sink->f_width = FLITE_DEFAULT_WIDTH;
	sink->f_height = FLITE_DEFAULT_HEIGHT;

	sink->rect.width = FLITE_DEFAULT_WIDTH;
	sink->rect.height = FLITE_DEFAULT_HEIGHT;
	sink->rect.left = 0;
	sink->rect.top = 0;

	*source = *sink;
}

1407 1408 1409 1410 1411 1412 1413
static int fimc_lite_create_capture_subdev(struct fimc_lite *fimc)
{
	struct v4l2_ctrl_handler *handler = &fimc->ctrl_handler;
	struct v4l2_subdev *sd = &fimc->subdev;
	int ret;

	v4l2_subdev_init(sd, &fimc_lite_subdev_ops);
1414
	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1415 1416
	snprintf(sd->name, sizeof(sd->name), "FIMC-LITE.%d", fimc->index);

1417 1418 1419
	fimc->subdev_pads[FLITE_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
	fimc->subdev_pads[FLITE_SD_PAD_SOURCE_DMA].flags = MEDIA_PAD_FL_SOURCE;
	fimc->subdev_pads[FLITE_SD_PAD_SOURCE_ISP].flags = MEDIA_PAD_FL_SOURCE;
1420
	ret = media_entity_pads_init(&sd->entity, FLITE_SD_PADS_NUM,
1421
				fimc->subdev_pads);
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
	if (ret)
		return ret;

	v4l2_ctrl_handler_init(handler, 1);
	fimc->test_pattern = v4l2_ctrl_new_custom(handler, &fimc_lite_ctrl,
						  NULL);
	if (handler->error) {
		media_entity_cleanup(&sd->entity);
		return handler->error;
	}

	sd->ctrl_handler = handler;
	sd->internal_ops = &fimc_lite_subdev_internal_ops;
1435
	sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_SCALER;
1436
	sd->entity.ops = &fimc_lite_subdev_media_ops;
1437
	sd->owner = THIS_MODULE;
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
	v4l2_set_subdevdata(sd, fimc);

	return 0;
}

static void fimc_lite_unregister_capture_subdev(struct fimc_lite *fimc)
{
	struct v4l2_subdev *sd = &fimc->subdev;

	v4l2_device_unregister_subdev(sd);
	media_entity_cleanup(&sd->entity);
	v4l2_ctrl_handler_free(&fimc->ctrl_handler);
	v4l2_set_subdevdata(sd, NULL);
}

static void fimc_lite_clk_put(struct fimc_lite *fimc)
{
1455
	if (IS_ERR(fimc->clock))
1456 1457 1458
		return;

	clk_put(fimc->clock);
1459
	fimc->clock = ERR_PTR(-EINVAL);
1460 1461 1462 1463 1464 1465 1466 1467
}

static int fimc_lite_clk_get(struct fimc_lite *fimc)
{
	fimc->clock = clk_get(&fimc->pdev->dev, FLITE_CLK_NAME);
	if (IS_ERR(fimc->clock))
		return PTR_ERR(fimc->clock);

1468
	return 0;
1469 1470
}

1471 1472
static const struct of_device_id flite_of_match[];

1473
static int fimc_lite_probe(struct platform_device *pdev)
1474
{
1475 1476 1477
	struct flite_drvdata *drv_data = NULL;
	struct device *dev = &pdev->dev;
	const struct of_device_id *of_id;
1478 1479 1480 1481
	struct fimc_lite *fimc;
	struct resource *res;
	int ret;

1482 1483 1484
	if (!dev->of_node)
		return -ENODEV;

1485
	fimc = devm_kzalloc(dev, sizeof(*fimc), GFP_KERNEL);
1486 1487 1488
	if (!fimc)
		return -ENOMEM;

1489 1490 1491 1492
	of_id = of_match_node(flite_of_match, dev->of_node);
	if (of_id)
		drv_data = (struct flite_drvdata *)of_id->data;
	fimc->index = of_alias_get_id(dev->of_node, "fimc-lite");
1493

1494 1495 1496 1497
	if (!drv_data || fimc->index >= drv_data->num_instances ||
						fimc->index < 0) {
		dev_err(dev, "Wrong %s node alias\n",
					dev->of_node->full_name);
1498
		return -EINVAL;
1499
	}
1500

1501
	fimc->dd = drv_data;
1502 1503 1504 1505 1506 1507 1508
	fimc->pdev = pdev;

	init_waitqueue_head(&fimc->irq_queue);
	spin_lock_init(&fimc->slock);
	mutex_init(&fimc->lock);

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1509
	fimc->regs = devm_ioremap_resource(dev, res);
1510 1511
	if (IS_ERR(fimc->regs))
		return PTR_ERR(fimc->regs);
1512 1513 1514

	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (res == NULL) {
1515
		dev_err(dev, "Failed to get IRQ resource\n");
1516 1517 1518 1519 1520 1521 1522
		return -ENXIO;
	}

	ret = fimc_lite_clk_get(fimc);
	if (ret)
		return ret;

1523 1524
	ret = devm_request_irq(dev, res->start, flite_irq_handler,
			       0, dev_name(dev), fimc);
1525
	if (ret) {
1526
		dev_err(dev, "Failed to install irq (%d)\n", ret);
1527
		goto err_clk_put;
1528 1529 1530 1531 1532
	}

	/* The video node will be created within the subdev's registered() op */
	ret = fimc_lite_create_capture_subdev(fimc);
	if (ret)
1533
		goto err_clk_put;
1534 1535

	platform_set_drvdata(pdev, fimc);
1536
	pm_runtime_enable(dev);
1537 1538

	if (!pm_runtime_enabled(dev)) {
1539
		ret = clk_prepare_enable(fimc->clock);
1540
		if (ret < 0)
1541
			goto err_sd;
1542
	}
1543

1544
	vb2_dma_contig_set_max_seg_size(dev, DMA_BIT_MASK(32));
1545 1546 1547

	fimc_lite_set_default_config(fimc);

1548
	dev_dbg(dev, "FIMC-LITE.%d registered successfully\n",
1549 1550
		fimc->index);
	return 0;
1551

1552 1553
err_sd:
	fimc_lite_unregister_capture_subdev(fimc);
1554
err_clk_put:
1555 1556 1557 1558
	fimc_lite_clk_put(fimc);
	return ret;
}

1559
#ifdef CONFIG_PM
1560 1561 1562 1563
static int fimc_lite_runtime_resume(struct device *dev)
{
	struct fimc_lite *fimc = dev_get_drvdata(dev);

1564
	clk_prepare_enable(fimc->clock);
1565 1566 1567 1568 1569 1570 1571
	return 0;
}

static int fimc_lite_runtime_suspend(struct device *dev)
{
	struct fimc_lite *fimc = dev_get_drvdata(dev);

1572
	clk_disable_unprepare(fimc->clock);
1573 1574
	return 0;
}
1575
#endif
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597

#ifdef CONFIG_PM_SLEEP
static int fimc_lite_resume(struct device *dev)
{
	struct fimc_lite *fimc = dev_get_drvdata(dev);
	struct flite_buffer *buf;
	unsigned long flags;
	int i;

	spin_lock_irqsave(&fimc->slock, flags);
	if (!test_and_clear_bit(ST_LPM, &fimc->state) ||
	    !test_bit(ST_FLITE_IN_USE, &fimc->state)) {
		spin_unlock_irqrestore(&fimc->slock, flags);
		return 0;
	}
	flite_hw_reset(fimc);
	spin_unlock_irqrestore(&fimc->slock, flags);

	if (!test_and_clear_bit(ST_FLITE_SUSPENDED, &fimc->state))
		return 0;

	INIT_LIST_HEAD(&fimc->active_buf_q);
1598
	fimc_pipeline_call(&fimc->ve, open,
1599
			   &fimc->ve.vdev.entity, false);
1600
	fimc_lite_hw_init(fimc, atomic_read(&fimc->out_path) == FIMC_IO_ISP);
1601 1602 1603 1604 1605 1606
	clear_bit(ST_FLITE_SUSPENDED, &fimc->state);

	for (i = 0; i < fimc->reqbufs_count; i++) {
		if (list_empty(&fimc->pending_buf_q))
			break;
		buf = fimc_lite_pending_queue_pop(fimc);
1607
		buffer_queue(&buf->vb.vb2_buf);
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
	}
	return 0;
}

static int fimc_lite_suspend(struct device *dev)
{
	struct fimc_lite *fimc = dev_get_drvdata(dev);
	bool suspend = test_bit(ST_FLITE_IN_USE, &fimc->state);
	int ret;

	if (test_and_set_bit(ST_LPM, &fimc->state))
		return 0;

	ret = fimc_lite_stop_capture(fimc, suspend);
1622
	if (ret < 0 || !fimc_lite_active(fimc))
1623 1624
		return ret;

1625
	return fimc_pipeline_call(&fimc->ve, close);
1626 1627 1628
}
#endif /* CONFIG_PM_SLEEP */

1629
static int fimc_lite_remove(struct platform_device *pdev)
1630 1631 1632 1633 1634 1635 1636
{
	struct fimc_lite *fimc = platform_get_drvdata(pdev);
	struct device *dev = &pdev->dev;

	pm_runtime_disable(dev);
	pm_runtime_set_suspended(dev);
	fimc_lite_unregister_capture_subdev(fimc);
1637
	vb2_dma_contig_clear_max_seg_size(dev);
1638 1639 1640 1641 1642 1643
	fimc_lite_clk_put(fimc);

	dev_info(dev, "Driver unloaded\n");
	return 0;
}

1644 1645 1646 1647 1648 1649
static const struct dev_pm_ops fimc_lite_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(fimc_lite_suspend, fimc_lite_resume)
	SET_RUNTIME_PM_OPS(fimc_lite_runtime_suspend, fimc_lite_runtime_resume,
			   NULL)
};

1650 1651
/* EXYNOS4212, EXYNOS4412 */
static struct flite_drvdata fimc_lite_drvdata_exynos4 = {
1652 1653 1654 1655 1656
	.max_width		= 8192,
	.max_height		= 8192,
	.out_width_align	= 8,
	.win_hor_offs_align	= 2,
	.out_hor_offs_align	= 8,
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
	.max_dma_bufs		= 1,
	.num_instances		= 2,
};

/* EXYNOS5250 */
static struct flite_drvdata fimc_lite_drvdata_exynos5 = {
	.max_width		= 8192,
	.max_height		= 8192,
	.out_width_align	= 8,
	.win_hor_offs_align	= 2,
	.out_hor_offs_align	= 8,
	.max_dma_bufs		= 32,
	.num_instances		= 3,
1670 1671
};

1672 1673 1674 1675 1676
static const struct of_device_id flite_of_match[] = {
	{
		.compatible = "samsung,exynos4212-fimc-lite",
		.data = &fimc_lite_drvdata_exynos4,
	},
1677 1678 1679 1680
	{
		.compatible = "samsung,exynos5250-fimc-lite",
		.data = &fimc_lite_drvdata_exynos5,
	},
1681
	{ /* sentinel */ },
1682
};
1683
MODULE_DEVICE_TABLE(of, flite_of_match);
1684 1685 1686

static struct platform_driver fimc_lite_driver = {
	.probe		= fimc_lite_probe,
1687
	.remove		= fimc_lite_remove,
1688
	.driver = {
1689
		.of_match_table = flite_of_match,
1690 1691 1692 1693 1694 1695 1696
		.name		= FIMC_LITE_DRV_NAME,
		.pm		= &fimc_lite_pm_ops,
	}
};
module_platform_driver(fimc_lite_driver);
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:" FIMC_LITE_DRV_NAME);