exynos_drm_rotator.c 12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright (C) 2012 Samsung Electronics Co.Ltd
 * Authors:
 *	YoungJun Cho <yj44.cho@samsung.com>
 *	Eunchul Kim <chulspro.kim@samsung.com>
 *
 * 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 Foundationr
 */

#include <linux/kernel.h>
13
#include <linux/component.h>
14 15 16 17 18
#include <linux/err.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
19
#include <linux/of_device.h>
20 21 22 23 24
#include <linux/pm_runtime.h>

#include <drm/drmP.h>
#include <drm/exynos_drm.h>
#include "regs-rotator.h"
25
#include "exynos_drm_drv.h"
26 27 28 29 30 31 32 33
#include "exynos_drm_ipp.h"

/*
 * Rotator supports image crop/rotator and input/output DMA operations.
 * input DMA reads image data from the memory.
 * output DMA writes image data to memory.
 */

34
#define ROTATOR_AUTOSUSPEND_DELAY	2000
35

36
#define rot_read(offset)	readl(rot->regs + (offset))
37 38 39 40 41 42 43
#define rot_write(cfg, offset)	writel(cfg, rot->regs + (offset))

enum rot_irq_status {
	ROT_IRQ_STATUS_COMPLETE	= 8,
	ROT_IRQ_STATUS_ILLEGAL	= 9,
};

44 45 46
struct rot_variant {
	const struct exynos_drm_ipp_formats *formats;
	unsigned int	num_formats;
47 48 49 50 51 52 53 54 55 56 57
};

/*
 * A structure of rotator context.
 * @ippdrv: prepare initialization using ippdrv.
 * @regs: memory mapped io registers.
 * @clock: rotator gate clock.
 * @limit_tbl: limitation of rotator.
 * @irq: irq number.
 */
struct rot_context {
58 59 60
	struct exynos_drm_ipp ipp;
	struct drm_device *drm_dev;
	struct device	*dev;
61 62
	void __iomem	*regs;
	struct clk	*clock;
63 64 65
	const struct exynos_drm_ipp_formats *formats;
	unsigned int	num_formats;
	struct exynos_drm_ipp_task	*task;
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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
};

static void rotator_reg_set_irq(struct rot_context *rot, bool enable)
{
	u32 val = rot_read(ROT_CONFIG);

	if (enable == true)
		val |= ROT_CONFIG_IRQ;
	else
		val &= ~ROT_CONFIG_IRQ;

	rot_write(val, ROT_CONFIG);
}

static enum rot_irq_status rotator_reg_get_irq_status(struct rot_context *rot)
{
	u32 val = rot_read(ROT_STATUS);

	val = ROT_STATUS_IRQ(val);

	if (val == ROT_STATUS_IRQ_VAL_COMPLETE)
		return ROT_IRQ_STATUS_COMPLETE;

	return ROT_IRQ_STATUS_ILLEGAL;
}

static irqreturn_t rotator_irq_handler(int irq, void *arg)
{
	struct rot_context *rot = arg;
	enum rot_irq_status irq_status;
	u32 val;

	/* Get execution result */
	irq_status = rotator_reg_get_irq_status(rot);

	/* clear status */
	val = rot_read(ROT_STATUS);
	val |= ROT_STATUS_IRQ_PENDING((u32)irq_status);
	rot_write(val, ROT_STATUS);

106 107 108 109 110 111 112 113
	if (rot->task) {
		struct exynos_drm_ipp_task *task = rot->task;

		rot->task = NULL;
		pm_runtime_mark_last_busy(rot->dev);
		pm_runtime_put_autosuspend(rot->dev);
		exynos_drm_ipp_task_done(task,
			irq_status == ROT_IRQ_STATUS_COMPLETE ? 0 : -EINVAL);
114
	}
115 116 117 118

	return IRQ_HANDLED;
}

119
static void rotator_src_set_fmt(struct rot_context *rot, u32 fmt)
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
{
	u32 val;

	val = rot_read(ROT_CONTROL);
	val &= ~ROT_CONTROL_FMT_MASK;

	switch (fmt) {
	case DRM_FORMAT_NV12:
		val |= ROT_CONTROL_FMT_YCBCR420_2P;
		break;
	case DRM_FORMAT_XRGB8888:
		val |= ROT_CONTROL_FMT_RGB888;
		break;
	}

	rot_write(val, ROT_CONTROL);
}

138 139
static void rotator_src_set_buf(struct rot_context *rot,
				struct exynos_drm_ipp_buffer *buf)
140 141 142 143
{
	u32 val;

	/* Set buffer size configuration */
144 145
	val = ROT_SET_BUF_SIZE_H(buf->buf.height) |
	      ROT_SET_BUF_SIZE_W(buf->buf.pitch[0] / buf->format->cpp[0]);
146 147 148
	rot_write(val, ROT_SRC_BUF_SIZE);

	/* Set crop image position configuration */
149
	val = ROT_CROP_POS_Y(buf->rect.y) | ROT_CROP_POS_X(buf->rect.x);
150
	rot_write(val, ROT_SRC_CROP_POS);
151 152
	val = ROT_SRC_CROP_SIZE_H(buf->rect.h) |
	      ROT_SRC_CROP_SIZE_W(buf->rect.w);
153 154
	rot_write(val, ROT_SRC_CROP_SIZE);

155 156 157
	/* Set buffer DMA address */
	rot_write(buf->dma_addr[0], ROT_SRC_BUF_ADDR(0));
	rot_write(buf->dma_addr[1], ROT_SRC_BUF_ADDR(1));
158 159
}

160 161
static void rotator_dst_set_transf(struct rot_context *rot,
				   unsigned int rotation)
162 163 164 165 166 167 168
{
	u32 val;

	/* Set transform configuration */
	val = rot_read(ROT_CONTROL);
	val &= ~ROT_CONTROL_FLIP_MASK;

169 170
	if (rotation & DRM_MODE_REFLECT_X)
		val |= ROT_CONTROL_FLIP_VERTICAL;
171 172
	if (rotation & DRM_MODE_REFLECT_Y)
		val |= ROT_CONTROL_FLIP_HORIZONTAL;
173 174 175

	val &= ~ROT_CONTROL_ROT_MASK;

176
	if (rotation & DRM_MODE_ROTATE_90)
177
		val |= ROT_CONTROL_ROT_90;
178
	else if (rotation & DRM_MODE_ROTATE_180)
179
		val |= ROT_CONTROL_ROT_180;
180
	else if (rotation & DRM_MODE_ROTATE_270)
181 182 183 184 185
		val |= ROT_CONTROL_ROT_270;

	rot_write(val, ROT_CONTROL);
}

186 187
static void rotator_dst_set_buf(struct rot_context *rot,
				struct exynos_drm_ipp_buffer *buf)
188
{
189
	u32 val;
190 191

	/* Set buffer size configuration */
192 193
	val = ROT_SET_BUF_SIZE_H(buf->buf.height) |
	      ROT_SET_BUF_SIZE_W(buf->buf.pitch[0] / buf->format->cpp[0]);
194 195 196
	rot_write(val, ROT_DST_BUF_SIZE);

	/* Set crop image position configuration */
197
	val = ROT_CROP_POS_Y(buf->rect.y) | ROT_CROP_POS_X(buf->rect.x);
198 199
	rot_write(val, ROT_DST_CROP_POS);

200 201 202
	/* Set buffer DMA address */
	rot_write(buf->dma_addr[0], ROT_DST_BUF_ADDR(0));
	rot_write(buf->dma_addr[1], ROT_DST_BUF_ADDR(1));
203 204
}

205
static void rotator_start(struct rot_context *rot)
206
{
207
	u32 val;
208

209 210 211 212 213 214
	/* Set interrupt enable */
	rotator_reg_set_irq(rot, true);

	val = rot_read(ROT_CONTROL);
	val |= ROT_CONTROL_START;
	rot_write(val, ROT_CONTROL);
215 216
}

217 218 219 220 221
static int rotator_commit(struct exynos_drm_ipp *ipp,
			  struct exynos_drm_ipp_task *task)
{
	struct rot_context *rot =
			container_of(ipp, struct rot_context, ipp);
222

223 224
	pm_runtime_get_sync(rot->dev);
	rot->task = task;
225

226 227 228 229 230
	rotator_src_set_fmt(rot, task->src.buf.fourcc);
	rotator_src_set_buf(rot, &task->src);
	rotator_dst_set_transf(rot, task->transform.rotation);
	rotator_dst_set_buf(rot, &task->dst);
	rotator_start(rot);
231 232 233 234

	return 0;
}

235 236 237
static const struct exynos_drm_ipp_funcs ipp_funcs = {
	.commit = rotator_commit,
};
238

239
static int rotator_bind(struct device *dev, struct device *master, void *data)
240
{
241 242 243
	struct rot_context *rot = dev_get_drvdata(dev);
	struct drm_device *drm_dev = data;
	struct exynos_drm_ipp *ipp = &rot->ipp;
244

245
	rot->drm_dev = drm_dev;
246
	exynos_drm_register_dma(drm_dev, dev);
247

248 249 250
	exynos_drm_ipp_register(drm_dev, ipp, &ipp_funcs,
			   DRM_EXYNOS_IPP_CAP_CROP | DRM_EXYNOS_IPP_CAP_ROTATE,
			   rot->formats, rot->num_formats, "rotator");
251

252
	dev_info(dev, "The exynos rotator has been probed successfully\n");
253 254 255 256

	return 0;
}

257 258
static void rotator_unbind(struct device *dev, struct device *master,
			void *data)
259 260
{
	struct rot_context *rot = dev_get_drvdata(dev);
261 262
	struct drm_device *drm_dev = data;
	struct exynos_drm_ipp *ipp = &rot->ipp;
263

264
	exynos_drm_ipp_unregister(drm_dev, ipp);
265
	exynos_drm_unregister_dma(rot->drm_dev, rot->dev);
266 267
}

268 269 270
static const struct component_ops rotator_component_ops = {
	.bind	= rotator_bind,
	.unbind = rotator_unbind,
271 272
};

273
static int rotator_probe(struct platform_device *pdev)
274 275
{
	struct device *dev = &pdev->dev;
276
	struct resource	*regs_res;
277
	struct rot_context *rot;
278 279
	const struct rot_variant *variant;
	int irq;
280 281 282
	int ret;

	rot = devm_kzalloc(dev, sizeof(*rot), GFP_KERNEL);
283
	if (!rot)
284 285
		return -ENOMEM;

286 287 288 289 290 291
	variant = of_device_get_match_data(dev);
	rot->formats = variant->formats;
	rot->num_formats = variant->num_formats;
	rot->dev = dev;
	regs_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	rot->regs = devm_ioremap_resource(dev, regs_res);
292 293
	if (IS_ERR(rot->regs))
		return PTR_ERR(rot->regs);
294

295 296
	irq = platform_get_irq(pdev, 0);
	if (irq < 0) {
297
		dev_err(dev, "failed to get irq\n");
298
		return irq;
299 300
	}

301 302
	ret = devm_request_irq(dev, irq, rotator_irq_handler, 0, dev_name(dev),
			       rot);
303 304
	if (ret < 0) {
		dev_err(dev, "failed to request irq\n");
305
		return ret;
306 307
	}

308
	rot->clock = devm_clk_get(dev, "rotator");
309
	if (IS_ERR(rot->clock)) {
310
		dev_err(dev, "failed to get clock\n");
311
		return PTR_ERR(rot->clock);
312 313
	}

314 315
	pm_runtime_use_autosuspend(dev);
	pm_runtime_set_autosuspend_delay(dev, ROTATOR_AUTOSUSPEND_DELAY);
316 317 318
	pm_runtime_enable(dev);
	platform_set_drvdata(pdev, rot);

319 320 321
	ret = component_add(dev, &rotator_component_ops);
	if (ret)
		goto err_component;
322 323 324

	return 0;

325 326
err_component:
	pm_runtime_dont_use_autosuspend(dev);
327 328 329 330
	pm_runtime_disable(dev);
	return ret;
}

331
static int rotator_remove(struct platform_device *pdev)
332 333 334
{
	struct device *dev = &pdev->dev;

335 336
	component_del(dev, &rotator_component_ops);
	pm_runtime_dont_use_autosuspend(dev);
337 338 339 340 341
	pm_runtime_disable(dev);

	return 0;
}

342
#ifdef CONFIG_PM
343 344 345 346
static int rotator_runtime_suspend(struct device *dev)
{
	struct rot_context *rot = dev_get_drvdata(dev);

347 348
	clk_disable_unprepare(rot->clock);
	return 0;
349 350 351 352 353 354
}

static int rotator_runtime_resume(struct device *dev)
{
	struct rot_context *rot = dev_get_drvdata(dev);

355
	return clk_prepare_enable(rot->clock);
356 357 358
}
#endif

359 360 361 362 363
static const struct drm_exynos_ipp_limit rotator_s5pv210_rbg888_limits[] = {
	{ IPP_SIZE_LIMIT(BUFFER, .h = { 8, SZ_16K }, .v = { 8, SZ_16K }) },
	{ IPP_SIZE_LIMIT(AREA, .h.align = 2, .v.align = 2) },
};

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
static const struct drm_exynos_ipp_limit rotator_4210_rbg888_limits[] = {
	{ IPP_SIZE_LIMIT(BUFFER, .h = { 8, SZ_16K }, .v = { 8, SZ_16K }) },
	{ IPP_SIZE_LIMIT(AREA, .h.align = 4, .v.align = 4) },
};

static const struct drm_exynos_ipp_limit rotator_4412_rbg888_limits[] = {
	{ IPP_SIZE_LIMIT(BUFFER, .h = { 8, SZ_8K }, .v = { 8, SZ_8K }) },
	{ IPP_SIZE_LIMIT(AREA, .h.align = 4, .v.align = 4) },
};

static const struct drm_exynos_ipp_limit rotator_5250_rbg888_limits[] = {
	{ IPP_SIZE_LIMIT(BUFFER, .h = { 8, SZ_8K }, .v = { 8, SZ_8K }) },
	{ IPP_SIZE_LIMIT(AREA, .h.align = 2, .v.align = 2) },
};

379 380 381 382 383
static const struct drm_exynos_ipp_limit rotator_s5pv210_yuv_limits[] = {
	{ IPP_SIZE_LIMIT(BUFFER, .h = { 32, SZ_64K }, .v = { 32, SZ_64K }) },
	{ IPP_SIZE_LIMIT(AREA, .h.align = 8, .v.align = 8) },
};

384 385 386 387 388 389 390 391 392 393
static const struct drm_exynos_ipp_limit rotator_4210_yuv_limits[] = {
	{ IPP_SIZE_LIMIT(BUFFER, .h = { 32, SZ_64K }, .v = { 32, SZ_64K }) },
	{ IPP_SIZE_LIMIT(AREA, .h.align = 8, .v.align = 8) },
};

static const struct drm_exynos_ipp_limit rotator_4412_yuv_limits[] = {
	{ IPP_SIZE_LIMIT(BUFFER, .h = { 32, SZ_32K }, .v = { 32, SZ_32K }) },
	{ IPP_SIZE_LIMIT(AREA, .h.align = 8, .v.align = 8) },
};

394 395 396 397 398
static const struct exynos_drm_ipp_formats rotator_s5pv210_formats[] = {
	{ IPP_SRCDST_FORMAT(XRGB8888, rotator_s5pv210_rbg888_limits) },
	{ IPP_SRCDST_FORMAT(NV12, rotator_s5pv210_yuv_limits) },
};

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
static const struct exynos_drm_ipp_formats rotator_4210_formats[] = {
	{ IPP_SRCDST_FORMAT(XRGB8888, rotator_4210_rbg888_limits) },
	{ IPP_SRCDST_FORMAT(NV12, rotator_4210_yuv_limits) },
};

static const struct exynos_drm_ipp_formats rotator_4412_formats[] = {
	{ IPP_SRCDST_FORMAT(XRGB8888, rotator_4412_rbg888_limits) },
	{ IPP_SRCDST_FORMAT(NV12, rotator_4412_yuv_limits) },
};

static const struct exynos_drm_ipp_formats rotator_5250_formats[] = {
	{ IPP_SRCDST_FORMAT(XRGB8888, rotator_5250_rbg888_limits) },
	{ IPP_SRCDST_FORMAT(NV12, rotator_4412_yuv_limits) },
};

414 415 416 417 418
static const struct rot_variant rotator_s5pv210_data = {
	.formats = rotator_s5pv210_formats,
	.num_formats = ARRAY_SIZE(rotator_s5pv210_formats),
};

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
static const struct rot_variant rotator_4210_data = {
	.formats = rotator_4210_formats,
	.num_formats = ARRAY_SIZE(rotator_4210_formats),
};

static const struct rot_variant rotator_4412_data = {
	.formats = rotator_4412_formats,
	.num_formats = ARRAY_SIZE(rotator_4412_formats),
};

static const struct rot_variant rotator_5250_data = {
	.formats = rotator_5250_formats,
	.num_formats = ARRAY_SIZE(rotator_5250_formats),
};

static const struct of_device_id exynos_rotator_match[] = {
	{
436 437 438
		.compatible = "samsung,s5pv210-rotator",
		.data = &rotator_s5pv210_data,
	}, {
439 440 441 442 443 444 445 446 447 448 449 450 451
		.compatible = "samsung,exynos4210-rotator",
		.data = &rotator_4210_data,
	}, {
		.compatible = "samsung,exynos4212-rotator",
		.data = &rotator_4412_data,
	}, {
		.compatible = "samsung,exynos5250-rotator",
		.data = &rotator_5250_data,
	}, {
	},
};
MODULE_DEVICE_TABLE(of, exynos_rotator_match);

452
static const struct dev_pm_ops rotator_pm_ops = {
453 454
	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
				pm_runtime_force_resume)
455 456 457 458 459 460
	SET_RUNTIME_PM_OPS(rotator_runtime_suspend, rotator_runtime_resume,
									NULL)
};

struct platform_driver rotator_driver = {
	.probe		= rotator_probe,
461
	.remove		= rotator_remove,
462
	.driver		= {
463
		.name	= "exynos-rotator",
464 465
		.owner	= THIS_MODULE,
		.pm	= &rotator_pm_ops,
466
		.of_match_table = exynos_rotator_match,
467 468
	},
};